2
\$\begingroup\$

I'm making my first steps with OpenGL with LWJGL. I am currenlty constructing a... wait for it... CUBE. Quite creative...

Up to now i made it to three of its faces and thats where the Problems began. Sometimes its faces overlap each other. Googeling that Problem brought up DEPTH TESTING thing which i tried immediately unfortunately with no luck. The Problem remains the same. The faces are still "overlapping" eachother and it seems that this is somehow related to the drawing order.

Here is my complete code:

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.gluPerspective;


public class Cube {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            Display.setDisplayMode(new DisplayMode(1024, 768));
            Display.create();
            Display.setVSyncEnabled(true);

        } catch (LWJGLException e) {
            e.printStackTrace();
        }

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(30f, 1024f / 768f, 0.00001f, 100);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glEnable(GL_3D_COLOR);
        glClearDepth(1.0f);                      // Depth Buffer Setup
        glEnable(GL_DEPTH_TEST);              // Enables Depth Testing
        //glDepthFunc(GL_ALWAYS);               // The Type Of Depth Testing To Do
        // Really Nice Perspective Calculations
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); 
        //glShadeModel(GL_SMOOTH);

        System.out.println(glGetString(GL_VERSION));

        float speed = 0.0f;
        float rotationX = 0;
        float rotationY = 0;
        float rotationZ = 0;

            while(!Display.isCloseRequested()){
                System.out.println(glGetError());
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

                glTranslatef(0, 0, speed);              

                if(Mouse.isButtonDown(0)){                  
                    rotationX +=1;
                }

                if(Mouse.isButtonDown(1)){                  
                    rotationY += 1;                 
                }

                if(Mouse.isButtonDown(2)){                  
                    rotationZ += 1;
                }

                rotationX %= 360;
                rotationY %= 360;
                rotationZ %= 360;

                glPushMatrix();
                    glTranslatef(0, 0, -50);
                    glRotatef(rotationX, 1, 0, 0);
                    glRotatef(rotationY, 0, 1, 0);
                    glRotatef(rotationZ, 0, 0, 1);
                    glBegin(GL_QUADS);

                        glColor3f(1, 0, 0);
                        //Deckel    
                        glVertex3f(0, 5, 0);
                        glVertex3f(5, 5, 0);
                        glVertex3f(5, 5, -5);
                        glVertex3f(0, 5, -5);

                        glColor3f(1, 1, 1);
                        //Front                     
                        glVertex3f(0, 0, 0);                        
                        glVertex3f(5, 0, 0);                        
                        glVertex3f(5, 5, 0);
                        glVertex3f(0, 5, 0);

                        glColor3f(0, 1, 0);

                        glVertex3f(0, 5, -5);
                        glVertex3f(5, 5, -5);
                        glVertex3f(5, 0, -5);
                        glVertex3f(0, 0,-5);

                    glEnd();
                glPopMatrix();

                if(Keyboard.isKeyDown(Keyboard.KEY_UP)){
                    speed += 0.05f;
                }

                if(Keyboard.isKeyDown(Keyboard.KEY_DOWN)){
                    speed -= 0.05f;
                }

                Display.update();
                Display.sync(60);
            }

            Display.destroy();      
    }
}

If you're using the code, you can rotate the "cube" by using the three mouse buttons

  • Is there anything that i'm obviously doing wrong?
  • Does this code work for anyone?
  • Could this problem result from using this on Linux?, 'cause that's what i'm doing.
\$\endgroup\$
1
  • \$\begingroup\$ I'm not a lawyer but I don't think this code is criminal on linux or any other OS for what matter. \$\endgroup\$
    – petervaz
    May 14, 2013 at 19:08

2 Answers 2

3
\$\begingroup\$

Sometimes using a 0 or very low value for the zNear will result in a bad depth calculation. Try using the following instead:

   gluPerspective(30f, 1024f / 768f, 1.0f, 100);
\$\endgroup\$
5
  • \$\begingroup\$ Thanks, but this doesn't seem to work. For zNear i already tried a wide range of values including 1.0f. But this does not help. \$\endgroup\$
    – Chris
    May 15, 2013 at 8:02
  • \$\begingroup\$ Well.... i just tested this on windows and at first with the class above i had the same issues as i have on linux. And after changing the zNear as you suggested it works perfectly. I'll try to play with that value on linux to fix this. \$\endgroup\$
    – Chris
    May 15, 2013 at 11:00
  • \$\begingroup\$ Actually this now solved the problem when i first tested it, i incidentally had depth desting deactivated. Thanks! \$\endgroup\$
    – Chris
    May 16, 2013 at 5:17
  • \$\begingroup\$ I forgot to update my zNear and zFar after I put some objects in my world. So this answer solved my problem! Thank you! \$\endgroup\$ Apr 15, 2015 at 5:48
  • \$\begingroup\$ I find 1.0f a bit excessive, you may want to use a smaller value, like 0.1f or such. \$\endgroup\$
    – rioki
    Apr 15, 2015 at 9:03
1
\$\begingroup\$

Having depth problem, I found out a topic telling to place glEnable(GL_DEPTH_TEST); on display function. So I played out with it, and found out that in my case it works fine as last call in init function. So putting in display function would be overdone.

Example of my init function:

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_ALPHA|GLUT_DOUBLE|GLUT_DEPTH);
glShadeModel(GL_SMOOTH);
glClearDepth(1.0f);
glDepthFunc(GL_LESS);
glDepthMask(GL_TRUE);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_DEPTH_TEST); // <-- it works here; anywhere above it doesn't work
\$\endgroup\$
1
  • \$\begingroup\$ Putting "glEnable(GL_DEPTH_TEST);"' as the last init command allowed me to have negative "ClippingPlaneNear". I am not using "gluPerspective()", so your answer is the only one that worked. \$\endgroup\$ Jul 3, 2020 at 15:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .