• Advertisement
Sign in to follow this  

GLSL and Fragment Depth Trouble

This topic is 3876 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

I'm trying to modify the fragment depths of my scene using a fragment shader and gl_FragDepth, but it's nowhere near giving me satisfactory results. Here's my short fragment shader:
uniform vec3 color;
uniform float depth;

void main()
{
 gl_FragColor = vec4(color, 1.0);
 if(depth < gl_FragDepth)
 gl_FragDepth = depth;
}



And here's the part of my code that uses it:
 set_shaders();
 
 GLint loc, loc2, loc3;
 loc = glGetUniformLocationARB(p, "color");
 loc2 = glGetUniformLocationARB(p, "k");
 loc3 = glGetUniformLocationARB(p, "depth");

 float z1 = -2.0, z2 = -4.0;

 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

 glUniform3fARB(loc, 0.5, 0.0, 0.0);
 glUniform1fARB(loc2, 0.2);
 glUniform1fARB(loc3, 0.5);

 glBegin(GL_QUADS);
  glColor3ub(255, 0, 0); glVertex3f(-1.0, 1.0, z1);
  glColor3ub(255, 0, 0); glVertex3f(1.0, 1.0, z1);
  glColor3ub(255, 0, 0); glVertex3f(1.0, -1.0, z1);
  glColor3ub(255, 0, 0); glVertex3f(-1.0, -1.0, z1);
 glEnd();
 
 glUniform3fARB(loc, 0.5, 0.5, 0.5);
 glUniform1fARB(loc2, 0.2);
 glUniform1fARB(loc3, 0.2);
 
 glBegin(GL_QUADS);
  glColor3ub(255, 0, 0); glVertex3f(-2.0, 1.0, z2);
  glColor3ub(0, 255, 0); glVertex3f(1.0, 1.0, z2);
  glColor3ub(0, 0, 255); glVertex3f(1.0, -1.0, z2);
  glColor3ub(255, 255, 255); glVertex3f(-2.0, -1.0, z2);
 glEnd();



All that is of course done only once. What I was trying to do there is to draw one quad in front of the other, then make them switch places using the fragment shader. I don't think there are technical problems with my code like syntax errors or the like. It seems to be flawed logic. :( It always draws the first quad in front of the other, no metter which one is further away, no matter how I set the depths in my shader or in my C code. Can anyone help me solve this problem? P.S.: I've tried all sorts of values for the depths, in ranges like [0 - 1], [-1, -60], [1, 60], nothing works. And no, I haven't forgot to enable depth tests. It draws the quads in the logically-correct order if I disable the shaders.

Share this post


Link to post
Share on other sites
Advertisement
gl_FragDepth is write-only but your pixel shader reads from it in the comparision. This can't work. Hand down the projected depth from the vertex shader instead.

Share this post


Link to post
Share on other sites
Fixed that, but it still didn't work, so I moved to another computer and it works now.
I was a driver bug. -__-
Thanks anyway.

Share this post


Link to post
Share on other sites
Quote:
gl_FragDepth is write-only

This is correct but remember that gl_FragCoord.z is readable and contains your fragment depth so no need to add anything extra to your vertex shader. Also, if you are setting gl_FragDepth you must do so for *all* control paths. You're not doing this. Your frag code should read:

uniform vec3 color;
uniform float depth;

void main()
{
gl_FragColor = vec4(color, 1.0);
if(depth < gl_FragDepth)
gl_FragDepth = depth;
else
gl_FragDepth = glFragCoord.z;
}

Share this post


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

  • Advertisement