00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00036
00037 static geEngine *CEngine;
00038 static geWorld *CWorld;
00039 static GBSP_BSPData *BSPData;
00040
00041
00042
00043
00044
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
00057
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
00070
00071 geBoolean GENESISCC Plane_SetGBSP(World_BSP *BSP)
00072 {
00073 assert(BSP != NULL);
00074
00075
00076 BSPData = &BSP->BSPData;
00077
00078 return GE_TRUE;
00079 }
00080
00081
00082
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
00107 Leaf = -(Node+1);
00108
00109 assert(Leaf >=0 && Leaf < World->CurrentBSP->BSPData.NumGFXLeafs);
00110
00111 return Leaf;
00112 }
00113
00114
00115
00116
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
00149
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
00165
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
00206
00207 void gePlane_SetFromVerts(GFX_Plane *Plane, const geVec3d *V1, const geVec3d *V2, const geVec3d *V3)
00208 {
00209 geVec3d Vect1, Vect2;
00210
00211
00212 geVec3d_Subtract(V1, V2, &Vect1);
00213 geVec3d_Subtract(V3, V2, &Vect2);
00214
00215
00216 geVec3d_CrossProduct(&Vect1, &Vect2, &Plane->Normal);
00217 geVec3d_Normalize(&Plane->Normal);
00218
00219
00220
00221 Plane->Dist = geVec3d_DotProduct(V1, &Plane->Normal);
00222
00223
00224 Plane->Type = PLANE_ANY;
00225 }
00226