Sign in to follow this  

OpenGL opengl 4 tessellation linker error

This topic is 2081 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I'm trying to do some basic tessellation, but I've ran into a strange problem. My glsl code compiles fine, but when I try to render some geometry with it, I get the invalid operation error. I tried to debug this issue with gdebugger, and interestingly it gives me a AP_PROGRAM_LINK_FAILED_ERROR. This is strange because I do check if the linking went right (GL_LINK_STATUS), and I get no errors even if I directly check the linking with glGetError. The geometry that I tried to render works fine with other shaders.
I followed this tutorial: [url="http://prideout.net/blog/?p=48"]http://prideout.net/blog/?p=48[/url]

Here's the glsl code:
[CODE]
[vertex shader]
#version 410

in vec4 in_vertex; //incoming vertex attribute in object space

out cross_shader_data
{
vec3 position;
} o;

void main()
{
o.position = in_vertex.xyz; //pass the data to the TC shader
}

[tessellation control shader]
#version 410

layout(vertices = 3) out; //a patch should consist of 3 vertices, thus essentially defining a vertex

in cross_shader_data
{
vec3 position;
} i[];

out cross_shader_data
{
vec3 position;
} o[];

uniform float tess_level_inner;
uniform float tess_level_outer;

void main()
{
o[gl_InvocationID].position = i[gl_InvocationID].position; //pass the vertex data to the TE shader

if(gl_InvocationID == 0) //define tessellation levels
{
gl_TessLevelInner[0] = tess_level_inner;

gl_TessLevelOuter[0] = tess_level_outer;
gl_TessLevelOuter[1] = tess_level_outer;
gl_TessLevelOuter[2] = tess_level_outer;
}
}

[tessellation evaluation shader]
#version 410

layout(triangles, equal_spacing, ccw) in; //process triangles, using equal spacing, and they'll be in counter-clockwise order

in cross_shader_data
{
vec3 position;
} i[];

out cross_shader_data
{
vec3 position;
vec3 patch_distance;
} o;

uniform mat4 modelviewproj;

void main()
{
vec3 p0 = gl_TessCoord.x * i[0].position;
vec3 p1 = gl_TessCoord.y * i[1].position;
vec3 p2 = gl_TessCoord.z * i[2].position;

o.patch_distance = gl_TessCoord;
o.position = normalize(p0 + p1 + p2); //create the new vertex
gl_Position = modelviewproj * vec4(o.position, 1); //do the transformation before passing to the geometry shader
}

[geometry shader]
#version 410

layout(triangles) in; //process triangles
layout(triangle_strip, max_vertices = 3) out; //spit out triangle strips that consist of max 3 vertices

in cross_shader_data
{
vec3 position;
vec3 patch_distance;
} i[3];

out cross_shader_data
{
vec3 face_normal;
vec3 patch_distance;
vec3 tri_distance;
} o;

uniform mat3 normal_mat;

void main()
{
vec3 a = i[2].position - i[0].position;
vec3 b = i[1].position - i[0].position;
o.face_normal = normal_mat * normalize(cross(a, b)); //transform face normal into view space

o.patch_distance = i[0].patch_distance;
o.tri_distance = vec3(1, 0, 0);
gl_Position = gl_in[0].gl_Position;
EmitVertex();

o.patch_distance = i[1].patch_distance;
o.tri_distance = vec3(0, 1, 0);
gl_Position = gl_in[1].gl_Position;
EmitVertex();

o.patch_distance = i[2].patch_distance;
o.tri_distance = vec3(0, 0, 1);
gl_Position = gl_in[2].gl_Position;
EmitVertex();
}

[pixel shader]
#version 410

in cross_shader_data
{
vec3 face_normal;
vec3 tri_distance;
vec3 patch_distance;
} i;

out vec4 color;

float amplify(float d, float scale, float offset)
{
d = scale * d + offset;
d = clamp(d, 0, 1);
d = 1 - exp2(-2 * d * d);
return d;
}

void main()
{
vec3 n = normalize(i.face_normal);
vec3 l = vec3(0, 1, 0);

float ndotl = max(dot(n, l), 0);

vec3 result = ndotl * vec3(1, 1, 1); //do some lighting

float d1 = min(min(i.tri_distance.x, i.tri_distance.y), i.tri_distance.z);
float d2 = min(min(i.patch_distance.x, i.patch_distance.y), i.patch_distance.z);

result *= amplify(d1, 40, -0.5) * amplify(d2, 60, -0.5); //just some edge outlining

color = vec4(result, 1);
}
[/CODE]

Best regards,
Yours3!f Edited by Yours3!f

Share this post


Link to post
Share on other sites
Hi!

For some reason passing structs between shader stages doesn’t work anymore (at least the way you/I did it…). At some point it did. I remember it fairly well, since I was very happy to see that GLSL finally allowed it, too (like HLSL). But with current drivers it doesn’t work anymore. I had to rewrite some of my old shaders, too. Maybe it just wasn’t specification compliant and only nVidia drivers allowed it.

If you follow the tutorial you linked above, you should be fine. So, something like:
[CODE]in vec3 i_position[];
out vec3 o_position[];[/CODE]
And so on.

And yes, I was also confused that the GLSL compiler doesn't complain at all.

Cheers!

Share this post


Link to post
Share on other sites
[quote name='Tsus' timestamp='1336123109' post='4937325']
Hi!

For some reason passing structs between shader stages doesn’t work anymore (at least the way you/I did it…). At some point it did. I remember it fairly well, since I was very happy to see that GLSL finally allowed it, too (like HLSL). But with current drivers it doesn’t work anymore. I had to rewrite some of my old shaders, too. Maybe it just wasn’t specification compliant and only nVidia drivers allowed it.

If you follow the tutorial you linked above, you should be fine. So, something like:
[CODE]in vec3 i_position[];
out vec3 o_position[];[/CODE]
And so on.

And yes, I was also confused that the GLSL compiler doesn't complain at all.

Cheers!
[/quote]

