最准确的方法来计算世界空间中的视锥体角

时间:2015-10-30 13:18:11

标签: opengl projection frustum ndc

我试图计算世界空间中的视锥体角。我通过使用FOV并使用平面的宽度/高度和一些矢量数学来实现它

然而,很多例子只是说明你可以通过逆viewProjection矩阵乘以像(1,1,1)这样的NDC角。但是当我这样做时,我会得到一些不同的结果。这是我现在用来测试的代码:

float nearHeight = 2 * tan(mFOV / 2) * mNear;
float nearWidth = mNear * mRatio;

float farHeight = 2 * tan(mFOV / 2) * mFar;
float farWidth = mFar * mRatio;

glm::vec3 fc = mPos + mFront * mFar;
glm::vec3 nc = mPos + mFront * mNear;

mFrustum.frustumCorners[0] = fc + (mUp * farHeight / 2.0f) - (mRight * farWidth / 2.0f);
mFrustum.frustumCorners[1] = fc + (mUp * farHeight / 2.0f) + (mRight * farWidth / 2.0f);
mFrustum.frustumCorners[2] = fc - (mUp * farHeight / 2.0f) - (mRight * farWidth / 2.0f);
mFrustum.frustumCorners[3] = fc - (mUp * farHeight / 2.0f) + (mRight * farWidth / 2.0f);

mFrustum.frustumCorners[4] = nc + (mUp * nearHeight / 2.0f) - (mRight * nearWidth / 2.0f);
mFrustum.frustumCorners[5] = nc + (mUp * nearHeight / 2.0f) + (mRight * nearWidth / 2.0f);
mFrustum.frustumCorners[6] = nc - (mUp * nearHeight / 2.0f) - (mRight * nearWidth / 2.0f);
mFrustum.frustumCorners[7] = nc - (mUp * nearHeight / 2.0f) + (mRight * nearWidth / 2.0f);

glm::vec4 test(1.0f, 1.0f, 1.0f,1.0f);
glm::vec4 test2(-1.0f, -1.0f, -1.0f, 1.0f);
glm::mat4 testingMatrix = glm::inverse(mProjectionMatrix * getViewMatrix());

test = testingMatrix*test;
test2 = testingMatrix*test2;

test2.x /= test2.w;
test2.y /= test2.w;
test2.z /= test2.w;

test.x /= test.w;
test.y /= test.w;
test.z /= test.w;

现在这两个结果都为[near,far] = [1,10000]提供了准确的z值,但是x值相当偏差,而y值几乎相同。我只是想知道哪种方式最准确?

反向viewProjection

enter image description here

定期计算

enter image description here

0 个答案:

没有答案