# Calculating frustum corners from a projection matrix

This topic is 1674 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites

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