DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

TraceModel.h (7020B)


      1 /*
      2 ===========================================================================
      3 
      4 Doom 3 BFG Edition GPL Source Code
      5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 
      6 
      7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").  
      8 
      9 Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
     10 it under the terms of the GNU General Public License as published by
     11 the Free Software Foundation, either version 3 of the License, or
     12 (at your option) any later version.
     13 
     14 Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
     15 but WITHOUT ANY WARRANTY; without even the implied warranty of
     16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17 GNU General Public License for more details.
     18 
     19 You should have received a copy of the GNU General Public License
     20 along with Doom 3 BFG Edition Source Code.  If not, see <http://www.gnu.org/licenses/>.
     21 
     22 In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code.  If not, please request a copy in writing from id Software at the address below.
     23 
     24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
     25 
     26 ===========================================================================
     27 */
     28 
     29 #ifndef __TRACEMODEL_H__
     30 #define __TRACEMODEL_H__
     31 
     32 /*
     33 ===============================================================================
     34 
     35 	A trace model is an arbitrary polygonal model which is used by the
     36 	collision detection system to find collisions, contacts or the contents
     37 	of a volume. For collision detection speed reasons the number of vertices
     38 	and edges are limited. The trace model can have any shape. However convex
     39 	models are usually preferred.
     40 
     41 ===============================================================================
     42 */
     43 
     44 class idVec3;
     45 class idMat3;
     46 class idBounds;
     47 
     48 // trace model type
     49 typedef enum {
     50 	TRM_INVALID,		// invalid trm
     51 	TRM_BOX,			// box
     52 	TRM_OCTAHEDRON,		// octahedron
     53 	TRM_DODECAHEDRON,	// dodecahedron
     54 	TRM_CYLINDER,		// cylinder approximation
     55 	TRM_CONE,			// cone approximation
     56 	TRM_BONE,			// two tetrahedrons attached to each other
     57 	TRM_POLYGON,		// arbitrary convex polygon
     58 	TRM_POLYGONVOLUME,	// volume for arbitrary convex polygon
     59 	TRM_CUSTOM			// loaded from map model or ASE/LWO
     60 } traceModel_t;
     61 
     62 // these are bit cache limits
     63 #define MAX_TRACEMODEL_VERTS		32
     64 #define MAX_TRACEMODEL_EDGES		32
     65 #define MAX_TRACEMODEL_POLYS		16
     66 #define MAX_TRACEMODEL_POLYEDGES	16
     67 
     68 typedef idVec3 traceModelVert_t;
     69 
     70 typedef struct {
     71 	int					v[2];
     72 	idVec3				normal;
     73 } traceModelEdge_t;
     74 
     75 typedef struct {
     76 	idVec3				normal;
     77 	float				dist;
     78 	idBounds			bounds;
     79 	int					numEdges;
     80 	int					edges[MAX_TRACEMODEL_POLYEDGES];
     81 } traceModelPoly_t;
     82 
     83 class idTraceModel {
     84 
     85 public:
     86 	traceModel_t		type;
     87 	int					numVerts;
     88 	traceModelVert_t	verts[MAX_TRACEMODEL_VERTS];
     89 	int					numEdges;
     90 	traceModelEdge_t	edges[MAX_TRACEMODEL_EDGES+1];
     91 	int					numPolys;
     92 	traceModelPoly_t	polys[MAX_TRACEMODEL_POLYS];
     93 	idVec3				offset;			// offset to center of model
     94 	idBounds			bounds;			// bounds of model
     95 	bool				isConvex;		// true when model is convex
     96 
     97 public:
     98 						idTraceModel();
     99 						// axial bounding box
    100 						idTraceModel( const idBounds &boxBounds );
    101 						// cylinder approximation
    102 						idTraceModel( const idBounds &cylBounds, const int numSides );
    103 						// bone
    104 						idTraceModel( const float length, const float width );
    105 
    106 						// axial box
    107 	void				SetupBox( const idBounds &boxBounds );
    108 	void				SetupBox( const float size );
    109 						// octahedron
    110 	void				SetupOctahedron( const idBounds &octBounds );
    111 	void				SetupOctahedron( const float size );
    112 						// dodecahedron
    113 	void				SetupDodecahedron( const idBounds &dodBounds );
    114 	void				SetupDodecahedron( const float size );
    115 						// cylinder approximation
    116 	void				SetupCylinder( const idBounds &cylBounds, const int numSides );
    117 	void				SetupCylinder( const float height, const float width, const int numSides );
    118 						// cone approximation
    119 	void				SetupCone( const idBounds &coneBounds, const int numSides );
    120 	void				SetupCone( const float height, const float width, const int numSides );
    121 						// two tetrahedrons attached to each other
    122 	void				SetupBone( const float length, const float width );
    123 						// arbitrary convex polygon
    124 	void				SetupPolygon( const idVec3 *v, const int count );
    125 	void				SetupPolygon( const idWinding &w );
    126 						// generate edge normals
    127 	int					GenerateEdgeNormals();
    128 						// translate the trm
    129 	void				Translate( const idVec3 &translation );
    130 						// rotate the trm
    131 	void				Rotate( const idMat3 &rotation );
    132 						// shrink the model m units on all sides
    133 	void				Shrink( const float m );
    134 						// compare
    135 	bool				Compare( const idTraceModel &trm ) const;
    136 	bool				operator==(	const idTraceModel &trm ) const;
    137 	bool				operator!=(	const idTraceModel &trm ) const;
    138 						// get the area of one of the polygons
    139 	float				GetPolygonArea( int polyNum ) const;
    140 						// get the silhouette edges
    141 	int					GetProjectionSilhouetteEdges( const idVec3 &projectionOrigin, int silEdges[MAX_TRACEMODEL_EDGES] ) const;
    142 	int					GetParallelProjectionSilhouetteEdges( const idVec3 &projectionDir, int silEdges[MAX_TRACEMODEL_EDGES] ) const;
    143 						// calculate mass properties assuming an uniform density
    144 	void				GetMassProperties( const float density, float &mass, idVec3 &centerOfMass, idMat3 &inertiaTensor ) const;
    145 
    146 private:
    147 	void				InitBox();
    148 	void				InitOctahedron();
    149 	void				InitDodecahedron();
    150 	void				InitBone();
    151 
    152 	void				ProjectionIntegrals( int polyNum, int a, int b, struct projectionIntegrals_s &integrals ) const;
    153 	void				PolygonIntegrals( int polyNum, int a, int b, int c, struct polygonIntegrals_s &integrals ) const;
    154 	void				VolumeIntegrals( struct volumeIntegrals_s &integrals ) const;
    155 	void				VolumeFromPolygon( idTraceModel &trm, float thickness ) const;
    156 	int					GetOrderedSilhouetteEdges( const int edgeIsSilEdge[MAX_TRACEMODEL_EDGES+1], int silEdges[MAX_TRACEMODEL_EDGES] ) const;
    157 };
    158 
    159 
    160 ID_INLINE idTraceModel::idTraceModel() {
    161 	type = TRM_INVALID;
    162 	numVerts = numEdges = numPolys = 0;
    163 	bounds.Zero();
    164 }
    165 
    166 ID_INLINE idTraceModel::idTraceModel( const idBounds &boxBounds ) {
    167 	InitBox();
    168 	SetupBox( boxBounds );
    169 }
    170 
    171 ID_INLINE idTraceModel::idTraceModel( const idBounds &cylBounds, const int numSides ) {
    172 	SetupCylinder( cylBounds, numSides );
    173 }
    174 
    175 ID_INLINE idTraceModel::idTraceModel( const float length, const float width ) {
    176 	InitBone();
    177 	SetupBone( length, width );
    178 }
    179 
    180 ID_INLINE bool idTraceModel::operator==( const idTraceModel &trm ) const {
    181 	return Compare( trm );
    182 }
    183 
    184 ID_INLINE bool idTraceModel::operator!=( const idTraceModel &trm ) const {
    185 	return !Compare( trm );
    186 }
    187 
    188 #endif /* !__TRACEMODEL_H__ */
    189