# Problem with the depth buffer(i suppose)

i’m having the following problem:
in my scene i have a cylinder (made with gluquadricobject etc) and let’s say a teapot(glutsolidteapot). the cylinder runs through the teapot. when i use gluperspective the teapot looks broken,distorted, sth like noise in the color or such,especially where the cyl runs it through.
I am using VS.NET 2003
Any ideas?
Thanx…

This page describes why this problem occurs and the solution to it. It may be a bit difficult to understand, but you should remember what it says in the conclusion at least.

Sounds like your near plane distance (parameter to gluPerspective) is too small. Try 1.0.

Due to perspective divide, depth precision is distributed in a non-linear fashion. Something like z’=z/(z+near_distance). You get the best resolution at and close behind the near plane, and it gradually gets worse towards the far plane.

Very small values for the near plane pull so much of the total precision towards the near plane that effective depth buffer resolution in the middle of the frustum gets totally horrible, even with lots of bits in the depth buffer.

thanx very much guys!
tried it and it happens exactly what the site bob gave me says:
“…they will randomly show through each other - sometimes in large zig-zags, sometimes in stripes.
…” regardless of the zNear i use.
isn’t there any other way?

p.s.any ideas how to see what z-buffer my card(9800XT) has?

just noticed that the z-buffer is generally screwed,especially when rotating the teapot on the y-axis.the handle and the nozzle do not show correctly.
so i figured i must do something with the depth buffer/func.i’ve tried every combination of the following, including all their values:

``````
glEnable( GL_DEPTH_TEST );
glDepthFunc( ... );
glClearDepth( 0.0 );

``````

thanx again…

No, you don’t need to fiddle with those.

## Conclusion Always put zNear as far from the eye as you can tolerate.

So never put 0 as zNear, and try with at least ten times your current value. Do it again until the scene gets clipped.
For zFar, set it as small as possible so that the scene is still displayed.

gluPerspective(GLdouble fovy,
GLdouble aspect,
GLdouble zNear,
GLdouble zFar)

There is frequently 2 precisions for the depth buffer, 16 and 24 bits. This is selected when creating the gl context. Can you post your code and values for the init and the gluPerspective call ?

i’ve devided my programm in two cpps:
the main that includes:

``````	glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);
glutInitWindowSize(800,800);
glutInitWindowPosition(50,50);
glutCreateWindow("Board v0.0");

Setup();

glutDisplayFunc(Render);
glutReshapeFunc(Resize);
glutKeyboardFunc(Key);
glutIdleFunc(Idle);

glutMainLoop();
``````

and the visuals:

``````//render method
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0+movex,0.0+movey,100.0-movez,0.0,0.0,0.0,0.0,1.0,0.0);

glPushMatrix();
glFrontFace(GL_CW);
glTranslatef(0.0,5*sin(f),0.0);
glColor3f(0.5,0.7,0.4);
glutSolidTeapot(10.0);
glPopMatrix();

glPushMatrix();
glFrontFace(GL_CCW);
glColor3f(0.0,0.0,1.0);
glTranslatef(0.0,-25.0,0.0);
glRotatef(-90.0,1.0,0.0,0.0);
gluCylinder(pole, 2.0, 2.0,50.0,100.0,100.0);
glPopMatrix();

glutSwapBuffers();

//resize method
if (h==0) h=1;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
gluPerspective(60.0,(float)w/(float)h,90.0,200.0);

//setup method:
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_GREATER );
glClearDepth( 0.0 );
glEnable( GL_CULL_FACE );
``````

in the satup are the light initialization and the polygon rendering mode setup

I’ve tried changing both of the clipping distances and still nothing.it shows exactly the same.

changed it to gluPerspective(60.0,(float)w/(float)h,90.0,120.0);
still doesn’t work.

Solved it!
Turned out the whole problem was with the light.
Changed the lightpos into:
GLfloat lightPos[] = { -50.0, 20.0, -150.0, 1.0 };
and it worked!
I was just experimenting so can anyone expolain why i had to put the light source behind the objects?(or so i think)

Thanx VERY much guys…

I’d like to add that you set up your depth buffer in an unusual way, which might confuse other programmers working with your code, or perhaps even yourself, in a few months.

You set ClearDepth to 0.0 and the test function to GREATER, which is the opposite of the default, and the opposite of what most people use (ClearDepth=1.0 and test=LESS or LEQUAL).

By convention, 0.0 in the depth buffer is the nearest possible value and 1.0 is the farthest possible value (e.g. look at glDepthRange).

I did some diving into the msdn library and saw it like you said.I’ve changed the code a bit and i think i got it to work.I found most of it by exprimenting so i am not sure exactly what did it.
I came to the conclusion,however,that it is a combination of what you said and the perspective initialization.

However i still cannot get it why, by changing the light, it seemed to work.It was wrong programmatically but in the eye seemed to work.

Thanx a lot again for all the help…