• Create Account

Calculating frustum corners from a projection matrix

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

#1JackShannon  Members

492
Like
0Likes
Like

Posted 20 June 2013 - 02:50 PM

I'm trying to multiply the NDC frustum points by the inverse of the projection matrix to give me the frustum corners in view space. Here is my code:

void getFrustumCorners(std::vector<glm::vec3>& corners, glm::mat4 projection)
{
corners.clear();

// homogeneous corner coords
glm::vec4 hcorners[8];
// near
hcorners[0] = glm::vec4(-1, 1, 1, 1);
hcorners[1] = glm::vec4(1, 1, 1, 1);
hcorners[2] = glm::vec4(1, -1, 1, 1);
hcorners[3] = glm::vec4(-1, -1, 1, 1);
// far
hcorners[4] = glm::vec4(-1, 1, -1, 1);
hcorners[5] = glm::vec4(1, 1, -1, 1);
hcorners[6] = glm::vec4(1, -1, -1, 1);
hcorners[7] = glm::vec4(-1, -1, -1, 1);

glm::mat4 inverseProj = glm::inverse(projection);
for (int i = 0; i < 8; i++) {
hcorners[i] = hcorners[i] * inverseProj;
hcorners[i] /= hcorners[i].w;

corners.push_back(glm::vec3(hcorners[i]));
}
}

int main()
{
auto proj = glm::perspective(56.25f, 720.0f / 450.0f, 0.1f, 100.0f);

std::vector<glm::vec3> corners;
getFrustumCorners(corners, proj);

for (auto c : corners) {
std::cout << c.x << " " << c.y << " " << c.z << std::endl;
}
return 0;
}


Now the output that this is giving me is:

-0.213538 0.133461 -1.24719
0.213538 0.133461 -1.24719
0.213538 -0.133461 -1.24719
-0.213538 -0.133461 -1.24719
-0.142418 0.089011 -0.831807
0.142418 0.089011 -0.831807
0.142418 -0.089011 -0.831807
-0.142418 -0.089011 -0.831807


This can't be correct? Surely when using a zNear of 0.1 and a zFar of 100.0, the difference between the near and far plane coords should be just under 100.0?

What am I doing wrong?

#2Brother Bob  Moderators

10103
Like
1Likes
Like

Posted 20 June 2013 - 05:48 PM

Multiply the vector on the right hand side of the matrix.

#3JackShannon  Members

492
Like
0Likes
Like

Posted 21 June 2013 - 01:55 AM

Thank you! It was just that!

Edited by JackShannon, 21 June 2013 - 05:34 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.