GLSL and Fragment Depth Trouble

Started by
2 comments, last by coordz 16 years, 9 months ago
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.
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.
----------
Gonna try that "Indie" stuff I keep hearing about. Let's start with Splatter.
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.
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;}

This topic is closed to new replies.

Advertisement