• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

Frustum.h

Go to the documentation of this file.
00001 // GMTL is (C) Copyright 2001-2010 by Allen Bierbaum
00002 // Distributed under the GNU Lesser General Public License 2.1 with an
00003 // addendum covering inlined code. (See accompanying files LICENSE and
00004 // LICENSE.addendum or http://www.gnu.org/copyleft/lesser.txt)
00005 
00006 #ifndef _GMTL_FRUSTUM_H_
00007 #define _GMTL_FRUSTUM_H_
00008 
00009 #include <gmtl/Defines.h>
00010 #include <gmtl/Plane.h>
00011 #include <gmtl/MatrixOps.h>
00012 
00013 
00014 namespace gmtl
00015 {
00016 
00022 template<typename DATA_TYPE>
00023 class Frustum
00024 {
00025 public:
00026    typedef DATA_TYPE DataType;
00027    typedef Frustum<DATA_TYPE> FrustumType;
00028 
00033    enum PlaneNames
00034    {
00035       PLANE_LEFT = 0,      
00036       PLANE_RIGHT = 1,   
00037       PLANE_BOTTOM = 2,   
00038       PLANE_TOP = 3,      
00039       PLANE_NEAR = 4,      
00040       PLANE_FAR = 5      
00041    };
00042 
00046    Frustum()
00047    {
00048    
00049    }
00050 
00057    Frustum(const gmtl::Matrix<DATA_TYPE, 4, 4>& projMatrix)
00058    {
00059       extractPlanes(projMatrix);
00060    }
00061 
00077    Frustum(const gmtl::Matrix<DATA_TYPE, 4, 4>& modelviewMatrix,
00078            const gmtl::Matrix<DATA_TYPE, 4, 4>& projMatrix)
00079    {
00080       extractPlanes(modelviewMatrix, projMatrix);
00081    }
00082 
00089    void extractPlanes(const gmtl::Matrix<DATA_TYPE, 4, 4>& modelviewMatrix,
00090                       const gmtl::Matrix<DATA_TYPE, 4, 4>& projMatrix)
00091    {
00092       extractPlanes(projMatrix * modelviewMatrix);
00093    }
00094 
00110    void extractPlanes(const gmtl::Matrix<DATA_TYPE, 4, 4>& projMatrix)
00111    {
00112       const gmtl::Matrix<DATA_TYPE, 4, 4>& m = projMatrix;
00113 
00114       //left
00115       mPlanes[PLANE_LEFT].setNormal(gmtl::Vec<DATA_TYPE, 3>(m[3][0] + m[0][0],
00116                                                             m[3][1] + m[0][1],
00117                                                             m[3][2] + m[0][2]));
00118       mPlanes[PLANE_LEFT].setOffset(m[3][3] + m[0][3]);
00119       //right
00120       mPlanes[PLANE_RIGHT].setNormal(gmtl::Vec<DATA_TYPE, 3>(m[3][0] - m[0][0],
00121                                                              m[3][1] - m[0][1],
00122                                                              m[3][2] - m[0][2]));
00123       mPlanes[PLANE_RIGHT].setOffset(m[3][3] - m[0][3]);
00124       //bottom
00125       mPlanes[PLANE_BOTTOM].setNormal(gmtl::Vec<DATA_TYPE, 3>(m[3][0] + m[1][0],
00126                                                               m[3][1] + m[1][1],
00127                                                               m[3][2] + m[1][2]));
00128       mPlanes[PLANE_BOTTOM].setOffset(m[3][3] + m[1][3]);
00129       //top
00130       mPlanes[PLANE_TOP].setNormal(gmtl::Vec<DATA_TYPE, 3>(m[3][0] - m[1][0],
00131                                                            m[3][1] - m[1][1],
00132                                                            m[3][2] - m[1][2]));
00133       mPlanes[PLANE_TOP].setOffset(m[3][3] - m[1][3]);
00134       //near
00135       mPlanes[PLANE_NEAR].setNormal(gmtl::Vec<DATA_TYPE, 3>(m[3][0] + m[2][0],
00136                                                             m[3][1] + m[2][1],
00137                                                             m[3][2] + m[2][2]));
00138       mPlanes[PLANE_NEAR].setOffset(m[2][3] + m[3][3]);
00139       //far
00140       mPlanes[PLANE_FAR].setNormal(gmtl::Vec<DATA_TYPE, 3>(m[3][0] - m[2][0],
00141                                                            m[3][1] - m[2][1],
00142                                                            m[3][2] - m[2][2]));
00143       mPlanes[PLANE_FAR].setOffset(m[3][3] - m[2][3]);
00144    }
00145 
00146    gmtl::Plane<DATA_TYPE> mPlanes[6];
00147 };
00148 
00149 typedef Frustum<float> Frustumf;
00150 typedef Frustum<double> Frustumd;
00151 
00152 }
00153 
00154 
00155 #endif /* _GMTL_FRUSTUM_H_ */

Generated on Sun Sep 19 2010 14:35:14 for GenericMathTemplateLibrary by  doxygen 1.7.1