btHeightfieldTerrainShape.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #include "btHeightfieldTerrainShape.h"
  14. #include "bullet/LinearMath/btTransformUtil.h"
  15. btHeightfieldTerrainShape::btHeightfieldTerrainShape
  16. (
  17. int heightStickWidth, int heightStickLength, const void* heightfieldData,
  18. btScalar heightScale, btScalar minHeight, btScalar maxHeight,int upAxis,
  19. PHY_ScalarType hdt, bool flipQuadEdges
  20. )
  21. {
  22. initialize(heightStickWidth, heightStickLength, heightfieldData,
  23. heightScale, minHeight, maxHeight, upAxis, hdt,
  24. flipQuadEdges);
  25. }
  26. btHeightfieldTerrainShape::btHeightfieldTerrainShape(int heightStickWidth, int heightStickLength,const void* heightfieldData,btScalar maxHeight,int upAxis,bool useFloatData,bool flipQuadEdges)
  27. {
  28. // legacy constructor: support only float or unsigned char,
  29. // and min height is zero
  30. PHY_ScalarType hdt = (useFloatData) ? PHY_FLOAT : PHY_UCHAR;
  31. btScalar minHeight = 0.0f;
  32. // previously, height = uchar * maxHeight / 65535.
  33. // So to preserve legacy behavior, heightScale = maxHeight / 65535
  34. btScalar heightScale = maxHeight / 65535;
  35. initialize(heightStickWidth, heightStickLength, heightfieldData,
  36. heightScale, minHeight, maxHeight, upAxis, hdt,
  37. flipQuadEdges);
  38. }
  39. void btHeightfieldTerrainShape::initialize
  40. (
  41. int heightStickWidth, int heightStickLength, const void* heightfieldData,
  42. btScalar heightScale, btScalar minHeight, btScalar maxHeight, int upAxis,
  43. PHY_ScalarType hdt, bool flipQuadEdges
  44. )
  45. {
  46. // validation
  47. btAssert(heightStickWidth > 1 && "bad width");
  48. btAssert(heightStickLength > 1 && "bad length");
  49. btAssert(heightfieldData && "null heightfield data");
  50. // btAssert(heightScale) -- do we care? Trust caller here
  51. btAssert(minHeight <= maxHeight && "bad min/max height");
  52. btAssert(upAxis >= 0 && upAxis < 3 &&
  53. "bad upAxis--should be in range [0,2]");
  54. btAssert(hdt != PHY_UCHAR || hdt != PHY_FLOAT || hdt != PHY_SHORT &&
  55. "Bad height data type enum");
  56. // initialize member variables
  57. m_shapeType = TERRAIN_SHAPE_PROXYTYPE;
  58. m_heightStickWidth = heightStickWidth;
  59. m_heightStickLength = heightStickLength;
  60. m_minHeight = minHeight;
  61. m_maxHeight = maxHeight;
  62. m_width = (btScalar) (heightStickWidth - 1);
  63. m_length = (btScalar) (heightStickLength - 1);
  64. m_heightScale = heightScale;
  65. m_heightfieldDataUnknown = heightfieldData;
  66. m_heightDataType = hdt;
  67. m_flipQuadEdges = flipQuadEdges;
  68. m_useDiamondSubdivision = false;
  69. m_useZigzagSubdivision = false;
  70. m_upAxis = upAxis;
  71. m_localScaling.setValue(btScalar(1.), btScalar(1.), btScalar(1.));
  72. // determine min/max axis-aligned bounding box (aabb) values
  73. switch (m_upAxis)
  74. {
  75. case 0:
  76. {
  77. m_localAabbMin.setValue(m_minHeight, 0, 0);
  78. m_localAabbMax.setValue(m_maxHeight, m_width, m_length);
  79. break;
  80. }
  81. case 1:
  82. {
  83. m_localAabbMin.setValue(0, m_minHeight, 0);
  84. m_localAabbMax.setValue(m_width, m_maxHeight, m_length);
  85. break;
  86. };
  87. case 2:
  88. {
  89. m_localAabbMin.setValue(0, 0, m_minHeight);
  90. m_localAabbMax.setValue(m_width, m_length, m_maxHeight);
  91. break;
  92. }
  93. default:
  94. {
  95. //need to get valid m_upAxis
  96. btAssert(0 && "Bad m_upAxis");
  97. }
  98. }
  99. // remember origin (defined as exact middle of aabb)
  100. m_localOrigin = btScalar(0.5) * (m_localAabbMin + m_localAabbMax);
  101. }
  102. btHeightfieldTerrainShape::~btHeightfieldTerrainShape()
  103. {
  104. }
  105. void btHeightfieldTerrainShape::getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const
  106. {
  107. btVector3 halfExtents = (m_localAabbMax-m_localAabbMin)* m_localScaling * btScalar(0.5);
  108. btVector3 localOrigin(0, 0, 0);
  109. localOrigin[m_upAxis] = (m_minHeight + m_maxHeight) * btScalar(0.5);
  110. localOrigin *= m_localScaling;
  111. btMatrix3x3 abs_b = t.getBasis().absolute();
  112. btVector3 center = t.getOrigin();
  113. btVector3 extent = halfExtents.dot3(abs_b[0], abs_b[1], abs_b[2]);
  114. extent += btVector3(getMargin(),getMargin(),getMargin());
  115. aabbMin = center - extent;
  116. aabbMax = center + extent;
  117. }
  118. /// This returns the "raw" (user's initial) height, not the actual height.
  119. /// The actual height needs to be adjusted to be relative to the center
  120. /// of the heightfield's AABB.
  121. btScalar
  122. btHeightfieldTerrainShape::getRawHeightFieldValue(int x,int y) const
  123. {
  124. btScalar val = 0.f;
  125. switch (m_heightDataType)
  126. {
  127. case PHY_FLOAT:
  128. {
  129. val = m_heightfieldDataFloat[(y*m_heightStickWidth)+x];
  130. break;
  131. }
  132. case PHY_UCHAR:
  133. {
  134. unsigned char heightFieldValue = m_heightfieldDataUnsignedChar[(y*m_heightStickWidth)+x];
  135. val = heightFieldValue * m_heightScale;
  136. break;
  137. }
  138. case PHY_SHORT:
  139. {
  140. short hfValue = m_heightfieldDataShort[(y * m_heightStickWidth) + x];
  141. val = hfValue * m_heightScale;
  142. break;
  143. }
  144. default:
  145. {
  146. btAssert(!"Bad m_heightDataType");
  147. }
  148. }
  149. return val;
  150. }
  151. /// this returns the vertex in bullet-local coordinates
  152. void btHeightfieldTerrainShape::getVertex(int x,int y,btVector3& vertex) const
  153. {
  154. btAssert(x>=0);
  155. btAssert(y>=0);
  156. btAssert(x<m_heightStickWidth);
  157. btAssert(y<m_heightStickLength);
  158. btScalar height = getRawHeightFieldValue(x,y);
  159. switch (m_upAxis)
  160. {
  161. case 0:
  162. {
  163. vertex.setValue(
  164. height - m_localOrigin.getX(),
  165. (-m_width/btScalar(2.0)) + x,
  166. (-m_length/btScalar(2.0) ) + y
  167. );
  168. break;
  169. }
  170. case 1:
  171. {
  172. vertex.setValue(
  173. (-m_width/btScalar(2.0)) + x,
  174. height - m_localOrigin.getY(),
  175. (-m_length/btScalar(2.0)) + y
  176. );
  177. break;
  178. };
  179. case 2:
  180. {
  181. vertex.setValue(
  182. (-m_width/btScalar(2.0)) + x,
  183. (-m_length/btScalar(2.0)) + y,
  184. height - m_localOrigin.getZ()
  185. );
  186. break;
  187. }
  188. default:
  189. {
  190. //need to get valid m_upAxis
  191. btAssert(0);
  192. }
  193. }
  194. vertex*=m_localScaling;
  195. }
  196. static inline int
  197. getQuantized
  198. (
  199. btScalar x
  200. )
  201. {
  202. if (x < 0.0) {
  203. return (int) (x - 0.5);
  204. }
  205. return (int) (x + 0.5);
  206. }
  207. /// given input vector, return quantized version
  208. /**
  209. This routine is basically determining the gridpoint indices for a given
  210. input vector, answering the question: "which gridpoint is closest to the
  211. provided point?".
  212. "with clamp" means that we restrict the point to be in the heightfield's
  213. axis-aligned bounding box.
  214. */
  215. void btHeightfieldTerrainShape::quantizeWithClamp(int* out, const btVector3& point,int /*isMax*/) const
  216. {
  217. btVector3 clampedPoint(point);
  218. clampedPoint.setMax(m_localAabbMin);
  219. clampedPoint.setMin(m_localAabbMax);
  220. out[0] = getQuantized(clampedPoint.getX());
  221. out[1] = getQuantized(clampedPoint.getY());
  222. out[2] = getQuantized(clampedPoint.getZ());
  223. }
  224. /// process all triangles within the provided axis-aligned bounding box
  225. /**
  226. basic algorithm:
  227. - convert input aabb to local coordinates (scale down and shift for local origin)
  228. - convert input aabb to a range of heightfield grid points (quantize)
  229. - iterate over all triangles in that subset of the grid
  230. */
  231. void btHeightfieldTerrainShape::processAllTriangles(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const
  232. {
  233. // scale down the input aabb's so they are in local (non-scaled) coordinates
  234. btVector3 localAabbMin = aabbMin*btVector3(1.f/m_localScaling[0],1.f/m_localScaling[1],1.f/m_localScaling[2]);
  235. btVector3 localAabbMax = aabbMax*btVector3(1.f/m_localScaling[0],1.f/m_localScaling[1],1.f/m_localScaling[2]);
  236. // account for local origin
  237. localAabbMin += m_localOrigin;
  238. localAabbMax += m_localOrigin;
  239. //quantize the aabbMin and aabbMax, and adjust the start/end ranges
  240. int quantizedAabbMin[3];
  241. int quantizedAabbMax[3];
  242. quantizeWithClamp(quantizedAabbMin, localAabbMin,0);
  243. quantizeWithClamp(quantizedAabbMax, localAabbMax,1);
  244. // expand the min/max quantized values
  245. // this is to catch the case where the input aabb falls between grid points!
  246. for (int i = 0; i < 3; ++i) {
  247. quantizedAabbMin[i]--;
  248. quantizedAabbMax[i]++;
  249. }
  250. int startX=0;
  251. int endX=m_heightStickWidth-1;
  252. int startJ=0;
  253. int endJ=m_heightStickLength-1;
  254. switch (m_upAxis)
  255. {
  256. case 0:
  257. {
  258. if (quantizedAabbMin[1]>startX)
  259. startX = quantizedAabbMin[1];
  260. if (quantizedAabbMax[1]<endX)
  261. endX = quantizedAabbMax[1];
  262. if (quantizedAabbMin[2]>startJ)
  263. startJ = quantizedAabbMin[2];
  264. if (quantizedAabbMax[2]<endJ)
  265. endJ = quantizedAabbMax[2];
  266. break;
  267. }
  268. case 1:
  269. {
  270. if (quantizedAabbMin[0]>startX)
  271. startX = quantizedAabbMin[0];
  272. if (quantizedAabbMax[0]<endX)
  273. endX = quantizedAabbMax[0];
  274. if (quantizedAabbMin[2]>startJ)
  275. startJ = quantizedAabbMin[2];
  276. if (quantizedAabbMax[2]<endJ)
  277. endJ = quantizedAabbMax[2];
  278. break;
  279. };
  280. case 2:
  281. {
  282. if (quantizedAabbMin[0]>startX)
  283. startX = quantizedAabbMin[0];
  284. if (quantizedAabbMax[0]<endX)
  285. endX = quantizedAabbMax[0];
  286. if (quantizedAabbMin[1]>startJ)
  287. startJ = quantizedAabbMin[1];
  288. if (quantizedAabbMax[1]<endJ)
  289. endJ = quantizedAabbMax[1];
  290. break;
  291. }
  292. default:
  293. {
  294. //need to get valid m_upAxis
  295. btAssert(0);
  296. }
  297. }
  298. for(int j=startJ; j<endJ; j++)
  299. {
  300. for(int x=startX; x<endX; x++)
  301. {
  302. btVector3 vertices[3];
  303. if (m_flipQuadEdges || (m_useDiamondSubdivision && !((j+x) & 1))|| (m_useZigzagSubdivision && !(j & 1)))
  304. {
  305. //first triangle
  306. getVertex(x,j,vertices[0]);
  307. getVertex(x+1,j,vertices[1]);
  308. getVertex(x+1,j+1,vertices[2]);
  309. callback->processTriangle(vertices,x,j);
  310. //second triangle
  311. // getVertex(x,j,vertices[0]);//already got this vertex before, thanks to Danny Chapman
  312. getVertex(x+1,j+1,vertices[1]);
  313. getVertex(x,j+1,vertices[2]);
  314. callback->processTriangle(vertices,x,j);
  315. } else
  316. {
  317. //first triangle
  318. getVertex(x,j,vertices[0]);
  319. getVertex(x,j+1,vertices[1]);
  320. getVertex(x+1,j,vertices[2]);
  321. callback->processTriangle(vertices,x,j);
  322. //second triangle
  323. getVertex(x+1,j,vertices[0]);
  324. //getVertex(x,j+1,vertices[1]);
  325. getVertex(x+1,j+1,vertices[2]);
  326. callback->processTriangle(vertices,x,j);
  327. }
  328. }
  329. }
  330. }
  331. void btHeightfieldTerrainShape::calculateLocalInertia(btScalar ,btVector3& inertia) const
  332. {
  333. //moving concave objects not supported
  334. inertia.setValue(btScalar(0.),btScalar(0.),btScalar(0.));
  335. }
  336. void btHeightfieldTerrainShape::setLocalScaling(const btVector3& scaling)
  337. {
  338. m_localScaling = scaling;
  339. }
  340. const btVector3& btHeightfieldTerrainShape::getLocalScaling() const
  341. {
  342. return m_localScaling;
  343. }