Plasma Engine  2.0
Loading...
Searching...
No Matches
Frustum_inl.h
1#pragma once
2
3#include <Foundation/Math/Frustum.h>
4
5PL_FORCE_INLINE bool plFrustum::Overlaps(const plSimdBBox& object) const
6{
7 plSimdVec4f center2 = object.m_Min + object.m_Max;
8 plSimdVec4f extents = object.GetExtents();
9
10 // We're working with center and extents scaled by two - but the plane equation still works out
11 // correctly since we set W = 2 here.
12 center2.SetW(plSimdFloat(2.0f));
13 extents.SetW(plSimdFloat::MakeZero());
14
15#if PL_SIMD_IMPLEMENTATION == PL_SIMD_IMPLEMENTATION_SSE
16 plSimdVec4f minusZero;
17 minusZero.Set(-0.0f);
18#endif
19
20 for (plUInt32 plane = 0; plane < PLANE_COUNT; ++plane)
21 {
22 plSimdVec4f equation;
23 equation.Load<4>(m_Planes[plane].m_vNormal.GetData());
24
25 // Change signs of extents to match signs of plane normal
26 plSimdVec4f maxExtent;
27
28#if PL_SIMD_IMPLEMENTATION == PL_SIMD_IMPLEMENTATION_SSE
29 // Specialized for SSE - this is faster than FlipSign for multiple calls since we can preload the constant -0.0f
30 maxExtent.m_v = _mm_xor_ps(extents.m_v, _mm_andnot_ps(equation.m_v, minusZero.m_v));
31#else
32 maxExtent = extents.FlipSign(equation >= plSimdVec4f::MakeZero());
33#endif
34
35 // Compute AABB corner which is the furthest along the plane normal
36 const plSimdVec4f offset = center2 + maxExtent;
37
38 if (equation.Dot<4>(offset) > plSimdFloat::MakeZero())
39 {
40 // outside
41 return false;
42 }
43 }
44
45 // inside or intersect
46 return true;
47}
48
49PL_FORCE_INLINE bool plFrustum::Overlaps(const plSimdBSphere& object) const
50{
51 // Calculate the minimum distance of the given sphere's center to all frustum planes.
52 plSimdFloat xSphere = object.m_CenterAndRadius.x();
53 plSimdFloat ySphere = object.m_CenterAndRadius.y();
54 plSimdFloat zSphere = object.m_CenterAndRadius.z();
55 plSimdVec4f radius;
56 radius.Set(object.m_CenterAndRadius.w());
57
58 plSimdVec4f xPlanes0;
59 plSimdVec4f yPlanes0;
60 plSimdVec4f zPlanes0;
61 plSimdVec4f dPlanes0;
62
63 xPlanes0.Load<4>(m_Planes[0].m_vNormal.GetData());
64 yPlanes0.Load<4>(m_Planes[1].m_vNormal.GetData());
65 zPlanes0.Load<4>(m_Planes[2].m_vNormal.GetData());
66 dPlanes0.Load<4>(m_Planes[3].m_vNormal.GetData());
67
68 {
69 plSimdMat4f tmp;
70 tmp.SetRows(xPlanes0, yPlanes0, zPlanes0, dPlanes0);
71 xPlanes0 = -tmp.m_col0;
72 yPlanes0 = -tmp.m_col1;
73 zPlanes0 = -tmp.m_col2;
74 dPlanes0 = -tmp.m_col3;
75 }
76
77 plSimdVec4f minDist0;
78 minDist0 = dPlanes0 + xPlanes0 * xSphere;
79 minDist0 += yPlanes0 * ySphere;
80 minDist0 += zPlanes0 * zSphere;
81
82 plSimdVec4f xPlanes1;
83 plSimdVec4f yPlanes1;
84 plSimdVec4f zPlanes1;
85 plSimdVec4f dPlanes1;
86
87 xPlanes1.Load<4>(m_Planes[4].m_vNormal.GetData());
88 yPlanes1.Load<4>(m_Planes[5].m_vNormal.GetData());
89 zPlanes1.Load<4>(m_Planes[5].m_vNormal.GetData());
90 dPlanes1.Load<4>(m_Planes[5].m_vNormal.GetData());
91
92 {
93 plSimdMat4f tmp;
94 tmp.SetRows(xPlanes1, yPlanes1, zPlanes1, dPlanes1);
95 xPlanes1 = -tmp.m_col0;
96 yPlanes1 = -tmp.m_col1;
97 zPlanes1 = -tmp.m_col2;
98 dPlanes1 = -tmp.m_col3;
99 }
100
101
102 plSimdVec4f minDist1 = dPlanes1 + xPlanes1 * xSphere;
103 minDist1 += yPlanes1 * ySphere;
104 minDist1 += zPlanes1 * zSphere;
105
106 plSimdVec4f minDist = minDist0.CompMin(minDist1);
107
108 // Add the sphere radius to cater for the sphere's extents.
109 minDist += radius;
110
111 // If the distance is still less than zero, the sphere is completely "outside" of at least one plane.
112 return (minDist < plSimdVec4f::MakeZero()).NoneSet();
113}
bool Overlaps(const plSimdBBox &object) const
Returns true if the object is fully inside the frustum or partially overlaps it. Returns false when t...
Definition Frustum_inl.h:5
Definition SimdBBox.h:6
Definition SimdBSphere.h:6
Definition SimdFloat.h:7
static plSimdFloat MakeZero()
Creates an plSimdFloat that is initialized to zero.
Definition FPUFloat_inl.h:36
A 4x4 matrix class.
Definition SimdMat4f.h:7
A 4-component SIMD vector class.
Definition SimdVec4f.h:8
static plSimdVec4f MakeZero()
Creates an plSimdVec4f that is initialized to zero.
Definition SimdVec4f_inl.h:8
const Type * GetData() const
Returns the data as an array.
Definition Vec3.h:75