Overlapping faces of cubes

Hey,

I wrote a simple application to show cubes. When rendering a single cube, everything works fine (no overlapping of faces of one cube). But when I render more cubes, some faces overlap faces of other cubes.:frowning:

My most important methods:


private void initGL(){
 GL11.glViewport(0, 0, width, height);
 GL11.glEnable(GL11.GL_DEPTH_TEST);
 GL11.glDepthFunc(GL11.GL_LEQUAL);
 GL11.glEnable(GL11.GL_CULL_FACE);
 GL11.glMatrixMode(GL11.GL_PROJECTION);
 GL11.glLoadIdentity();
 GLU.gluPerspective(45, (float)width / height, 0, 1000);
 GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
private void render(){
 GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
 GL11.glLoadIdentity();
 GL11.glPushMatrix();
 GL11.glTranslatef(0, 0, -7);
 GL11.glRotatef(rotationY, 0, 1, 0);
 GL11.glRotatef(rotationX, 1, 0, 0);
 GL11.glTranslatef(-1, 0, 0);
 renderCube(true, false, true, true, true, true);
 GL11.glTranslatef(1, 0, 0);
 renderCube(false, false, false, true, true, true);
 GL11.glTranslatef(1, 0, 0);
 renderCube(false, true, true, true, true, true);
 GL11.glTranslatef(-1, 1, 0);
 renderCube(true, true, true, false, true, true);
 GL11.glTranslatef(0, -2, 0);
 renderCube(true, true, false, true, true, true);
 GL11.glTranslatef(0, 1, 1);
 renderCube(true, true, true, true, true, false);
 GL11.glTranslatef(0, 0, -2);
 renderCube(true, true, true, true, false, true);
 GL11.glPopMatrix();
}
private void renderCube(boolean left, boolean right, boolean top, boolean bottom, boolean front, boolean back){
 GL11.glBegin(GL11.GL_QUADS);
 //front
 if(front){
  GL11.glColor3f(1, 0, 0);
  GL11.glVertex3f(-0.5f, -0.5f, 0.5f);
  GL11.glVertex3f(0.5f, -0.5f, 0.5f);
  GL11.glVertex3f(0.5f, 0.5f, 0.5f);
  GL11.glVertex3f(-0.5f, 0.5f, 0.5f);
 }
 //left
 if(left){
  GL11.glColor3f(0, 1, 0);
  GL11.glVertex3f(-0.5f, -0.5f, -0.5f);
  GL11.glVertex3f(-0.5f, -0.5f, 0.5f);
  GL11.glVertex3f(-0.5f, 0.5f, 0.5f);
  GL11.glVertex3f(-0.5f, 0.5f, -0.5f);
 }
 //right
 if(right){
  GL11.glColor3f(0, 0, 1);
  GL11.glVertex3f(0.5f, -0.5f, -0.5f);
  GL11.glVertex3f(0.5f, 0.5f, -0.5f);
  GL11.glVertex3f(0.5f, 0.5f, 0.5f);
  GL11.glVertex3f(0.5f, -0.5f, 0.5f);
 }
 //bottom
 if(bottom){
  GL11.glColor3f(1, 1, 0);
  GL11.glVertex3f(-0.5f, -0.5f, -0.5f);
  GL11.glVertex3f(0.5f, -0.5f, -0.5f);
  GL11.glVertex3f(0.5f, -0.5f, 0.5f);
  GL11.glVertex3f(-0.5f, -0.5f, 0.5f);
 }
 //top
 if(top){
  GL11.glColor3f(1, 0, 1);
  GL11.glVertex3f(0.5f, 0.5f, -0.5f);
  GL11.glVertex3f(-0.5f, 0.5f, -0.5f);
  GL11.glVertex3f(-0.5f, 0.5f, 0.5f);
  GL11.glVertex3f(0.5f, 0.5f, 0.5f);
 }
 //back
 if(back){
  GL11.glColor3f(0, 1, 1);
  GL11.glVertex3f(0.5f, -0.5f, -0.5f);
  GL11.glVertex3f(-0.5f, -0.5f, -0.5f);
  GL11.glVertex3f(-0.5f, 0.5f, -0.5f);
  GL11.glVertex3f(0.5f, 0.5f, -0.5f);
 }
 GL11.glEnd();
}

I thought, with the statements…

GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthFunc(GL11.GL_LEQUAL);

…OpenGL always only change pixels, if the depth is less or equal to the current pixel’s depth, so it shouldn’t cause problems with overlapping cube faces, not depending on the order, the cubes are rendered.

Thanks for any help

hi there,

this is a math issue… called z-fighting …
first you may try to set:

 GLU.gluPerspective(45, (float)width / height, 0, 20); // 1000 is a quite big sene...

the bigger the scene the more difficult is it to render objects that are close to one an other …
you may try also :

GL11.glDepthFunc(GL11.GL_LESS); // not less equal ...

anyway its all based on flot point calculations.

cu
uwi

Thanks for the fast answer.

I reduced the zFar to 20. Unfortunately there’s no difference :(. And after changing the depthFunc to GL_LESS, the cubes disappear completely!

Anyway, the overlapping of the faces depends on the order, the cubes are rendered. Of course it would be possible to compute, which order to choose, but all this OpenGL methods should already support this render issue, don’t they?
I think, it’s one little thing, I did wrong, a single OpenGL option or something…

This sets a near clip plane value of 0. That’s not valid. Please see the Notes here and follow the recommendation mentioned.

Yeah, now it works!:surprise:

Thank you very much!