1

我上周写了这台相机,一切正常,接受我不如何使用外观位置作为 Z 位置的事实。

如果你尝试一下,你会亲眼看到的。

import static java.lang.Math.cos;
import static java.lang.Math.sin;
import static java.lang.Math.toRadians;
import static org.lwjgl.opengl.GL11.*;

import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

import org.lwjgl.util.vector.Vector3f;

public class Camera{
    private Vector3f    position    = null; //X, Y, Z position of the PandaCam
    private float       yaw         = 0.0f; //Yaw of the PandaCam
    private float       pitch       = 0.0f; //Pitch of the PandaCam

    private float dx = 0.0f, dy = 0.0f;

    private float multi = 0.005f;
    private float moveSpeed = 0.25f;
    private float mouseSensitivity = 0.02f;

    private float tDelta = getDelta();

    private long lastFrame, time;



    /**
     * Create a new PandaCam
     * default:
     * x = 0
     * y = 0
     * z = 0
     */
    public Camera(){
        position = new Vector3f(0, 0, 0);
    }

    /**
     * Create a new PandaCam
     * default:
     * z = 0
     * 
     * @param x Set the Starting X position of the camera
     * @param y Set the Starting Y position of the camera
     */
    public Camera(float x, float y){
        position = new Vector3f(x, y, 0);
    }

    /**
     * Create a new PandaCam
     * 
     * @param x Set the Starting X position of the camera
     * @param y Set the Starting Y position of the camera
     * @param z Set the Starting Z position of the camera
     */
    public Camera(float x, float y, float z){
        position = new Vector3f(x, y, z);
    }

    /**
     * Call this function every frame to update the PandaCam
     */
    public void update(){
        glLoadIdentity();

        mouseInput();
        keyboardInput();
        setPosition();
    }

    /**
     * @return Returns the delta time.
     */
    public int getDelta(){
        time = (Sys.getTime() * 1000) / Sys.getTimerResolution();
        int delta = (int) (time - lastFrame);
        lastFrame = time;

        return delta;
    }

    /**
     * Updates the PandaCam yaw
     * @param amount The amount the yaw moves. takes numbers below and above zero
     */
    public void yaw(float amount){
        yaw -= amount;
    }

    /**
     * Updates the PandaCam pitch
     * @param amount The amount the pitch moves. takes numbers below and above zero
     */
    public void pitch(float amount){
        pitch += amount;
    }

    /**
     * Moves the PandaCam forward.
     * @param distance The distance the camera move's forward
     */
    public void walkForward(float distance){
        calculatePosition(0, 0, multi * distance * 0.003f);
    }

    /**
     * Moves the PandaCam backward.
     * @param distance The distance the camera move's backward
     */
    public void walkBackwards(float distance){
        calculatePosition(0, 0, -multi * distance * 0.003f);
    }

    /**
     * Moves the PandaCam left.
     * @param distance The distance the camera move's left
     */
    public void strafeLeft(float distance){
        calculatePosition(multi * distance * 0.003f, 0, 0);
    }

    /**
     * Moves the PandaCam right.
     * @param distance The distance the camera move's right
     */
    public void strafeRight(float distance){
        calculatePosition(-multi * distance * 0.003f, 0, 0);
    }

    /**
     * Sets the PandaCam position
     * @param dx Is an amount that the PandaCam add's or subtracts from the current X
     * @param dy Is an amount that the PandaCam add's or subtracts from the current Y
     * @param dz Is an amount that the PandaCam add's or subtracts from the current Z
     */
    private void calculatePosition(float dx, float dy, float dz){
        position.x -= dx * (float) sin(toRadians(yaw - 90)) + dz * sin(toRadians(yaw));
        position.y += dy * (float) sin(toRadians(pitch - 90)) + dz * sin(toRadians(pitch));
        position.z += dx * (float) cos(toRadians(yaw - 90)) + dz * cos(toRadians(yaw));
    }    

    /**
     * Handles the keyboard input
     */
    public void keyboardInput(){
        //Move forward
        if (Keyboard.isKeyDown(Keyboard.KEY_W) || Keyboard.isKeyDown(Keyboard.KEY_UP)){
            walkForward(moveSpeed * (tDelta/10));
        }
        //Move backwards
        if (Keyboard.isKeyDown(Keyboard.KEY_S) || Keyboard.isKeyDown(Keyboard.KEY_DOWN)){
            walkBackwards(moveSpeed * (tDelta/10));
        }
        //Strafe left
        if (Keyboard.isKeyDown(Keyboard.KEY_A) || Keyboard.isKeyDown(Keyboard.KEY_LEFT)){
            strafeLeft(moveSpeed * (tDelta/10));
        }
        //Strafe right
        if (Keyboard.isKeyDown(Keyboard.KEY_D) || Keyboard.isKeyDown(Keyboard.KEY_RIGHT)){
            strafeRight(moveSpeed * (tDelta/10));
        }
    }
    /**
     * Handles mouse Input
     */
    public void mouseInput(){
        dx = Mouse.getDX();
        dy = Mouse.getDY();


        yaw(dx * mouseSensitivity);
        pitch(dy * mouseSensitivity);
    }

    /**
     * Sets the position of the PandaCam
     */
    public void setPosition(){
        //Rotate the pitch around the X axis
        glRotatef(-pitch, 1.0f, 0.0f, 0.0f);

        //Rotate the yaw around the Y axis
        glRotatef(-yaw, 0.0f, 1.0f, 0.0f);

        //translate to the position vector's location
        glTranslatef(position.x, position.y, position.z);

    }

    public float getMX(){
        return Mouse.getX();
    }
    public float getMY(){
        return Mouse.getY();
    }

    /**
     * @return The Pitch position of the PandaCam
     */
    public float getPitch(){
        return -pitch;
    }

    /**
     * @return The Yaw position of the PandaCam
     */
    public float getYaw(){
        return -yaw;
    }

    /**
     * @return The X position of the PandaCam
     */
    public float getX(){
        return position.x;
    }

    /**
     * @return The Y position of the PandaCam
     */
    public float getY(){
        return position.y;
    }

    /**
     * @return The Z position of the PandaCam
     */
    public float getZ(){
        return position.z;
    }
}
4

1 回答 1

1

在 OpenGL 中,相机(视图)矩阵通常使用定义相机视图方向的 lookAt 方法构建。是一个源,您可以在其中找到lookAt 方法实现。

或者这是我对有相机问题的人的回答,其中包括 LWJGL 查看示例代码:)

顺便说一句,如果您使用的是固定管道(似乎是这种情况),请查看:

gluLookAt ()

于 2013-03-18T11:58:17.773 回答