Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

frustum.cpp

Go to the documentation of this file.
00001 /* -*- mode: c++ -*-
00002    
00003    this file is part of rcssserver3D
00004    Fri May 9 2003
00005    Copyright (C) 2003 Koblenz University
00006    $Id: frustum.cpp,v 1.3 2003/09/10 05:54:53 tomhoward Exp $
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; version 2 of the License.
00011   
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016  
00017    You should have received a copy of the GNU General Public License
00018    along with this program; if not, write to the Free Software
00019    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020 */
00021 #include "frustum.h"
00022 #include "matrix.h"
00023 
00024 #include <cstdio>
00025 
00026 using namespace salt;
00027 
00028 void Frustum::Dump() const
00029 {
00030     printf("Frustum %p:\n", this);
00031     for(int i=0; i<6; ++i)
00032     {
00033         printf("  %d: %f %f %f %f\n", i, mPlanes[i].normal.x(), mPlanes[i].normal.y(), mPlanes[i].normal.z(), mPlanes[i].d);
00034     }
00035 }
00036 void Frustum::Set(const Matrix& worldTransform, float fov, float zNear, float zFar, float aspect)
00037 {
00038     Matrix viewTransform = worldTransform;
00039     viewTransform.InvertRotationMatrix();
00040 
00041     float halfWorldWidth    = zNear * (float)tan(gDegToRad(fov*0.5f));
00042     float halfWorldHeight   = halfWorldWidth * (aspect);
00043     
00044     Matrix projectionTransform;
00045     
00046     projectionTransform.Identity();
00047     projectionTransform(0,0) = zNear/halfWorldWidth;
00048     projectionTransform(1,1) = zNear/halfWorldHeight;
00049     projectionTransform(2,2) = -(zFar+zNear)/(zFar-zNear);
00050     projectionTransform(2,3) = -(2.0f*zFar*zNear)/(zFar-zNear);
00051     projectionTransform(3,2) = -1;
00052     projectionTransform(3,3) = 0;
00053     
00054     // concatenate projection and view transform
00055     Matrix frustumMatrix = projectionTransform * viewTransform;
00056 
00057     // Get plane parameters
00058     float *m= frustumMatrix.m;
00059    
00060     Plane *p = &mPlanes[Frustum::PI_RIGHT];
00061     p->normal.Set(m[3]-m[0], m[7]-m[4], m[11]-m[8]);
00062     p->d = m[15]-m[12];
00063     
00064     p = &mPlanes[Frustum::PI_LEFT];
00065     p->normal.Set(m[3]+m[0], m[7]+m[4], m[11]+m[8]);
00066     p->d = m[15]+m[12];
00067 
00068     p = &mPlanes[Frustum::PI_BOTTOM];
00069     p->normal.Set(m[3]+m[1], m[7]+m[5], m[11]+m[9]);
00070     p->d = m[15]+m[13];
00071 
00072     p = &mPlanes[Frustum::PI_TOP];
00073     p->normal.Set(m[3]-m[1], m[7]-m[5], m[11]-m[9]);
00074     p->d = m[15]-m[13];
00075 
00076     p = &mPlanes[Frustum::PI_NEAR];
00077     p->normal.Set(m[3]-m[2], m[7]-m[6], m[11]-m[10]);
00078     p->d = m[15]-m[14];
00079 
00080     p = &mPlanes[Frustum::PI_FAR];
00081     p->normal.Set(m[3]+m[2], m[7]+m[6], m[11]+m[10]);
00082     p->d = m[15]+m[14];
00083 
00084     // Normalize all plane normals
00085     for(int i=0;i<6;++i)
00086     {
00087         mPlanes[i].Normalize();
00088     }
00089 }
00090 
00091 Frustum::eFrustumSide Frustum::Intersects(const AABB3& bb) const
00092 {
00093     bool split = false;
00094 
00095     for(int i=0; i<6; ++i)
00096     {
00097         EPlaneSide result = mPlanes[i].ClassifyBox(bb);
00098         if(result == PLANESIDE_SPLIT) split = true;
00099 
00100         if(result == PLANESIDE_BACK) return FS_OUTSIDE;
00101     }
00102 
00103     if(split)
00104         return FS_SPLIT;
00105     else
00106         return FS_INSIDE;
00107 }
00108 

Generated on Thu Apr 6 15:25:38 2006 for rcssserver3d by  doxygen 1.4.4