doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TraceModel.h
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
9 Doom 3 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 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 Source Code. If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the Doom 3 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 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 
69 
70 typedef struct {
71  int v[2];
74 
75 typedef struct {
77  float dist;
79  int numEdges;
82 
83 class idTraceModel {
84 
85 public:
87  int numVerts;
89  int numEdges;
91  int numPolys;
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( void );
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( void );
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( void );
148  void InitOctahedron( void );
149  void InitDodecahedron( void );
150  void InitBone( void );
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( void ) {
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 
traceModel_t type
Definition: TraceModel.h:86
void SetupPolygon(const idVec3 *v, const int count)
Definition: TraceModel.cpp:866
void Rotate(const idMat3 &rotation)
void Zero(void)
Definition: Bounds.h:206
bool operator!=(const idTraceModel &trm) const
Definition: TraceModel.h:184
int GetProjectionSilhouetteEdges(const idVec3 &projectionOrigin, int silEdges[MAX_TRACEMODEL_EDGES]) const
void SetupBox(const idBounds &boxBounds)
Definition: TraceModel.cpp:40
traceModelVert_t verts[MAX_TRACEMODEL_VERTS]
Definition: TraceModel.h:88
const GLdouble * v
Definition: glext.h:2936
bool operator==(const idTraceModel &trm) const
Definition: TraceModel.h:180
void SetupCylinder(const idBounds &cylBounds, const int numSides)
Definition: TraceModel.cpp:546
Definition: Vector.h:316
GLuint GLuint GLsizei GLenum type
Definition: glext.h:2845
traceModelEdge_t edges[MAX_TRACEMODEL_EDGES+1]
Definition: TraceModel.h:90
void GetMassProperties(const float density, float &mass, idVec3 &centerOfMass, idMat3 &inertiaTensor) const
idVec3 traceModelVert_t
Definition: TraceModel.h:68
int GetOrderedSilhouetteEdges(const int edgeIsSilEdge[MAX_TRACEMODEL_EDGES+1], int silEdges[MAX_TRACEMODEL_EDGES]) const
bool Compare(const idTraceModel &trm) const
void InitOctahedron(void)
Definition: TraceModel.cpp:239
idTraceModel(void)
Definition: TraceModel.h:160
traceModel_t
Definition: TraceModel.h:49
#define MAX_TRACEMODEL_POLYS
Definition: TraceModel.h:65
idVec3 offset
Definition: TraceModel.h:93
void Translate(const idVec3 &translation)
GLuint GLuint GLsizei count
Definition: glext.h:2845
void VolumeFromPolygon(idTraceModel &trm, float thickness) const
Definition: TraceModel.cpp:935
void SetupBone(const float length, const float width)
Definition: TraceModel.cpp:761
const GLubyte * c
Definition: glext.h:4677
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:3454
void SetupOctahedron(const idBounds &octBounds)
Definition: TraceModel.cpp:171
void SetupCone(const idBounds &coneBounds, const int numSides)
Definition: TraceModel.cpp:659
void InitBone(void)
Definition: TraceModel.cpp:808
GLenum GLsizei width
Definition: glext.h:2846
GLubyte GLubyte GLubyte a
Definition: glext.h:4662
idBounds bounds
Definition: TraceModel.h:78
bool isConvex
Definition: TraceModel.h:95
GLenum GLsizei GLsizei height
Definition: glext.h:2856
void ProjectionIntegrals(int polyNum, int a, int b, struct projectionIntegrals_s &integrals) const
GLubyte GLubyte b
Definition: glext.h:4662
int GetParallelProjectionSilhouetteEdges(const idVec3 &projectionDir, int silEdges[MAX_TRACEMODEL_EDGES]) const
void Shrink(const float m)
#define MAX_TRACEMODEL_EDGES
Definition: TraceModel.h:64
Definition: Matrix.h:333
GLsizeiptr size
Definition: glext.h:3112
GLsizei const GLcharARB const GLint * length
Definition: glext.h:3599
float GetPolygonArea(int polyNum) const
void PolygonIntegrals(int polyNum, int a, int b, int c, struct polygonIntegrals_s &integrals) const
void VolumeIntegrals(struct volumeIntegrals_s &integrals) const
traceModelPoly_t polys[MAX_TRACEMODEL_POLYS]
Definition: TraceModel.h:92
int GenerateEdgeNormals(void)
Definition: TraceModel.cpp:971
#define MAX_TRACEMODEL_VERTS
Definition: TraceModel.h:63
void SetupDodecahedron(const idBounds &dodBounds)
Definition: TraceModel.cpp:310
#define MAX_TRACEMODEL_POLYEDGES
Definition: TraceModel.h:66
void InitDodecahedron(void)
Definition: TraceModel.cpp:413
idBounds bounds
Definition: TraceModel.h:94
void InitBox(void)
Definition: TraceModel.cpp:99