PDA

View Full Version : Depth buffer test



nithya
08-11-2003, 09:22 PM
Hi,

I am doing a project which involves displaying 3D objects. I have used the line primitive to draw the object. My problem is to remove the hidden lines of the object to get a understandable form of the object. I tried enabling the GL_DEPTH_TEST and using the depth function but it did not work. Is it possible to do depth test for objects which are drawn using GL_LINES or GL_LINE_LOOP. I haven't used shading is it necessary? how can we get a 3D view of the object?
this is the code segment that i have used:

if(m_bHide)
{
glEnable(GL_DEPTH_TEST);
glClearDepth(1.0f);
glDepthFunc(GL_LESS);
DrawScene();
}
else
{
glDisable(GL_DEPTH_TEST);
DrawScene();
}
My Z axis is away from the user
Please help me out

Nithya

Sourpotato
08-11-2003, 10:26 PM
place the glEnable(GL_DEPTH_TEST) after the glDepthFunc()

Relic
08-12-2003, 12:44 AM
OpenGL is a state machine, there is not really a difference in the above code if glEnable is moved there.
I would recommend to always set values and then enable things, this avoids some pitfalls.

Of course wireframes alone are depth tested against each other but you can look through the interior of faces. To remove hidden lines in a 3D drawing with depth test you need to draw something into the faces occluding them.
The standard method is (from top of my head, not compiled):

Draw the model shaded into the depth buffer first fo get occluders:
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glPolygonOffset(1.0f, 1.0f); // Move shaded back (in screen space depth).
glEnable(GL_POLYGON_OFFSET_FILL);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthFunc(GL_LESS); // default;
glEnable(GL_DEPTH_TEST);
DrawScene();
Draw depth tested wireframes on top of that, only the frontmost remain.
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDisable(GL_POLYGON_OFFSET_FILL);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
DrawScene();

This is probably a little different than using lines. Polygon offset is only working with polygonal geometry (all OpenGL primitives except GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP). To get correct results you should not use it mixing lines and polygons, but only with switching polygon mode.
But the idea is the same. Experiment.


[This message has been edited by Relic (edited 08-12-2003).]

rtargosz
08-12-2003, 04:54 AM
glClearDepth(1.0) doesn't actually clear the depth buffer, but sets the clear value for the depth buffer. You must also call glClear(GL_COLOR_BUFFER | GL_DEPTH_BUFFER) to clear the depth buffer. From the code snippet you gave, it isn't clear whether you are doing this.