CollisionModel_local.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 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 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 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. 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.
  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. /*
  21. ===============================================================================
  22. Trace model vs. polygonal model collision detection.
  23. ===============================================================================
  24. */
  25. #include "CollisionModel.h"
  26. #define MIN_NODE_SIZE 64.0f
  27. #define MAX_NODE_POLYGONS 128
  28. #define CM_MAX_POLYGON_EDGES 64
  29. #define CIRCLE_APPROXIMATION_LENGTH 64.0f
  30. #define MAX_SUBMODELS 2048
  31. #define TRACE_MODEL_HANDLE MAX_SUBMODELS
  32. #define VERTEX_HASH_BOXSIZE (1<<6) // must be power of 2
  33. #define VERTEX_HASH_SIZE (VERTEX_HASH_BOXSIZE*VERTEX_HASH_BOXSIZE)
  34. #define EDGE_HASH_SIZE (1<<14)
  35. #define NODE_BLOCK_SIZE_SMALL 8
  36. #define NODE_BLOCK_SIZE_LARGE 256
  37. #define REFERENCE_BLOCK_SIZE_SMALL 8
  38. #define REFERENCE_BLOCK_SIZE_LARGE 256
  39. #define MAX_WINDING_LIST 128 // quite a few are generated at times
  40. #define INTEGRAL_EPSILON 0.01f
  41. #define VERTEX_EPSILON 0.1f
  42. #define CHOP_EPSILON 0.1f
  43. typedef struct cm_windingList_s {
  44. int numWindings; // number of windings
  45. idFixedWinding w[MAX_WINDING_LIST]; // windings
  46. idVec3 normal; // normal for all windings
  47. idBounds bounds; // bounds of all windings in list
  48. idVec3 origin; // origin for radius
  49. float radius; // radius relative to origin for all windings
  50. int contents; // winding surface contents
  51. int primitiveNum; // number of primitive the windings came from
  52. } cm_windingList_t;
  53. /*
  54. ===============================================================================
  55. Collision model
  56. ===============================================================================
  57. */
  58. typedef struct cm_vertex_s {
  59. idVec3 p; // vertex point
  60. int checkcount; // for multi-check avoidance
  61. unsigned long side; // each bit tells at which side this vertex passes one of the trace model edges
  62. unsigned long sideSet; // each bit tells if sidedness for the trace model edge has been calculated yet
  63. } cm_vertex_t;
  64. typedef struct cm_edge_s {
  65. int checkcount; // for multi-check avoidance
  66. unsigned short internal; // a trace model can never collide with internal edges
  67. unsigned short numUsers; // number of polygons using this edge
  68. unsigned long side; // each bit tells at which side of this edge one of the trace model vertices passes
  69. unsigned long sideSet; // each bit tells if sidedness for the trace model vertex has been calculated yet
  70. int vertexNum[2]; // start and end point of edge
  71. idVec3 normal; // edge normal
  72. } cm_edge_t;
  73. typedef struct cm_polygonBlock_s {
  74. int bytesRemaining;
  75. byte * next;
  76. } cm_polygonBlock_t;
  77. typedef struct cm_polygon_s {
  78. idBounds bounds; // polygon bounds
  79. int checkcount; // for multi-check avoidance
  80. int contents; // contents behind polygon
  81. const idMaterial * material; // material
  82. idPlane plane; // polygon plane
  83. int numEdges; // number of edges
  84. int edges[1]; // variable sized, indexes into cm_edge_t list
  85. } cm_polygon_t;
  86. typedef struct cm_polygonRef_s {
  87. cm_polygon_t * p; // pointer to polygon
  88. struct cm_polygonRef_s *next; // next polygon in chain
  89. } cm_polygonRef_t;
  90. typedef struct cm_polygonRefBlock_s {
  91. cm_polygonRef_t * nextRef; // next polygon reference in block
  92. struct cm_polygonRefBlock_s *next; // next block with polygon references
  93. } cm_polygonRefBlock_t;
  94. typedef struct cm_brushBlock_s {
  95. int bytesRemaining;
  96. byte * next;
  97. } cm_brushBlock_t;
  98. typedef struct cm_brush_s {
  99. int checkcount; // for multi-check avoidance
  100. idBounds bounds; // brush bounds
  101. int contents; // contents of brush
  102. const idMaterial * material; // material
  103. int primitiveNum; // number of brush primitive
  104. int numPlanes; // number of bounding planes
  105. idPlane planes[1]; // variable sized
  106. } cm_brush_t;
  107. typedef struct cm_brushRef_s {
  108. cm_brush_t * b; // pointer to brush
  109. struct cm_brushRef_s * next; // next brush in chain
  110. } cm_brushRef_t;
  111. typedef struct cm_brushRefBlock_s {
  112. cm_brushRef_t * nextRef; // next brush reference in block
  113. struct cm_brushRefBlock_s *next; // next block with brush references
  114. } cm_brushRefBlock_t;
  115. typedef struct cm_node_s {
  116. int planeType; // node axial plane type
  117. float planeDist; // node plane distance
  118. cm_polygonRef_t * polygons; // polygons in node
  119. cm_brushRef_t * brushes; // brushes in node
  120. struct cm_node_s * parent; // parent of this node
  121. struct cm_node_s * children[2]; // node children
  122. } cm_node_t;
  123. typedef struct cm_nodeBlock_s {
  124. cm_node_t * nextNode; // next node in block
  125. struct cm_nodeBlock_s *next; // next block with nodes
  126. } cm_nodeBlock_t;
  127. typedef struct cm_model_s {
  128. idStr name; // model name
  129. idBounds bounds; // model bounds
  130. int contents; // all contents of the model ored together
  131. bool isConvex; // set if model is convex
  132. // model geometry
  133. int maxVertices; // size of vertex array
  134. int numVertices; // number of vertices
  135. cm_vertex_t * vertices; // array with all vertices used by the model
  136. int maxEdges; // size of edge array
  137. int numEdges; // number of edges
  138. cm_edge_t * edges; // array with all edges used by the model
  139. cm_node_t * node; // first node of spatial subdivision
  140. // blocks with allocated memory
  141. cm_nodeBlock_t * nodeBlocks; // list with blocks of nodes
  142. cm_polygonRefBlock_t * polygonRefBlocks; // list with blocks of polygon references
  143. cm_brushRefBlock_t * brushRefBlocks; // list with blocks of brush references
  144. cm_polygonBlock_t * polygonBlock; // memory block with all polygons
  145. cm_brushBlock_t * brushBlock; // memory block with all brushes
  146. // statistics
  147. int numPolygons;
  148. int polygonMemory;
  149. int numBrushes;
  150. int brushMemory;
  151. int numNodes;
  152. int numBrushRefs;
  153. int numPolygonRefs;
  154. int numInternalEdges;
  155. int numSharpEdges;
  156. int numRemovedPolys;
  157. int numMergedPolys;
  158. int usedMemory;
  159. } cm_model_t;
  160. /*
  161. ===============================================================================
  162. Data used during collision detection calculations
  163. ===============================================================================
  164. */
  165. typedef struct cm_trmVertex_s {
  166. int used; // true if this vertex is used for collision detection
  167. idVec3 p; // vertex position
  168. idVec3 endp; // end point of vertex after movement
  169. int polygonSide; // side of polygon this vertex is on (rotational collision)
  170. idPluecker pl; // pluecker coordinate for vertex movement
  171. idVec3 rotationOrigin; // rotation origin for this vertex
  172. idBounds rotationBounds; // rotation bounds for this vertex
  173. } cm_trmVertex_t;
  174. typedef struct cm_trmEdge_s {
  175. int used; // true when vertex is used for collision detection
  176. idVec3 start; // start of edge
  177. idVec3 end; // end of edge
  178. int vertexNum[2]; // indexes into cm_traceWork_t->vertices
  179. idPluecker pl; // pluecker coordinate for edge
  180. idVec3 cross; // (z,-y,x) of cross product between edge dir and movement dir
  181. idBounds rotationBounds; // rotation bounds for this edge
  182. idPluecker plzaxis; // pluecker coordinate for rotation about the z-axis
  183. unsigned short bitNum; // vertex bit number
  184. } cm_trmEdge_t;
  185. typedef struct cm_trmPolygon_s {
  186. int used;
  187. idPlane plane; // polygon plane
  188. int numEdges; // number of edges
  189. int edges[MAX_TRACEMODEL_POLYEDGES]; // index into cm_traceWork_t->edges
  190. idBounds rotationBounds; // rotation bounds for this polygon
  191. } cm_trmPolygon_t;
  192. typedef struct cm_traceWork_s {
  193. int numVerts;
  194. cm_trmVertex_t vertices[MAX_TRACEMODEL_VERTS]; // trm vertices
  195. int numEdges;
  196. cm_trmEdge_t edges[MAX_TRACEMODEL_EDGES+1]; // trm edges
  197. int numPolys;
  198. cm_trmPolygon_t polys[MAX_TRACEMODEL_POLYS]; // trm polygons
  199. cm_model_t *model; // model colliding with
  200. idVec3 start; // start of trace
  201. idVec3 end; // end of trace
  202. idVec3 dir; // trace direction
  203. idBounds bounds; // bounds of full trace
  204. idBounds size; // bounds of transformed trm relative to start
  205. idVec3 extents; // largest of abs(size[0]) and abs(size[1]) for BSP trace
  206. int contents; // ignore polygons that do not have any of these contents flags
  207. trace_t trace; // collision detection result
  208. bool rotation; // true if calculating rotational collision
  209. bool pointTrace; // true if only tracing a point
  210. bool positionTest; // true if not tracing but doing a position test
  211. bool isConvex; // true if the trace model is convex
  212. bool axisIntersectsTrm; // true if the rotation axis intersects the trace model
  213. bool getContacts; // true if retrieving contacts
  214. bool quickExit; // set to quickly stop the collision detection calculations
  215. idVec3 origin; // origin of rotation in model space
  216. idVec3 axis; // rotation axis in model space
  217. idMat3 matrix; // rotates axis of rotation to the z-axis
  218. float angle; // angle for rotational collision
  219. float maxTan; // max tangent of half the positive angle used instead of fraction
  220. float radius; // rotation radius of trm start
  221. idRotation modelVertexRotation; // inverse rotation for model vertices
  222. contactInfo_t *contacts; // array with contacts
  223. int maxContacts; // max size of contact array
  224. int numContacts; // number of contacts found
  225. idPlane heartPlane1; // polygons should be near anough the trace heart planes
  226. float maxDistFromHeartPlane1;
  227. idPlane heartPlane2;
  228. float maxDistFromHeartPlane2;
  229. idPluecker polygonEdgePlueckerCache[CM_MAX_POLYGON_EDGES];
  230. idPluecker polygonVertexPlueckerCache[CM_MAX_POLYGON_EDGES];
  231. idVec3 polygonRotationOriginCache[CM_MAX_POLYGON_EDGES];
  232. } cm_traceWork_t;
  233. /*
  234. ===============================================================================
  235. Collision Map
  236. ===============================================================================
  237. */
  238. typedef struct cm_procNode_s {
  239. idPlane plane;
  240. int children[2]; // negative numbers are (-1 - areaNumber), 0 = solid
  241. } cm_procNode_t;
  242. class idCollisionModelManagerLocal : public idCollisionModelManager {
  243. public:
  244. // load collision models from a map file
  245. void LoadMap( const idMapFile *mapFile );
  246. // frees all the collision models
  247. void FreeMap( void );
  248. // get clip handle for model
  249. cmHandle_t LoadModel( const char *modelName, const bool precache );
  250. // sets up a trace model for collision with other trace models
  251. cmHandle_t SetupTrmModel( const idTraceModel &trm, const idMaterial *material );
  252. // create trace model from a collision model, returns true if succesfull
  253. bool TrmFromModel( const char *modelName, idTraceModel &trm );
  254. // name of the model
  255. const char * GetModelName( cmHandle_t model ) const;
  256. // bounds of the model
  257. bool GetModelBounds( cmHandle_t model, idBounds &bounds ) const;
  258. // all contents flags of brushes and polygons ored together
  259. bool GetModelContents( cmHandle_t model, int &contents ) const;
  260. // get the vertex of a model
  261. bool GetModelVertex( cmHandle_t model, int vertexNum, idVec3 &vertex ) const;
  262. // get the edge of a model
  263. bool GetModelEdge( cmHandle_t model, int edgeNum, idVec3 &start, idVec3 &end ) const;
  264. // get the polygon of a model
  265. bool GetModelPolygon( cmHandle_t model, int polygonNum, idFixedWinding &winding ) const;
  266. // translates a trm and reports the first collision if any
  267. void Translation( trace_t *results, const idVec3 &start, const idVec3 &end,
  268. const idTraceModel *trm, const idMat3 &trmAxis, int contentMask,
  269. cmHandle_t model, const idVec3 &modelOrigin, const idMat3 &modelAxis );
  270. // rotates a trm and reports the first collision if any
  271. void Rotation( trace_t *results, const idVec3 &start, const idRotation &rotation,
  272. const idTraceModel *trm, const idMat3 &trmAxis, int contentMask,
  273. cmHandle_t model, const idVec3 &modelOrigin, const idMat3 &modelAxis );
  274. // returns the contents the trm is stuck in or 0 if the trm is in free space
  275. int Contents( const idVec3 &start,
  276. const idTraceModel *trm, const idMat3 &trmAxis, int contentMask,
  277. cmHandle_t model, const idVec3 &modelOrigin, const idMat3 &modelAxis );
  278. // stores all contact points of the trm with the model, returns the number of contacts
  279. int Contacts( contactInfo_t *contacts, const int maxContacts, const idVec3 &start, const idVec6 &dir, const float depth,
  280. const idTraceModel *trm, const idMat3 &trmAxis, int contentMask,
  281. cmHandle_t model, const idVec3 &modelOrigin, const idMat3 &modelAxis );
  282. // test collision detection
  283. void DebugOutput( const idVec3 &origin );
  284. // draw a model
  285. void DrawModel( cmHandle_t model, const idVec3 &origin, const idMat3 &axis,
  286. const idVec3 &viewOrigin, const float radius, const idVec4 &linecolor );
  287. // print model information, use -1 handle for accumulated model info
  288. void ModelInfo( cmHandle_t model );
  289. // list all loaded models
  290. void ListModels( void );
  291. // write a collision model file for the map entity
  292. bool WriteCollisionModelForMapEntity( const idMapEntity *mapEnt, const char *filename, const bool testTraceModel = true );
  293. private: // CollisionMap_translate.cpp
  294. int TranslateEdgeThroughEdge( idVec3 &cross, idPluecker &l1, idPluecker &l2, float *fraction );
  295. void TranslateTrmEdgeThroughPolygon( cm_traceWork_t *tw, cm_polygon_t *poly, cm_trmEdge_t *trmEdge );
  296. void TranslateTrmVertexThroughPolygon( cm_traceWork_t *tw, cm_polygon_t *poly, cm_trmVertex_t *v, int bitNum );
  297. void TranslatePointThroughPolygon( cm_traceWork_t *tw, cm_polygon_t *poly, cm_trmVertex_t *v );
  298. void TranslateVertexThroughTrmPolygon( cm_traceWork_t *tw, cm_trmPolygon_t *trmpoly, cm_polygon_t *poly, cm_vertex_t *v, idVec3 &endp, idPluecker &pl );
  299. bool TranslateTrmThroughPolygon( cm_traceWork_t *tw, cm_polygon_t *p );
  300. void SetupTranslationHeartPlanes( cm_traceWork_t *tw );
  301. void SetupTrm( cm_traceWork_t *tw, const idTraceModel *trm );
  302. private: // CollisionMap_rotate.cpp
  303. int CollisionBetweenEdgeBounds( cm_traceWork_t *tw, const idVec3 &va, const idVec3 &vb,
  304. const idVec3 &vc, const idVec3 &vd, float tanHalfAngle,
  305. idVec3 &collisionPoint, idVec3 &collisionNormal );
  306. int RotateEdgeThroughEdge( cm_traceWork_t *tw, const idPluecker &pl1,
  307. const idVec3 &vc, const idVec3 &vd,
  308. const float minTan, float &tanHalfAngle );
  309. int EdgeFurthestFromEdge( cm_traceWork_t *tw, const idPluecker &pl1,
  310. const idVec3 &vc, const idVec3 &vd,
  311. float &tanHalfAngle, float &dir );
  312. void RotateTrmEdgeThroughPolygon( cm_traceWork_t *tw, cm_polygon_t *poly, cm_trmEdge_t *trmEdge );
  313. int RotatePointThroughPlane( const cm_traceWork_t *tw, const idVec3 &point, const idPlane &plane,
  314. const float angle, const float minTan, float &tanHalfAngle );
  315. int PointFurthestFromPlane( const cm_traceWork_t *tw, const idVec3 &point, const idPlane &plane,
  316. const float angle, float &tanHalfAngle, float &dir );
  317. int RotatePointThroughEpsilonPlane( const cm_traceWork_t *tw, const idVec3 &point, const idVec3 &endPoint,
  318. const idPlane &plane, const float angle, const idVec3 &origin,
  319. float &tanHalfAngle, idVec3 &collisionPoint, idVec3 &endDir );
  320. void RotateTrmVertexThroughPolygon( cm_traceWork_t *tw, cm_polygon_t *poly, cm_trmVertex_t *v, int vertexNum);
  321. void RotateVertexThroughTrmPolygon( cm_traceWork_t *tw, cm_trmPolygon_t *trmpoly, cm_polygon_t *poly,
  322. cm_vertex_t *v, idVec3 &rotationOrigin );
  323. bool RotateTrmThroughPolygon( cm_traceWork_t *tw, cm_polygon_t *p );
  324. void BoundsForRotation( const idVec3 &origin, const idVec3 &axis, const idVec3 &start, const idVec3 &end, idBounds &bounds );
  325. void Rotation180( trace_t *results, const idVec3 &rorg, const idVec3 &axis,
  326. const float startAngle, const float endAngle, const idVec3 &start,
  327. const idTraceModel *trm, const idMat3 &trmAxis, int contentMask,
  328. cmHandle_t model, const idVec3 &origin, const idMat3 &modelAxis );
  329. private: // CollisionMap_contents.cpp
  330. bool TestTrmVertsInBrush( cm_traceWork_t *tw, cm_brush_t *b );
  331. bool TestTrmInPolygon( cm_traceWork_t *tw, cm_polygon_t *p );
  332. cm_node_t * PointNode( const idVec3 &p, cm_model_t *model );
  333. int PointContents( const idVec3 p, cmHandle_t model );
  334. int TransformedPointContents( const idVec3 &p, cmHandle_t model, const idVec3 &origin, const idMat3 &modelAxis );
  335. int ContentsTrm( trace_t *results, const idVec3 &start,
  336. const idTraceModel *trm, const idMat3 &trmAxis, int contentMask,
  337. cmHandle_t model, const idVec3 &modelOrigin, const idMat3 &modelAxis );
  338. private: // CollisionMap_trace.cpp
  339. void TraceTrmThroughNode( cm_traceWork_t *tw, cm_node_t *node );
  340. void TraceThroughAxialBSPTree_r( cm_traceWork_t *tw, cm_node_t *node, float p1f, float p2f, idVec3 &p1, idVec3 &p2);
  341. void TraceThroughModel( cm_traceWork_t *tw );
  342. void RecurseProcBSP_r( trace_t *results, int parentNodeNum, int nodeNum, float p1f, float p2f, const idVec3 &p1, const idVec3 &p2 );
  343. private: // CollisionMap_load.cpp
  344. void Clear( void );
  345. void FreeTrmModelStructure( void );
  346. // model deallocation
  347. void RemovePolygonReferences_r( cm_node_t *node, cm_polygon_t *p );
  348. void RemoveBrushReferences_r( cm_node_t *node, cm_brush_t *b );
  349. void FreeNode( cm_node_t *node );
  350. void FreePolygonReference( cm_polygonRef_t *pref );
  351. void FreeBrushReference( cm_brushRef_t *bref );
  352. void FreePolygon( cm_model_t *model, cm_polygon_t *poly );
  353. void FreeBrush( cm_model_t *model, cm_brush_t *brush );
  354. void FreeTree_r( cm_model_t *model, cm_node_t *headNode, cm_node_t *node );
  355. void FreeModel( cm_model_t *model );
  356. // merging polygons
  357. void ReplacePolygons( cm_model_t *model, cm_node_t *node, cm_polygon_t *p1, cm_polygon_t *p2, cm_polygon_t *newp );
  358. cm_polygon_t * TryMergePolygons( cm_model_t *model, cm_polygon_t *p1, cm_polygon_t *p2 );
  359. bool MergePolygonWithTreePolygons( cm_model_t *model, cm_node_t *node, cm_polygon_t *polygon );
  360. void MergeTreePolygons( cm_model_t *model, cm_node_t *node );
  361. // finding internal edges
  362. bool PointInsidePolygon( cm_model_t *model, cm_polygon_t *p, idVec3 &v );
  363. void FindInternalEdgesOnPolygon( cm_model_t *model, cm_polygon_t *p1, cm_polygon_t *p2 );
  364. void FindInternalPolygonEdges( cm_model_t *model, cm_node_t *node, cm_polygon_t *polygon );
  365. void FindInternalEdges( cm_model_t *model, cm_node_t *node );
  366. void FindContainedEdges( cm_model_t *model, cm_polygon_t *p );
  367. // loading of proc BSP tree
  368. void ParseProcNodes( idLexer *src );
  369. void LoadProcBSP( const char *name );
  370. // removal of contained polygons
  371. int R_ChoppedAwayByProcBSP( int nodeNum, idFixedWinding *w, const idVec3 &normal, const idVec3 &origin, const float radius );
  372. int ChoppedAwayByProcBSP( const idFixedWinding &w, const idPlane &plane, int contents );
  373. void ChopWindingListWithBrush( cm_windingList_t *list, cm_brush_t *b );
  374. void R_ChopWindingListWithTreeBrushes( cm_windingList_t *list, cm_node_t *node );
  375. idFixedWinding *WindingOutsideBrushes( idFixedWinding *w, const idPlane &plane, int contents, int patch, cm_node_t *headNode );
  376. // creation of axial BSP tree
  377. cm_model_t * AllocModel( void );
  378. cm_node_t * AllocNode( cm_model_t *model, int blockSize );
  379. cm_polygonRef_t*AllocPolygonReference( cm_model_t *model, int blockSize );
  380. cm_brushRef_t * AllocBrushReference( cm_model_t *model, int blockSize );
  381. cm_polygon_t * AllocPolygon( cm_model_t *model, int numEdges );
  382. cm_brush_t * AllocBrush( cm_model_t *model, int numPlanes );
  383. void AddPolygonToNode( cm_model_t *model, cm_node_t *node, cm_polygon_t *p );
  384. void AddBrushToNode( cm_model_t *model, cm_node_t *node, cm_brush_t *b );
  385. void SetupTrmModelStructure( void );
  386. void R_FilterPolygonIntoTree( cm_model_t *model, cm_node_t *node, cm_polygonRef_t *pref, cm_polygon_t *p );
  387. void R_FilterBrushIntoTree( cm_model_t *model, cm_node_t *node, cm_brushRef_t *pref, cm_brush_t *b );
  388. cm_node_t * R_CreateAxialBSPTree( cm_model_t *model, cm_node_t *node, const idBounds &bounds );
  389. cm_node_t * CreateAxialBSPTree( cm_model_t *model, cm_node_t *node );
  390. // creation of raw polygons
  391. void SetupHash(void);
  392. void ShutdownHash(void);
  393. void ClearHash( idBounds &bounds );
  394. int HashVec(const idVec3 &vec);
  395. int GetVertex( cm_model_t *model, const idVec3 &v, int *vertexNum );
  396. int GetEdge( cm_model_t *model, const idVec3 &v1, const idVec3 &v2, int *edgeNum, int v1num );
  397. void CreatePolygon( cm_model_t *model, idFixedWinding *w, const idPlane &plane, const idMaterial *material, int primitiveNum );
  398. void PolygonFromWinding( cm_model_t *model, idFixedWinding *w, const idPlane &plane, const idMaterial *material, int primitiveNum );
  399. void CalculateEdgeNormals( cm_model_t *model, cm_node_t *node );
  400. void CreatePatchPolygons( cm_model_t *model, idSurface_Patch &mesh, const idMaterial *material, int primitiveNum );
  401. void ConvertPatch( cm_model_t *model, const idMapPatch *patch, int primitiveNum );
  402. void ConvertBrushSides( cm_model_t *model, const idMapBrush *mapBrush, int primitiveNum );
  403. void ConvertBrush( cm_model_t *model, const idMapBrush *mapBrush, int primitiveNum );
  404. void PrintModelInfo( const cm_model_t *model );
  405. void AccumulateModelInfo( cm_model_t *model );
  406. void RemapEdges( cm_node_t *node, int *edgeRemap );
  407. void OptimizeArrays( cm_model_t *model );
  408. void FinishModel( cm_model_t *model );
  409. void BuildModels( const idMapFile *mapFile );
  410. cmHandle_t FindModel( const char *name );
  411. cm_model_t * CollisionModelForMapEntity( const idMapEntity *mapEnt ); // brush/patch model from .map
  412. cm_model_t * LoadRenderModel( const char *fileName ); // ASE/LWO models
  413. bool TrmFromModel_r( idTraceModel &trm, cm_node_t *node );
  414. bool TrmFromModel( const cm_model_t *model, idTraceModel &trm );
  415. private: // CollisionMap_files.cpp
  416. // writing
  417. void WriteNodes( idFile *fp, cm_node_t *node );
  418. int CountPolygonMemory( cm_node_t *node ) const;
  419. void WritePolygons( idFile *fp, cm_node_t *node );
  420. int CountBrushMemory( cm_node_t *node ) const;
  421. void WriteBrushes( idFile *fp, cm_node_t *node );
  422. void WriteCollisionModel( idFile *fp, cm_model_t *model );
  423. void WriteCollisionModelsToFile( const char *filename, int firstModel, int lastModel, unsigned int mapFileCRC );
  424. // loading
  425. cm_node_t * ParseNodes( idLexer *src, cm_model_t *model, cm_node_t *parent );
  426. void ParseVertices( idLexer *src, cm_model_t *model );
  427. void ParseEdges( idLexer *src, cm_model_t *model );
  428. void ParsePolygons( idLexer *src, cm_model_t *model );
  429. void ParseBrushes( idLexer *src, cm_model_t *model );
  430. bool ParseCollisionModel( idLexer *src );
  431. bool LoadCollisionModelFile( const char *name, unsigned int mapFileCRC );
  432. private: // CollisionMap_debug
  433. int ContentsFromString( const char *string ) const;
  434. const char * StringFromContents( const int contents ) const;
  435. void DrawEdge( cm_model_t *model, int edgeNum, const idVec3 &origin, const idMat3 &axis, const idVec4 &linecolor );
  436. void DrawPolygon( cm_model_t *model, cm_polygon_t *p, const idVec3 &origin, const idMat3 &axis,
  437. const idVec3 &viewOrigin, const idVec4 &linecolor );
  438. void DrawNodePolygons( cm_model_t *model, cm_node_t *node, const idVec3 &origin, const idMat3 &axis,
  439. const idVec3 &viewOrigin, const float radius, const idVec4 &linecolor );
  440. private: // collision map data
  441. idStr mapName;
  442. ID_TIME_T mapFileTime;
  443. int loaded;
  444. // for multi-check avoidance
  445. int checkCount;
  446. // models
  447. int maxModels;
  448. int numModels;
  449. cm_model_t ** models;
  450. // polygons and brush for trm model
  451. cm_polygonRef_t*trmPolygons[MAX_TRACEMODEL_POLYS];
  452. cm_brushRef_t * trmBrushes[1];
  453. const idMaterial *trmMaterial;
  454. // for data pruning
  455. int numProcNodes;
  456. cm_procNode_t * procNodes;
  457. // for retrieving contact points
  458. bool getContacts;
  459. contactInfo_t * contacts;
  460. int maxContacts;
  461. int numContacts;
  462. };
  463. // for debugging
  464. extern idCVar cm_debugCollision;