TraceModel.h 6.9 KB

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