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.