Main Page | Alphabetical List | Compound List | File List | Compound Members | File Members

Plane.c

Go to the documentation of this file.
00001 /****************************************************************************************/
00002 /*  Plane.c                                                                             */
00003 /*                                                                                      */
00004 /*  Author: John Pollard                                                                */
00005 /*  Description: Handy functions that deal with GFX_Plane's                             */
00006 /*                                                                                      */
00007 /*  The contents of this file are subject to the Genesis3D Public License               */
00008 /*  Version 1.01 (the "License"); you may not use this file except in                   */
00009 /*  compliance with the License. You may obtain a copy of the License at                */
00010 /*  http://www.genesis3d.com                                                            */
00011 /*                                                                                      */
00012 /*  Software distributed under the License is distributed on an "AS IS"                 */
00013 /*  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See                */
00014 /*  the License for the specific language governing rights and limitations              */
00015 /*  under the License.                                                                  */
00016 /*                                                                                      */
00017 /*  The Original Code is Genesis3D, released March 25, 1999.                            */
00018 /*Genesis3D Version 1.1 released November 15, 1999                            */
00019 /*  Copyright (C) 1999 WildTangent, Inc. All Rights Reserved           */
00020 /*                                                                                      */
00021 /****************************************************************************************/
00022 #include <Assert.h>
00023 #include <Windows.h>
00024 #include <Math.h>
00025 
00026 #include "BaseType.h"
00027 #include "System.h"
00028 #include "World.h"
00029 #include "GBSPFile.h"
00030 #include "Vec3d.h"
00031 #include "XForm3d.h"
00032 #include "Plane.h"
00033 
00034 //=====================================================================================
00035 //      static local globals
00036 //=====================================================================================
00037 static  geEngine                *CEngine;               
00038 static  geWorld                 *CWorld;
00039 static  GBSP_BSPData    *BSPData;               // This is in globals, but is also kept here for speed
00040 
00041 
00042 //=====================================================================================
00043 //      Plane_SetEngine
00044 //      Lets this module know that the engine has changed
00045 //=====================================================================================
00046 geBoolean GENESISCC Plane_SetEngine(geEngine *Engine)
00047 {
00048         assert (Engine != NULL);
00049 
00050         CEngine = Engine;
00051 
00052         return GE_TRUE;
00053 }
00054 
00055 //=====================================================================================
00056 //      Plane_SetWorld
00057 //      Lets this module know that the world has changed
00058 //=====================================================================================
00059 geBoolean GENESISCC Plane_SetWorld(geWorld *World)
00060 {
00061         assert(World != NULL);
00062         
00063         CWorld = World;
00064 
00065         return GE_TRUE;
00066 }
00067 
00068 //=====================================================================================
00069 //      Plane_SetGBSP
00070 //=====================================================================================
00071 geBoolean GENESISCC Plane_SetGBSP(World_BSP *BSP)
00072 {
00073         assert(BSP != NULL);
00074 
00075         // Make quick pointer to the world bsp data
00076         BSPData = &BSP->BSPData;
00077 
00078         return GE_TRUE;
00079 }
00080 
00081 //=====================================================================================
00082 //      Plane_FindLeaf
00083 //=====================================================================================
00084 int32 GENESISCC Plane_FindLeaf(const geWorld *World, int32 Node, const geVec3d *POV)
00085 {
00086     geFloat             Dist;
00087         GFX_Node        *GFXNodes;
00088         GFX_Plane       *GFXPlanes;
00089         int32           Leaf;
00090 
00091         GFXNodes = World->CurrentBSP->BSPData.GFXNodes;
00092         GFXPlanes = World->CurrentBSP->BSPData.GFXPlanes;
00093 
00094     while (Node >= 0) 
00095         {
00096                 assert(Node >= 0 && Node < World->CurrentBSP->BSPData.NumGFXNodes);
00097         
00098                 Dist = Plane_PlaneDistanceFast(&GFXPlanes[GFXNodes[Node].PlaneNum], POV);
00099         
00100                 if (Dist < 0) 
00101             Node = GFXNodes[Node].Children[1];
00102                 else
00103             Node = GFXNodes[Node].Children[0];
00104     }
00105         
00106         // We are now in a leaf
00107         Leaf = -(Node+1);
00108 
00109         assert(Leaf >=0 && Leaf < World->CurrentBSP->BSPData.NumGFXLeafs);
00110         
00111         return Leaf;
00112 }
00113 
00114 //=====================================================================================
00115 //      Plane_PlaneDistanceFast
00116 //      Fast axial aligned plane distance
00117 //=====================================================================================
00118 geFloat GENESISCC Plane_PlaneDistanceFast(const GFX_Plane *Plane, const geVec3d *Point)
00119 {
00120         geFloat Dist, PDist;
00121 
00122         assert(Plane != NULL);
00123         assert(Point != NULL);
00124 
00125         PDist = Plane->Dist;
00126 
00127         switch (Plane->Type)
00128         {
00129                 case PLANE_X:
00130                         Dist = (Point->X - PDist);
00131                         break;
00132                 case PLANE_Y:
00133                         Dist = (Point->Y - PDist);
00134                         break;
00135                 case PLANE_Z:
00136                         Dist = (Point->Z - PDist);
00137                         break;
00138               
00139                 default:
00140                         Dist = geVec3d_DotProduct(Point, &Plane->Normal) - PDist;
00141                         break;
00142         }
00143 
00144         return Dist;
00145 }
00146 
00147 //=====================================================================================
00148 //      Plane_PlaneDistance
00149 //      Normal (slow) plane distance
00150 //=====================================================================================
00151 geFloat GENESISCC Plane_PlaneDistance(const GFX_Plane *Plane, const geVec3d *Point)
00152 {
00153         geFloat Dist;
00154 
00155         assert(Plane != NULL);
00156         assert(Point != NULL);
00157 
00158         Dist = geVec3d_DotProduct(Point, &Plane->Normal) - Plane->Dist;
00159 
00160         return Dist;
00161 }
00162 
00163 //=====================================================================================
00164 //      Plane_PlaneDistanceFast
00165 //      Fast axial aligned face distance
00166 //=====================================================================================
00167 geFloat GENESISCC Plane_FaceDistanceFast(const GFX_Face *Face, const geVec3d *Point)
00168 {
00169         geFloat         Dist, PDist;
00170         GFX_Plane       *Plane;
00171 
00172         assert(Face != NULL);
00173         assert(Point != NULL);
00174 
00175         assert(BSPData != NULL);
00176         assert(BSPData->GFXPlanes != NULL);
00177         
00178         Plane = &BSPData->GFXPlanes[Face->PlaneNum];
00179         PDist = Plane->Dist;
00180 
00181         switch (Plane->Type)
00182         {
00183                 case PLANE_X:
00184                         Dist = (Point->X - PDist);
00185                         break;
00186                 case PLANE_Y:
00187                         Dist = (Point->Y - PDist);
00188                         break;
00189                 case PLANE_Z:
00190                         Dist = (Point->Z - PDist);
00191                         break;
00192               
00193                 default:
00194                         Dist = geVec3d_DotProduct(Point, &Plane->Normal) - PDist;
00195                         break;
00196         }
00197 
00198         if (Face->PlaneSide)
00199                 Dist = -Dist;
00200 
00201         return Dist;
00202 }
00203 
00204 //====================================================================================
00205 //      gePlane_SetFromVerts
00206 //====================================================================================
00207 void gePlane_SetFromVerts(GFX_Plane *Plane, const geVec3d *V1, const geVec3d *V2, const geVec3d *V3)
00208 {
00209         geVec3d         Vect1, Vect2;
00210         
00211         // Get the 2 vectors to derive the normal
00212         geVec3d_Subtract(V1, V2, &Vect1);
00213         geVec3d_Subtract(V3, V2, &Vect2);
00214         
00215         // The normal is the cross between these 2 vectors
00216         geVec3d_CrossProduct(&Vect1, &Vect2, &Plane->Normal);
00217         geVec3d_Normalize(&Plane->Normal);
00218 
00219         // Get the planes distance from the origin, by projecting a vert on the plane
00220         // along the plane normal, to the origin...
00221         Plane->Dist = geVec3d_DotProduct(V1, &Plane->Normal);
00222 
00223         // Finally, get the plane type
00224         Plane->Type = PLANE_ANY;
00225 }
00226 

Generated on Tue Sep 30 12:36:10 2003 for GTestAndEngine by doxygen 1.3.2