| 1 | #ifndef _OGREBULLET_HEIGHTMAP_ | 
|---|
| 2 | #define _OGREBULLET_HEIGHTMAP_ | 
|---|
| 3 |  | 
|---|
| 4 |  | 
|---|
| 5 | #include "OgreBulletCollisionsPrerequisites.h" | 
|---|
| 6 |  | 
|---|
| 7 | #include "OgreBulletCollisionsShape.h" | 
|---|
| 8 | #include "Debug/OgreBulletCollisionsDebugLines.h" | 
|---|
| 9 |  | 
|---|
| 10 | #include "BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h" | 
|---|
| 11 | #include "BulletCollision/CollisionShapes/btTriangleCallback.h" | 
|---|
| 12 | #include "BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h" | 
|---|
| 13 |  | 
|---|
| 14 | namespace OgreBulletCollisions | 
|---|
| 15 | { | 
|---|
| 16 |          | 
|---|
| 17 |         class DebugHelper : public btIDebugDraw | 
|---|
| 18 |         { | 
|---|
| 19 |         public: | 
|---|
| 20 |  | 
|---|
| 21 |                 DebugHelper(DebugLines* pLines) : m_pLines(pLines) {} | 
|---|
| 22 |                 ~DebugHelper () {} | 
|---|
| 23 |  | 
|---|
| 24 |                 void    drawLine(const btVector3& from,const btVector3& to,const btVector3& color) | 
|---|
| 25 |                 { | 
|---|
| 26 |                         Ogre::Vector3 a(from.x(), from.y(), from.z()); | 
|---|
| 27 |                         Ogre::Vector3 b(to.x(), to.y(), to.z()); | 
|---|
| 28 |                         m_pLines->addLine(a, b); | 
|---|
| 29 |                 } | 
|---|
| 30 |                 void    draw3dText(const btVector3 &,const char *) | 
|---|
| 31 |                 { | 
|---|
| 32 |  | 
|---|
| 33 |                 } | 
|---|
| 34 |  | 
|---|
| 35 |                 void    drawContactPoint(const btVector3& PointOnB,const btVector3& normalOnB,btScalar distance,int lifeTime,const btVector3& color) | 
|---|
| 36 |                 { | 
|---|
| 37 |                 } | 
|---|
| 38 |  | 
|---|
| 39 |                 void    reportErrorWarning(const char* warningString) | 
|---|
| 40 |                 { | 
|---|
| 41 |                 } | 
|---|
| 42 |  | 
|---|
| 43 |                 void    setDebugMode(int debugMode) | 
|---|
| 44 |                 { | 
|---|
| 45 |                 } | 
|---|
| 46 |  | 
|---|
| 47 |                 int             getDebugMode() const | 
|---|
| 48 |                 { | 
|---|
| 49 |                         return -1; | 
|---|
| 50 |                 } | 
|---|
| 51 |  | 
|---|
| 52 |         private: | 
|---|
| 53 |                 DebugLines* m_pLines; | 
|---|
| 54 |         }; | 
|---|
| 55 |  | 
|---|
| 56 |         class DebugTriangleDrawCallback : public btTriangleCallback | 
|---|
| 57 |         { | 
|---|
| 58 |         private: | 
|---|
| 59 |  | 
|---|
| 60 |                 DebugHelper     *mDebugHelper; | 
|---|
| 61 |                 btTransform     mTransform; | 
|---|
| 62 |                 btVector3               mColor; | 
|---|
| 63 |  | 
|---|
| 64 |         public: | 
|---|
| 65 |  | 
|---|
| 66 |                 DebugTriangleDrawCallback(DebugHelper *db, btTransform &bt, const btVector3& color) : | 
|---|
| 67 |                   btTriangleCallback(), | 
|---|
| 68 |                         mDebugHelper(db), | 
|---|
| 69 |                         mTransform(bt), | 
|---|
| 70 |                         mColor(color) | 
|---|
| 71 |                 { | 
|---|
| 72 |  | 
|---|
| 73 |                 } | 
|---|
| 74 |  | 
|---|
| 75 |                 void processTriangle(btVector3* triangle, int partId, int triangleIndex) | 
|---|
| 76 |                 { | 
|---|
| 77 |                         mDebugHelper->drawLine (*triangle,     *(triangle+1), mColor); | 
|---|
| 78 |                         mDebugHelper->drawLine (*(triangle+1), *(triangle+2), mColor); | 
|---|
| 79 |                         mDebugHelper->drawLine (*(triangle+2), *triangle,     mColor); | 
|---|
| 80 |                 }                                                 | 
|---|
| 81 |         }; | 
|---|
| 82 |  | 
|---|
| 83 |         class HeightmapCollisionShape : public CollisionShape | 
|---|
| 84 |         { | 
|---|
| 85 |         public: | 
|---|
| 86 |                 HeightmapCollisionShape(int width, int length, Ogre::Vector3& scale, Ogre::Real* pHeightData, bool bFlip) | 
|---|
| 87 |                 { | 
|---|
| 88 |                         int upIndex = 1; | 
|---|
| 89 |                         bool useFloatDatam=true; | 
|---|
| 90 |                         bool flipQuadEdges=bFlip; | 
|---|
| 91 |  | 
|---|
| 92 |                         btHeightfieldTerrainShape* pHeightShape =  | 
|---|
| 93 |                                 new btHeightfieldTerrainShape(width, length, pHeightData, scale.y, upIndex, useFloatDatam, flipQuadEdges); | 
|---|
| 94 |                         pHeightShape->setUseDiamondSubdivision(true); | 
|---|
| 95 |  | 
|---|
| 96 |                         mShape = pHeightShape; | 
|---|
| 97 |  | 
|---|
| 98 |                         btVector3 sc(scale.x, scale.y, scale.z); | 
|---|
| 99 |                         mShape->setLocalScaling(sc); | 
|---|
| 100 |  | 
|---|
| 101 |                 } | 
|---|
| 102 |  | 
|---|
| 103 |                 virtual ~HeightmapCollisionShape() | 
|---|
| 104 |                 { | 
|---|
| 105 |                 } | 
|---|
| 106 |  | 
|---|
| 107 |                 bool drawWireFrame(DebugLines *wire,  | 
|---|
| 108 |                         const Ogre::Vector3 &pos = Ogre::Vector3::ZERO,  | 
|---|
| 109 |                         const Ogre::Quaternion &quat= Ogre::Quaternion::IDENTITY) const | 
|---|
| 110 |                 { | 
|---|
| 111 |                         btHeightfieldTerrainShape* pHeightShape = static_cast<btHeightfieldTerrainShape*>(mShape); | 
|---|
| 112 |  | 
|---|
| 113 |                         btTransform bt; | 
|---|
| 114 |                         bt.setIdentity(); | 
|---|
| 115 |  | 
|---|
| 116 |                         btVector3 colour(255.0, 255.0, 255.0); | 
|---|
| 117 |  | 
|---|
| 118 |                         DebugHelper ddraw(wire); | 
|---|
| 119 |                         DebugTriangleDrawCallback cb(&ddraw, bt, colour); | 
|---|
| 120 |  | 
|---|
| 121 |                         btVector3 aabbMax(btScalar(1e30),btScalar(1e30),btScalar(1e30)); | 
|---|
| 122 |                         btVector3 aabbMin(btScalar(-1e30),btScalar(-1e30),btScalar(-1e30)); | 
|---|
| 123 |                         pHeightShape->processAllTriangles(&cb, aabbMin, aabbMax); | 
|---|
| 124 |                         return true; | 
|---|
| 125 |                 } | 
|---|
| 126 |  | 
|---|
| 127 |         }; | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 |  | 
|---|
| 131 | #endif // _OGREBULLET_HEIGHTMAP_ | 
|---|