Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

CFrustum.h

Go to the documentation of this file.
00001 /*
00002 
00003         ZMACHINE 2.0 (SIGFRID)
00004 
00005         CFRUSTUM.H
00006 
00007         author:                 Trombetta Roberto
00008         filename:               CFrustum.h      
00009         description:    main file
00010         date:                   24/06/2003
00011 
00012 */
00013 
00014 #ifndef __CFRUSTUM_H_
00015 #define __CFRUSTUM_H_
00016 
00017 #include "Zdef.h"
00018 #include "Zmacro.h"
00019 #include "CVector3.h"
00020 #include "CPlane.h"
00021 #include "CMatrix.h"
00022 
00023 #define ZM_FRUSTUM_PLANE_RIGHT          0
00024 #define ZM_FRUSTUM_PLANE_LEFT           1
00025 #define ZM_FRUSTUM_PLANE_BOTTOM         2
00026 #define ZM_FRUSTUM_PLANE_TOP            3
00027 #define ZM_FRUSTUM_PLANE_FAR            4
00028 #define ZM_FRUSTUM_PLANE_NEAR           5
00029 
00031 
00033 class CFrustum {
00034 
00035 private: 
00036 
00038 
00046         CPlane Frustumplane[6];
00047 
00048 public:
00049 
00051         CPlane GetPlane (zulong i) {
00052         
00053                 return Frustumplane[i];
00054         
00055         }
00056 
00058 
00059         inline void ExtractFrustum(const CMatrix &ProjectionMatrix) {
00060         
00061                 CMatrix clip;                                                                   
00062                 zfloat  t = 1.0;                                                                                        
00063         
00064                 clip = ProjectionMatrix;
00065              /*
00066                 // Estrae il piano RIGHT
00067                 Frustumplane[0].fA = clip.fm[ 3] - clip.fm[ 0];
00068                 Frustumplane[0].fB = clip.fm[ 7] - clip.fm[ 4];
00069                 Frustumplane[0].fC = clip.fm[11] - clip.fm[ 8];
00070                 Frustumplane[0].fD = clip.fm[15] - clip.fm[12];
00071                 // Lo normalizza
00072                 //t = sqrt( Frustumplane[0].fA * Frustumplane[0].fA + Frustumplane[0].fB * Frustumplane[0].fB + Frustumplane[0].fC * Frustumplane[0].fC );
00073                 Frustumplane[0].fA /= t;
00074                 Frustumplane[0].fB /= t;
00075                 Frustumplane[0].fC /= t;
00076                 Frustumplane[0].fD /= t;
00077         
00078                 // Estrae il piano LEFT
00079                 Frustumplane[1].fA = clip.fm[ 3] + clip.fm[ 0];
00080                 Frustumplane[1].fB = clip.fm[ 7] + clip.fm[ 4];
00081                 Frustumplane[1].fC = clip.fm[11] + clip.fm[ 8];
00082                 Frustumplane[1].fD = clip.fm[15] + clip.fm[12];
00083                 // Lo normalizza
00084                 //t = sqrt( Frustumplane[1].fA * Frustumplane[1].fA + Frustumplane[1].fB * Frustumplane[1].fB + Frustumplane[1].fC * Frustumplane[1].fC );
00085                 Frustumplane[1].fA /= t;
00086                 Frustumplane[1].fB /= t;
00087                 Frustumplane[1].fC /= t;
00088                 Frustumplane[1].fD /= t;
00089                 
00090                 // Estrae il piano BOTTOM
00091                 Frustumplane[2].fA = clip.fm[ 3] + clip.fm[ 1];
00092                 Frustumplane[2].fB = clip.fm[ 7] + clip.fm[ 5];
00093                 Frustumplane[2].fC = clip.fm[11] + clip.fm[ 9];
00094                 Frustumplane[2].fD = clip.fm[15] + clip.fm[13];
00095                 // Lo normalizza
00096                 //t = sqrt( Frustumplane[2].fA * Frustumplane[2].fA + Frustumplane[2].fB * Frustumplane[2].fB + Frustumplane[2].fC * Frustumplane[2].fC );
00097                 Frustumplane[2].fA /= t;
00098                 Frustumplane[2].fB /= t;
00099                 Frustumplane[2].fC /= t;
00100                 Frustumplane[2].fD /= t;
00101         
00102                 // Estrae il piano TOP
00103                 Frustumplane[3].fA = clip.fm[ 3] - clip.fm[ 1];
00104                 Frustumplane[3].fB = clip.fm[ 7] - clip.fm[ 5];
00105                 Frustumplane[3].fC = clip.fm[11] - clip.fm[ 9];
00106                 Frustumplane[3].fD = clip.fm[15] - clip.fm[13];
00107                 // Lo normalizza
00108                 //t = sqrt( Frustumplane[3].fA * Frustumplane[3].fA + Frustumplane[3].fB * Frustumplane[3].fB + Frustumplane[3].fC * Frustumplane[3].fC );
00109                 Frustumplane[3].fA /= t;
00110                 Frustumplane[3].fB /= t;
00111                 Frustumplane[3].fC /= t;
00112                 Frustumplane[3].fD /= t;
00113         
00114                 // Estrae il piano FAR
00115                 Frustumplane[4].fA = clip.fm[ 3] - clip.fm[ 2];
00116                 Frustumplane[4].fB = clip.fm[ 7] - clip.fm[ 6];
00117                 Frustumplane[4].fC = clip.fm[11] - clip.fm[10];
00118                 Frustumplane[4].fD = clip.fm[15] - clip.fm[14]; 
00119                 // Lo normalizza
00120                 //t = sqrt( Frustumplane[4].fA * Frustumplane[4].fA + Frustumplane[4].fB * Frustumplane[4].fB + Frustumplane[4].fC * Frustumplane[4].fC );
00121                 Frustumplane[4].fA /= t;
00122                 Frustumplane[4].fB /= t;
00123                 Frustumplane[4].fC /= t;
00124                 Frustumplane[4].fD /= t;
00125 
00126                 // Estrae il piano NEAR
00127                 Frustumplane[5].fA = clip.fm[ 3] + clip.fm[ 2];
00128                 Frustumplane[5].fB = clip.fm[ 7] + clip.fm[ 6];
00129                 Frustumplane[5].fC = clip.fm[11] + clip.fm[10];
00130                 Frustumplane[5].fD = clip.fm[15] + clip.fm[14];
00131                 // Lo normalizza
00132                 //t = sqrt( Frustumplane[5].fA * Frustumplane[5].fA + Frustumplane[5].fB * Frustumplane[5].fB + Frustumplane[5].fC * Frustumplane[5].fC );
00133                 Frustumplane[5].fA /= t;
00134                 Frustumplane[5].fB /= t;
00135                 Frustumplane[5].fC /= t;
00136                 Frustumplane[5].fD /= t;
00137                 */
00138            
00139 
00140                 // RIGHT
00141                 Frustumplane[0].fA = clip.fm[12] - clip.fm[ 0];
00142                 Frustumplane[0].fB = clip.fm[13] - clip.fm[ 1];
00143                 Frustumplane[0].fC = clip.fm[14] - clip.fm[ 2];
00144                 Frustumplane[0].fD = clip.fm[15] - clip.fm[ 3];
00145 
00146                 // LEFT
00147                 Frustumplane[1].fA = clip.fm[12] + clip.fm[ 0];
00148                 Frustumplane[1].fB = clip.fm[13] + clip.fm[ 1];
00149                 Frustumplane[1].fC = clip.fm[14] + clip.fm[ 2];
00150                 Frustumplane[1].fD = clip.fm[15] + clip.fm[ 3];
00151 
00152                 // BOTTOM
00153                 Frustumplane[2].fA = clip.fm[12] + clip.fm[ 4];
00154                 Frustumplane[2].fB = clip.fm[13] + clip.fm[ 5];
00155                 Frustumplane[2].fC = clip.fm[14] + clip.fm[ 6];
00156                 Frustumplane[2].fD = clip.fm[15] + clip.fm[ 7];
00157 
00158                 // TOP
00159                 Frustumplane[3].fA = clip.fm[12] - clip.fm[ 4];
00160                 Frustumplane[3].fB = clip.fm[13] - clip.fm[ 5];
00161                 Frustumplane[3].fC = clip.fm[14] - clip.fm[ 6];
00162                 Frustumplane[3].fD = clip.fm[15] - clip.fm[ 7];
00163 
00164                 // FAR
00165                 Frustumplane[4].fA = clip.fm[12] - clip.fm[ 8];
00166                 Frustumplane[4].fB = clip.fm[13] - clip.fm[ 9];
00167                 Frustumplane[4].fC = clip.fm[14] - clip.fm[10];
00168                 Frustumplane[4].fD = clip.fm[15] - clip.fm[11];
00169 
00170                 // NEAR
00171                 Frustumplane[5].fA = clip.fm[12] + clip.fm[ 8];
00172                 Frustumplane[5].fB = clip.fm[13] + clip.fm[ 9];
00173                 Frustumplane[5].fC = clip.fm[14] + clip.fm[10];
00174                 Frustumplane[5].fD = clip.fm[15] + clip.fm[11];
00175                 
00176                 
00177 
00178         }
00179 
00181 
00195         inline zint PointInCFrustum(const CVector3 &v) const {
00196 
00197                 if (Frustumplane[ZM_FRUSTUM_PLANE_RIGHT].Dist(v)        <= 0)
00198                         return false;
00199                 if (Frustumplane[ZM_FRUSTUM_PLANE_LEFT].Dist(v)         <= 0)
00200                         return false;
00201                 if (Frustumplane[ZM_FRUSTUM_PLANE_BOTTOM].Dist(v)       <= 0)
00202                         return false;
00203                 if (Frustumplane[ZM_FRUSTUM_PLANE_TOP].Dist(v)          <= 0)
00204                         return false;
00205                 if (Frustumplane[ZM_FRUSTUM_PLANE_NEAR].Dist(v)         <= 0)
00206                         return false;
00207                 if (Frustumplane[ZM_FRUSTUM_PLANE_FAR].Dist(v)          <= 0)
00208                         return false;
00209 
00210                 return true;
00211         }
00212 
00214 
00233         inline zint SphereInCFrustum(const CVector3 &v, zfloat radius) const {
00234 
00235                 if (Frustumplane[ZM_FRUSTUM_PLANE_RIGHT].Dist(v)        <= -radius)
00236                         return false;
00237                 if (Frustumplane[ZM_FRUSTUM_PLANE_LEFT].Dist(v)         <= -radius)
00238                         return false;
00239                 if (Frustumplane[ZM_FRUSTUM_PLANE_BOTTOM].Dist(v)       <= -radius)
00240                         return false;
00241                 if (Frustumplane[ZM_FRUSTUM_PLANE_TOP].Dist(v)          <= -radius)
00242                         return false;
00243                 if (Frustumplane[ZM_FRUSTUM_PLANE_NEAR].Dist(v)         <= -radius)
00244                         return false;
00245                 if (Frustumplane[ZM_FRUSTUM_PLANE_FAR].Dist(v)          <= -radius)
00246                         return false;
00247         
00248                 return true;
00249         }
00250 
00251         
00253 
00258         /*
00259         inline int CubeInFrustum(const CCube &c) const {
00260                 
00261                 bool allin = true;
00262 
00263                 if (c.Intersect(Frustumplane[ZM_FRUSTUM_PLANE_RIGHT])   != ZM_INT_ALLIN) allin = false;
00264                 if (c.Intersect(Frustumplane[ZM_FRUSTUM_PLANE_RIGHT])   == ZM_INT_ALLOUT) return ZM_INT_ALLOUT;
00265                 if (c.Intersect(Frustumplane[ZM_FRUSTUM_PLANE_LEFT])    != ZM_INT_ALLIN) allin = false;
00266                 if (c.Intersect(Frustumplane[ZM_FRUSTUM_PLANE_LEFT])    == ZM_INT_ALLOUT) return ZM_INT_ALLOUT;
00267                 if (c.Intersect(Frustumplane[ZM_FRUSTUM_PLANE_BOTTOM])  != ZM_INT_ALLIN) allin = false;
00268                 if (c.Intersect(Frustumplane[ZM_FRUSTUM_PLANE_BOTTOM])  == ZM_INT_ALLOUT) return ZM_INT_ALLOUT;
00269                 if (c.Intersect(Frustumplane[ZM_FRUSTUM_PLANE_TOP])             != ZM_INT_ALLIN) allin = false;
00270                 if (c.Intersect(Frustumplane[ZM_FRUSTUM_PLANE_TOP])             == ZM_INT_ALLOUT) return ZM_INT_ALLOUT;
00271                 if (c.Intersect(Frustumplane[ZM_FRUSTUM_PLANE_FAR])             != ZM_INT_ALLIN) allin = false;
00272                 if (c.Intersect(Frustumplane[ZM_FRUSTUM_PLANE_FAR])             == ZM_INT_ALLOUT) return ZM_INT_ALLOUT;
00273                 if (c.Intersect(Frustumplane[ZM_FRUSTUM_PLANE_NEAR])    != ZM_INT_ALLIN) allin = false;
00274                 if (c.Intersect(Frustumplane[ZM_FRUSTUM_PLANE_NEAR])    == ZM_INT_ALLOUT) return ZM_INT_ALLOUT;
00275 
00276                 if (allin) return ZM_INT_ALLIN;
00277                 return ZM_INT_OK;
00278         }
00279         */
00280 };
00281 
00282 #endif // __CFRUSTUM_H_



SourceForge.net Logo