【发布时间】:2014-01-17 11:40:49
【问题描述】:
我正在制作我的自定义 frustum 和 perspective 实现,部分原因是我将来可能需要自己做,没有库,我想在考试。
我正在看一本书,我正在将他们用 C++ 编写的示例转换为我使用 Java/LWJGL 编写的示例,但是有一个例子让我印象深刻,它是立方体的投影。
我的立方体投影得比他们的更远(大约是两倍?),这很奇怪。
这是我按列主要顺序的实现:
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);
}
我在某处看到他们使用与我类似的y2,但将其除以 2,但是我不想在不完全了解为什么出错的情况下进行肮脏的修复。
有人可以验证我是否正在使用perspective 矩阵,并指出我的对象看起来更远的其他原因吗?
我的其他矩阵数学正确,我只是将立方体转换为 (0f, 0f, -4f) 并验证这也是正确的。
错误实现的perspective 矩阵会导致我的对象更远吗?
【问题讨论】:
-
fovy通常是完整的视野,所以 0.5 在那里,因此假设 y 的范围从 -tan(fov/2) 到 +tan(fov/ 2)