• Sign Up
• Login
• API
• FAQ
• Tools
• Archive
SHARE
TWEET # frustum plane extraction lutza  Aug 9th, 2011 1,205 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. // i extract planes from projection matrix using this class:
2. // mat is column major, mat[i] is ith column of matrix
3.
4. class Frustum
5. {
6.     public:
7.         Frustum(const glm::mat4& mat, bool normalize_planes = true)
8.         // create frustum from  matrix
9.         // if extracted from projection matrix only, planes will be in eye-space
10.         // if extracted from view*projection, planes will be in world space
11.         // if extracted from model*view*projection planes will be in model space
12.         {
13.             // create non-normalized clipping planes
14.             planes = Plane(mat+mat);       // left
15.             planes = Plane(mat-mat);       // right
16.             planes = Plane(mat-mat);       // top
17.             planes = Plane(mat+mat);       // bottom
18.             planes = Plane(mat+mat);       // near
19.             planes = Plane(mat-mat);       // far
20.             // normalize the plane equations, if requested
21.             if (normalize_planes)
22.             {
23.                 planes.normalize();
24.                 planes.normalize();
25.                 planes.normalize();
26.                 planes.normalize();
27.                 planes.normalize();
28.                 planes.normalize();
29.             }
30.         }
31.
32.     Plane planes;        // plane normals point into frustum
33. };
34.
35. class Plane
36. {
37.
38.     public:
39.         Plane(const glm::vec4& abcd)
40.          : normal(abcd.x, abcd.y, abcd.z), d(abcd.w) {}
41.
42.         void normalize()
43.         {
44.             float mag = glm::length(normal);
45.             normal /= mag;
46.             d /= mag;
47.         }
48.
49.         glm::vec3 normal;
50.         float d;        // distance from origin
51. };
52.
53.
54. // this is proj. matrix i use
55. glm::perspective(60.0f /*fovy*/, 800.0f/600 /*aspect*/, 0.01f /*near_dist*/, 100.0f/*far_dist*/);
56.
57. // this is how this matrix looks when printed out
58. 1.2990 0.0000 0.0000 0.0000
59. 0.0000 1.7321 0.0000 0.0000
60. 0.0000 0.0000 -1.0002 0.0200
61. 0.0000 0.0000 -1.0000 0.0000
62.
63. // and these are the planes constructed from this matrix when printed out
64. left = normal[0.999881 0.000000 -0.015396], d=-0.015396
65. right = normal[-0.999881 0.000000 -0.015396], d=-0.015396
66. top = normal[0.000000 -0.999933 -0.011547], d=-0.011547
67. bottom = normal[0.000000 0.999933 -0.011547], d=-0.011547
68. near = normal[0.000000 0.000000 -1.000000], d=-0.980198
69. far = normal[0.000000 0.000000 1.000000], d=1.020202
70.
71. // i expect "d" to be distance from origin, which is camera space,
72. // so it should be 100 for far plane, and 0.01 for near plane, right ?
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top