Search code examples
openglrenderinglwjglcube

LWJGL Cube Demo strange rendering


I seem to be getting some odd issues with clipping from this small demo of a rotating cube:

    private static int x = 0;

    public static void start(Demo lf) {
        try {
            Display.setDisplayMode(new DisplayMode(640,640));
            Display.setTitle("Cube Demo 3D");
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
        }

        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(-5, 5, -5, 5, -1, 5);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        while (!Display.isCloseRequested()) {
            Display.sync(60);
            CubeRenderer.render(lf);
        }

        Display.destroy();
    }

    public static void render(Demo lf) {
        GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT|GL11.GL_COLOR_BUFFER_BIT);
        GL11.glEnable(GL11.GL_CULL_FACE);
        GL11.glEnable(GL11.GL_DEPTH_TEST);
        GL11.glColor3f(1, 0, 0);

        GL11.glRotatef(x++, 1, 1, 1);

        GL11.glBegin(GL11.GL_QUADS);
        GL11.glColor3f(1.0f,1.0f,1.0f);
        GL11.glVertex3f( 1.0f, 1.0f,-1.0f);
        GL11.glVertex3f(-1.0f, 1.0f,-1.0f);
        GL11.glVertex3f(-1.0f, 1.0f, 1.0f);
        GL11.glVertex3f( 1.0f, 1.0f, 1.0f);
        GL11.glColor3f(1.0f,1.0f,1.0f);
        GL11.glVertex3f( 1.0f,-1.0f, 1.0f);
        GL11.glVertex3f(-1.0f,-1.0f, 1.0f);
        GL11.glVertex3f(-1.0f,-1.0f,-1.0f);
        GL11.glVertex3f( 1.0f,-1.0f,-1.0f);
        GL11.glColor3f(1.0f,1.0f,1.0f);
        GL11.glVertex3f( 1.0f, 1.0f, 1.0f);
        GL11.glVertex3f(-1.0f, 1.0f, 1.0f);
        GL11.glVertex3f(-1.0f,-1.0f, 1.0f);
        GL11.glVertex3f( 1.0f,-1.0f, 1.0f);
        GL11.glColor3f(1.0f,1.0f,1.0f);
        GL11.glVertex3f( 1.0f,-1.0f,-1.0f);
        GL11.glVertex3f(-1.0f,-1.0f,-1.0f);
        GL11.glVertex3f(-1.0f, 1.0f,-1.0f);
        GL11.glVertex3f( 1.0f, 1.0f,-1.0f);
        GL11.glColor3f(1.0f,1.0f,1.0f);
        GL11.glVertex3f(-1.0f, 1.0f, 1.0f);
        GL11.glVertex3f(-1.0f, 1.0f,-1.0f);
        GL11.glVertex3f(-1.0f,-1.0f,-1.0f);
        GL11.glVertex3f(-1.0f,-1.0f, 1.0f);
        GL11.glColor3f(1.0f,1.0f,1.0f);
        GL11.glVertex3f( 1.0f, 1.0f,-1.0f);
        GL11.glVertex3f( 1.0f, 1.0f, 1.0f);
        GL11.glVertex3f( 1.0f,-1.0f, 1.0f);
        GL11.glVertex3f( 1.0f,-1.0f,-1.0f);
        GL11.glEnd();

        GL11.glLoadIdentity();
        Display.update();

    }

    public static void main(String argv[]) {
        Demo lf = new Demo(100);
        lf.step();
        CubeRenderer.start(lf);
    }
}

Here's a picture of the result:

Problems with clipping

It looks like the front face of the cube occasionally becomes invisible as the cube rotates, showing a black triangle.


Solution

  • Your orthographic projection matrix is cutting off part of the cube:

    GL11.glOrtho(-5, 5, -5, 5, -1, 5);
    

    The near plane is at -1. However, as your rotate a unit cube, some points on the transformed surface will be closer than -1. The rasterizer will drop the parts of the geometry that extend past the near plane.

    Since you have face culling turned on you're not seeing the rear-facing triangles through the cube. The effect would be more obvious if you assigned each face of the cube a different color and turned off face culling. Then you'd be able to see the opposite sides of the cube through the hole being created.

    Try setting your near plane to -5, or try reducing the cube in size.

    Also as an aside, I suggest you avoid Display.sync() and simply use Display.setVSyncEnabled(true) during initialization to control the framerate.