简体   繁体   English

第一人称相机胶卷

[英]First Person Camera Roll

I am writing a java Game Engine for OpenGL using LWJGL and OpenGL 3.2. 我正在使用LWJGL和OpenGL 3.2为OpenGL编写一个Java游戏引擎。 Whenever the mouse is moved to rotate the camera it adds roll as well as pitch and yaw. 每当移动鼠标旋转相机时,它都会增加滚动以及俯仰和偏航。

Camera code 相机代码

public Matrix4f getViewMatrix() {
    getParent().getTransform().getWorldRot().mul(rotation, (Quaternion) null).toRotationMatrix(viewMatrix);
    viewMatrix.mul(Matrix4f.setTranslation(getParent().getTransform().getWorldPos(), null));
    return viewMatrix;
}


public boolean mouseMoved(MouseEvent event) {
    Quaternion.mul(rotation, new Quaternion((float) Math.toRadians(event.x / 1f), rotation.getUp(null)), rotation);
    Quaternion.mul(rotation, new Quaternion((float) Math.toRadians(event.y / 1f), rotation.getRight(null)), rotation);
    return true;
}

Quaternion code 四元数代码

public Vector4f getRight(Vector4f dest) {
    if (dest == null)
        dest = new Vector4f();

    dest.x = 1.0f - 2.0f * (y * y + z * z);
    dest.y = 2.0f * (x * y - w * z);
    dest.z = 2.0f * (x * z + w * y);

    return dest;
}

public Vector4f getUp(Vector4f dest) {
    if (dest == null)
        dest = new Vector4f();

    dest.x = 2.0f * (x * y + w * z);
    dest.y = 1.0f - 2.0f * (x * x + z * z);
    dest.z = 2.0f * (y * z - w * x);

    return dest;
}

public Vector4f getForward(Vector4f dest) {
    if (dest == null)
        dest = new Vector4f();

    dest.x = 2.0f * (x * z - w * y);
    dest.y = 2.0f * (y * z + w * x);
    dest.z = 1.0f - 2.0f * (x * x + y * y);

    return dest;
}

I don't need to get the up. 我不需要起床。

public boolean mouseMoved(MouseEvent event) {
    Quaternion.mul(rotation, new Quaternion((float) Math.toRadians(event.x / 1f), Vector4f.UP), rotation);
    Quaternion.mul(rotation, new Quaternion((float) Math.toRadians(event.y / 1f), rotation.getRight(null)), rotation);
    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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