// i extract planes from projection matrix using this class:
// mat is column major, mat[i] is ith column of matrix
class Frustum
{
public:
Frustum(const glm::mat4& mat, bool normalize_planes = true)
// create frustum from matrix
// if extracted from projection matrix only, planes will be in eye-space
// if extracted from view*projection, planes will be in world space
// if extracted from model*view*projection planes will be in model space
{
// create non-normalized clipping planes
planes[0] = Plane(mat[3]+mat[0]); // left
planes[1] = Plane(mat[3]-mat[0]); // right
planes[2] = Plane(mat[3]-mat[1]); // top
planes[3] = Plane(mat[3]+mat[1]); // bottom
planes[4] = Plane(mat[3]+mat[2]); // near
planes[5] = Plane(mat[3]-mat[2]); // far
// normalize the plane equations, if requested
if (normalize_planes)
{
planes[0].normalize();
planes[1].normalize();
planes[2].normalize();
planes[3].normalize();
planes[4].normalize();
planes[5].normalize();
}
}
Plane planes[6]; // plane normals point into frustum
};
class Plane
{
public:
Plane(const glm::vec4& abcd)
: normal(abcd.x, abcd.y, abcd.z), d(abcd.w) {}
void normalize()
{
float mag = glm::length(normal);
normal /= mag;
d /= mag;
}
glm::vec3 normal;
float d; // distance from origin
};
// this is proj. matrix i use
glm::perspective(60.0f /*fovy*/, 800.0f/600 /*aspect*/, 0.01f /*near_dist*/, 100.0f/*far_dist*/);
// this is how this matrix looks when printed out
1.2990 0.0000 0.0000 0.0000
0.0000 1.7321 0.0000 0.0000
0.0000 0.0000 -1.0002 0.0200
0.0000 0.0000 -1.0000 0.0000
// and these are the planes constructed from this matrix when printed out
left = normal[0.999881 0.000000 -0.015396], d=-0.015396
right = normal[-0.999881 0.000000 -0.015396], d=-0.015396
top = normal[0.000000 -0.999933 -0.011547], d=-0.011547
bottom = normal[0.000000 0.999933 -0.011547], d=-0.011547
near = normal[0.000000 0.000000 -1.000000], d=-0.980198
far = normal[0.000000 0.000000 1.000000], d=1.020202
// i expect "d" to be distance from origin, which is camera space,
// so it should be 100 for far plane, and 0.01 for near plane, right ?