LWJGL glTranslatef没有渲染深度

时间:2014-09-19 04:30:30

标签: java opengl lwjgl

我在3D空间中渲染一个正方形。当我使用glTranslatef()方法并添加z坐标时,方形不再可见。当z坐标大于1或小于-1时,则不再显示方形(我通过将z坐标设置为1来测试它。如果它没有显示)。

Main.java

import java.util.logging.Level;
import java.util.logging.Logger;

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

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

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        initDisplay();
        gameLoop();
        cleanUp();
    }

    public static void initDisplay(){
        try {
            Display.setDisplayMode(new DisplayMode(800, 600));
            Display.setTitle("AWESOMENESS!");
            Display.create();
        } catch (LWJGLException e) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    public static void gameLoop(){
        Camera cam = new Camera(70, (float)Display.getWidth() / (float)Display.getHeight(), 0.3f, 1000);


        while(!Display.isCloseRequested()){

            glClear(GL_COLOR_BUFFER_BIT);
            glLoadIdentity();
            cam.useView();

            glPushMatrix();{
                glColor3f(1.0f, 0.5f, 0f);
                //This is the problematic line. When the 3rd param goes above 1 or below -1 the
                //square disappers.
                glTranslatef(0,0,-10);
                glBegin(GL_QUADS);{
                    glVertex3f(0,0,0);
                    glVertex3f(0,1,0);
                    glVertex3f(1,1,0);
                    glVertex3f(1,0,0);
                }
                glEnd();
            }
            glPopMatrix();

            Display.update();
        }
    }

    public static void cleanUp(){
        Display.destroy();
    }

}

Camera.java

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.*;

public class Camera {

    //Position
    private float x;
    private float y;
    private float z;
    //Rotation
    private float rx;
    private float ry;
    private float rz;

    private float fov;
    private float aspect;
    private float near;
    private float far;

    public Camera(float fov, float aspect, float near, float far) {
        x = 0;
        y = 0;
        z = 0;
        rx = 0;
        ry = 0;
        rz = 0;
        this.fov = fov;
        this.aspect = aspect;
        this.near = near;
        this.far = far;
        initProjection();
    }

    private void initProjection(){
        glMatrixMode(GL_PROJECTION_MATRIX);
        glLoadIdentity();
        gluPerspective(fov, aspect, near, far);
        glMatrixMode(GL_MODELVIEW);
    }

    public void useView(){
        glRotatef(rx, 1, 0, 0);
        glRotatef(ry, 0, 1, 0);
        glRotatef(rz, 0, 0, 1);
        glTranslatef(x, y, z);
    }

    public float getX() {
        return x;
    }

    public float getY() {
        return y;
    }

    public float getZ() {
        return z;
    }

    public float getRx() {
        return rx;
    }

    public float getRy() {
        return ry;
    }

    public float getRz() {
        return rz;
    }

    public float getFov() {
        return fov;
    }

    public float getAspect() {
        return aspect;
    }

    public float getNear() {
        return near;
    }

    public float getFar() {
        return far;
    }

    public void setX(float x) {
        this.x = x;
    }

    public void setY(float y) {
        this.y = y;
    }

    public void setZ(float z) {
        this.z = z;
    }

    public void setRx(float rx) {
        this.rx = rx;
    }

    public void setRy(float ry) {
        this.ry = ry;
    }

    public void setRz(float rz) {
        this.rz = rz;
    }

    public void setFov(float fov) {
        this.fov = fov;
    }

    public void setAspect(float aspect) {
        this.aspect = aspect;
    }

    public void setNear(float near) {
        this.near = near;
    }

    public void setFar(float far) {
        this.far = far;
    }

}

1 个答案:

答案 0 :(得分:1)

glMatrixMode(GL_PROJECTION_MATRIX);

应该是:

 glMatrixMode(GL_PROJECTION);

GL_PROJECTION_MATRIX不呈现第三维。没有关于此事的进一步文件。