Hi,
I’m trying to use the depth test and culling to render the front faces of some transparent geometry (let’s say a cube). When I run the following code:
setPerspectiveProjection(60, 0.1, 5);
glLoadIdentity();
glTranslatef(0,0,-2);
glMultMatrixd(viewTransform.getRow(0));
glTranslatef(-0.5,-0.5,-0.5);
glEnable(GL_DEPTH_TEST);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
drawCube();
glTranslatef(1,0,0);
drawCube();
glutSwapBuffers();
I get strange results. The first cube is opaque, the second cube when you rotate allows me to see the sides of the other cube. That seems inconsistent. This is not how I expected it to behave at all. I expect all the geometry behind not to be rendered because of the depth test.
The drawCube() function is just:
glBegin(GL_QUADS)
glColor4f(0,0,0,0.5);
glTexCoord3f(0,0,0);
glVertex3f(0,0,0);
glColor4f(0,1,0,0.5);
glTexCoord3f(0,1,0);
glVertex3f(0,1,0);
glColor4f(1,1,0,0.5);
glTexCoord3f(1,1,0);
glVertex3f(1,1,0);
glColor4f(1,0,0,0.5);
glTexCoord3f(1,0,0);
glVertex3f(1,0,0);
...
Can anyone help me out?
Thanks.