Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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));

        } catch (LWJGLException e) {

        gluPerspective(30f, 1024f / 768f, 0.00001f, 100);
        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


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

                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

                glTranslatef(0, 0, speed);              

                    rotationX +=1;

                    rotationY += 1;                 

                    rotationZ += 1;

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

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

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

                        glColor3f(1, 1, 1);
                        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);


                    speed += 0.05f;

                    speed -= 0.05f;



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.
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
up vote 2 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
I forgot to update my zNear and zFar after I put some objects in my world. So this answer solved my problem! Thank you! – Hamed Rajabi Apr 15 '15 at 5:48
I find 1.0f a bit excessive, you may want to use a smaller value, like 0.1f or such. – rioki Apr 15 '15 at 9:03

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);
glEnable(GL_DEPTH_TEST); // <-- it works here; anywhere above it doesn't work
share|improve this answer

Your Answer


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.