【问题标题】:First Person Camera Roll第一人称相机胶卷
【发布时间】:2016-04-02 01:09:51
【问题描述】:

我正在使用 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;
}

【问题讨论】:

    标签: java opengl matrix camera quaternions


    【解决方案1】:

    我不需要起床。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-16
      • 2018-06-16
      相关资源
      最近更新 更多