java - 第一人称相机胶卷

标签 java opengl matrix camera quaternions

我正在使用 LWJGL 和 OpenGL 3.2 为 OpenGL 编写一个 java 游戏引擎。 每当移动鼠标来旋转相机时,它都会添加滚动以及俯仰和偏航。

相机代码

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

四元数代码

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

最佳答案

我不需要起床。

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

关于java - 第一人称相机胶卷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36368103/

相关文章:

java - 从一个应用程序中的数据库在另一个应用程序中运行 SQLite 命令

java - 在 Java 中读取数万个文件并写入数百万个文件

c++ - opengl 1D 纹理 : fill with arbitrary float values

opengl - 如何呈现为无符号整数格式

matlab - 获取在 matlab 稀疏矩阵中按降序排序的前 N ​​个值的索引

c++ - 在 C++ 中使用 vector 从文本文件中读取矩阵

Javamail 535 5.7.8 身份验证失败

java - ArrayList 中数组的 ArrayAdapter

performance - 处理 OpenGL 中的 alpha 混合以获得更好的性能

java - 查找具有最多数字 < 10 方法的矩阵行