View Full Version : Reg : glClearDepth and glDepthFunc
03-04-2012, 08:25 PM
I am not able to understand the working of glDepthFunc() for different modes. If suppose glClearDepth(1.0); and if glDepthFunc(GL_LEQUAL) and if glVertex(1.0,1.0,0.7); Does it work like this.." the 1/z value mentioned in glVertex if less than or equal to the 1/z value mentioned in glClearDepth API will be drawn..????" is my understanding correct. And one more query i have read that glClearDepth( ) accepts values in range 0 1 means it accepts only 0 or 1 or does it mean it accepts values between 0..1.
03-05-2012, 07:37 AM
The X, Y and Z values in glVertex() are given in object coordinates. Object coordinates are different coordinate from what glClearDepth() and glDepthRange() use. glClearDepth() uses the range 0..1 for depth values, as used in depth buffer. Object Coordinates are transformed by the ModelView matrix to produce Eye Coordinates Eye Coordinates are transformed by the Projection matrix to produce Clip Coordinates. Clip Coordinate X, Y, and Z are divided by Clip Coordinate W to produce Normalized Device Coordinates. Normalized Device Coordinates are scaled and translated by the viewport parameters to produce Window Coordinates.Effectively your projection near is mapped to 0.0 and far is mapped to 1.0, if you use default depth range. Thus glClearDepth(0.0) means "clear depth buffer with near plane" and glClearDepth(1.0) means "clear depth buffer with far plane.
Depth testing happens with the window Z coordinate, which is in 0 to 1 range. This is the value that gets written to the depth buffer if depth and other tests pass. Further away fragments have larger value, in usual setups.
If you have specified non-default values to glDepthRange(), then depth values will be mapped to your choice of depth range, however this range still needs to be in 0..1. glDepthRange(1.0, 1.0) makes all rendered fragments to go to the far plane. Notice that near and far given to glDepthRange() are different from near and far used in projections.
Typically you clear with "far plane", 1.0, and render with depth test less / lequal. If you want to render the away facing surfaces instead, you can clear with "near plane", 0.0 and render with depth test greater / gequal. You also need to pay attention to have your backface culling setup properly.
03-05-2012, 09:07 PM
Thank you... I will try with it..:)
03-05-2012, 09:29 PM
glClearColor(1.0, 1.0, 1.0, 1.0);
void display( void )
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glRotatef( 45, 0.0, 1.0, 0.0 );
glBegin( GL_TRIANGLES );
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 0.5, 0.8);
glVertex3f(-0.5, -0.7, 0.8);
glVertex3f(0.5, -0.7, 0.8);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.5, 0.5, 1.0);
glVertex3f(-0.3, 0.6, 1.0);
glVertex3f(0.6, -0.4, 1.0);
int main(int argc, char** argv)
glutInitWindowSize( 600, 600);
glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE);
This is my code.where i have taken GL_LESS as DepthFunc()parameter Here the 1/z value of my vertices i.e. 1/0.8 is less than 1/0.7 and 1/1 is gretaer than 1/0.7. Hence only Red color triangle should have been rendered and never green color triangle. But I could see both the triangles rendered. Kindly help me with this..?
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.