简体   繁体   中英

LWJGL glTranslatef not rendering depth

I am render a square with in 3D space. When I use the glTranslatef() method and add az coordinate the square is no longer visible. When the z coordinate is above 1 or below -1 then square is no longer displayed (I tested it by setting the z coordinate to 1. If and it didn't display).

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;
    }

}
glMatrixMode(GL_PROJECTION_MATRIX);

Instead should be:

 glMatrixMode(GL_PROJECTION);

GL_PROJECTION_MATRIX does not render the 3rd dimension. There is no further documentation on this matter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM