doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Clip.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 __CLIP_H__
30 #define __CLIP_H__
31 
32 /*
33 ===============================================================================
34 
35  Handles collision detection with the world and between physics objects.
36 
37 ===============================================================================
38 */
39 
40 #define CLIPMODEL_ID_TO_JOINT_HANDLE( id ) ( ( id ) >= 0 ? INVALID_JOINT : ((jointHandle_t) ( -1 - id )) )
41 #define JOINT_HANDLE_TO_CLIPMODEL_ID( id ) ( -1 - id )
42 
43 class idClip;
44 class idClipModel;
45 class idEntity;
46 
47 
48 //===============================================================
49 //
50 // idClipModel
51 //
52 //===============================================================
53 
54 class idClipModel {
55 
56  friend class idClip;
57 
58 public:
59  idClipModel( void );
60  explicit idClipModel( const char *name );
61  explicit idClipModel( const idTraceModel &trm );
62  explicit idClipModel( const int renderModelHandle );
63  explicit idClipModel( const idClipModel *model );
64  ~idClipModel( void );
65 
66  bool LoadModel( const char *name );
67  void LoadModel( const idTraceModel &trm );
68  void LoadModel( const int renderModelHandle );
69 
70  void Save( idSaveGame *savefile ) const;
71  void Restore( idRestoreGame *savefile );
72 
73  void Link( idClip &clp ); // must have been linked with an entity and id before
74  void Link( idClip &clp, idEntity *ent, int newId, const idVec3 &newOrigin, const idMat3 &newAxis, int renderModelHandle = -1 );
75  void Unlink( void ); // unlink from sectors
76  void SetPosition( const idVec3 &newOrigin, const idMat3 &newAxis ); // unlinks the clip model
77  void Translate( const idVec3 &translation ); // unlinks the clip model
78  void Rotate( const idRotation &rotation ); // unlinks the clip model
79  void Enable( void ); // enable for clipping
80  void Disable( void ); // keep linked but disable for clipping
81  void SetMaterial( const idMaterial *m );
82  const idMaterial * GetMaterial( void ) const;
83  void SetContents( int newContents ); // override contents
84  int GetContents( void ) const;
85  void SetEntity( idEntity *newEntity );
86  idEntity * GetEntity( void ) const;
87  void SetId( int newId );
88  int GetId( void ) const;
89  void SetOwner( idEntity *newOwner );
90  idEntity * GetOwner( void ) const;
91  const idBounds & GetBounds( void ) const;
92  const idBounds & GetAbsBounds( void ) const;
93  const idVec3 & GetOrigin( void ) const;
94  const idMat3 & GetAxis( void ) const;
95  bool IsTraceModel( void ) const; // returns true if this is a trace model
96  bool IsRenderModel( void ) const; // returns true if this is a render model
97  bool IsLinked( void ) const; // returns true if the clip model is linked
98  bool IsEnabled( void ) const; // returns true if enabled for collision detection
99  bool IsEqual( const idTraceModel &trm ) const;
100  cmHandle_t Handle( void ) const; // returns handle used to collide vs this model
101  const idTraceModel * GetTraceModel( void ) const;
102  void GetMassProperties( const float density, float &mass, idVec3 &centerOfMass, idMat3 &inertiaTensor ) const;
103 
104  static cmHandle_t CheckModel( const char *name );
105  static void ClearTraceModelCache( void );
106  static int TraceModelCacheSize( void );
107 
108  static void SaveTraceModels( idSaveGame *savefile );
109  static void RestoreTraceModels( idRestoreGame *savefile );
110 
111 private:
112  bool enabled; // true if this clip model is used for clipping
113  idEntity * entity; // entity using this clip model
114  int id; // id for entities that use multiple clip models
115  idEntity * owner; // owner of the entity that owns this clip model
116  idVec3 origin; // origin of clip model
117  idMat3 axis; // orientation of clip model
118  idBounds bounds; // bounds
119  idBounds absBounds; // absolute bounds
120  const idMaterial * material; // material for trace models
121  int contents; // all contents ored together
122  cmHandle_t collisionModelHandle; // handle to collision model
123  int traceModelIndex; // trace model used for collision detection
124  int renderModelHandle; // render model def handle
125 
126  struct clipLink_s * clipLinks; // links into sectors
128 
129  void Init( void ); // initialize
130  void Link_r( struct clipSector_s *node );
131 
132  static int AllocTraceModel( const idTraceModel &trm );
133  static void FreeTraceModel( int traceModelIndex );
135  static int GetTraceModelHashKey( const idTraceModel &trm );
136 };
137 
138 
139 ID_INLINE void idClipModel::Translate( const idVec3 &translation ) {
140  Unlink();
141  origin += translation;
142 }
143 
144 ID_INLINE void idClipModel::Rotate( const idRotation &rotation ) {
145  Unlink();
146  origin *= rotation;
147  axis *= rotation.ToMat3();
148 }
149 
150 ID_INLINE void idClipModel::Enable( void ) {
151  enabled = true;
152 }
153 
154 ID_INLINE void idClipModel::Disable( void ) {
155  enabled = false;
156 }
157 
158 ID_INLINE void idClipModel::SetMaterial( const idMaterial *m ) {
159  material = m;
160 }
161 
162 ID_INLINE const idMaterial * idClipModel::GetMaterial( void ) const {
163  return material;
164 }
165 
166 ID_INLINE void idClipModel::SetContents( int newContents ) {
167  contents = newContents;
168 }
169 
170 ID_INLINE int idClipModel::GetContents( void ) const {
171  return contents;
172 }
173 
174 ID_INLINE void idClipModel::SetEntity( idEntity *newEntity ) {
175  entity = newEntity;
176 }
177 
178 ID_INLINE idEntity *idClipModel::GetEntity( void ) const {
179  return entity;
180 }
181 
182 ID_INLINE void idClipModel::SetId( int newId ) {
183  id = newId;
184 }
185 
186 ID_INLINE int idClipModel::GetId( void ) const {
187  return id;
188 }
189 
190 ID_INLINE void idClipModel::SetOwner( idEntity *newOwner ) {
191  owner = newOwner;
192 }
193 
194 ID_INLINE idEntity *idClipModel::GetOwner( void ) const {
195  return owner;
196 }
197 
198 ID_INLINE const idBounds &idClipModel::GetBounds( void ) const {
199  return bounds;
200 }
201 
202 ID_INLINE const idBounds &idClipModel::GetAbsBounds( void ) const {
203  return absBounds;
204 }
205 
206 ID_INLINE const idVec3 &idClipModel::GetOrigin( void ) const {
207  return origin;
208 }
209 
210 ID_INLINE const idMat3 &idClipModel::GetAxis( void ) const {
211  return axis;
212 }
213 
214 ID_INLINE bool idClipModel::IsRenderModel( void ) const {
215  return ( renderModelHandle != -1 );
216 }
217 
218 ID_INLINE bool idClipModel::IsTraceModel( void ) const {
219  return ( traceModelIndex != -1 );
220 }
221 
222 ID_INLINE bool idClipModel::IsLinked( void ) const {
223  return ( clipLinks != NULL );
224 }
225 
226 ID_INLINE bool idClipModel::IsEnabled( void ) const {
227  return enabled;
228 }
229 
230 ID_INLINE bool idClipModel::IsEqual( const idTraceModel &trm ) const {
231  return ( traceModelIndex != -1 && *GetCachedTraceModel( traceModelIndex ) == trm );
232 }
233 
234 ID_INLINE const idTraceModel *idClipModel::GetTraceModel( void ) const {
235  if ( !IsTraceModel() ) {
236  return NULL;
237  }
239 }
240 
241 
242 //===============================================================
243 //
244 // idClip
245 //
246 //===============================================================
247 
248 class idClip {
249 
250  friend class idClipModel;
251 
252 public:
253  idClip( void );
254 
255  void Init( void );
256  void Shutdown( void );
257 
258  // clip versus the rest of the world
259  bool Translation( trace_t &results, const idVec3 &start, const idVec3 &end,
260  const idClipModel *mdl, const idMat3 &trmAxis, int contentMask, const idEntity *passEntity );
261  bool Rotation( trace_t &results, const idVec3 &start, const idRotation &rotation,
262  const idClipModel *mdl, const idMat3 &trmAxis, int contentMask, const idEntity *passEntity );
263  bool Motion( trace_t &results, const idVec3 &start, const idVec3 &end, const idRotation &rotation,
264  const idClipModel *mdl, const idMat3 &trmAxis, int contentMask, const idEntity *passEntity );
265  int Contacts( contactInfo_t *contacts, const int maxContacts, const idVec3 &start, const idVec6 &dir, const float depth,
266  const idClipModel *mdl, const idMat3 &trmAxis, int contentMask, const idEntity *passEntity );
267  int Contents( const idVec3 &start,
268  const idClipModel *mdl, const idMat3 &trmAxis, int contentMask, const idEntity *passEntity );
269 
270  // special case translations versus the rest of the world
271  bool TracePoint( trace_t &results, const idVec3 &start, const idVec3 &end,
272  int contentMask, const idEntity *passEntity );
273  bool TraceBounds( trace_t &results, const idVec3 &start, const idVec3 &end, const idBounds &bounds,
274  int contentMask, const idEntity *passEntity );
275 
276  // clip versus a specific model
277  void TranslationModel( trace_t &results, const idVec3 &start, const idVec3 &end,
278  const idClipModel *mdl, const idMat3 &trmAxis, int contentMask,
279  cmHandle_t model, const idVec3 &modelOrigin, const idMat3 &modelAxis );
280  void RotationModel( trace_t &results, const idVec3 &start, const idRotation &rotation,
281  const idClipModel *mdl, const idMat3 &trmAxis, int contentMask,
282  cmHandle_t model, const idVec3 &modelOrigin, const idMat3 &modelAxis );
283  int ContactsModel( contactInfo_t *contacts, const int maxContacts, const idVec3 &start, const idVec6 &dir, const float depth,
284  const idClipModel *mdl, const idMat3 &trmAxis, int contentMask,
285  cmHandle_t model, const idVec3 &modelOrigin, const idMat3 &modelAxis );
286  int ContentsModel( const idVec3 &start,
287  const idClipModel *mdl, const idMat3 &trmAxis, int contentMask,
288  cmHandle_t model, const idVec3 &modelOrigin, const idMat3 &modelAxis );
289 
290  // clip versus all entities but not the world
291  void TranslationEntities( trace_t &results, const idVec3 &start, const idVec3 &end,
292  const idClipModel *mdl, const idMat3 &trmAxis, int contentMask, const idEntity *passEntity );
293 
294  // get a contact feature
295  bool GetModelContactFeature( const contactInfo_t &contact, const idClipModel *clipModel, idFixedWinding &winding ) const;
296 
297  // get entities/clip models within or touching the given bounds
298  int EntitiesTouchingBounds( const idBounds &bounds, int contentMask, idEntity **entityList, int maxCount ) const;
299  int ClipModelsTouchingBounds( const idBounds &bounds, int contentMask, idClipModel **clipModelList, int maxCount ) const;
300 
301  const idBounds & GetWorldBounds( void ) const;
302  idClipModel * DefaultClipModel( void );
303 
304  // stats and debug drawing
305  void PrintStatistics( void );
306  void DrawClipModels( const idVec3 &eye, const float radius, const idEntity *passEntity );
307  bool DrawModelContactFeature( const contactInfo_t &contact, const idClipModel *clipModel, int lifetime ) const;
308 
309 private:
315  mutable int touchCount;
316  // statistics
323 
324 private:
325  struct clipSector_s * CreateClipSectors_r( const int depth, const idBounds &bounds, idVec3 &maxSector );
326  void ClipModelsTouchingBounds_r( const struct clipSector_s *node, struct listParms_s &parms ) const;
327  const idTraceModel * TraceModelForClipModel( const idClipModel *mdl ) const;
328  int GetTraceClipModels( const idBounds &bounds, int contentMask, const idEntity *passEntity, idClipModel **clipModelList ) const;
329  void TraceRenderModel( trace_t &trace, const idVec3 &start, const idVec3 &end, const float radius, const idMat3 &axis, idClipModel *touch ) const;
330 };
331 
332 
333 ID_INLINE bool idClip::TracePoint( trace_t &results, const idVec3 &start, const idVec3 &end, int contentMask, const idEntity *passEntity ) {
334  Translation( results, start, end, NULL, mat3_identity, contentMask, passEntity );
335  return ( results.fraction < 1.0f );
336 }
337 
338 ID_INLINE bool idClip::TraceBounds( trace_t &results, const idVec3 &start, const idVec3 &end, const idBounds &bounds, int contentMask, const idEntity *passEntity ) {
340  Translation( results, start, end, &temporaryClipModel, mat3_identity, contentMask, passEntity );
341  return ( results.fraction < 1.0f );
342 }
343 
344 ID_INLINE const idBounds & idClip::GetWorldBounds( void ) const {
345  return worldBounds;
346 }
347 
349  return &defaultClipModel;
350 }
351 
352 #endif /* !__CLIP_H__ */
int ClipModelsTouchingBounds(const idBounds &bounds, int contentMask, idClipModel **clipModelList, int maxCount) const
Definition: Clip.cpp:804
idEntity * GetEntity(void) const
Definition: Clip.h:178
bool TracePoint(trace_t &results, const idVec3 &start, const idVec3 &end, int contentMask, const idEntity *passEntity)
Definition: Clip.h:333
int GetContents(void) const
Definition: Clip.h:170
const idMaterial * GetMaterial(void) const
Definition: Clip.h:162
void SetId(int newId)
Definition: Clip.h:182
void DrawClipModels(const idVec3 &eye, const float radius, const idEntity *passEntity)
Definition: Clip.cpp:1617
static void ClearTraceModelCache(void)
Definition: Clip.cpp:81
void Link(idClip &clp)
Definition: Clip.cpp:545
idEntity * entity
Definition: Clip.h:113
idMat3 mat3_identity(idVec3(1, 0, 0), idVec3(0, 1, 0), idVec3(0, 0, 1))
void SetContents(int newContents)
Definition: Clip.h:166
bool IsEnabled(void) const
Definition: Clip.h:226
idBounds bounds
Definition: Clip.h:118
int Contacts(contactInfo_t *contacts, const int maxContacts, const idVec3 &start, const idVec6 &dir, const float depth, const idClipModel *mdl, const idMat3 &trmAxis, int contentMask, const idEntity *passEntity)
Definition: Clip.cpp:1358
void Translate(const idVec3 &translation)
Definition: Clip.h:139
bool GetModelContactFeature(const contactInfo_t &contact, const idClipModel *clipModel, idFixedWinding &winding) const
Definition: Clip.cpp:1545
static int TraceModelCacheSize(void)
Definition: Clip.cpp:91
const idMat3 & GetAxis(void) const
Definition: Clip.h:210
int numMotions
Definition: Clip.h:319
static void FreeTraceModel(int traceModelIndex)
Definition: Clip.cpp:127
idMat3 axis
Definition: Clip.h:117
Definition: Clip.h:248
Definition: Vector.h:316
bool enabled
Definition: Clip.h:112
GLint GLint GLsizei GLsizei GLsizei depth
Definition: glext.h:2878
idBounds absBounds
Definition: Clip.h:119
const idVec3 & GetOrigin(void) const
Definition: Clip.h:206
int numTranslations
Definition: Clip.h:317
int cmHandle_t
bool IsLinked(void) const
Definition: Clip.h:222
const idTraceModel * TraceModelForClipModel(const idClipModel *mdl) const
Definition: Clip.cpp:944
int EntitiesTouchingBounds(const idBounds &bounds, int contentMask, idEntity **entityList, int maxCount) const
Definition: Clip.cpp:833
float fraction
const idBounds & GetWorldBounds(void) const
Definition: Clip.h:344
int GetTraceClipModels(const idBounds &bounds, int contentMask, const idEntity *passEntity, idClipModel **clipModelList) const
Definition: Clip.cpp:870
void Init(void)
Definition: Clip.cpp:272
int numContacts
Definition: Clip.h:322
idClipModel temporaryClipModel
Definition: Clip.h:313
int numContents
Definition: Clip.h:321
int numRenderModelTraces
Definition: Clip.h:320
idClipModel defaultClipModel
Definition: Clip.h:314
void SetOwner(idEntity *newOwner)
Definition: Clip.h:190
void Shutdown(void)
Definition: Clip.cpp:715
void GetMassProperties(const float density, float &mass, idVec3 &centerOfMass, idMat3 &inertiaTensor) const
Definition: Clip.cpp:475
int axis
Definition: Clip.cpp:38
idBounds worldBounds
Definition: Clip.h:312
static void RestoreTraceModels(idRestoreGame *savefile)
Definition: Clip.cpp:178
int touchCount
Definition: Clip.h:127
struct clipSector_s * clipSectors
Definition: Clip.h:311
GLuint GLuint end
Definition: glext.h:2845
cmHandle_t Handle(void) const
Definition: Clip.cpp:457
#define NULL
Definition: Lib.h:88
idClipModel * DefaultClipModel(void)
Definition: Clip.h:348
const idBounds & GetBounds(void) const
Definition: Clip.h:198
idEntity * GetOwner(void) const
Definition: Clip.h:194
static int AllocTraceModel(const idTraceModel &trm)
Definition: Clip.cpp:100
int ContactsModel(contactInfo_t *contacts, const int maxContacts, const idVec3 &start, const idVec6 &dir, const float depth, const idClipModel *mdl, const idMat3 &trmAxis, int contentMask, cmHandle_t model, const idVec3 &modelOrigin, const idMat3 &modelAxis)
Definition: Clip.cpp:1519
static void SaveTraceModels(idSaveGame *savefile)
Definition: Clip.cpp:159
void PrintStatistics(void)
Definition: Clip.cpp:1606
bool IsTraceModel(void) const
Definition: Clip.h:218
void SetMaterial(const idMaterial *m)
Definition: Clip.h:158
void Save(idSaveGame *savefile) const
Definition: Clip.cpp:374
int numRotations
Definition: Clip.h:318
int touchCount
Definition: Clip.h:315
int renderModelHandle
Definition: Clip.h:124
void SetEntity(idEntity *newEntity)
Definition: Clip.h:174
int GetId(void) const
Definition: Clip.h:186
bool IsEqual(const idTraceModel &trm) const
Definition: Clip.h:230
void TraceRenderModel(trace_t &trace, const idVec3 &start, const idVec3 &end, const float radius, const idMat3 &axis, idClipModel *touch) const
Definition: Clip.cpp:913
struct clipLink_s * clipLinks
Definition: Clip.h:126
void Link_r(struct clipSector_s *node)
Definition: Clip.cpp:513
idVec3 origin
Definition: Clip.h:116
~idClipModel(void)
Definition: Clip.cpp:361
int contents
Definition: Clip.h:121
int id
Definition: Clip.h:114
struct clipSector_s * CreateClipSectors_r(const int depth, const idBounds &bounds, idVec3 &maxSector)
Definition: Clip.cpp:636
void Init(void)
Definition: Clip.cpp:684
void ClipModelsTouchingBounds_r(const struct clipSector_s *node, struct listParms_s &parms) const
Definition: Clip.cpp:747
bool TraceBounds(trace_t &results, const idVec3 &start, const idVec3 &end, const idBounds &bounds, int contentMask, const idEntity *passEntity)
Definition: Clip.h:338
Definition: Matrix.h:333
bool DrawModelContactFeature(const contactInfo_t &contact, const idClipModel *clipModel, int lifetime) const
Definition: Clip.cpp:1645
void Enable(void)
Definition: Clip.h:150
bool LoadModel(const char *name)
Definition: Clip.cpp:215
const idMat3 & ToMat3(void) const
Definition: Rotation.cpp:60
const GLcharARB * name
Definition: glext.h:3629
const idMaterial * material
Definition: Clip.h:120
bool Rotation(trace_t &results, const idVec3 &start, const idRotation &rotation, const idClipModel *mdl, const idMat3 &trmAxis, int contentMask, const idEntity *passEntity)
Definition: Clip.cpp:1130
GLsizei maxCount
Definition: glext.h:3628
int Contents(const idVec3 &start, const idClipModel *mdl, const idMat3 &trmAxis, int contentMask, const idEntity *passEntity)
Definition: Clip.cpp:1429
void RotationModel(trace_t &results, const idVec3 &start, const idRotation &rotation, const idClipModel *mdl, const idMat3 &trmAxis, int contentMask, cmHandle_t model, const idVec3 &modelOrigin, const idMat3 &modelAxis)
Definition: Clip.cpp:1506
int ContentsModel(const idVec3 &start, const idClipModel *mdl, const idMat3 &trmAxis, int contentMask, cmHandle_t model, const idVec3 &modelOrigin, const idMat3 &modelAxis)
Definition: Clip.cpp:1532
idEntity * owner
Definition: Clip.h:115
idClipModel(void)
Definition: Clip.cpp:295
cmHandle_t collisionModelHandle
Definition: Clip.h:122
idClip(void)
Definition: Clip.cpp:622
void TranslationModel(trace_t &results, const idVec3 &start, const idVec3 &end, const idClipModel *mdl, const idMat3 &trmAxis, int contentMask, cmHandle_t model, const idVec3 &modelOrigin, const idMat3 &modelAxis)
Definition: Clip.cpp:1493
int numClipSectors
Definition: Clip.h:310
void TranslationEntities(trace_t &results, const idVec3 &start, const idVec3 &end, const idClipModel *mdl, const idMat3 &trmAxis, int contentMask, const idEntity *passEntity)
Definition: Clip.cpp:995
const idBounds & GetAbsBounds(void) const
Definition: Clip.h:202
static cmHandle_t CheckModel(const char *name)
Definition: Clip.cpp:604
void Disable(void)
Definition: Clip.h:154
const idTraceModel * GetTraceModel(void) const
Definition: Clip.h:234
void Rotate(const idRotation &rotation)
Definition: Clip.h:144
bool Translation(trace_t &results, const idVec3 &start, const idVec3 &end, const idClipModel *mdl, const idMat3 &trmAxis, int contentMask, const idEntity *passEntity)
Definition: Clip.cpp:1056
static idTraceModel * GetCachedTraceModel(int traceModelIndex)
Definition: Clip.cpp:140
int traceModelIndex
Definition: Clip.h:123
void Unlink(void)
Definition: Clip.cpp:491
void Restore(idRestoreGame *savefile)
Definition: Clip.cpp:401
void SetPosition(const idVec3 &newOrigin, const idMat3 &newAxis)
Definition: Clip.cpp:444
bool IsRenderModel(void) const
Definition: Clip.h:214
GLuint start
Definition: glext.h:2845
bool Motion(trace_t &results, const idVec3 &start, const idVec3 &end, const idRotation &rotation, const idClipModel *mdl, const idMat3 &trmAxis, int contentMask, const idEntity *passEntity)
Definition: Clip.cpp:1197
static int GetTraceModelHashKey(const idTraceModel &trm)
Definition: Clip.cpp:149