7 #include <Eigen/Geometry> 10 using namespace Eigen;
14 template<
typename Scalar,
int Dim >
15 ostream& operator<<( ostream& out, const Hyperplane< Scalar, Dim >& plane )
17 out <<
"normal:" << plane.normal() << endl <<
", offset: " << plane.offset();
22 template<
typename Scalar,
int Dim >
23 ostream& operator<<( ostream& out, const AlignedBox< Scalar, Dim >& box )
25 out <<
"min:" << box.min() << endl <<
", max: " << box.max() << endl <<
", size: " << box.sizes();
35 using Plane = Hyperplane< float, 3 >;
36 using Box = AlignedBox< float, 3 >;
42 for(
int i = 0; i < 6; ++i )
44 m_planes.push_back(
new Plane() );
47 extractPlanes( mvp,
true );
52 for(
Plane* plane : m_planes )
62 extractPlanes( mvp,
true );
70 bool isCullable(
const Box& box );
74 cout <<
"Frustum planes:" << endl << endl;
75 for(
int i = 0; i < 6; ++i )
79 case 0: cout <<
"Left: ";
break;
80 case 1: cout <<
"Right: ";
break;
81 case 2: cout <<
"Top: ";
break;
82 case 3: cout <<
"Bottom: ";
break;
83 case 4: cout <<
"Near: ";
break;
84 case 5: cout <<
"Far: ";
break;
86 cout << *f.
m_planes[ i ] << endl << endl;
88 cout <<
"End of frustum planes." << endl;
104 Vector4f& leftCoeffs = m_planes[ 0 ]->coeffs();
105 leftCoeffs[ 0 ] = -( mvp( 3, 0 ) + mvp( 0, 0 ) );
106 leftCoeffs[ 1 ] = -( mvp( 3, 1 ) + mvp( 0, 1 ) );
107 leftCoeffs[ 2 ] = -( mvp( 3, 2 ) + mvp( 0, 2 ) );
108 leftCoeffs[ 3 ] = -( mvp( 3, 3 ) + mvp( 0, 3 ) );
111 Vector4f& rightCoeffs = m_planes[ 1 ]->coeffs();
112 rightCoeffs[ 0 ] = -( mvp( 3, 0 ) - mvp( 0, 0 ) );
113 rightCoeffs[ 1 ] = -( mvp( 3, 1 ) - mvp( 0, 1 ) );
114 rightCoeffs[ 2 ] = -( mvp( 3, 2 ) - mvp( 0, 2 ) );
115 rightCoeffs[ 3 ] = -( mvp( 3, 3 ) - mvp( 0, 3 ) );
118 Vector4f& topCoeffs = m_planes[ 2 ]->coeffs();
119 topCoeffs[ 0 ] = -( mvp( 3, 0 ) - mvp( 1, 0 ) );
120 topCoeffs[ 1 ] = -( mvp( 3, 1 ) - mvp( 1, 1 ) );
121 topCoeffs[ 2 ] = -( mvp( 3, 2 ) - mvp( 1, 2 ) );
122 topCoeffs[ 3 ] = -( mvp( 3, 3 ) - mvp( 1, 3 ) );
125 Vector4f& botCoeffs = m_planes[ 3 ]->coeffs();
126 botCoeffs[ 0 ] = -( mvp( 3, 0 ) + mvp( 1, 0 ) );
127 botCoeffs[ 1 ] = -( mvp( 3, 1 ) + mvp( 1, 1 ) );
128 botCoeffs[ 2 ] = -( mvp( 3, 2 ) + mvp( 1, 2 ) );
129 botCoeffs[ 3 ] = -( mvp( 3, 3 ) + mvp( 1, 3 ) );
132 Vector4f& nearCoeffs = m_planes[ 4 ]->coeffs();
133 nearCoeffs[ 0 ] = -( mvp( 3, 0 ) + mvp( 2, 0 ) );
134 nearCoeffs[ 1 ] = -( mvp( 3, 1 ) + mvp( 2, 1 ) );
135 nearCoeffs[ 2 ] = -( mvp( 3, 2 ) + mvp( 2, 2 ) );
136 nearCoeffs[ 3 ] = -( mvp( 3, 3 ) + mvp( 2, 3 ) );
139 Vector4f& farCoeffs = m_planes[ 5 ]->coeffs();
140 farCoeffs[ 0 ] = -( mvp( 3, 0 ) - mvp( 2, 0 ) );
141 farCoeffs[ 1 ] = -( mvp( 3, 1 ) - mvp( 2, 1 ) );
142 farCoeffs[ 2 ] = -( mvp( 3, 2 ) - mvp( 2, 2 ) );
143 farCoeffs[ 3 ] = -( mvp( 3, 3 ) - mvp( 2, 3 ) );
147 for(
Plane* plane : m_planes )
158 inline bool Frustum::isCullable(
const Box& box )
162 Vector3f boxSizes = box.sizes();
163 Vector3f boxMin = box.min();
164 Vector3f boxMax = box.max();
168 for(
int i = 0; i < 6; ++i )
170 Plane* plane = m_planes[ i ];
171 Vector3f normal = plane->normal();
172 float dilatation = abs( boxSizes.dot( normal ) );
173 float offset = plane->offset() - dilatation;
174 Plane dilatatedPlane( normal, offset );
180 n[ 0 ] = ( normal[ 0 ] < 0 ) ? boxMax[ 0 ] : boxMin[ 0 ];
181 n[ 1 ] = ( normal[ 1 ] < 0 ) ? boxMax[ 1 ] : boxMin[ 1 ];
182 n[ 2 ] = ( normal[ 2 ] < 0 ) ? boxMax[ 2 ] : boxMin[ 2 ];
184 float signedDist = dilatatedPlane.signedDistance( n );
Hyperplane< float, 3 > Plane
Definition: frustum.hpp:35
Definition: bufferobject.hpp:34
vector< Plane * > m_planes
Definition: frustum.hpp:155
Frustum(const Matrix4f &mvp)
Definition: frustum.hpp:39
AlignedBox< float, 3 > Box
Definition: frustum.hpp:36
Definition: frustum.hpp:33
friend ostream & operator<<(ostream &out, const Frustum &f)
Definition: frustum.hpp:72
~Frustum()
Definition: frustum.hpp:50
void update(const Matrix4f &mvp)
Definition: frustum.hpp:60
void extractPlanes(const Matrix4f &mvp, const bool &normalize)
Definition: frustum.hpp:98