Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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 i'm oviously doing wrong?
  • Does this code work for anyone?
  • Could this problem result from using this on Linux?, 'cause that's what i'm doing.
share|improve this question
    
I'm not a lawyer but I don't think this code is criminal on linux or any other OS for what matter. –  petervaz May 14 '13 at 19:08
add comment

1 Answer

up vote 1 down vote accepted

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);
share|improve this answer
    
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. –  Chris May 15 '13 at 8:02
    
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. –  Chris May 15 '13 at 11:00
    
Actually this now solved the problem when i first tested it, i incidentally had depth desting deactivated. Thanks! –  Chris May 16 '13 at 5:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.