Hi,
Here is my problem: I would like to know if the center of a specific object is occluded or not by another object in the scene.
I know about several method how to do it, I chose projecting and reading depth buffer.
Here is a part of a code I use for tests:
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, 1, 0 .1, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt( 0,0,0, 0,0,-1, 0,1,0);
glColor3f(1,0,0);
glPushMatrix();
glTranslatef(0,0,-400);
glutSolidSphere(50,60,60); //First sphere
glPopMatrix();
glColor3f(1,1,0);
glPushMatrix();
glTranslatef(0,0,-300);
glutSolidSphere(10,60,60); //Second sphere
glPopMatrix();
glGetDoublev(GL_PROJECTION_MATRIX,projMatrix);
glGetDoublev(GL_MODELVIEW_MATRIX,modelMatrix);
glGetIntegerv(GL_VIEWPORT,viewPort);
gluProject(0,0,-400,modelMatrix,projMatrix,viewPort,&wx,&wy,&wz);
glReadBuffer(GL_BACK_LEFT);
glReadPixels((GLint) wx, (GLint) wy, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &zd);
glutSwapBuffers();
in this case wz = 0.9998499916 and zd = 0.997406006 seams to be ok as second sphere occludes the first one (zd < wz)
now when I translate before second sphere to 100,0,-300 the results are
wz = 0.9998499916 and zd = 0.9998016357
as you see still zd < wz, BUT the first sphere is NOT occluded by the second one.
The main question is WHY I always get zd < wz? Maybe I dont anderstand how glProject works? Please explain me.
Thnak you in advance.