无法在LWJGL中绘制多维数据集

时间:2013-11-22 05:10:37

标签: opengl lwjgl cube depth

我正在尝试学习如何编写一些代码来绘制立方体并旋转它。我决定使用三角形绘制它们,在我绘制立方体的三个边之后,我很快意识到立方体的白色边与红色和蓝色面重叠,蓝色与红色面重叠。

我注意到: - 最后画出白脸 - 在白脸前画出蓝色的脸 - 红脸在蓝脸前绘制

我怀疑这可能会导致问题,因为白色被绘制在红色顶部的蓝色和蓝色之上。

我是否在正确的轨道上?任何人都可以帮我找到解决这个问题的方法吗?

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

import static org.lwjgl.opengl.GL11.*;

public class RubiksCube {

    int angle = 0;

    public void start() {

        final int width = 800;
        final int height = 600;
        try {
            Display.setDisplayMode(new DisplayMode(width, height));
            Display.create();
        } catch (LWJGLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.exit(0);
        }

        // init OpenGL here

        while (!Display.isCloseRequested()) {  
            render();

            angle = (angle+1)%360;
            Display.update();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        Display.destroy();
    }

    public void render(){

        float edgeLength= 20.0f;
        edgeLength /= 2.0f;

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0.0f, Display.getDisplayMode().getWidth(), Display.getDisplayMode().getHeight(), 0.0f, -50.0f, 50.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear screen
        glPushMatrix();
        glTranslatef((Display.getWidth()/2), (Display.getHeight()/2), 0.0f);
        glRotatef(angle, 1.0f, 1.0f, 1.0f);
        glBegin(GL_TRIANGLES);

        //Back

        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex3f(-edgeLength, edgeLength, edgeLength);
        glVertex3f(-edgeLength, -edgeLength, edgeLength);
        glVertex3f(edgeLength, -edgeLength, edgeLength);
        glVertex3f(-edgeLength, edgeLength, edgeLength);
        glVertex3f(edgeLength, edgeLength, edgeLength);
        glVertex3f(edgeLength, -edgeLength, edgeLength);

        //Front

        glColor3f(0.0f, 0.0f, 1.0f);
        glVertex3f(-edgeLength, edgeLength, -edgeLength);
        glVertex3f(-edgeLength, -edgeLength, -edgeLength);
        glVertex3f(edgeLength, -edgeLength, -edgeLength);
        glVertex3f(-edgeLength, edgeLength, -edgeLength);
        glVertex3f(edgeLength, edgeLength, -edgeLength);
        glVertex3f(edgeLength, -edgeLength, -edgeLength);

        // Right
        glColor3f(1.0f, 1.0f, 1.0f);
        glVertex3f(edgeLength, edgeLength, -edgeLength);
        glVertex3f(edgeLength, -edgeLength, -edgeLength);
        glVertex3f(edgeLength, -edgeLength, edgeLength);
        glVertex3f(edgeLength, edgeLength, -edgeLength);
        glVertex3f(edgeLength, edgeLength, edgeLength);
        glVertex3f(edgeLength, -edgeLength, edgeLength);

        glEnd();
        glPopMatrix();
    }
}

1 个答案:

答案 0 :(得分:0)

请求深度缓冲区并通过glEnable(GL_DEPTH_TEST)启用深度测试。