[1963] | 1 | /* |
---|
| 2 | Bullet Continuous Collision Detection and Physics Library |
---|
| 3 | Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/ |
---|
| 4 | |
---|
| 5 | This software is provided 'as-is', without any express or implied warranty. |
---|
| 6 | In no event will the authors be held liable for any damages arising from the use of this software. |
---|
| 7 | Permission is granted to anyone to use this software for any purpose, |
---|
| 8 | including commercial applications, and to alter it and redistribute it freely, |
---|
| 9 | subject to the following restrictions: |
---|
| 10 | |
---|
| 11 | 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. |
---|
| 12 | 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. |
---|
| 13 | 3. This notice may not be removed or altered from any source distribution. |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | #ifndef HEIGHTFIELD_TERRAIN_SHAPE_H |
---|
| 17 | #define HEIGHTFIELD_TERRAIN_SHAPE_H |
---|
| 18 | |
---|
| 19 | #include "btConcaveShape.h" |
---|
| 20 | |
---|
[2430] | 21 | ///btHeightfieldTerrainShape simulates a 2D heightfield terrain |
---|
| 22 | /** |
---|
| 23 | The caller is responsible for maintaining the heightfield array; this |
---|
| 24 | class does not make a copy. |
---|
| 25 | |
---|
| 26 | The heightfield can be dynamic so long as the min/max height values |
---|
| 27 | capture the extremes (heights must always be in that range). |
---|
| 28 | |
---|
| 29 | The local origin of the heightfield is assumed to be the exact |
---|
| 30 | center (as determined by width and length and height, with each |
---|
| 31 | axis multiplied by the localScaling). |
---|
| 32 | |
---|
| 33 | Most (but not all) rendering and heightfield libraries assume upAxis = 1 |
---|
| 34 | (that is, the y-axis is "up"). This class allows any of the 3 coordinates |
---|
| 35 | to be "up". Make sure your choice of axis is consistent with your rendering |
---|
| 36 | system. |
---|
| 37 | |
---|
| 38 | The heightfield heights are determined from the data type used for the |
---|
| 39 | heightfieldData array. |
---|
| 40 | |
---|
| 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 | |
---|
| 46 | - PHY_SHORT: height at a point is the short int value at that grid |
---|
| 47 | point, multipled by heightScale. |
---|
| 48 | |
---|
| 49 | - PHY_FLOAT: height at a point is the float value at that grid |
---|
| 50 | point. heightScale is ignored when using the float heightfield |
---|
| 51 | data type. |
---|
| 52 | |
---|
| 53 | Whatever the caller specifies as minHeight and maxHeight will be honored. |
---|
| 54 | The class will not inspect the heightfield to discover the actual minimum |
---|
| 55 | or maximum heights. These values are used to determine the heightfield's |
---|
| 56 | axis-aligned bounding box, multiplied by localScaling. |
---|
| 57 | |
---|
| 58 | For usage and testing see the TerrainDemo. |
---|
| 59 | */ |
---|
[1963] | 60 | class btHeightfieldTerrainShape : public btConcaveShape |
---|
| 61 | { |
---|
| 62 | protected: |
---|
| 63 | btVector3 m_localAabbMin; |
---|
| 64 | btVector3 m_localAabbMax; |
---|
[2430] | 65 | btVector3 m_localOrigin; |
---|
| 66 | |
---|
[1963] | 67 | ///terrain data |
---|
| 68 | int m_heightStickWidth; |
---|
| 69 | int m_heightStickLength; |
---|
[2430] | 70 | btScalar m_minHeight; |
---|
[1963] | 71 | btScalar m_maxHeight; |
---|
| 72 | btScalar m_width; |
---|
| 73 | btScalar m_length; |
---|
[2430] | 74 | btScalar m_heightScale; |
---|
[1963] | 75 | union |
---|
| 76 | { |
---|
| 77 | unsigned char* m_heightfieldDataUnsignedChar; |
---|
[2430] | 78 | short* m_heightfieldDataShort; |
---|
[1963] | 79 | btScalar* m_heightfieldDataFloat; |
---|
| 80 | void* m_heightfieldDataUnknown; |
---|
| 81 | }; |
---|
[2430] | 82 | |
---|
| 83 | PHY_ScalarType m_heightDataType; |
---|
[1963] | 84 | bool m_flipQuadEdges; |
---|
| 85 | bool m_useDiamondSubdivision; |
---|
| 86 | |
---|
| 87 | int m_upAxis; |
---|
| 88 | |
---|
| 89 | btVector3 m_localScaling; |
---|
| 90 | |
---|
| 91 | virtual btScalar getHeightFieldValue(int x,int y) const; |
---|
| 92 | void quantizeWithClamp(int* out, const btVector3& point,int isMax) const; |
---|
| 93 | void getVertex(int x,int y,btVector3& vertex) const; |
---|
| 94 | |
---|
| 95 | |
---|
[2430] | 96 | |
---|
| 97 | /// protected initialization |
---|
| 98 | /** |
---|
| 99 | Handles the work of constructors so that public constructors can be |
---|
| 100 | backwards-compatible without a lot of copy/paste. |
---|
| 101 | */ |
---|
| 102 | void initialize(int heightStickWidth, int heightStickLength, |
---|
| 103 | void* heightfieldData, btScalar heightScale, |
---|
| 104 | btScalar minHeight, btScalar maxHeight, int upAxis, |
---|
| 105 | PHY_ScalarType heightDataType, bool flipQuadEdges); |
---|
| 106 | |
---|
[1963] | 107 | public: |
---|
[2430] | 108 | /// preferred constructor |
---|
| 109 | /** |
---|
| 110 | This constructor supports a range of heightfield |
---|
| 111 | data types, and allows for a non-zero minimum height value. |
---|
| 112 | heightScale is needed for any integer-based heightfield data types. |
---|
| 113 | */ |
---|
| 114 | btHeightfieldTerrainShape(int heightStickWidth,int heightStickLength, |
---|
| 115 | void* heightfieldData, btScalar heightScale, |
---|
| 116 | btScalar minHeight, btScalar maxHeight, |
---|
| 117 | int upAxis, PHY_ScalarType heightDataType, |
---|
| 118 | bool flipQuadEdges); |
---|
[1963] | 119 | |
---|
[2430] | 120 | /// legacy constructor |
---|
| 121 | /** |
---|
| 122 | The legacy constructor assumes the heightfield has a minimum height |
---|
| 123 | of zero. Only unsigned char or floats are supported. For legacy |
---|
| 124 | compatibility reasons, heightScale is calculated as maxHeight / 65535 |
---|
| 125 | (and is only used when useFloatData = false). |
---|
| 126 | */ |
---|
| 127 | btHeightfieldTerrainShape(int heightStickWidth,int heightStickLength,void* heightfieldData, btScalar maxHeight,int upAxis,bool useFloatData,bool flipQuadEdges); |
---|
| 128 | |
---|
[1963] | 129 | virtual ~btHeightfieldTerrainShape(); |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | void setUseDiamondSubdivision(bool useDiamondSubdivision=true) { m_useDiamondSubdivision = useDiamondSubdivision;} |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | virtual void getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const; |
---|
| 136 | |
---|
| 137 | virtual void processAllTriangles(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const; |
---|
| 138 | |
---|
| 139 | virtual void calculateLocalInertia(btScalar mass,btVector3& inertia) const; |
---|
| 140 | |
---|
| 141 | virtual void setLocalScaling(const btVector3& scaling); |
---|
| 142 | |
---|
| 143 | virtual const btVector3& getLocalScaling() const; |
---|
| 144 | |
---|
| 145 | //debugging |
---|
| 146 | virtual const char* getName()const {return "HEIGHTFIELD";} |
---|
| 147 | |
---|
| 148 | }; |
---|
| 149 | |
---|
| 150 | #endif //HEIGHTFIELD_TERRAIN_SHAPE_H |
---|