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:
And I’m sorry that I don’t know where exactly the problem is… so the full-code about only 50 lines is at:
(the header’s path is for Mac OS X)
I would be really grateful to hear from any possible suggestion, Thank a lot.
looks like depth testing isn’t enabled.
try glEnable(GL_DEPTH_TEST) and glWriteMask(GL_TRUE).
EDIT: post scriptum: same here, link to code dud’n work.
Good recovery Relic!
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
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
gluPerspective(45.0, 1.0, zNear, zFar);
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).
Thanks for you two’s reply. I must apologize for my mis-typing about the code’s link. It should work now.
Although I did enabled GL_DEPTH_TEST, I don’t know if I put the statement in right place…XD, and I will check the left part of your suggestion later.
And may I ask that why should I make the perspective diff( Zfar-Znear ) as small as possible? I just wonder why? Is that gonna make debugging easier?
About the zfar-znear, it is about depth precision, and the stripes you have clearly show that you need more
Second problem, face winding. You are culling the wrong faces, see the “bugs” section here :
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
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?
Ouch: glEnable( GL_DEPTH_TEST | GL_CULL_FACE );
glEnable takes an enum not a bitfield.
You must call one glEnable per feature you want to enable.
Thanks for your help & sorry for my foolish… XD
It’s really nice to have this forum here. I feel much more confident to learn OpenGL. Thank you all!!