Thank you Tsus for the reply [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

I tried to convert the interface blocks to traditional in-outs but somethings still wrong... What am I missing?

drivers... I even made a segfault to the compiler while converting the shaders... (I mean it segfaulted when linking, and the last function in the stack was somewhere in the catalyst drivers... 12.4 64bit linux)

this is how the shaders look like now:
[CODE]
[vertex shader]
#version 410
in vec4 in_vertex;
out vec3 v_position;
void main()
{
v_position = in_vertex.xyz;
}
[tessellation control shader]
#version 410
layout(vertices = 3) out;
in vec3 v_position[];
out vec3 tc_position[];
uniform float tess_level_inner;
uniform float tess_level_outer;
void main()
{
tc_position[gl_InvocationID] = v_position[gl_InvocationID];
if(gl_InvocationID == 0)
{
gl_TessLevelInner[0] = tess_level_inner;
gl_TessLevelOuter[0] = tess_level_outer;
gl_TessLevelOuter[1] = tess_level_outer;
gl_TessLevelOuter[2] = tess_level_outer;
}
}
[tessellation evaluation shader]
#version 410
layout(triangles, equal_spacing, ccw) in;
in vec3 tc_position[];
out vec3 te_position;
out vec3 te_patch_distance;
uniform mat4 modelviewproj;
void main()
{
vec3 p0 = gl_TessCoord.x * tc_position[0];
vec3 p1 = gl_TessCoord.y * tc_position[1];
vec3 p2 = gl_TessCoord.z * tc_position[2];

te_patch_distance = gl_TessCoord;
te_position = normalize(p0 + p1 + p2);
gl_Position = modelviewproj * vec4(te_position, 1);
}
[geometry shader]
#version 410
layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out;
in vec3 te_position[3];
in vec3 te_patch_distance[3];
out vec3 g_face_normal;
out vec3 g_patch_distance;
out vec3 g_tri_distance;
uniform mat3 normal_mat;
void main()
{
vec3 a = te_position[2] - te_position[0];
vec3 b = te_position[1] - te_position[0];
g_face_normal = normal_mat * normalize(cross(a, b));

g_patch_distance = te_patch_distance[0];
g_tri_distance = vec3(1, 0, 0);
gl_Position = gl_in[0].gl_Position;
EmitVertex();

g_patch_distance = te_patch_distance[1];
g_tri_distance = vec3(0, 1, 0);
gl_Position = gl_in[1].gl_Position;
EmitVertex();

g_patch_distance = te_patch_distance[2];
g_tri_distance = vec3(0, 0, 1);
gl_Position = gl_in[2].gl_Position;
EmitVertex();
}
[pixel shader]
#version 410
in vec3 g_face_normal;
in vec3 g_tri_distance;
in vec3 g_patch_distance;
out vec4 color;
float amplify(float d, float scale, float offset)
{
d = scale * d + offset;
d = clamp(d, 0, 1);
d = 1 - exp2(-2 * d * d);
return d;
}
void main()
{
vec3 n = normalize(g_face_normal);
vec3 l = vec3(0, 1, 0);

float ndotl = max(dot(n, l), 0);

vec3 result = ndotl * vec3(1, 1, 1);

float d1 = min(min(g_tri_distance.x, g_tri_distance.y), g_tri_distance.z);
float d2 = min(min(g_patch_distance.x, g_patch_distance.y), g_patch_distance.z);

result *= amplify(d1, 40, -0.5) * amplify(d2, 60, -0.5);

color = vec4(result, 1);
}
[/CODE]

Best regards,
Yours3!f Edited by Yours3!f

Share this post


Link to post
Share on other sites
Hi!

The code you posted worked fairly well. I just changed some minor things to make it fit to my demo app, here.
In particular, I used explicit vertex attribute binding in the vertex shader:
[CODE]layout(location=0) in vec3 in_vertex;[/CODE]

For all uniform parameters I utilized a uniform buffer object, e.g.:
[CODE]layout(std140) uniform TessFactor
{
float tess_level_inner;
float tess_level_outer;
};[/CODE]

Eventually, I removed the normalization of te_position in your tessellation evaluation shader (barycentric interpolation doesn't require it).
[CODE]//te_position = normalize(p0 + p1 + p2);
te_position = p0 + p1 + p2;[/CODE]

It works on my end. I tested on a GeForce 460 GTX (Win 7, 64 Bit).

Good luck! Hopefully you find a way to get it running, too.
Best regards

Share this post


Link to post
Share on other sites
just a couple of questions in connection with your modifications:

[quote name='Tsus' timestamp='1336296166' post='4937734']
The code you posted worked fairly well. I just changed some minor things to make it fit to my demo app, here.
In particular, I used explicit vertex attribute binding in the vertex shader:
[CODE]layout(location=0) in vec3 in_vertex;[/CODE]
[/quote]

I use special "shader controller" files for this :) so I don't have to worry about which attribute belong to which location.

[quote]For all uniform parameters I utilized a uniform buffer object, e.g.:
[CODE]layout(std140) uniform TessFactor
{
float tess_level_inner;
float tess_level_outer;
};[/CODE]
[/quote]

is that a performance win, or you just like to use uniforms this way?

[quote]
Eventually, I removed the normalization of te_position in your tessellation evaluation shader (barycentric interpolation doesn't require it).
[CODE]//te_position = normalize(p0 + p1 + p2);
te_position = p0 + p1 + p2;[/CODE]
[/quote]

yeah I still need to dig deeper into the barycentric coordinates...

[quote]It works on my end. I tested on a GeForce 460 GTX (Win 7, 64 Bit).[/quote]

I guess this is the relevant bit. I have an AMD Radeon HD 5670 (Linux, 64 bit, same problem on Win 7 64 bit with same drivers) with newest drivers. So can you please verify the code on AMD as well? Because if thats the problem, then I guess I should contact the AMD dev guys to fix this...

Share this post


Link to post
Share on other sites
Hi,

I did the modifications only to get the code running in a small tessellation exercise I wrote a while back. I didn’t want to change much on the CPU side, so I just used the uniform buffer objects that were passed in already.
On the CPU side I didn’t specify the attribute binding, so I had to do it in GLSL. Doing this in GLSL is just a personal favor.

[quote name='Yours3!f' timestamp='1336304020' post='4937748']
[quote name='Tsus' timestamp='1336296166' post='4937734']
For all uniform parameters I utilized a uniform buffer object, e.g.:
[CODE]layout(std140) uniform TessFactor
{
float tess_level_inner;
float tess_level_outer;
};[/CODE]
[/quote]
is that a performance win, or you just like to use uniforms this way?
[/quote]
I think the performance win isn’t that big. It’s more that I like that sort of grouping as it reminds me of constant buffers in Dx.

[quote name='Yours3!f' timestamp='1336304020' post='4937748']
[quote name='Tsus' timestamp='1336296166' post='4937734']
It works on my end. I tested on a GeForce 460 GTX (Win 7, 64 Bit).
[/quote]
I guess this is the relevant bit. I have an AMD Radeon HD 5670 (Linux, 64 bit, same problem on Win 7 64 bit with same drivers) with newest drivers. So can you please verify the code on AMD as well? Because if thats the problem, then I guess I should contact the AMD dev guys to fix this...
[/quote]
Sorry, I neither have an AMD at home nor in the lab.
I attached the code so you can test it yourself.

Yes, you should probably report this to AMD if it turns out to be some sort of bug.

Cheers!

Share this post


Link to post
Share on other sites
[quote name='Tsus' timestamp='1336306788' post='4937754']
Hi,

I did the modifications only to get the code running in a small tessellation exercise I wrote a while back. I didn’t want to change much on the CPU side, so I just used the uniform buffer objects that were passed in already.
On the CPU side I didn’t specify the attribute binding, so I had to do it in GLSL. Doing this in GLSL is just a personal favor.

[quote name='Yours3!f' timestamp='1336304020' post='4937748']
[quote name='Tsus' timestamp='1336296166' post='4937734']
For all uniform parameters I utilized a uniform buffer object, e.g.:
[CODE]layout(std140) uniform TessFactor
{
float tess_level_inner;
float tess_level_outer;
};[/CODE]
[/quote]
is that a performance win, or you just like to use uniforms this way?
[/quote]
I think the performance win isn’t that big. It’s more that I like that sort of grouping as it reminds me of constant buffers in Dx.

[quote name='Yours3!f' timestamp='1336304020' post='4937748']
[quote name='Tsus' timestamp='1336296166' post='4937734']
It works on my end. I tested on a GeForce 460 GTX (Win 7, 64 Bit).
[/quote]
I guess this is the relevant bit. I have an AMD Radeon HD 5670 (Linux, 64 bit, same problem on Win 7 64 bit with same drivers) with newest drivers. So can you please verify the code on AMD as well? Because if thats the problem, then I guess I should contact the AMD dev guys to fix this...
[/quote]
Sorry, I neither have an AMD at home nor in the lab.
I attached the code so you can test it yourself.

Yes, you should probably report this to AMD if it turns out to be some sort of bug.

Cheers!
[/quote]

Thank you [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] this saved me writing an example.

Well I had to do some modifications to get it running, but eventually it was running, so I guess I'm doing something wrong.
I've attached the modified code. (tessellation.dom, and Tessellation_end.cpp, plus file renamings and I added headers, libs and dlls) Edited by Yours3!f

Share this post


Link to post
Share on other sites
HEUREKA XD

SOLVED [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

I examined your example, and found out one thing that's entirely different: how you draw your mesh.

so I just had to include the glPatchParameter function and change the glDrawElements from GL_TRIANGLES to GL_PATCHES... this is quite stupid... so this is what a working source does to me [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Thank your for the help!!! Edited by Yours3!f

Share this post


Link to post
Share on other sites
Hi,

just out of curiosity I tried the technique with interface blocks, and it worked, so that wasn't the problem [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] seems like the newest drivers are capable of this.

another thing is that the normalization in the tessellation evaluation shader IS needed, without it the geometry won't "emerge" out of the surface. (see pictures)

Share this post


Link to post
Share on other sites
Hi,

[quote name='Yours3!f' timestamp='1336324235' post='4937814']
just out of curiosity I tried the technique with interface blocks, and it worked, so that wasn't the problem [img]http://1.1.1.3/bmi/public.gamedev.net//public/style_emoticons/default/smile.png[/img] seems like the newest drivers are capable of this.
[/quote]
Nice! Thanks for the info! I’ll give it a try again, later.

[quote name='Yours3!f' timestamp='1336324235' post='4937814']
another thing is that the normalization in the tessellation evaluation shader IS needed, without it the geometry won't "emerge" out of the surface.
[/quote]
Wouldn't this only work for unit-spheres?
Most often, I just pick an easy tessellation scheme like [url="http://perso.telecom-paristech.fr/~boubek/papers/PhongTessellation/"]phong tessellation[/url]. Edited by Tsus

Share this post


Link to post
Share on other sites
[quote name='Tsus' timestamp='1336328969' post='4937835']
[quote name='Yours3!f' timestamp='1336324235' post='4937814']
another thing is that the normalization in the tessellation evaluation shader IS needed, without it the geometry won't "emerge" out of the surface.
[/quote]
Wouldn't this only work for unit-spheres?
Most often, I just pick an easy tessellation scheme like [url="http://perso.telecom-paristech.fr/~boubek/papers/PhongTessellation/"]phong tessellation[/url].
[/quote]

hmmm... I don't know [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] well if no emerging happens then normalize otherwise don't. I'm not an expert in tessellation yet so I only wrote this by experience. Seems like for this scheme you do need normalization. Try it out with the elephant! Edited by Yours3!f

Share this post


Link to post
Share on other sites
[quote name='Yours3!f' timestamp='1336333001' post='4937854']
Try it out with the elephant!
[/quote]
I did, that’s why I was asking. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

I have the te_position in world space, thus normalizing it projects the elephant back on the unit sphere (looks funny [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]). If we’re trying to achieve a perfect sphere like in the example you linked, then sure, this is the best way to go, but in general it doesn’t seem right, I think.
Have you tried it with the elephant? (You probably have to change in the cpp-file the center to {0,0,0} and rStart to 10 in order to see something.)

Share this post


Link to post
Share on other sites
[quote name='Tsus' timestamp='1336335237' post='4937862']
[quote name='Yours3!f' timestamp='1336333001' post='4937854']
Try it out with the elephant!
[/quote]
I did, that’s why I was asking. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

I have the te_position in world space, thus normalizing it projects the elephant back on the unit sphere (looks funny [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]). If we’re trying to achieve a perfect sphere like in the example you linked, then sure, this is the best way to go, but in general it doesn’t seem right, I think.
Have you tried it with the elephant? (You probably have to change in the cpp-file the center to {0,0,0} and rStart to 10 in order to see something.)
[/quote]

ummm... frankly? no I haven't XD but now I did and it does project back onto the unit sphere... is this a bug or feature >) ? Well you're right I guess.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Similar Content

    • By xhcao
      Does sync be needed to read texture content after access texture image in compute shader?
      My simple code is as below,
      glUseProgram(program.get());
      glBindImageTexture(0, texture[0], 0, GL_FALSE, 3, GL_READ_ONLY, GL_R32UI);
      glBindImageTexture(1, texture[1], 0, GL_FALSE, 4, GL_WRITE_ONLY, GL_R32UI);
      glDispatchCompute(1, 1, 1);
      // Does sync be needed here?
      glUseProgram(0);
      glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
      glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                                     GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, texture[1], 0);
      glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
       
      Compute shader is very simple, imageLoad content from texture[0], and imageStore content to texture[1]. Does need to sync after dispatchCompute?
    • By Jonathan2006
      My question: is it possible to transform multiple angular velocities so that they can be reinserted as one? My research is below:
      // This works quat quaternion1 = GEQuaternionFromAngleRadians(angleRadiansVector1); quat quaternion2 = GEMultiplyQuaternions(quaternion1, GEQuaternionFromAngleRadians(angleRadiansVector2)); quat quaternion3 = GEMultiplyQuaternions(quaternion2, GEQuaternionFromAngleRadians(angleRadiansVector3)); glMultMatrixf(GEMat4FromQuaternion(quaternion3).array); // The first two work fine but not the third. Why? quat quaternion1 = GEQuaternionFromAngleRadians(angleRadiansVector1); vec3 vector1 = GETransformQuaternionAndVector(quaternion1, angularVelocity1); quat quaternion2 = GEQuaternionFromAngleRadians(angleRadiansVector2); vec3 vector2 = GETransformQuaternionAndVector(quaternion2, angularVelocity2); // This doesn't work //quat quaternion3 = GEQuaternionFromAngleRadians(angleRadiansVector3); //vec3 vector3 = GETransformQuaternionAndVector(quaternion3, angularVelocity3); vec3 angleVelocity = GEAddVectors(vector1, vector2); // Does not work: vec3 angleVelocity = GEAddVectors(vector1, GEAddVectors(vector2, vector3)); static vec3 angleRadiansVector; vec3 angularAcceleration = GESetVector(0.0, 0.0, 0.0); // Sending it through one angular velocity later in my motion engine angleVelocity = GEAddVectors(angleVelocity, GEMultiplyVectorAndScalar(angularAcceleration, timeStep)); angleRadiansVector = GEAddVectors(angleRadiansVector, GEMultiplyVectorAndScalar(angleVelocity, timeStep)); glMultMatrixf(GEMat4FromEulerAngle(angleRadiansVector).array); Also how do I combine multiple angularAcceleration variables? Is there an easier way to transform the angular values?
    • By dpadam450
      I have this code below in both my vertex and fragment shader, however when I request glGetUniformLocation("Lights[0].diffuse") or "Lights[0].attenuation", it returns -1. It will only give me a valid uniform location if I actually use the diffuse/attenuation variables in the VERTEX shader. Because I use position in the vertex shader, it always returns a valid uniform location. I've read that I can share uniforms across both vertex and fragment, but I'm confused what this is even compiling to if this is the case.
       
      #define NUM_LIGHTS 2
      struct Light
      {
          vec3 position;
          vec3 diffuse;
          float attenuation;
      };
      uniform Light Lights[NUM_LIGHTS];
       
       
    • By pr033r
      Hello,
      I have a Bachelor project on topic "Implenet 3D Boid's algorithm in OpenGL". All OpenGL issues works fine for me, all rendering etc. But when I started implement the boid's algorithm it was getting worse and worse. I read article (http://natureofcode.com/book/chapter-6-autonomous-agents/) inspirate from another code (here: https://github.com/jyanar/Boids/tree/master/src) but it still doesn't work like in tutorials and videos. For example the main problem: when I apply Cohesion (one of three main laws of boids) it makes some "cycling knot". Second, when some flock touch to another it scary change the coordination or respawn in origin (x: 0, y:0. z:0). Just some streng things. 
      I followed many tutorials, change a try everything but it isn't so smooth, without lags like in another videos. I really need your help. 
      My code (optimalizing branch): https://github.com/pr033r/BachelorProject/tree/Optimalizing
      Exe file (if you want to look) and models folder (for those who will download the sources):
      http://leteckaposta.cz/367190436
      Thanks for any help...

    • By Andrija
      I am currently trying to implement shadow mapping into my project , but although i can render my depth map to the screen and it looks okay , when i sample it with shadowCoords there is no shadow.
      Here is my light space matrix calculation
      mat4x4 lightViewMatrix; vec3 sun_pos = {SUN_OFFSET * the_sun->direction[0], SUN_OFFSET * the_sun->direction[1], SUN_OFFSET * the_sun->direction[2]}; mat4x4_look_at(lightViewMatrix,sun_pos,player->pos,up); mat4x4_mul(lightSpaceMatrix,lightProjMatrix,lightViewMatrix); I will tweak the values for the size and frustum of the shadow map, but for now i just want to draw shadows around the player position
      the_sun->direction is a normalized vector so i multiply it by a constant to get the position.
      player->pos is the camera position in world space
      the light projection matrix is calculated like this:
      mat4x4_ortho(lightProjMatrix,-SHADOW_FAR,SHADOW_FAR,-SHADOW_FAR,SHADOW_FAR,NEAR,SHADOW_FAR); Shadow vertex shader:
      uniform mat4 light_space_matrix; void main() { gl_Position = light_space_matrix * transfMatrix * vec4(position, 1.0f); } Shadow fragment shader:
      out float fragDepth; void main() { fragDepth = gl_FragCoord.z; } I am using deferred rendering so i have all my world positions in the g_positions buffer
      My shadow calculation in the deferred fragment shader:
      float get_shadow_fac(vec4 light_space_pos) { vec3 shadow_coords = light_space_pos.xyz / light_space_pos.w; shadow_coords = shadow_coords * 0.5 + 0.5; float closest_depth = texture(shadow_map, shadow_coords.xy).r; float current_depth = shadow_coords.z; float shadow_fac = 1.0; if(closest_depth < current_depth) shadow_fac = 0.5; return shadow_fac; } I call the function like this:
      get_shadow_fac(light_space_matrix * vec4(position,1.0)); Where position is the value i got from sampling the g_position buffer
      Here is my depth texture (i know it will produce low quality shadows but i just want to get it working for now):
      sorry because of the compression , the black smudges are trees ... https://i.stack.imgur.com/T43aK.jpg
      EDIT: Depth texture attachment:
      glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH_COMPONENT24,fbo->width,fbo->height,0,GL_DEPTH_COMPONENT,GL_FLOAT,NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, fbo->depthTexture, 0);
  • Popular Now