PDA

View Full Version : Bizzare depth buffer problem



Virusx86
07-23-2003, 08:00 PM
I have no idea why this is occuring... but for some reason the depth buffer is not being cleared in between frames in my program. It has something to do with enabling/disabling glDepthMask.

Here's the situation:
If i enable glDepthMask (disabling GL_BLEND) to render my opaque object, then disable it (enabling GL_BLEND) for my transparent object, the depth information for the opaque object is not removed when i use glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) and opaque object is occluded if it, for example, is moved further away.

However... if i leave glDepthMask either enabled or disabled for the entirety of the program, everything will go fine (Except of course for depth checking issues)

i'm sure it has something to do with the specifics of my code, so i'll post my rendering portion here:

int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity();

if(!blend)
{
glDisable(GL_BLEND);
glDepthMask(GL_TRUE);
}

glPushMatrix(); //start screwing with stuff now!
glTranslatef(0.0f, 0.0f, zdepth);// Move Into The Screen

glRotatef(xrot,1.0f,0.0f,0.0f);// Rotate On The X Axis
glRotatef(yrot,0.0f,1.0f,0.0f);// Rotate On The Y Axis

glCallList(box);
glPopMatrix();

glDepthMask(GL_FALSE);
glEnable(GL_BLEND);

glPushMatrix(); // Reset The Current Modelview Matrix
glPushAttrib(GL_LIGHTING_BIT);
glDisable(GL_LIGHTING);
glTranslatef(LightPosition[0], LightPosition[1], LightPosition[2]);

glCallList(light);
glEnable(GL_LIGHTING);
glPopAttrib();

glLightfv(GL_LIGHT1, GL_POSITION,LightPosition); // Re-Position The Light
glPopMatrix();

return TRUE;// Keep Going
}

and my gl initilization

int InitGL(GLvoid) // All Setup For OpenGL Goes Here
{
glMatrixMode(GL_MODELVIEW);
multitextureSupported=initMultitexture();
glLoadIdentity();

if (!LoadGLTextures()) // Jump To Texture Loading Routine ( NEW )
{
return FALSE; // If Texture Didn't Load Return FALSE ( NEW )
}
glEnable(GL_TEXTURE_2D);
glDisable(GL_NORMALIZE);

glEnable(GL_COLOR_MATERIAL);// Enable Material Coloring
glColor4f(1.0f,1.0f,1.0f,1.0f);
glMaterialf(GL_FRONT, GL_SHININESS, 25.0f);
glMaterialfv(GL_FRONT,GL_SPECULAR, matSpec);
buildLists();
// Enable Texture Mapping ( NEW )
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(fogColor[0],fogColor[1],fogColor[2],fogColor[3]); // We'll Clear To The Color Of The Fog ( Modified )

glClearDepth(1.0f);

// Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL);

glEnable(GL_CULL_FACE);

glBlendFunc(GL_SRC_ALPHA,GL_ONE); // Blending Function For Translucency Based On Source Alpha Value ( NEW )

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations

initLights();

//fog
glFogi(GL_FOG_MODE, GL_LINEAR);// Fog Mode
glFogfv(GL_FOG_COLOR, fogColor);// Set Fog Color
glFogf(GL_FOG_DENSITY, fogDensity);// How Dense Will The Fog Be
glHint(GL_FOG_HINT, GL_NICEST);// Fog Hint Value
glFogf(GL_FOG_START, 1.0f);// Fog Start Depth
glFogf(GL_FOG_END, 200.0f);// Fog End Depth

return TRUE; // Initialization Went OK
}

dorbie
07-23-2003, 08:44 PM
OpenGL state is persistent and your last call w.r.t. masking in your loop is glDepthMask(GL_FALSE). When you finish the previous frame you leave the depth buffer masked, so when you come around the next frame the depth buffer is still masked when you call clear and so the depth buffer clear has no effect.

Virusx86
07-24-2003, 12:10 PM
Ahhhh, that was it. Thank you. For some reason i assumed that the glClear command wasn't restricted by glDepthMask, but i was clearly wrong.