Extracting frustum planes from modelview-projection matrix

Extracting viewing frustum planes from the modelview-projection matrix

In this post I’m going to explain how to extract the parameters of each frustum plane in the local space of the object with a certain modelview-projection matrix. Knowing the frustum planes may be useful in some cases. The first one that comes to my mind is to perform frustum culling, which I will explain soon in a separated post.

Suppose we have a modelview-projection matrix MP:

Modelview Projection Matrix

Here I write down the formulas to extract the six frustum planes defined by a modelview-projection OpenGL matrix. Those planes are expressed in their implicit form Ax + By + Cz + D = 0.

Extraction of the left and right frustum planes from the matrix

 

Extraction of the bottom and top frustum planes from the matrix

 

Extraction of the near and far frustum planes from the matrix

And here is the corresponding code, assuming that the matrix is stored in column-major order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const float mat[16]; // Modelview-projection matrix
Plane l,r,b,t,n,f; // Frustum-planes

// Code to fill the modelview-projection matrix
// ...

// Left Plane
// col4 + col1
l.a = mat[3] + mat[0];
l.b = mat[7] + mat[4];
l.c = mat[11] + mat[8];
l.d = mat[15] + mat[12];

// Right Plane
// col4 - col1
r.a = mat[3] - mat[0];
r.b = mat[7] - mat[4];
r.c = mat[11] - mat[8];
r.d = mat[15] - mat[12];

// Bottom Plane
// col4 + col2
b.a = mat[3] + mat[1];
b.b = mat[7] + mat[5];
b.c = mat[11] + mat[9];
b.d = mat[15] + mat[13];

// Top Plane
// col4 - col2
t.a = mat[3] - mat[1];
t.b = mat[7] - mat[5];
t.c = mat[11] - mat[9];
t.d = mat[15] - mat[13];

// Near Plane
// col4 + col3
n.a = mat[3] + mat[2];
n.b = mat[7] + mat[6];
n.c = mat[11] + mat[10];
n.d = mat[15] + mat[14];

// Far Plane
// col4 - col3
f.a = mat[3] - mat[2];
f.b = mat[7] - mat[6];
f.c = mat[11] - mat[10];
f.d = mat[15] - mat[14];

Each plane could be normalized after being extracted from mat, although this is not always necessary but just convenient in some cases (e.g. measuring distances).

A more in depth explanation of this can be found in Clip Space Approach – Extracting the Planes (Lighthouse3D)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>