problem with depth clamping

i’ve read that OpenGL has the option to turn off clipping gainst the near and far planes and instead clamp the generated depth values to the range zero to one. This means that any geometry that protrudes behind the near plane or past the far plane will essentially be projected onto that plane. To enable depth clamping you must call glEnable(GL_DEPTH_CLAMP) and to disable, glDisable(GL_DEPTH_CLAMP).
My problem is that depth clamping is only working for the near plane, but is not working for the far plane.

my code is this:

//setting up matrix with nearplane=1 and farplane=10
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1,1,-1,1,1,10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
//i eanble depth clamping
glEnable(GL_DEPTH_CLAMP);

glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//i draw a triangle with 2 vertices inside the view frustum
//and 1 vertice outside , beyond the far plane

glBegin(GL_TRIANGLES);

glVertex4f(0,0,-100,1);
glVertex4f(-1,-1,-2,1);
glVertex4f(1,-1,-2,1);

glEnd();

glFlush();

//end

i compile and run, but i don’t see any clamping, but just the usual clipping.I don’t see a triangle, but i see a quadrilateral cutted on the far plane. Just like depth clamping was disabled. But strange thing, depth clamping on the near plane is working. Anyone knows why?

PS: i’m running ubuntu, nvidia GTX460, driver downloaded from nvidia web site.

It is maybe a driver bug as I don’t see any problem with your code.

ok i found out. Just in case someone else is involving in the same problem, I had to change the depth test from the default GL_LESS to GL_LEQUAL. This is cause when opengl clamps a poligon, it puts the poligon exactly in the far plane, so z=1 (ranging from -1 to 1). And the depth of the pixels of the polygon is 1 (from 0 to 1). And when you clear the depth buffer you fill it with 1. So the depth func must be GL_LEQUAL.

1 Like