PDA

View Full Version : correct glDepthFunc() parameter?


Vertizor
2004.09.27, 05:43 PM
Rather than start a new thread, I figured I would just revive this discussion on depth testing.

Not sure how similar my current problem is to Wheatie's problem, but here goes:

I'm trying to setup a scene like a 3D modeling application(*). My problem is, I'm not entirely sure how to setup depth testing properly. When I draw my [standard, required] grid plane using glBeing(GL_LINES), if it's draw first, then my geometry after, the polygons always overlap the grid lines. If I invert it (draw model first, grid after) the gridlines completely overlap the polygons, even though based on the world coords, the polygons should obscure the gridlines. Basically when I want to achieve is: the grid plane will cover the 0y plane, if a model (a box for ex.) is half above this plane, and have below this plane, the part that's below it should appear to be behind the grid lines. The part that's above the 0y mark should appear on top of the grid lines. This whole "appearance" invovles rotating the view to certain angles where I can see the grid plane intersect the model.

I realize that I could just partition my model (or better yet partition my whole scene) and reorder the drawing calls to create the desired effect. I was hoping for a better solution from OpenGL.

* - Before anyone rolls their eyes and say: "oh great another 3D modeling app." Let me explain. I'm not trying to make another 3D modeling app per se. I'd rather work on making a UV unwrapping tool for skinning.

NYGhost
2004.09.27, 07:41 PM
Seems what you need is enable depth test before you draw anything and keep it on for all your drawings.
glEnable(GL_DEPTH_TEST);

if you do so your grid lines will draw just like any 3D editor program.

Vertizor
2004.09.27, 09:54 PM
I probably should have posted my code, but I was using glEnable(GL_DEPTH_TEST) with glDepthfunc(GL_GREATER). So here's my code, using a subclassed NSOpenGLView:

- (void) prepareOpenGL
{
glShadeModel(GL_SMOOTH);
glClearColor( 0.7, 0.7, 0.7, 0 ) ;
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glFrontFace(GL_CW);
glClearDepth(1.0f); // not sure how this really works, grabbed it from NeHe tutorial
glDepthFunc(GL_GREATER); // have tried this both ways, enabling deptht test before/after depth func
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE); // I've tried this, and commented out the line. Same results.
glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
glEnable (GL_LINE_SMOOTH);
}

- (void) drawRect: (NSRect) bounds
{
float row;
int i;

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;

glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix

glLoadIdentity();
glTranslatef(0.0f, 0.0f, fZoom);
glRotatef(fRotX, 1.0f, 0.0f, 0.0f);
glRotatef(fRotY, 0.0f, 1.0f, 0.0f);

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glBegin( GL_LINES );
// inner grid lines (lighter)
glLineWidth(0.2f);
glColor3f(0.5f, 0.5f, 0.5f);
for(row = -3.5f; row<4.0f; row+=0.5f)
{
glVertex3f(-4.0f, 0.0f, row);
glVertex3f(4.0f, 0.0f, row);
}
for(row = -3.5; row<4.0f; row+=0.5f)
{
glVertex3f(row, 0.0f, -4.0f);
glVertex3f(row, 0.0f, 4.0f);
}

// outter grid borders (darker)
glLineWidth(2.0f);
glColor3f(0.3f, 0.3f, 0.3f);
glVertex3f(-4.0f, 0.0f, -4.0f);
glVertex3f(4.0f, 0.0f, -4.0f);

glVertex3f(-4.0f, 0.0f, 4.0f);
glVertex3f(4.0f, 0.0f, 4.0f);

glVertex3f(-4.0f, 0.0f, -4.0f);
glVertex3f(-4.0f, 0.0f, 4.0f);

glVertex3f(4.0f, 0.0f, -4.0f);
glVertex3f(4.0f, 0.0f, 4.0f);

glVertex3f(0.0f, 0.0f, -4.0f);
glVertex3f(0.0f, 0.0f, 4.0f);

glVertex3f(-4.0f, 0.0f, 0.0f);
glVertex3f(4.0f, 0.0f, 0.0f);

glEnd();

glDisable(GL_BLEND);
glBegin( GL_QUADS ) ;
// front
glColor3f( 0.0f, 1.0f, 0.0f) ;

for(i=0; i<4; i++)
glVertex3f(qFront[i][X], qFront[i][Y], qFront[i][Z]);

// top
glColor3f( 0.0f, 0.0f, 1.0f) ;
glVertex3f( -1.0f, 1.0f, 1.0f );
glVertex3f( -1.0f, 1.0f, -1.0f );
glVertex3f( 1.0f, 1.0f, -1.0f );
glVertex3f( 1.0f, 1.0f, 1.0f );
glEnd() ;

glFlush() ;
}

DoG
2004.09.28, 06:53 AM
glDepthfunc(GL_GREATER) makes everything draw that is BEHIND already existing objects, and should normally be invisible. You should use GL_LEQUAL instead, which means new things will draw on top of old geometry if they are closer to the viewpoint.

Vertizor
2004.09.28, 12:32 PM
Some how I knew I should have brought my iBook to work with me today.... *sigh* Maybe I'll whip up the same program in GLUT real fast to test it. Much obliged DoG!