开发者

OpenGL Z-Index Problem?

开发者 https://www.devze.com 2023-03-08 18:10 出处:网络
I am drawing a cube with LWJGL library for Java, and it draws fine, but when I rotate the cube, it seems like the front side will draw on top of the back side when it should be cut off.Here is the cod

I am drawing a cube with LWJGL library for Java, and it draws fine, but when I rotate the cube, it seems like the front side will draw on top of the back side when it should be cut off. Here is the code:

Init code:

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);     开发者_如何学Python    // 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;

Pictures:

OpenGL Z-Index Problem?

OpenGL Z-Index Problem?


Try enabling depth testing:

glEnable(GL_DEPTH_TEST);
0

精彩评论

暂无评论...
验证码 换一张
取 消