btHeightfieldTerrainShape.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #ifndef BT_HEIGHTFIELD_TERRAIN_SHAPE_H
  14. #define BT_HEIGHTFIELD_TERRAIN_SHAPE_H
  15. #include "btConcaveShape.h"
  16. ///btHeightfieldTerrainShape simulates a 2D heightfield terrain
  17. /**
  18. The caller is responsible for maintaining the heightfield array; this
  19. class does not make a copy.
  20. The heightfield can be dynamic so long as the min/max height values
  21. capture the extremes (heights must always be in that range).
  22. The local origin of the heightfield is assumed to be the exact
  23. center (as determined by width and length and height, with each
  24. axis multiplied by the localScaling).
  25. \b NOTE: be careful with coordinates. If you have a heightfield with a local
  26. min height of -100m, and a max height of +500m, you may be tempted to place it
  27. at the origin (0,0) and expect the heights in world coordinates to be
  28. -100 to +500 meters.
  29. Actually, the heights will be -300 to +300m, because bullet will re-center
  30. the heightfield based on its AABB (which is determined by the min/max
  31. heights). So keep in mind that once you create a btHeightfieldTerrainShape
  32. object, the heights will be adjusted relative to the center of the AABB. This
  33. is different to the behavior of many rendering engines, but is useful for
  34. physics engines.
  35. Most (but not all) rendering and heightfield libraries assume upAxis = 1
  36. (that is, the y-axis is "up"). This class allows any of the 3 coordinates
  37. to be "up". Make sure your choice of axis is consistent with your rendering
  38. system.
  39. The heightfield heights are determined from the data type used for the
  40. heightfieldData array.
  41. - PHY_UCHAR: height at a point is the uchar value at the
  42. grid point, multipled by heightScale. uchar isn't recommended
  43. because of its inability to deal with negative values, and
  44. low resolution (8-bit).
  45. - PHY_SHORT: height at a point is the short int value at that grid
  46. point, multipled by heightScale.
  47. - PHY_FLOAT: height at a point is the float value at that grid
  48. point. heightScale is ignored when using the float heightfield
  49. data type.
  50. Whatever the caller specifies as minHeight and maxHeight will be honored.
  51. The class will not inspect the heightfield to discover the actual minimum
  52. or maximum heights. These values are used to determine the heightfield's
  53. axis-aligned bounding box, multiplied by localScaling.
  54. For usage and testing see the TerrainDemo.
  55. */
  56. ATTRIBUTE_ALIGNED16(class) btHeightfieldTerrainShape : public btConcaveShape
  57. {
  58. protected:
  59. btVector3 m_localAabbMin;
  60. btVector3 m_localAabbMax;
  61. btVector3 m_localOrigin;
  62. ///terrain data
  63. int m_heightStickWidth;
  64. int m_heightStickLength;
  65. btScalar m_minHeight;
  66. btScalar m_maxHeight;
  67. btScalar m_width;
  68. btScalar m_length;
  69. btScalar m_heightScale;
  70. union
  71. {
  72. const unsigned char* m_heightfieldDataUnsignedChar;
  73. const short* m_heightfieldDataShort;
  74. const btScalar* m_heightfieldDataFloat;
  75. const void* m_heightfieldDataUnknown;
  76. };
  77. PHY_ScalarType m_heightDataType;
  78. bool m_flipQuadEdges;
  79. bool m_useDiamondSubdivision;
  80. bool m_useZigzagSubdivision;
  81. int m_upAxis;
  82. btVector3 m_localScaling;
  83. virtual btScalar getRawHeightFieldValue(int x,int y) const;
  84. void quantizeWithClamp(int* out, const btVector3& point,int isMax) const;
  85. void getVertex(int x,int y,btVector3& vertex) const;
  86. /// protected initialization
  87. /**
  88. Handles the work of constructors so that public constructors can be
  89. backwards-compatible without a lot of copy/paste.
  90. */
  91. void initialize(int heightStickWidth, int heightStickLength,
  92. const void* heightfieldData, btScalar heightScale,
  93. btScalar minHeight, btScalar maxHeight, int upAxis,
  94. PHY_ScalarType heightDataType, bool flipQuadEdges);
  95. public:
  96. BT_DECLARE_ALIGNED_ALLOCATOR();
  97. /// preferred constructor
  98. /**
  99. This constructor supports a range of heightfield
  100. data types, and allows for a non-zero minimum height value.
  101. heightScale is needed for any integer-based heightfield data types.
  102. */
  103. btHeightfieldTerrainShape(int heightStickWidth,int heightStickLength,
  104. const void* heightfieldData, btScalar heightScale,
  105. btScalar minHeight, btScalar maxHeight,
  106. int upAxis, PHY_ScalarType heightDataType,
  107. bool flipQuadEdges);
  108. /// legacy constructor
  109. /**
  110. The legacy constructor assumes the heightfield has a minimum height
  111. of zero. Only unsigned char or floats are supported. For legacy
  112. compatibility reasons, heightScale is calculated as maxHeight / 65535
  113. (and is only used when useFloatData = false).
  114. */
  115. btHeightfieldTerrainShape(int heightStickWidth,int heightStickLength,const void* heightfieldData, btScalar maxHeight,int upAxis,bool useFloatData,bool flipQuadEdges);
  116. virtual ~btHeightfieldTerrainShape();
  117. void setUseDiamondSubdivision(bool useDiamondSubdivision=true) { m_useDiamondSubdivision = useDiamondSubdivision;}
  118. ///could help compatibility with Ogre heightfields. See https://code.google.com/p/bullet/issues/detail?id=625
  119. void setUseZigzagSubdivision(bool useZigzagSubdivision=true) { m_useZigzagSubdivision = useZigzagSubdivision;}
  120. virtual void getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const;
  121. virtual void processAllTriangles(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const;
  122. virtual void calculateLocalInertia(btScalar mass,btVector3& inertia) const;
  123. virtual void setLocalScaling(const btVector3& scaling);
  124. virtual const btVector3& getLocalScaling() const;
  125. //debugging
  126. virtual const char* getName()const {return "HEIGHTFIELD";}
  127. };
  128. #endif //BT_HEIGHTFIELD_TERRAIN_SHAPE_H