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 | // dxHeightfieldData |
---|
18 | // |
---|
19 | // Heightfield Data structure |
---|
20 | // |
---|
21 | struct dxHeightfieldData |
---|
22 | { |
---|
23 | dReal m_fWidth; // World space heightfield dimension on X axis |
---|
24 | dReal m_fDepth; // World space heightfield dimension on Z axis |
---|
25 | dReal m_fSampleWidth; // Vertex count on X axis edge (== m_vWidth / (m_nWidthSamples-1)) |
---|
26 | dReal m_fSampleDepth; // Vertex count on Z axis edge (== m_vDepth / (m_nDepthSamples-1)) |
---|
27 | dReal m_fInvSampleWidth; // Cache of inverse Vertex count on X axis edge (== m_vWidth / (m_nWidthSamples-1)) |
---|
28 | dReal m_fInvSampleDepth; // Cache of inverse Vertex count on Z axis edge (== m_vDepth / (m_nDepthSamples-1)) |
---|
29 | |
---|
30 | dReal m_fHalfWidth; // Cache of half of m_fWidth |
---|
31 | dReal m_fHalfDepth; // Cache of half of m_fDepth |
---|
32 | |
---|
33 | dReal m_fMinHeight; // Min sample height value (scaled and offset) |
---|
34 | dReal m_fMaxHeight; // Max sample height value (scaled and offset) |
---|
35 | dReal m_fThickness; // Surface thickness (added to bottom AABB) |
---|
36 | dReal m_fScale; // Sample value multiplier |
---|
37 | dReal m_fOffset; // Vertical sample offset |
---|
38 | |
---|
39 | int m_nWidthSamples; // Vertex count on X axis edge (number of samples) |
---|
40 | int m_nDepthSamples; // Vertex count on Z axis edge (number of samples) |
---|
41 | int m_bCopyHeightData; // Do we own the sample data? |
---|
42 | int m_bWrapMode; // Heightfield wrapping mode (0=finite, 1=infinite) |
---|
43 | int m_nGetHeightMode; // GetHeight mode ( 0=callback, 1=byte, 2=short, 3=float ) |
---|
44 | |
---|
45 | const void* m_pHeightData; // Sample data array |
---|
46 | void* m_pUserData; // Callback user data |
---|
47 | |
---|
48 | dContactGeom m_contacts[HEIGHTFIELDMAXCONTACTPERCELL]; |
---|
49 | |
---|
50 | dHeightfieldGetHeight* m_pGetHeightCallback; // Callback pointer. |
---|
51 | |
---|
52 | dxHeightfieldData(); |
---|
53 | ~dxHeightfieldData(); |
---|
54 | |
---|
55 | void SetData( int nWidthSamples, int nDepthSamples, |
---|
56 | dReal fWidth, dReal fDepth, |
---|
57 | dReal fScale, dReal fOffset, |
---|
58 | dReal fThickness, int bWrapMode ); |
---|
59 | |
---|
60 | void ComputeHeightBounds(); |
---|
61 | |
---|
62 | bool IsOnHeightfield ( const dReal * const CellOrigin, const dReal * const pos, const bool isABC) const; |
---|
63 | bool IsOnHeightfield2 ( const dReal * const CellOrigin, const dReal * const pos, const bool isABC) const; |
---|
64 | |
---|
65 | dReal GetHeight(int x, int z); |
---|
66 | dReal GetHeight(dReal x, dReal z); |
---|
67 | |
---|
68 | }; |
---|
69 | |
---|
70 | class HeightFieldVertex; |
---|
71 | class HeightFieldEdge; |
---|
72 | class HeightFieldTriangle; |
---|
73 | |
---|
74 | class HeightFieldVertex |
---|
75 | { |
---|
76 | public: |
---|
77 | HeightFieldVertex(){}; |
---|
78 | |
---|
79 | dVector3 vertex; |
---|
80 | bool state; |
---|
81 | }; |
---|
82 | |
---|
83 | class HeightFieldEdge |
---|
84 | { |
---|
85 | public: |
---|
86 | HeightFieldEdge(){}; |
---|
87 | |
---|
88 | HeightFieldVertex *vertices[2]; |
---|
89 | }; |
---|
90 | // |
---|
91 | // HeightFieldTriangle |
---|
92 | // |
---|
93 | // HeightFieldTriangle composing heightfield mesh |
---|
94 | // |
---|
95 | class HeightFieldTriangle |
---|
96 | { |
---|
97 | public: |
---|
98 | HeightFieldTriangle(){}; |
---|
99 | |
---|
100 | inline void setMinMax() |
---|
101 | { |
---|
102 | maxAAAB = vertices[0]->vertex[1] > vertices[1]->vertex[1] ? vertices[0]->vertex[1] : vertices[1]->vertex[1]; |
---|
103 | maxAAAB = vertices[2]->vertex[1] > maxAAAB ? vertices[2]->vertex[1] : maxAAAB; |
---|
104 | }; |
---|
105 | |
---|
106 | HeightFieldVertex *vertices[3]; |
---|
107 | dReal planeDef[4]; |
---|
108 | dReal maxAAAB; |
---|
109 | |
---|
110 | bool isUp; |
---|
111 | bool state; |
---|
112 | }; |
---|
113 | // |
---|
114 | // HeightFieldTriangle |
---|
115 | // |
---|
116 | // HeightFieldPlane composing heightfield mesh |
---|
117 | // |
---|
118 | class HeightFieldPlane |
---|
119 | { |
---|
120 | public: |
---|
121 | HeightFieldPlane(): |
---|
122 | trianglelist(0), |
---|
123 | trianglelistReservedSize(0), |
---|
124 | trianglelistCurrentSize(0) |
---|
125 | { |
---|
126 | |
---|
127 | }; |
---|
128 | ~HeightFieldPlane() |
---|
129 | { |
---|
130 | delete [] trianglelist; |
---|
131 | }; |
---|
132 | |
---|
133 | inline void setMinMax() |
---|
134 | { |
---|
135 | const size_t asize = trianglelistCurrentSize; |
---|
136 | if (asize > 0) |
---|
137 | { |
---|
138 | maxAAAB = trianglelist[0]->maxAAAB; |
---|
139 | for (size_t k = 1; asize > k; k++) |
---|
140 | { |
---|
141 | if (trianglelist[k]->maxAAAB > maxAAAB) |
---|
142 | maxAAAB = trianglelist[k]->maxAAAB; |
---|
143 | } |
---|
144 | } |
---|
145 | }; |
---|
146 | |
---|
147 | void resetTriangleListSize(const size_t newSize) |
---|
148 | { |
---|
149 | if (trianglelistReservedSize < newSize) |
---|
150 | { |
---|
151 | delete [] trianglelist; |
---|
152 | trianglelistReservedSize = newSize; |
---|
153 | trianglelist = new HeightFieldTriangle *[newSize]; |
---|
154 | } |
---|
155 | trianglelistCurrentSize = 0; |
---|
156 | } |
---|
157 | |
---|
158 | void addTriangle(HeightFieldTriangle *tri) |
---|
159 | { |
---|
160 | dIASSERT(trianglelistCurrentSize < trianglelistReservedSize); |
---|
161 | |
---|
162 | trianglelist[trianglelistCurrentSize++] = tri; |
---|
163 | } |
---|
164 | |
---|
165 | HeightFieldTriangle **trianglelist; |
---|
166 | size_t trianglelistReservedSize; |
---|
167 | size_t trianglelistCurrentSize; |
---|
168 | |
---|
169 | dReal maxAAAB; |
---|
170 | dReal planeDef[4]; |
---|
171 | }; |
---|
172 | // |
---|
173 | // dxHeightfield |
---|
174 | // |
---|
175 | // Heightfield geom structure |
---|
176 | // |
---|
177 | struct dxHeightfield : public dxGeom |
---|
178 | { |
---|
179 | dxHeightfieldData* m_p_data; |
---|
180 | |
---|
181 | dxHeightfield( dSpaceID space, dHeightfieldDataID data, int bPlaceable ); |
---|
182 | ~dxHeightfield(); |
---|
183 | |
---|
184 | void computeAABB(); |
---|
185 | |
---|
186 | int dCollideHeightfieldZone( const int minX, const int maxX, const int minZ, const int maxZ, |
---|
187 | dxGeom *o2, const int numMaxContacts, |
---|
188 | int flags, dContactGeom *contact, int skip ); |
---|
189 | |
---|
190 | enum |
---|
191 | { |
---|
192 | TEMP_PLANE_BUFFER_ELEMENT_COUNT_ALIGNMENT = 4, |
---|
193 | TEMP_HEIGHT_BUFFER_ELEMENT_COUNT_ALIGNMENT_X = 4, |
---|
194 | TEMP_HEIGHT_BUFFER_ELEMENT_COUNT_ALIGNMENT_Z = 4, |
---|
195 | TEMP_TRIANGLE_BUFFER_ELEMENT_COUNT_ALIGNMENT = 1, // Triangles are easy to reallocate and hard to predict |
---|
196 | }; |
---|
197 | |
---|
198 | static inline size_t AlignBufferSize(size_t value, size_t alignment) { dIASSERT((alignment & (alignment - 1)) == 0); return (value + (alignment - 1)) & ~(alignment - 1); } |
---|
199 | |
---|
200 | void allocateTriangleBuffer(size_t numTri); |
---|
201 | void resetTriangleBuffer(); |
---|
202 | void allocatePlaneBuffer(size_t numTri); |
---|
203 | void resetPlaneBuffer(); |
---|
204 | void allocateHeightBuffer(size_t numX, size_t numZ); |
---|
205 | void resetHeightBuffer(); |
---|
206 | |
---|
207 | void sortPlanes(const size_t numPlanes); |
---|
208 | |
---|
209 | HeightFieldPlane **tempPlaneBuffer; |
---|
210 | HeightFieldPlane *tempPlaneInstances; |
---|
211 | size_t tempPlaneBufferSize; |
---|
212 | |
---|
213 | HeightFieldTriangle *tempTriangleBuffer; |
---|
214 | size_t tempTriangleBufferSize; |
---|
215 | |
---|
216 | HeightFieldVertex **tempHeightBuffer; |
---|
217 | HeightFieldVertex *tempHeightInstances; |
---|
218 | size_t tempHeightBufferSizeX; |
---|
219 | size_t tempHeightBufferSizeZ; |
---|
220 | |
---|
221 | }; |
---|
222 | |
---|
223 | |
---|
224 | //------------------------------------------------------------------------------ |
---|
225 | #endif //_DHEIGHTFIELD_H_ |
---|