【问题标题】:JOGL setPerspective wrong?JOGL setPerspective 错误?
【发布时间】:2015-10-28 18:20:59
【问题描述】:

我决定在我的项目中使用 JOGL。但这是设置视角的问题。简单代码:

System.out.println("BEFORE:");
projectionMatrix.identity();
projectionMatrix.setPerspective(fovy, aspect, zNear, zFar);
System.out.println(projectionMatrix);

System.out.println("AFTER:");
projectionMatrix.identity();        
projectionMatrix.m00 =
    (float)(1.0 / (aspect*Math.tan(viewAngle)));
projectionMatrix.m11 =
    (float)(1.0 / Math.tan(viewAngle));
projectionMatrix.m22 =
    (float)((-zNear-zFar) / (zNear-zFar));
projectionMatrix.m33 = 0.0f;

projectionMatrix.m23 =
        (float)((2*zFar*zNear) / (zNear-zFar));
projectionMatrix.m32 = 1.0f;
System.out.println(projectionMatrix);

输出:

BEFORE:
2.414E+0  0.000E+0  0.000E+0  0.000E+0
0.000E+0  2.414E+0  0.000E+0  0.000E+0
0.000E+0  0.000E+0 -1.000E+0 -2.000E-2
0.000E+0  0.000E+0 -1.000E+0  0.000E+0

AFTER:
1.000E+0  0.000E+0  0.000E+0  0.000E+0
0.000E+0  1.000E+0  0.000E+0  0.000E+0
0.000E+0  0.000E+0  1.000E+0  1.000E+0
0.000E+0  0.000E+0 -2.000E-2  0.000E+0

如您所见,矩阵是不同的。他们的(慢跑)代码:

public Matrix4f setPerspective(float fovy, float aspect, float zNear, float zFar) {
    float h = (float) Math.tan(fovy * 0.5f) * zNear;
    float w = h * aspect;
    m00 = zNear / w;
    m01 = 0.0f;
    m02 = 0.0f;
    m03 = 0.0f;
    m10 = 0.0f;
    m11 = zNear / h;
    m12 = 0.0f;
    m13 = 0.0f;
    m20 = 0.0f;
    m21 = 0.0f;
    m22 = -(zFar + zNear) / (zFar - zNear);
    m23 = -1.0f;
    m30 = 0.0f;
    m31 = 0.0f;
    m32 = -2.0f * zFar * zNear / (zFar - zNear);
    m33 = 0.0f;
    return this;
}

给出的和我的不同。程序适用于我的代码版本。我如何使用他们的库?

另一个问题:简单地从 javax.vecmath.Matrix4f 切换到 org.joml.Matrix4f 旋转会被破坏(围绕错误的轴和稍微偏离相机的点旋转)。

【问题讨论】:

  • 你的似乎是转置的,没有考虑纵横比,并且沿 +z 而不是 -z 投影,到底是什么不工作?
  • 我在尝试使用他们的投影矩阵时出现黑屏。
  • 也许是因为你看错了方向?
  • 这似乎不是问题。我试图改变翻译(相机位置) - 同样的结果 - 黑屏。
  • glUniformMatrix4fv 有一个转置参数:您是否将其设置为与您的矩阵相反的值? (因为他们的矩阵是转置的)

标签: opengl matrix camera rotation jogl


【解决方案1】:

一是更新jogl,二是使用FloatUtil.makePerspective(final float[] m, final int m_off, final boolean initM, final float fovy_rad, final float aspect, final float zNear, final float zFar)

float[] m 是您需要提交的矩阵。这为您提供了灵活性,这意味着如果您想使用池,您可以。否则只需传递new float[16] 并捕获返回结果

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多