ode/inc/heightfield.h
changeset 0 2f259fa3e83a
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 // dHeightfield Collider
       
     2 //  Martijn Buijs 2006 http://home.planet.nl/~buijs512/
       
     3 // Based on Terrain & Cone contrib by:
       
     4 //  Benoit CHAPEROT 2003-2004 http://www.jstarlab.com
       
     5 
       
     6 #ifndef _DHEIGHTFIELD_H_
       
     7 #define _DHEIGHTFIELD_H_
       
     8 //------------------------------------------------------------------------------
       
     9 
       
    10 #include <ode/common.h>
       
    11 #include "collision_kernel.h"
       
    12 
       
    13 
       
    14 #define HEIGHTFIELDMAXCONTACTPERCELL 10
       
    15 
       
    16 
       
    17 //
       
    18 // dxHeightfieldData
       
    19 //
       
    20 // Heightfield Data structure
       
    21 //
       
    22 struct dxHeightfieldData
       
    23 {
       
    24     dReal m_fWidth;				// World space heightfield dimension on X axis
       
    25     dReal m_fDepth;				// World space heightfield dimension on Z axis
       
    26     dReal m_fSampleWidth;		// Vertex count on X axis edge (== m_vWidth / (m_nWidthSamples-1))
       
    27     dReal m_fSampleDepth;		// Vertex count on Z axis edge (== m_vDepth / (m_nDepthSamples-1))
       
    28     dReal m_fInvSampleWidth;		// Cache of inverse Vertex count on X axis edge (== m_vWidth / (m_nWidthSamples-1))
       
    29     dReal m_fInvSampleDepth;		// Cache of inverse Vertex count on Z axis edge (== m_vDepth / (m_nDepthSamples-1))
       
    30 
       
    31     dReal m_fHalfWidth;			// Cache of half of m_fWidth
       
    32     dReal m_fHalfDepth;			// Cache of half of m_fDepth
       
    33 
       
    34     dReal m_fMinHeight;        // Min sample height value (scaled and offset)
       
    35     dReal m_fMaxHeight;        // Max sample height value (scaled and offset)
       
    36     dReal m_fThickness;        // Surface thickness (added to bottom AABB)
       
    37     dReal m_fScale;            // Sample value multiplier
       
    38     dReal m_fOffset;           // Vertical sample offset
       
    39 
       
    40     int	m_nWidthSamples;       // Vertex count on X axis edge (number of samples)
       
    41     int	m_nDepthSamples;       // Vertex count on Z axis edge (number of samples)
       
    42     int m_bCopyHeightData;     // Do we own the sample data?
       
    43     int	m_bWrapMode;           // Heightfield wrapping mode (0=finite, 1=infinite)
       
    44     int m_nGetHeightMode;      // GetHeight mode ( 0=callback, 1=byte, 2=short, 3=float )
       
    45 
       
    46     const void* m_pHeightData; // Sample data array
       
    47     void* m_pUserData;         // Callback user data
       
    48 
       
    49     dContactGeom            m_contacts[HEIGHTFIELDMAXCONTACTPERCELL];
       
    50 
       
    51     dHeightfieldGetHeight* m_pGetHeightCallback;		// Callback pointer.
       
    52 
       
    53     dxHeightfieldData();
       
    54     ~dxHeightfieldData();
       
    55 
       
    56     void SetData( int nWidthSamples, int nDepthSamples,
       
    57         dReal fWidth, dReal fDepth,
       
    58         dReal fScale, dReal fOffset,
       
    59         dReal fThickness, int bWrapMode );
       
    60 
       
    61     void ComputeHeightBounds();
       
    62 
       
    63     bool IsOnHeightfield  ( const dReal * const CellOrigin, const dReal * const pos,  const bool isABC) const;
       
    64     bool IsOnHeightfield2  ( const dReal * const CellOrigin, const dReal * const pos,  const bool isABC) const;
       
    65 
       
    66     dReal GetHeight(int x, int z);
       
    67     dReal GetHeight(dReal x, dReal z);
       
    68 
       
    69 };
       
    70 
       
    71 class HeightFieldVertex;
       
    72 class HeightFieldEdge;
       
    73 class HeightFieldTriangle;
       
    74 
       
    75 class HeightFieldVertex
       
    76 {
       
    77 public:
       
    78     HeightFieldVertex(){};
       
    79 
       
    80     dVector3 vertex;
       
    81     bool state;
       
    82 };
       
    83 
       
    84 class HeightFieldEdge
       
    85 {
       
    86 public:
       
    87     HeightFieldEdge(){};
       
    88 
       
    89     HeightFieldVertex   *vertices[2];
       
    90 };
       
    91 //
       
    92 // HeightFieldTriangle
       
    93 //
       
    94 // HeightFieldTriangle composing heightfield mesh
       
    95 //
       
    96 class HeightFieldTriangle
       
    97 {
       
    98 public:
       
    99     HeightFieldTriangle(){};
       
   100 
       
   101     inline void setMinMax()
       
   102     {
       
   103         maxAAAB = vertices[0]->vertex[1] > vertices[1]->vertex[1] ? vertices[0]->vertex[1] : vertices[1]->vertex[1];
       
   104         maxAAAB = vertices[2]->vertex[1] > maxAAAB  ? vertices[2]->vertex[1] : maxAAAB;
       
   105     };
       
   106 
       
   107     HeightFieldVertex   *vertices[3];
       
   108     dReal               planeDef[4];
       
   109     dReal               maxAAAB;
       
   110 
       
   111     bool                isUp;
       
   112     bool                state;
       
   113 };
       
   114 //
       
   115 // HeightFieldTriangle
       
   116 //
       
   117 // HeightFieldPlane composing heightfield mesh
       
   118 //
       
   119 class HeightFieldPlane
       
   120 {
       
   121 public:
       
   122     HeightFieldPlane():
       
   123       trianglelist(0),
       
   124       trianglelistReservedSize(0),
       
   125       trianglelistCurrentSize(0)
       
   126     {
       
   127 
       
   128     };
       
   129     ~HeightFieldPlane()
       
   130     {
       
   131         delete [] trianglelist;
       
   132     };
       
   133 
       
   134     inline void setMinMax()
       
   135     {
       
   136         const size_t asize = trianglelistCurrentSize;
       
   137         if (asize > 0)
       
   138         {  
       
   139             maxAAAB = trianglelist[0]->maxAAAB;
       
   140             for (size_t k = 1; asize > k; k++)
       
   141             {   
       
   142                 if (trianglelist[k]->maxAAAB >  maxAAAB)
       
   143                     maxAAAB = trianglelist[k]->maxAAAB;
       
   144             }
       
   145         }
       
   146     };
       
   147 
       
   148     void resetTriangleListSize(const size_t newSize)
       
   149     {
       
   150         if (trianglelistReservedSize < newSize)
       
   151         {
       
   152             delete [] trianglelist;
       
   153             trianglelistReservedSize = newSize;
       
   154             trianglelist = new HeightFieldTriangle *[newSize];
       
   155         }
       
   156         trianglelistCurrentSize = 0;
       
   157     }
       
   158 
       
   159     void addTriangle(HeightFieldTriangle *tri)
       
   160     {
       
   161         trianglelist[trianglelistCurrentSize++] = tri;
       
   162     }
       
   163 
       
   164     HeightFieldTriangle **trianglelist;
       
   165     size_t              trianglelistReservedSize;
       
   166     size_t              trianglelistCurrentSize;
       
   167 
       
   168     dReal   maxAAAB;
       
   169     dReal   planeDef[4];
       
   170 };
       
   171 //
       
   172 // dxHeightfield
       
   173 //
       
   174 // Heightfield geom structure
       
   175 //
       
   176 struct dxHeightfield : public dxGeom
       
   177 {
       
   178     dxHeightfieldData* m_p_data;
       
   179 
       
   180     dxHeightfield( dSpaceID space, dHeightfieldDataID data, int bPlaceable );
       
   181     ~dxHeightfield();
       
   182 
       
   183     void computeAABB();
       
   184 
       
   185     int dCollideHeightfieldZone( const int minX, const int maxX, const int minZ, const int maxZ,  
       
   186         dxGeom *o2, const int numMaxContacts,
       
   187         int flags, dContactGeom *contact, int skip );
       
   188 
       
   189     void  resetHeightBuffer();
       
   190 
       
   191     void  sortPlanes(const size_t numPlanes);
       
   192 
       
   193     HeightFieldPlane    **tempPlaneBuffer;
       
   194     size_t              tempPlaneBufferSize;
       
   195 
       
   196     HeightFieldTriangle *tempTriangleBuffer;
       
   197     size_t              tempTriangleBufferSize;
       
   198 
       
   199     HeightFieldVertex   **tempHeightBuffer;
       
   200     size_t              tempHeightBufferSizeX;
       
   201     size_t              tempHeightBufferSizeZ;
       
   202 
       
   203 };
       
   204 
       
   205 //------------------------------------------------------------------------------
       
   206 #endif //_DHEIGHTFIELD_H_