OpenGL Z-Index问题?

时间:2011-05-28 02:54:56

标签: java opengl z-index lwjgl

我正在使用LWJGL库绘制一个用于Java的立方体,并且绘制得很好,但是当我旋转立方体时,看起来正面会在应该被切断时在背面的顶部绘制。这是代码:

初始化代码:

GL11.glViewport(0, 0, width, height);

GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();

GLU.gluPerspective(45.0f, (float)width/(float)height, 0.1f, 100.0f);

GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();

Cube Code:

    GL11.glTranslatef(0.0f, 0.0f, -6.0f);

    GL11.glRotatef(rquad, 0.0f, 1.0f, 0.0f);

    GL11.glBegin(GL11.GL_QUADS);
        GL11.glColor3f(0.0f, 1.0f, 0.0f);           // Set The Color To Green
        GL11.glVertex3f( 1.0f, 1.0f,-1.0f);         // Top Right Of The Quad (Top)
        GL11.glVertex3f(-1.0f, 1.0f,-1.0f);         // Top Left Of The Quad (Top)
        GL11.glVertex3f(-1.0f, 1.0f, 1.0f);         // Bottom Left Of The Quad (Top)
        GL11.glVertex3f( 1.0f, 1.0f, 1.0f);         // Bottom Right Of The Quad (Top)

        GL11.glColor3f(1.0f, 0.5f, 0.0f);           // Set The Color To Orange
        GL11.glVertex3f( 1.0f,-1.0f, 1.0f);         // Top Right Of The Quad (Bottom)
        GL11.glVertex3f(-1.0f,-1.0f, 1.0f);         // Top Left Of The Quad (Bottom)
        GL11.glVertex3f(-1.0f,-1.0f,-1.0f);         // Bottom Left Of The Quad (Bottom)
        GL11.glVertex3f( 1.0f,-1.0f,-1.0f);         // Bottom Right Of The Quad (Bottom)

        GL11.glColor3f(1.0f, 0.0f, 0.0f);           // Set The Color To Red
        GL11.glVertex3f( 1.0f, 1.0f, 1.0f);         // Top Right Of The Quad (Front)
        GL11.glVertex3f(-1.0f, 1.0f, 1.0f);         // Top Left Of The Quad (Front)
        GL11.glVertex3f(-1.0f,-1.0f, 1.0f);         // Bottom Left Of The Quad (Front)
        GL11.glVertex3f( 1.0f,-1.0f, 1.0f);         // Bottom Right Of The Quad (Front)

        GL11.glColor3f(1.0f, 1.0f, 0.0f);           // Set The Color To Yellow
        GL11.glVertex3f( 1.0f,-1.0f,-1.0f);         // Bottom Left Of The Quad (Back)
        GL11.glVertex3f(-1.0f,-1.0f,-1.0f);         // Bottom Right Of The Quad (Back)
        GL11.glVertex3f(-1.0f, 1.0f,-1.0f);         // Top Right Of The Quad (Back)
        GL11.glVertex3f( 1.0f, 1.0f,-1.0f);         // Top Left Of The Quad (Back)

        GL11.glColor3f(0.0f, 0.0f, 1.0f);           // Set The Color To Blue
        GL11.glVertex3f(-1.0f, 1.0f, 1.0f);         // Top Right Of The Quad (Left)
        GL11.glVertex3f(-1.0f, 1.0f,-1.0f);         // Top Left Of The Quad (Left)
        GL11.glVertex3f(-1.0f,-1.0f,-1.0f);         // Bottom Left Of The Quad (Left)
        GL11.glVertex3f(-1.0f,-1.0f, 1.0f);         // Bottom Right Of The Quad (Left)

        GL11.glColor3f(1.0f, 0.0f, 1.0f);           // Set The Color To Violet
        GL11.glVertex3f( 1.0f, 1.0f,-1.0f);         // Top Right Of The Quad (Right)
        GL11.glVertex3f( 1.0f, 1.0f, 1.0f);         // Top Left Of The Quad (Right)
        GL11.glVertex3f( 1.0f,-1.0f, 1.0f);         // Bottom Left Of The Quad (Right)
        GL11.glVertex3f( 1.0f,-1.0f,-1.0f);         // Bottom Right Of The Quad (Right)
    GL11.glEnd();

    rquad += 0.1;

图片:

Cube Picture #1

Cube Picture #2

1 个答案:

答案 0 :(得分:3)

尝试启用深度测试:

glEnable(GL_DEPTH_TEST);