用于截锥剔除的java截锥未正确创建
简介:
我正在尝试实现截锥剔除,为此我创建了一个projectionViewMatrix,然后用矩阵转换向量。然而,有些向量的计算似乎不正确。(坐标系:+x向右,+y向上,+z离开屏幕)
代码:
public static void updateFrustum(Camera camera) {
Vector3f[] points = calculateFrustumVertices(camera);
plane[0].setPlane(points[1], points[0], points[2]);
plane[1].setPlane(points[4], points[5], points[7]);
plane[2].setPlane(points[0], points[4], points[3]);
plane[3].setPlane(points[5], points[1], points[6]);
plane[4].setPlane(points[2], points[3], points[6]);
plane[5].setPlane(points[4], points[0], points[1]);
}
private static Vector3f[] calculateFrustumVertices(Camera camera) {
// projectionMatrix was saved once at the beginning
Matrix4f inverseProjView = Matrix4f.mul(projectionMatrix, Maths.createViewMatrix(camera), null);
inverseProjView.invert();
Vector3f[] points = new Vector3f[8];
Vector4f vertex = new Vector4f();
vertex.w = 1;
for (int i = 0; i < 8; i++) {
vertex.x = clipCube[i].x;
vertex.y = clipCube[i].y;
vertex.z = clipCube[i].z;
Matrix4f.transform(inverseProjView, vertex, vertex);
vertex.x /= vertex.w;
vertex.y /= vertex.w;
vertex.z /= vertex.w;
vertex.w /= vertex.w;
points[i] = new Vector3f(vertex);
}
return points;
}
static Matrix4f viewMatrix = new Matrix4f();
public static Matrix4f createViewMatrix(Camera camera) {
viewMatrix.setIdentity();
Matrix4f.rotate((float) Math.toRadians(camera.getPitch()), Maths.xRotation, viewMatrix, viewMatrix);
Matrix4f.rotate((float) Math.toRadians(camera.getYaw()), Maths.yRotation, viewMatrix, viewMatrix);
Matrix4f.rotate((float) Math.toRadians(camera.getRoll()), Maths.zRotation, viewMatrix, viewMatrix);
Maths.reusableVector = camera.getPosition();
Maths.reusableVector2.set(-Maths.reusableVector.x, -Maths.reusableVector.y, -Maths.reusableVector.z);
Matrix4f.translate(Maths.reusableVector2, viewMatrix, viewMatrix);
return viewMatrix;
}
示例输出:
为此,我站在原点(0, 0, 0)
,朝+z方向看。我的近平面是0.001
,远平面是1000
,视场是60°
。结果是(在calculateFrustumVertices()
中打印出for循环中的每个点):
points[0] = (0, 0, 0)
points[1] = (0, 0, 0)
points[2] = (0, 0, 0)
points[3] = (0, 0, 0)
points[4] = (1127, -591, 1110)
points[5] = (-1114, -591, 1110)
points[6] = (-1114, 668, 1110)
points[7] = (1127, 668, 1110)
请注意,前四个点并不完全相同,但由于非常小的近平面距离(0.001),它们几乎等于零。我省略了小数点(因为它们是不相关的)
问题:
大体上,平截头体的形状是正确的。但在我看来,这些观点有点错误
- 如果原点在
(0, 0, 0)
,坐标不应该是 对称的?例如,如果点4和点7的x值等于1127, 那么第5点和第6点的值不应该等于-1127吗?(y和z相同)李> (Here is a sketch for clarification)如果视场为60°,远平面距离为1000。那么在我看来,z值应该等于
![zOffset/farPlaneDist=cos(30°)](https://latex.codecogs.com/gif.latex?%5Cfrac%7BzOffset%7D%7BfarPlaneDistance%7D%20%3D%20cos%2830%B0%29%20%5CLeftrightarrow%20zOffset%20%3D%20farPlaneDistance%20%5Ccdot%20cos%2830%29)
(我不允许发布链接,sry。也许有人可以编辑帖子并去掉“`”,这样它就是链接而不是代码块。谢谢!)如果你计算它,你会得到
zOffset = 866
。这比我通过程序得到的值小300个单位
问题:
计算积分时我做错了什么?基本形式是一样的,但要点仍然不同于它们应该是什么。我在什么地方出错了吗?如果你需要更多的信息,请这样说,然后我会提供它
共 (0) 个答案