Hello, I am trying to understand how work enable depth buffer with glDepthMask(false) and glDepthFunc(GL_GREATER).
I try to render the first frame with a depth buffer enabled and glDepthMask(true)
this generate my depth mask to apply to successive render frame.
the successive frame I translate and update offset variable and translate the object along z negative.
The object translate result behind the initial object (object render at the first frame with zero offset) so I expected that
cause glDepthFunc(GL_GREATER) it should be rendered. But I saw that lighting is incorrect and I don’t understand
why? The object without lighting is render as I expected while with light enabled I saw it incorrect. Any idea?
void GLApp::initializeGL()
{
GLint alpha_bits;
glGetIntegerv(GL_ALPHA_BITS, & alpha_bits);
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 100.0 };
GLfloat mat_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 0.0, 0.0, 1.0, 0.0 };
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glPixelStorei(GL_PACK_ROW_LENGTH, 0);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glEnable(GL_NORMALIZE);
glEnable(GL_POINT_SMOOTH);
glShadeModel (GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
GLenum err = glewInit();
if (GLEW_OK != err)
{
fprintf(stderr, "Error: %s
", glewGetErrorString(err));
}
glClear(GL_DEPTH_BUFFER_BIT);
makeCurrent();
}
void GLApp::paintGL()
{
static bool first=true;
if (lighting) glEnable(GL_LIGHTING);
else glDisable(GL_LIGHTING);
glClearColor(0.0,0.0,0.0,0.0);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0,0, this->width(),this->height());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat) this->width()/(GLfloat) this->height(), 0.1, farPlane);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0,0,-5);
if (first)
{
glDepthMask(GL_TRUE);
first=false;
glColor3f(1,1,1);
}
else
{
glTranslatef(0,0,offset);
glDepthFunc(GL_GREATER);
glDepthMask(GL_FALSE);
}
glutSolidTeapot(1);
renderText(0,height()-20,QString("offset ")+QString::number(offset));
renderText(0,height()-10,QString("farPlane ")+QString::number(farPlane));
}
Here the result with and without lighting.