@@ -174,41 +174,59 @@ std::array<uint8_t *, 6> equirectangularToCubemap(uint8_t *inputPixels, int widt
174174 return outputPixels;
175175}
176176
177- std::array<Eigen::Vector4f, 6 > extractFrustumPlanes (const Eigen::Matrix4f &PV) {
178- std::array<Eigen::Vector4f, 6 > planes;
179- // Left
180- planes[0 ] = {PV (3 , 0 ) + PV (0 , 0 ), PV (3 , 1 ) + PV (0 , 1 ), PV (3 , 2 ) + PV (0 , 2 ), PV (3 , 3 ) + PV (0 , 3 )};
181- // Right
182- planes[1 ] = {PV (3 , 0 ) - PV (0 , 0 ), PV (3 , 1 ) - PV (0 , 1 ), PV (3 , 2 ) - PV (0 , 2 ), PV (3 , 3 ) - PV (0 , 3 )};
183- // Bottom
184- planes[2 ] = {PV (3 , 0 ) + PV (1 , 0 ), PV (3 , 1 ) + PV (1 , 1 ), PV (3 , 2 ) + PV (1 , 2 ), PV (3 , 3 ) + PV (1 , 3 )};
185- // Top
186- planes[3 ] = {PV (3 , 0 ) - PV (1 , 0 ), PV (3 , 1 ) - PV (1 , 1 ), PV (3 , 2 ) - PV (1 , 2 ), PV (3 , 3 ) - PV (1 , 3 )};
187- // Near
188- planes[4 ] = {PV (3 , 0 ) + PV (2 , 0 ), PV (3 , 1 ) + PV (2 , 1 ), PV (3 , 2 ) + PV (2 , 2 ), PV (3 , 3 ) + PV (2 , 3 )};
189- // Far
190- planes[5 ] = {PV (3 , 0 ) - PV (2 , 0 ), PV (3 , 1 ) - PV (2 , 1 ), PV (3 , 2 ) - PV (2 , 2 ), PV (3 , 3 ) - PV (2 , 3 )};
191- // Normalize planes
192- for (int i = 0 ; i < 6 ; i++) {
193- float length = sqrt (planes[i](0 ) * planes[i](0 ) + planes[i](1 ) * planes[i](1 ) + planes[i](2 ) * planes[i](2 ));
194- planes[i](0 ) /= length;
195- planes[i](1 ) /= length;
196- planes[i](2 ) /= length;
197- planes[i](3 ) /= length;
198- }
199- return planes;
177+ Frustum extractFrustumPlanes (const Eigen::Matrix4f &PV) {
178+ Frustum frustum;
179+ auto extract = [](const Eigen::Vector4f &p) {
180+ Plane plane;
181+
182+ Eigen::Vector3f n = p.head <3 >();
183+ float length = n.norm ();
184+
185+ plane.normal = n / length;
186+ plane.distance = p[3 ] / length;
187+
188+ return plane;
189+ };
190+
191+ frustum.planes [0 ] = extract (PV.row (3 ) + PV.row (0 )); // Left
192+ frustum.planes [1 ] = extract (PV.row (3 ) - PV.row (0 )); // Right
193+ frustum.planes [2 ] = extract (PV.row (3 ) + PV.row (1 )); // Bottom
194+ frustum.planes [3 ] = extract (PV.row (3 ) - PV.row (1 )); // Top
195+ frustum.planes [4 ] = extract (PV.row (3 ) + PV.row (2 )); // Near
196+ frustum.planes [5 ] = extract (PV.row (3 ) - PV.row (2 )); // Far
197+
198+ return frustum;
200199}
201200
202- bool isAABBInFrustum (const std::array<Eigen::Vector4f, 6 > &frustum, const AABB &aabb) {
201+ bool isAABBInFrustum (const Frustum &frustum, const AABB &aabb) {
203202 for (int i = 0 ; i < 6 ; i++) {
204- Eigen::Vector3f positive (frustum[i](0 ) >= 0 ? aabb.getMax ().x () : aabb.getMin ().x (),
205- frustum[i](1 ) >= 0 ? aabb.getMax ().y () : aabb.getMin ().y (),
206- frustum[i](2 ) >= 0 ? aabb.getMax ().z () : aabb.getMin ().z ());
203+ Eigen::Vector3f positive (frustum. planes [i]. normal (0 ) >= 0 ? aabb.getMax ().x () : aabb.getMin ().x (),
204+ frustum. planes [i]. normal (1 ) >= 0 ? aabb.getMax ().y () : aabb.getMin ().y (),
205+ frustum. planes [i]. normal (2 ) >= 0 ? aabb.getMax ().z () : aabb.getMin ().z ());
207206
208- if (frustum[i](0 ) * positive.x () + frustum[i](1 ) * positive.y () + frustum[i](2 ) * positive.z () + frustum[i](3 ) < 0 ) {
207+ if (frustum.planes [i].normal (0 ) * positive.x () + frustum.planes [i].normal (1 ) * positive.y () + frustum.planes [i].normal (2 ) * positive.z ()
208+ + frustum.planes [i].distance
209+ < 0 ) {
209210 return false ;
210211 }
211212 }
212213 return true ;
213214}
215+
216+ bool isAABBInFrustum (const Frustum &frustum, const Eigen::Vector3f ¢er, const Eigen::Vector3f &extents) {
217+ for (int i = 0 ; i < 6 ; ++i) {
218+ const Plane &plane = frustum.planes [i];
219+
220+ const Eigen::Vector3f &n = plane.normal ;
221+
222+ float r = extents.x () * std::abs (n.x ()) + extents.y () * std::abs (n.y ()) + extents.z () * std::abs (n.z ());
223+
224+ float s = n.dot (center) + plane.distance ;
225+
226+ if (s + r < 0 .0f )
227+ return false ;
228+ }
229+
230+ return true ; // At least partially inside
231+ }
214232} // namespace ICE
0 commit comments