简体   繁体   English

具有fovy和aspect的透视OpenGL投影矩阵

[英]OpenGL projection matrix for perspective with fovy and aspect

I'm making my custom frustum and perspective implementations, part of the reasons are that I might need to do it myself, without library, in the future, and I want to polish up computer graphics skills before the exam. 我正在制作我的自定义视frustumperspective实现,部分原因是我可能需要自己做,没有库,将来,我想在考试之前提高计算机图形技能。

I'm following a book and I am converting their examples in C++ to my ones in Java/LWJGL, however one example strikes me, it is the projection of a cube. 我正在关注一本书,我正在用C ++将它们的例子转换成我在Java / LWJGL中的例子,但是一个例子让我感到震惊,它是一个立方体的投影。
My cube is projected much further away (about double?) as theirs, which is odd. 我的立方体被投射得更远(大约两倍?),因为它们很奇怪。

This are my implemenations in column-major order: 这是我在列主要顺序中的实现:

public static Matrix4f frustum(final float left, final float right, final float bottom, final float top, final float near, final float far) {
    return new Matrix4f(new float[] {
        2 * near / (right - left),          0.0f,                               0.0f,                           0.0f,   //X column
        0.0f,                               2 * near / (top - bottom),          0.0f,                           0.0f,   //Y column
        (right + left) / (right - left),    (top + bottom) / (top - bottom),    (near + far) / (near - far),    -1.0f,  //Z column
        0.0f,                               0.0f,                               2 * near * far / (near - far),  0.0f    //Z column
    });
}

public static Matrix4f perspective(final float fovy, final float aspect, final float near, final float far) {
    float y2 = near * (float)Math.tan(Math.toRadians(fovy));
    float y1 = -y2;
    float x1 = y1 * aspect;
    float x2 = y2 * aspect;
    return frustum(x1, x2, y1, y2, near, far);
}

I've seen somewhere where they use a similar y2 as I have but divide it by 2, however I do not want to make a dirty fix without exactly understanding why it is going wrong. 我已经看到过他们使用类似y2地方,但除以2,但是我不想在没有完全理解为什么会出错的情况下进行肮脏的修复。

Could someone verify whether the perspective matrix I am using, and point me out what the other reasons could be that my object looks to be further away? 有人可以验证我正在使用的perspective矩阵,并指出我的对象看起来更远的其他原因是什么?

I have got my other matrix math right and I am only translating the cube by (0f, 0f, -4f) and verified that that was also correct. 我有其他矩阵数学正确,我只是通过(0f, 0f, -4f)翻译立方体并验证这也是正确的。

Would a wrongly implemented perspective matrix cause my object to be further away? 错误实现的perspective矩阵会导致我的对象更远吗?

Your implementation of frustum seems correct. 你对frustum的实现似乎是正确的。

In your implementation of perspective, fovy should be fovy / 2 instead. 在你的透视实现中,fovy应该是fovy / 2。

See this answer: https://stackoverflow.com/a/21064935/3146587 请参阅此答案: https//stackoverflow.com/a/21064935/3146587

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

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