Advertisement
lutza

frustum plane extraction

Aug 9th, 2011
1,938
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  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[0] = Plane(mat[3]+mat[0]);       // left
  15.             planes[1] = Plane(mat[3]-mat[0]);       // right
  16.             planes[2] = Plane(mat[3]-mat[1]);       // top
  17.             planes[3] = Plane(mat[3]+mat[1]);       // bottom
  18.             planes[4] = Plane(mat[3]+mat[2]);       // near
  19.             planes[5] = Plane(mat[3]-mat[2]);       // far
  20.             // normalize the plane equations, if requested
  21.             if (normalize_planes)
  22.             {
  23.                 planes[0].normalize();
  24.                 planes[1].normalize();
  25.                 planes[2].normalize();
  26.                 planes[3].normalize();
  27.                 planes[4].normalize();
  28.                 planes[5].normalize();
  29.             }
  30.         }
  31.  
  32.     Plane planes[6];        // 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 ?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement