java - 第一人称相机在移动眼睛时有奇怪的行为

标签 java opengl matrix camera lwjgl

我正在尝试使用 Java/LWJGL 制作游戏/演示,但在使用第一人称相机时遇到了问题:

我使用 WASD 移动 eye vector 周围,​​无论发生什么奇怪的事情:

  • 当我将视线移开或移向我时,正确的 react 。 View 的左侧部分在我的 View 中被遮挡了。
  • 当我将眼睛移到我的右边或左边时,然后是顶部 resp。对于我的 View , View 的底部被遮挡了。

我已经成功地实现了鼠标移动(环顾四周),现在相关的代码片段:

(注意:Matrix4f 类按列优先顺序工作)

Matrix4f viewMatrix = new Matrix4f();

private void checkKeys() {
    if (isKeyCurrentlyDown(Keyboard.KEY_W)) {
        eye.updateTranslate(1.0f, 0.0f, 0.0f);
        updateView();
    }
    else if (isKeyCurrentlyDown(Keyboard.KEY_S)) {
        eye.updateTranslate(-1.0f, 0.0f, 0.0f);
        updateView();
    }
    else if (isKeyCurrentlyDown(Keyboard.KEY_A)) {
        eye.updateTranslate(0.0f, -1.0f, 0.0f);
        updateView();
    }
    else if (isKeyCurrentlyDown(Keyboard.KEY_D)) {
        eye.updateTranslate(0.0f, 1.0f, 0.0f);
        updateView();
    }
}

private void updateView() {
    System.out.println("eye = " + eye);
    viewMatrix.identity().viewFPS(eye, roll, yaw, pitch);
    System.out.println("viewMatrix = " + viewMatrix);
    Uniforms.setUniformMatrix4(UNIFORM_VIEW_MATRIX, false, viewMatrix);
}

@Override
protected void render(final double msDelta) {
    super.render(msDelta);

    glClearColor(0.0f, 0.25f, 0.0f, 1.0f);
    glClearDepthf(1f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    program.use();

    for (int i = 0; i < 24; i++) {
        float fVar = i + currentTime / 1000f * 0.3f;
        modelviewMatrix.identity()
                .translate(0.0f, 0.0f, -8.0f)
                .rotate(currentTime / 1000f * 45.0f, 0.0f, 1.0f, 0.0f)
                .rotate(currentTime / 1000f * 21.0f, 1.0f, 0.0f, 0.0f)
                .translate(
                    (float)Math.sin(2.1f * fVar) * 2.0f,
                    (float)Math.cos(1.7f * fVar) * 2.0f,
                    (float)Math.sin(1.3f * fVar) * (float)Math.cos(1.5f * fVar) * 2.0f
                );
        Uniforms.setUniformMatrix4(UNIFORM_MODEL_MATRIX, false, modelviewMatrix.writeToFloatBuffer(modelViewMatrixBuffer));
        program.drawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
    }
}

#version 440 core

layout(location = 0) in vec4 position;

out VS_OUT {
    vec4 color;
} vs_out;

layout(location = 0) uniform mat4 model_matrix;
layout(location = 1) uniform mat4 view_matrix;
layout(location = 2) uniform mat4 proj_matrix;

void main() {
    gl_Position = proj_matrix * view_matrix * model_matrix * position;
    vs_out.color = position * 2.0 + vec4(0.5, 0.5, 0.5, 0.0);
}

public enum UniformLocation {
    UNIFORM_MODEL_MATRIX(0),
    UNIFORM_VIEW_MATRIX(1),
    UNIFORM_PROJECTION_MATRIX(2)
    ;

    private final int location;

    private UniformLocation(final int location) {
        this.location = location;
    }

    public int getLocation() {
        return this.location;
    }
}

//Column-major order

public Matrix4f viewFPS(final Vector3f eye, final float rollAngle, final float yawAngle, final float pitchAngle) {
    //roll = rolling your head, Q&E
    //yaw = looking left/right, mouseY
    //pitch = looking up/down, mouseX
    float sinRoll = (float)Math.sin(Math.toRadians(rollAngle));
    float cosRoll = (float)Math.cos(Math.toRadians(rollAngle));
    float sinYaw = (float)Math.sin(Math.toRadians(yawAngle));
    float cosYaw = (float)Math.cos(Math.toRadians(yawAngle));
    float sinPitch = (float)Math.sin(Math.toRadians(pitchAngle));
    float cosPitch = (float)Math.cos(Math.toRadians(pitchAngle));

    Vector3f xAxis = new Vector3f(
        cosYaw * cosPitch,
        sinYaw * cosPitch,
        -sinPitch
    );
    Vector3f yAxis = new Vector3f(
        cosYaw * sinPitch * sinRoll - sinYaw * cosRoll,
        sinYaw * sinPitch * sinRoll + cosYaw * cosRoll,
        cosPitch * sinRoll
    );
    Vector3f zAxis = new Vector3f(
        cosYaw * sinPitch * cosRoll + sinYaw * sinRoll,
        sinYaw * sinPitch * cosRoll - cosYaw * sinRoll,
        cosPitch * cosRoll
    );
    return multiply(
        xAxis.getX(),   xAxis.getY(),   xAxis.getZ(),   -xAxis.dot(eye),    //X column
        yAxis.getX(),   yAxis.getY(),   yAxis.getZ(),   -yAxis.dot(eye),    //Y column  
        zAxis.getX(),   zAxis.getY(),   zAxis.getZ(),   -zAxis.dot(eye),    //Z column
        0.0f,           0.0f,           0.0f,           1.0f                //W column
    );
}

我真的不知道为什么相机表现如此奇怪,甚至意味着什么?
图片的一部分怎么突然变得不可见了?

更新:这可能与 viewFPS() 方法有关,因为那里的翻译看起来有点奇怪,有人可以确认吗?

最佳答案

注意到在 viewFPS 中,您将旋转和平移结合在一个矩阵乘法中。

我不熟悉你构建矩阵的方式,但我可以告诉你什么对我有用。

  • 创建 3 个矩阵,一个用于 X 旋转、Y 旋转和 Z 旋转值。
  • 按照您想要的顺序将这些矩阵相乘。 (我使用 Z、Y 然后 X)
  • 将结果与翻译矩阵相乘。 (即你眼睛的位置)
  • 生成的矩阵就是您的 View 矩阵。

这可能比您正在使用的方法效率低,但让它工作然后从那里优化是不是很棒?拆分旋转轴还带来了一次能够调试 1 个轴的好处。

我使用的旋转矩阵:

enter image description here

enter image description here

enter image description here

其中 、(phi) 和 (psi) 是绕 X、Y 和 Z 轴的旋转。( http://www.fastgraph.com/makegames/3drotation/ )

关于java - 第一人称相机在移动眼睛时有奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21235494/

相关文章:

java - 如何使用 HttpClient

java - 'java' 、 'javaw' 和 'javaws' 之间有什么区别?

c++ - 以不同的方式转换同一3D空间中的两个对象?

c++ - OpenGL 阴影映射 - 阴影在错误的位置

c++ - 如何定义自定义比较函数以根据一维数组排序对矩阵进行排序

math - 半正定矩阵和数值稳定性?

c++ - 投影矩阵实现

java - 逐行读取文件 - 到达最后一行后终止 while 循环

java - Gui 和可序列化错误

c++ - FreeImage 不能与 CEGUI 一起工作