00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "CFrustum.h"
00014
00015
00016
00017
00018
00019
00020
00021
00022 void CFrustum::CalculateFrustum(const CMatrix44 &projection, const CMatrix44 &view)
00023 {
00024 CMatrix44 temp;
00025 CVector3f v;
00026 float n;
00027
00028 temp.Multiply(projection, view);
00029
00030
00031
00032 v.Set(temp._41 + temp._11, temp._42 + temp._12, temp._43 + temp._13);
00033 n = 1.0f / v.Length();
00034 m_Planes[0].m_A = v.m_X * n;
00035 m_Planes[0].m_B = v.m_Y * n;
00036 m_Planes[0].m_C = v.m_Z * n;
00037 m_Planes[0].m_D = (temp._44 + temp._14) * n;
00038
00039
00040 v.Set(temp._41 - temp._11, temp._42 - temp._12, temp._43 - temp._13);
00041 n = 1.0f / v.Length();
00042 m_Planes[1].m_A = v.m_X * n;
00043 m_Planes[1].m_B = v.m_Y * n;
00044 m_Planes[1].m_C = v.m_Z * n;
00045 m_Planes[1].m_D = (temp._44 - temp._14) * n;
00046
00047
00048 v.Set(temp._41 + temp._21, temp._42 + temp._22, temp._43 + temp._23);
00049 n = 1.0f / v.Length();
00050 m_Planes[2].m_A = v.m_X * n;
00051 m_Planes[2].m_B = v.m_Y * n;
00052 m_Planes[2].m_C = v.m_Z * n;
00053 m_Planes[2].m_D = (temp._44 + temp._24) * n;
00054
00055
00056 v.Set(temp._41 - temp._21, temp._42 - temp._22, temp._43 - temp._23);
00057 n = 1.0f / v.Length();
00058 m_Planes[3].m_A = v.m_X * n;
00059 m_Planes[3].m_B = v.m_Y * n;
00060 m_Planes[3].m_C = v.m_Z * n;
00061 m_Planes[3].m_D = (temp._44 - temp._24) * n;
00062
00063
00064 v.Set(temp._41 + temp._31, temp._42 + temp._32, temp._43 + temp._33);
00065 n = 1.0f / v.Length();
00066 m_Planes[4].m_A = v.m_X * n;
00067 m_Planes[4].m_B = v.m_Y * n;
00068 m_Planes[4].m_C = v.m_Z * n;
00069 m_Planes[4].m_D = (temp._44 + temp._34) * n;
00070
00071
00072 v.Set(temp._41 - temp._31, temp._42 - temp._32, temp._43 - temp._33);
00073 n = 1.0f / v.Length();
00074 m_Planes[5].m_A = v.m_X * n;
00075 m_Planes[5].m_B = v.m_Y * n;
00076 m_Planes[5].m_C = v.m_Z * n;
00077 m_Planes[5].m_D = (temp._44 - temp._34) * n;
00078 }
00079
00080 sbyte CFrustum::SphereInFrustum(const CSpheref &sphere) const
00081 {
00082 float fDistance;
00083 for(byte p = 0; p < 6; ++p)
00084 {
00085 fDistance = m_Planes[p].m_A * sphere.m_vCenter.m_X + m_Planes[p].m_B * sphere.m_vCenter.m_Y + m_Planes[p].m_C * sphere.m_vCenter.m_Z + m_Planes[p].m_D;
00086 if(fDistance < -sphere.m_Radius)
00087 return FRUSTUM_OUTSIDE;
00088 else if(fDistance > -sphere.m_Radius && fDistance < sphere.m_Radius)
00089 return FRUSTUM_INTERSECTS;
00090 }
00091 return FRUSTUM_INSIDE;
00092 }
00093
00094 bool CFrustum::BoxInFrustum(const CAABox3f &box) const
00095 {
00096 byte p;
00097 for(p = 0; p < 6; ++p)
00098 {
00099 if(m_Planes[p].m_A * box.m_MinX + m_Planes[p].m_B * box.m_MinY + m_Planes[p].m_C * box.m_MinZ + m_Planes[p].m_D > 0)
00100 continue;
00101 if(m_Planes[p].m_A * box.m_MaxX + m_Planes[p].m_B * box.m_MinY + m_Planes[p].m_C * box.m_MinZ + m_Planes[p].m_D > 0)
00102 continue;
00103 if(m_Planes[p].m_A * box.m_MinX + m_Planes[p].m_B * box.m_MaxY + m_Planes[p].m_C * box.m_MinZ + m_Planes[p].m_D > 0)
00104 continue;
00105 if(m_Planes[p].m_A * box.m_MaxX + m_Planes[p].m_B * box.m_MaxY + m_Planes[p].m_C * box.m_MinZ + m_Planes[p].m_D > 0)
00106 continue;
00107 if(m_Planes[p].m_A * box.m_MinX + m_Planes[p].m_B * box.m_MinY + m_Planes[p].m_C * box.m_MaxZ + m_Planes[p].m_D > 0)
00108 continue;
00109 if(m_Planes[p].m_A * box.m_MaxX + m_Planes[p].m_B * box.m_MinY + m_Planes[p].m_C * box.m_MaxZ + m_Planes[p].m_D > 0)
00110 continue;
00111 if(m_Planes[p].m_A * box.m_MinX + m_Planes[p].m_B * box.m_MaxY + m_Planes[p].m_C * box.m_MaxZ + m_Planes[p].m_D > 0)
00112 continue;
00113 if(m_Planes[p].m_A * box.m_MaxX + m_Planes[p].m_B * box.m_MaxY + m_Planes[p].m_C * box.m_MaxZ + m_Planes[p].m_D > 0)
00114 continue;
00115 return false;
00116 }
00117 return true;
00118 }
00119
00120 bool CFrustum::BoxInFrustum(const CBox3f &box) const
00121 {
00122 byte p;
00123 for(p = 0; p < 6; ++p)
00124 {
00125 if(m_Planes[p].Distance(box.m_vPoint) > 0.0f)
00126 continue;
00127 if(m_Planes[p].Distance(box.m_vPoint + box.m_vA) > 0.0f)
00128 continue;
00129 if(m_Planes[p].Distance(box.m_vPoint + box.m_vB) > 0.0f)
00130 continue;
00131 if(m_Planes[p].Distance(box.m_vPoint + box.m_vC) > 0.0f)
00132 continue;
00133 if(m_Planes[p].Distance(box.m_vPoint + box.m_vA + box.m_vB) > 0.0f)
00134 continue;
00135 if(m_Planes[p].Distance(box.m_vPoint + box.m_vA + box.m_vC) > 0.0f)
00136 continue;
00137 if(m_Planes[p].Distance(box.m_vPoint + box.m_vB + box.m_vC) > 0.0f)
00138 continue;
00139 if(m_Planes[p].Distance(box.m_vPoint + box.m_vA + box.m_vB + box.m_vC) > 0.0f)
00140 continue;
00141
00142 return false;
00143 }
00144 return true;
00145 }
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155