0

我正在做一个项目来学习渲染 3d 形状。为了获得正确的深度,我正在尝试使用 glEnable(GL_DEPTH_TEST)。正如预期的那样,它不会渲染任何东西,所以我添加了 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT),但我仍然没有得到任何渲染。这是我的代码的简化版本。

public class Main {

public static void main(String[] args) {

    initDisplay();
    gameLoop();
    cleanUp();
}
public static void initDisplay(){
    try {

        Display.setDisplayMode(new DisplayMode(500,500));
        Display.create();

    } catch (LWJGLException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
}


public static void gameLoop(){
    float x = 0;
    float y = 0;
    float z = -20;
    float cubeX = 0;
    float cubeY = 0;
    float cubeZ = 0;
    float apothem = 1;
    float aspect = Display.getWidth()/Display.getHeight();
    Camera cam = new Camera(aspect,70,0.0f, 1000);
    Cube cube = new Cube(1);
    while(!Display.isCloseRequested() && Display.isActive()){
        if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT)){
            x+=.003;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_LEFT)){
            x-=.003;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_UP)){
            z-=.003;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_DOWN)){
            z+=.003;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_D)){
            cubeZ+=.03;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_A)){
            cubeZ-=.03;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_W)){
            cubeX+=.03;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_S)){
            cubeX-=.03;
        }
        cam.setX(x);
        cam.setY(y);
        cam.setZ(z);

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glLoadIdentity();
        cam.useView();
        cube.render(cubeX,cubeY,cubeZ);
        Display.update();
    }

}

这是用于创建和渲染立方体的类

public class Cube {
float apothem;
Cube(float apothem){
    this.apothem = apothem;
}
public void render(float x, float y, float z){

    glPushMatrix();

        glRotatef(x,1,0,0);
        glRotatef(y,0,1,0);
        glRotatef(z,0,0,1);
        glBegin(GL_QUADS);
            //front face
            glColor3f(.22f, .64f, .14f);
            glVertex3d(-apothem,apothem,apothem);
            glVertex3d(apothem,apothem,apothem);
            glVertex3d(apothem,-apothem,apothem);
            glVertex3d(-apothem,-apothem,apothem);

            //back face
            glColor3f(.23f, .26f, .64f);
            glVertex3d(-apothem,apothem,-apothem);
            glVertex3d(apothem,apothem,-apothem);
            glVertex3d(apothem,-apothem,-apothem);
            glVertex3d(-apothem,-apothem,-apothem);

            //left face
            glColor3f(.72f, .16f, .39f);
            glVertex3d(-apothem,apothem,-apothem);
            glVertex3d(-apothem,apothem,apothem);
            glVertex3d(-apothem,-apothem,apothem);
            glVertex3d(-apothem,-apothem,-apothem);

            //right face
            glColor3f(.53f, .21f, .84f);
            glVertex3d(apothem,-apothem,apothem);
            glVertex3d(apothem,-apothem,-apothem);
            glVertex3d(apothem,apothem,-apothem);
            glVertex3d(apothem,apothem,apothem);

            //top face
            glColor3f(.42f, .99f, .37f);
            glVertex3d(-apothem,apothem,apothem);
            glVertex3d(-apothem,apothem,-apothem);
            glVertex3d(apothem,apothem,-apothem);
            glVertex3d(apothem,apothem,apothem);

            //bottom face
            glColor4f(.44f, .11f, .74f,25f);
            glVertex3d(apothem,-apothem,-apothem);
            glVertex3d(apothem,-apothem,apothem);
            glVertex3d(-apothem,-apothem,apothem);
            glVertex3d(-apothem,-apothem,-apothem);
       glEnd();
   glPopMatrix();




}

这是我用来查看事物的相机

private float x,y,z;
private float rx,ry,rz;

private float aspect;
private float fov;
private float zNear, zFar;

Camera(float aspect, float fov, float zNear, float zFar){
    x = 0;
    y = 0;
    z = 0;
    rx = 0;
    ry = 0;
    rz = 0;
    this.aspect = aspect;
    this.fov = fov;
    this.zNear = zNear;
    this.zFar = zFar;
    initProjection();

}
private void initProjection(){
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(fov, aspect, zNear, zFar);
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_DEPTH_TEST);
}
public void useView(){
    glRotatef(rx,1,0,0);
    glRotatef(ry,0,1,0);
    glRotatef(rz,0,0,1);
    glTranslatef(x,y,z);
}
4

0 回答 0