Hello, I’m a newbie to OpenGL, and currently using GLUT to do some experiment.
Here is a problem I got when I try to light a glutSolidTeapot by a light in direction of (0, 0, -1). But I got the teapot that half of it “soaking” in a totally non-transparent surface, and the it seems also has the perspective problem( some surface near the eye become totally transparent) like this:
The link to the code doesn’t work for me, but from the image it looks like a simple depth buffer problem.
Make sure you have
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
in your code.
Also check if your viewing frustum is overly deep.
Make the zFar/zNear ratio as small as possible without clipping your teapot.
If the teapot is unit size use something like:
// For clarity:
GLdouble zNear = 1.0; // Must be > 0.0!
GLdouble zFar = 5.0; // Must be > zNear
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.0, zNear, zFar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, (float) (-0.5 * (zFar - zNear))); // Move origin in the z center, here 2.0 in world coordinates. But z-translation need to be negative in this matrix, read about the handedness of coordinate systems in the RedBook.
For debugging set the glClearColor to something you don’t normally use, like magenta, to avoid black-screen-of-death and to see if the black parts inside the teapot are actually background pixels. They shouldn’t be if your problem is depth testing.
Aeluned beat me, but dropped the ball, it’s glDepthMask(GL_TRUE).
Hello, seems the problem came with the glEnable(GL_CULL_FACE), after getting rid of the statement and glFrontFace(), everything goes fine !!
But allow me to ask further that, even I follow the suggestion of shorten (Zfar-Znear) and to cull the face in order of
glFrontFace(GL_CW);
glutSolidTeapot(size);
glFrontFace(GL_CCW);
but my code just can’t work properly, and here's the code I’ve made change on ZBuffer and cull face by the suggestions above. May anyone help once again?