سؤال

I'm trying to learn how to write some code to draw a cube and rotate it. I decided to draw them using triangles and after I drew 3 sides of the cube, I quickly realized there was an issue where the white side of the cube would overlap the red and blue faces and the blue would overlap the red face.

I did notice that: - the white face is drawn last - the blue face is drawn before the white face - the red face is drawn before the blue face

I suspect this may be causing the problem since the white is drawn on top of the blue and blue on top of the red.

Am I on the right track? Can anyone help me find a solution to this problem?

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();
    }
}
هل كانت مفيدة؟

المحلول

Request a depth buffer and enable depth testing via glEnable(GL_DEPTH_TEST).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top