home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / idlib / geometry / TraceModel.h < prev    next >
C/C++ Source or Header  |  2005-11-14  |  6KB  |  175 lines

  1.  
  2. #ifndef __TRACEMODEL_H__
  3. #define __TRACEMODEL_H__
  4.  
  5. /*
  6. ===============================================================================
  7.  
  8.     A trace model is an arbitrary polygonal model which is used by the
  9.     collision detection system to find collisions, contacts or the contents
  10.     of a volume. For collision detection speed reasons the number of vertices
  11.     and edges are limited. The trace model can have any shape. However convex
  12.     models are usually preferred.
  13.  
  14. ===============================================================================
  15. */
  16.  
  17. class idVec3;
  18. class idMat3;
  19. class idBounds;
  20.  
  21. // trace model type
  22. enum traceModel_t {
  23.     TRM_INVALID,        // invalid trm
  24.     TRM_BOX,            // box
  25.     TRM_OCTAHEDRON,        // octahedron
  26.     TRM_DODECAHEDRON,    // dodecahedron
  27.     TRM_CYLINDER,        // cylinder approximation
  28.     TRM_CONE,            // cone approximation
  29.     TRM_BONE,            // two tetrahedrons attached to each other
  30.     TRM_POLYGON,        // arbitrary convex polygon
  31.     TRM_POLYGONVOLUME,    // volume for arbitrary convex polygon
  32.     TRM_CUSTOM            // loaded from map model or ASE/LWO
  33. };
  34.  
  35. // these are bit cache limits
  36. #define MAX_TRACEMODEL_VERTS        32
  37. #define MAX_TRACEMODEL_EDGES        32
  38. #define MAX_TRACEMODEL_POLYS        16
  39. #define MAX_TRACEMODEL_POLYEDGES    16
  40.  
  41. typedef idVec3 traceModelVert_t;
  42.  
  43. struct traceModelEdge_t {
  44.     int                    v[2];
  45.     idVec3                normal;
  46. };
  47.  
  48. struct traceModelPoly_t {
  49.     idVec3                normal;
  50.     float                dist;
  51.     idBounds            bounds;
  52.     int                    numEdges;
  53.     int                    edges[MAX_TRACEMODEL_POLYEDGES];
  54. };
  55.  
  56. class idTraceModel {
  57.  
  58. public:
  59.     traceModel_t        type;
  60.     int                    numVerts;
  61.     traceModelVert_t    verts[MAX_TRACEMODEL_VERTS];
  62.     int                    numEdges;
  63.     traceModelEdge_t    edges[MAX_TRACEMODEL_EDGES+1];    // edges[0] is unused because edge index signs are used for direction
  64.     int                    numPolys;
  65.     traceModelPoly_t    polys[MAX_TRACEMODEL_POLYS];
  66.     idVec3                offset;            // offset to center of model
  67.     idBounds            bounds;            // bounds of model
  68.     bool                isConvex;        // true when model is convex
  69.  
  70. public:
  71.                         idTraceModel( void );
  72.                         // axial bounding box
  73.                         idTraceModel( const idBounds &boxBounds );
  74.                         // cylinder approximation
  75.                         idTraceModel( const idBounds &cylBounds, const int numSides );
  76.                         // bone
  77.                         idTraceModel( const float length, const float width );
  78.  
  79.                         // axial box
  80.     void                SetupBox( const idBounds &boxBounds );
  81.     void                SetupBox( const float size );
  82.                         // octahedron
  83.     void                SetupOctahedron( const idBounds &octBounds );
  84.     void                SetupOctahedron( const float size );
  85.                         // dodecahedron
  86.     void                SetupDodecahedron( const idBounds &dodBounds );
  87.     void                SetupDodecahedron( const float size );
  88.                         // cylinder approximation
  89.     void                SetupCylinder( const idBounds &cylBounds, const int numSides );
  90.     void                SetupCylinder( const float height, const float width, const int numSides );
  91.                         // cone approximation
  92.     void                SetupCone( const idBounds &coneBounds, const int numSides );
  93.     void                SetupCone( const float height, const float width, const int numSides );
  94.                         // two tetrahedrons attached to each other
  95.     void                SetupBone( const float length, const float width );
  96.                         // arbitrary convex polygon
  97.     void                SetupPolygon( const idVec3 *v, const int count );
  98.     void                SetupPolygon( const idWinding &w );
  99.  
  100.                         // generate edge normals
  101.     int                    GenerateEdgeNormals( void );
  102.                         // test whether or not the model is convex and set isConvex accordingly
  103.     void                TestConvexity( void );
  104.                         // translate the trm
  105.     void                Translate( const idVec3 &translation );
  106.                         // rotate the trm
  107.     void                Rotate( const idMat3 &rotation );
  108.                         // shrink the model m units on all sides
  109.     void                Shrink( const float m );
  110.                         // clear unused spots in the arrays
  111.     void                ClearUnused( void );
  112.                         // make sure the trace model is well formed
  113.     bool                Verify( void );
  114.  
  115.                         // compare
  116.     bool                Compare( const idTraceModel &trm ) const;
  117.     bool                operator==(    const idTraceModel &trm ) const;
  118.     bool                operator!=(    const idTraceModel &trm ) const;
  119.  
  120.                         // returns true of the model is a closed surface
  121.     bool                IsClosedSurface( void ) const;
  122.  
  123.                         // get the area of one of the polygons
  124.     float                GetPolygonArea( int polyNum ) const;
  125.                         // get the silhouette edges
  126.     int                    GetProjectionSilhouetteEdges( const idVec3 &projectionOrigin, int silEdges[MAX_TRACEMODEL_EDGES] ) const;
  127.     int                    GetParallelProjectionSilhouetteEdges( const idVec3 &projectionDir, int silEdges[MAX_TRACEMODEL_EDGES] ) const;
  128.                         // calculate mass properties assuming an uniform density
  129.     void                GetMassProperties( const float density, float &mass, idVec3 ¢erOfMass, idMat3 &inertiaTensor ) const;
  130.  
  131. private:
  132.     void                InitBox( void );
  133.     void                InitOctahedron( void );
  134.     void                InitDodecahedron( void );
  135.     void                InitBone( void );
  136.  
  137.     void                ProjectionIntegrals( int polyNum, int a, int b, struct projectionIntegrals_t &integrals ) const;
  138.     void                PolygonIntegrals( int polyNum, int a, int b, int c, struct polygonIntegrals_t &integrals ) const;
  139.     void                VolumeIntegrals( struct volumeIntegrals_t &integrals ) const;
  140.     void                VolumeFromPolygon( idTraceModel &trm, float thickness ) const;
  141.     int                    GetOrderedSilhouetteEdges( const int edgeIsSilEdge[MAX_TRACEMODEL_EDGES+1], int silEdges[MAX_TRACEMODEL_EDGES] ) const;
  142. };
  143.  
  144.  
  145. ID_INLINE idTraceModel::idTraceModel( void ) {
  146.     type = TRM_INVALID;
  147.     numVerts = numEdges = numPolys = 0;
  148.     bounds.Zero();
  149. }
  150.  
  151. ID_INLINE idTraceModel::idTraceModel( const idBounds &boxBounds ) {
  152.     InitBox();
  153.     SetupBox( boxBounds );
  154. }
  155.  
  156. ID_INLINE idTraceModel::idTraceModel( const idBounds &cylBounds, const int numSides ) {
  157.     SetupCylinder( cylBounds, numSides );
  158. }
  159.  
  160. ID_INLINE idTraceModel::idTraceModel( const float length, const float width ) {
  161.     InitBone();
  162.     SetupBone( length, width );
  163. }
  164.  
  165. ID_INLINE bool idTraceModel::operator==( const idTraceModel &trm ) const {
  166.     return Compare( trm );
  167. }
  168.  
  169. ID_INLINE bool idTraceModel::operator!=( const idTraceModel &trm ) const {
  170.     return !Compare( trm );
  171. }
  172.  
  173. #endif /* !__TRACEMODEL_H__ */
  174.  
  175.