Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Overlapping faces of cubes

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2013
    Posts
    5

    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.

    My most important methods:

    Code :
    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

  2. #2
    Intern Contributor uwi2k2's Avatar
    Join Date
    Mar 2011
    Posts
    98

    math math math

    hi there,

    this is a math issue... called z-fighting ....
    first you may try to set:
    Code :
     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 :
    Code :
    GL11.glDepthFunc(GL11.GL_LESS); // not less equal ...
    anyway its all based on flot point calculations.

    cu
    uwi
    uwi2k2 - OpenGL Trainer: www.opengl-trainer.com
    ---------------------------------------------------------
    Free Programming Helpline: www.programming-hotline.com
    ---------------------------------------------------------
    A Friday Night Programmer: www.uwiworld.net

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2013
    Posts
    5
    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...

  4. #4
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,184
    Quote Originally Posted by bene2808 View Post
    GLU.gluPerspective(45, (float)width / height, 0, 1000);
    This sets a near clip plane value of 0. That's not valid. Please see the Notes here and follow the recommendation mentioned.

    * https://www.opengl.org/sdk/docs/man2...erspective.xml

  5. #5
    Junior Member Newbie
    Join Date
    Dec 2013
    Posts
    5
    Yeah, now it works!

    Thank you very much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •