DeferredRenderer 1.0

Frustum.cpp

Go to the documentation of this file.
00001 #include "Frustum.h"
00002 #include "ngl/Vector.h"
00003 
00004 void Frustum::buildPlane(Plane &_plane, const ngl::Matrix &_mat, int _row, int _sign)
00005 {
00006     _plane.a = _mat.m_m[0][3] + _sign * _mat.m_m[0][_row];
00007     _plane.b = _mat.m_m[1][3] + _sign * _mat.m_m[1][_row];
00008     _plane.c = _mat.m_m[2][3] + _sign * _mat.m_m[2][_row];
00009     _plane.d = _mat.m_m[3][3] + _sign * _mat.m_m[3][_row];
00010 
00011     normalizePlane(_plane);
00012 }
00013 
00014 void Frustum::generate(const ngl::Matrix &_mat)
00015 {
00016     buildPlane(m_planes[e_left],_mat,0,e_plus);
00017     buildPlane(m_planes[e_right],_mat,0,e_minus);
00018     buildPlane(m_planes[e_bottom],_mat,1,e_plus);
00019     buildPlane(m_planes[e_top],_mat,1,e_minus);
00020     buildPlane(m_planes[e_near],_mat,2,e_plus);
00021     buildPlane(m_planes[e_far],_mat,2,e_minus);
00022 }
00023 
00024 bool Frustum::isAbovePlane(unsigned int _planeSide, const ngl::Vector &_point)const
00025 {
00026     return m_planes[_planeSide].a*_point.m_x + m_planes[_planeSide].b*_point.m_y + m_planes[_planeSide].c*_point.m_z + m_planes[_planeSide].d >= 0;
00027 }
00028 
00029 bool Frustum::isInFrustum(const ngl::Vector &_point)const
00030 {
00031     for (unsigned int i=0;i<6;i++) {
00032             if (!isAbovePlane(i,_point))
00033                     return false;
00034     }
00035     return true;
00036 }
00037 
00038 bool Frustum::isInFrustum(const ngl::Vector &_min,const ngl::Vector &_max)const
00039 {
00040     ngl::Vector vmin, vmax;
00041     for (unsigned int i=0;i<6;i++) {
00042             if (m_planes[i].a>0) {
00043                     vmin.m_x=_min.m_x;
00044                     vmax.m_x=_max.m_x;
00045             } else {
00046                     vmin.m_x=_max.m_x;
00047                     vmax.m_x=_min.m_x;
00048             }
00049             if (m_planes[i].b>0) {
00050                     vmin.m_y=_min.m_y;
00051                     vmax.m_y=_max.m_y;
00052             } else {
00053                     vmin.m_y=_max.m_y;
00054                     vmax.m_y=_min.m_y;
00055             }
00056             if (m_planes[i].c>0) {
00057                     vmin.m_z=_min.m_z;
00058                     vmax.m_z=_max.m_z;
00059             } else {
00060                     vmin.m_z=_max.m_z;
00061                     vmax.m_z=_min.m_z;
00062             }
00063             if (!isAbovePlane(i,vmax))
00064                     return false;
00065     }
00066     return true;
00067 
00068 }
00069 
00070 void Frustum::normalizePlane(Plane &_plane)
00071 {
00072     float mag;
00073     mag = sqrt(_plane.a * _plane.a + _plane.b * _plane.b + _plane.c * _plane.c);
00074     _plane.a = _plane.a / mag;
00075     _plane.b = _plane.b / mag;
00076     _plane.c = _plane.c / mag;
00077     _plane.d = _plane.d / mag;
00078 
00079 }
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines