00001
00002 template<typename Real>
00003 Frustum<Real>::Frustum(void) {
00004 for(int i=0; i<6; plane[i++].zero());
00005 }
00006
00007 template<typename Real>
00008 Frustum<Real>::Frustum(const m4x4<Real>& vp) {
00009 compute(vp);
00010 }
00011
00012 template<typename Real>
00013 void Frustum<Real>::compute(const m4x4<Real>& vp) {
00014
00015 plane[0].x = vp(3,0) + vp(0,0);
00016 plane[0].y = vp(3,1) + vp(0,1);
00017 plane[0].z = vp(3,2) + vp(0,2);
00018 plane[0].w = vp(3,3) + vp(0,3);
00019
00020 plane[1].x = vp(3,0) - vp(0,0);
00021 plane[1].y = vp(3,1) - vp(0,1);
00022 plane[1].z = vp(3,2) - vp(0,2);
00023 plane[1].w = vp(3,3) - vp(0,3);
00024
00025 plane[2].x = vp(3,0) - vp(1,0);
00026 plane[2].y = vp(3,1) - vp(1,1);
00027 plane[2].z = vp(3,2) - vp(1,2);
00028 plane[2].w = vp(3,3) - vp(1,3);
00029
00030 plane[3].x = vp(3,0) + vp(1,0);
00031 plane[3].y = vp(3,1) + vp(1,1);
00032 plane[3].z = vp(3,2) + vp(1,2);
00033 plane[3].w = vp(3,3) + vp(1,3);
00034
00035 plane[4].x = vp(3,0) - vp(2,0);
00036 plane[4].y = vp(3,1) - vp(2,1);
00037 plane[4].z = vp(3,2) - vp(2,2);
00038 plane[4].w = vp(3,3) - vp(2,3);
00039
00040 plane[5].x = vp(2,0);
00041 plane[5].y = vp(2,1);
00042 plane[5].z = vp(2,2);
00043 plane[5].w = vp(2,3);
00044
00045 float mag;
00046 #define NORM(plane) \
00047 mag = 1/sqrt(plane.x*plane.x + plane.y*plane.y + plane.z*plane.z);\
00048 plane.x *= mag;\
00049 plane.y *= mag;\
00050 plane.z *= mag;\
00051 plane.w *= mag;
00052
00053 for(int i=0; i<6; i++){
00054 NORM(plane[i]);
00055 }
00056 #undef NORM
00057 }
00058
00059 template<typename Real>
00060 bool Frustum<Real>::intersect(float3 position, float radius) {
00061 return ((plane[0].x*position.x + plane[0].y*position.y + plane[0].z*position.z + plane[0].w + radius) > 0.f)
00062 && ((plane[1].x*position.x + plane[1].y*position.y + plane[1].z*position.z + plane[1].w + radius) > 0.f)
00063 && ((plane[2].x*position.x + plane[2].y*position.y + plane[2].z*position.z + plane[2].w + radius) > 0.f)
00064 && ((plane[3].x*position.x + plane[3].y*position.y + plane[3].z*position.z + plane[3].w + radius) > 0.f)
00065 && ((plane[4].x*position.x + plane[4].y*position.y + plane[4].z*position.z + plane[4].w + radius) > 0.f)
00066 && ((plane[5].x*position.x + plane[5].y*position.y + plane[5].z*position.z + plane[5].w + radius) > 0.f);
00067 }