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?

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.

Did you read the site that Bob posted ?

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);
	glutCreateWindow("Board v0.0");


and the visuals:

//render method


	GLUquadricObj* pole = gluNewQuadric();
	gluCylinder(pole, 2.0, 2.0,50.0,100.0,100.0);

//resize method
	if (h==0) h=1;

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

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

I’ve tried changing both of the clipping distances and still 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…