| 1 | /** | 
|---|
| 2 | * @file BulletDebugDrawer.cc | 
|---|
| 3 | * Originally from http://www.ogre3d.org/tikiwiki/BulletDebugDrawer&structure=Cookbook | 
|---|
| 4 | * This source code is released into the Public Domain. | 
|---|
| 5 | * | 
|---|
| 6 | * Modified by Fabian 'x3n' Landau by using DebugDrawer and Orxonox specific utilities (e.g. output). | 
|---|
| 7 | */ | 
|---|
| 8 |  | 
|---|
| 9 | #include "BulletDebugDrawer.h" | 
|---|
| 10 | #include "OgreBulletUtils.h" | 
|---|
| 11 |  | 
|---|
| 12 | #include <OgreRoot.h> | 
|---|
| 13 | #include <OgreManualObject.h> | 
|---|
| 14 | #include <OgreSceneManager.h> | 
|---|
| 15 | #include <OgreTimer.h> | 
|---|
| 16 |  | 
|---|
| 17 | #include "util/Output.h" | 
|---|
| 18 | #include "DebugDrawer.h" | 
|---|
| 19 |  | 
|---|
| 20 | namespace orxonox | 
|---|
| 21 | { | 
|---|
| 22 | BulletDebugDrawer::BulletDebugDrawer(Ogre::SceneManager* sceneManager) | 
|---|
| 23 | { | 
|---|
| 24 | this->drawer_ = new DebugDrawer(sceneManager, 0.5f); | 
|---|
| 25 | this->bFill_ = true; | 
|---|
| 26 |  | 
|---|
| 27 | mContactPoints = &mContactPoints1; | 
|---|
| 28 |  | 
|---|
| 29 | mDebugMode = (DebugDrawModes) DBG_DrawWireframe; | 
|---|
| 30 | Ogre::Root::getSingleton().addFrameListener(this); | 
|---|
| 31 | } | 
|---|
| 32 |  | 
|---|
| 33 | BulletDebugDrawer::~BulletDebugDrawer() | 
|---|
| 34 | { | 
|---|
| 35 | Ogre::Root::getSingleton().removeFrameListener(this); | 
|---|
| 36 | delete this->drawer_; | 
|---|
| 37 | } | 
|---|
| 38 |  | 
|---|
| 39 | void BulletDebugDrawer::drawLine(const btVector3& from, const btVector3& to, const btVector3& color) | 
|---|
| 40 | { | 
|---|
| 41 | this->drawer_->drawLine(vector3(from), vector3(to), colour(color, 1.0f)); | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 | //    void BulletDebugDrawer::drawTriangle(const btVector3& v0, const btVector3& v1, const btVector3& v2, const btVector3& color, btScalar alpha) | 
|---|
| 45 | //    { | 
|---|
| 46 | //        // TODO | 
|---|
| 47 | //    } | 
|---|
| 48 |  | 
|---|
| 49 | void BulletDebugDrawer::drawSphere(const btVector3& p, btScalar radius, const btVector3& color) | 
|---|
| 50 | { | 
|---|
| 51 | this->drawer_->drawSphere(vector3(p), Ogre::Quaternion::IDENTITY, radius, colour(color, 1.0f), this->bFill_); | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | void BulletDebugDrawer::drawSphere(btScalar radius, const btTransform& transform, const btVector3& color) | 
|---|
| 55 | { | 
|---|
| 56 | Ogre::Matrix4 matrix = matrix4(transform); | 
|---|
| 57 | this->drawer_->drawSphere(matrix.getTrans(), matrix.extractQuaternion(), radius, colour(color, 1.0f), this->bFill_); | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | void BulletDebugDrawer::drawBox(const btVector3& bbMin, const btVector3& bbMax, const btVector3& color) | 
|---|
| 61 | { | 
|---|
| 62 | Ogre::Vector3* corners = new Ogre::Vector3[8]; | 
|---|
| 63 | corners[0]  = Ogre::Vector3(bbMin[0], bbMin[1], bbMin[2]); | 
|---|
| 64 | corners[1]  = Ogre::Vector3(bbMin[0], bbMax[1], bbMin[2]); | 
|---|
| 65 | corners[2]  = Ogre::Vector3(bbMax[0], bbMax[1], bbMin[2]); | 
|---|
| 66 | corners[3]  = Ogre::Vector3(bbMax[0], bbMin[1], bbMin[2]); | 
|---|
| 67 | corners[4]  = Ogre::Vector3(bbMax[0], bbMax[1], bbMax[2]); | 
|---|
| 68 | corners[5]  = Ogre::Vector3(bbMin[0], bbMax[1], bbMax[2]); | 
|---|
| 69 | corners[6]  = Ogre::Vector3(bbMin[0], bbMin[1], bbMax[2]); | 
|---|
| 70 | corners[7]  = Ogre::Vector3(bbMax[0], bbMin[1], bbMax[2]); | 
|---|
| 71 | this->drawer_->drawCuboid(corners, colour(color, 1.0f), this->bFill_); | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | void BulletDebugDrawer::drawBox(const btVector3& bbMin, const btVector3& bbMax, const btTransform& trans, const btVector3& color) | 
|---|
| 75 | { | 
|---|
| 76 | Ogre::Vector3* corners = new Ogre::Vector3[8]; | 
|---|
| 77 | corners[0]  = Ogre::Vector3(trans * btVector3(bbMin[0], bbMin[1], bbMin[2])); | 
|---|
| 78 | corners[1]  = Ogre::Vector3(trans * btVector3(bbMin[0], bbMax[1], bbMin[2])); | 
|---|
| 79 | corners[2]  = Ogre::Vector3(trans * btVector3(bbMax[0], bbMax[1], bbMin[2])); | 
|---|
| 80 | corners[3]  = Ogre::Vector3(trans * btVector3(bbMax[0], bbMin[1], bbMin[2])); | 
|---|
| 81 | corners[4]  = Ogre::Vector3(trans * btVector3(bbMax[0], bbMax[1], bbMax[2])); | 
|---|
| 82 | corners[5]  = Ogre::Vector3(trans * btVector3(bbMin[0], bbMax[1], bbMax[2])); | 
|---|
| 83 | corners[6]  = Ogre::Vector3(trans * btVector3(bbMin[0], bbMin[1], bbMax[2])); | 
|---|
| 84 | corners[7]  = Ogre::Vector3(trans * btVector3(bbMax[0], bbMin[1], bbMax[2])); | 
|---|
| 85 | this->drawer_->drawCuboid(corners, colour(color, 1.0f), this->bFill_); | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | void BulletDebugDrawer::drawCylinder(btScalar radius, btScalar halfHeight, int upAxis, const btTransform& transform, const btVector3& color) | 
|---|
| 89 | { | 
|---|
| 90 | Ogre::Matrix4 matrix = matrix4(transform); | 
|---|
| 91 | this->drawer_->drawCylinder(matrix.getTrans(), matrix.extractQuaternion(), radius, halfHeight * 2, colour(color, 1.0f), this->bFill_); | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | void BulletDebugDrawer::drawCone(btScalar radius, btScalar height, int upAxis, const btTransform& transform, const btVector3& color) | 
|---|
| 95 | { | 
|---|
| 96 | Ogre::Matrix4 matrix = matrix4(transform); | 
|---|
| 97 | this->drawer_->drawCone(matrix.getTrans(), matrix.extractQuaternion(), radius, height, colour(color, 1.0f), this->bFill_); | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | void BulletDebugDrawer::drawContactPoint(const btVector3& PointOnB, const btVector3& normalOnB, btScalar distance, int lifeTime, const btVector3& color) | 
|---|
| 101 | { | 
|---|
| 102 | mContactPoints->resize(mContactPoints->size() + 1); | 
|---|
| 103 | ContactPoint p = mContactPoints->back(); | 
|---|
| 104 | p.from = vector3(PointOnB); | 
|---|
| 105 | p.to = p.from + vector3(normalOnB) * distance; | 
|---|
| 106 | p.dieTime = Ogre::Root::getSingleton().getTimer()->getMilliseconds() + lifeTime; | 
|---|
| 107 | p.color.r = color.x(); | 
|---|
| 108 | p.color.g = color.y(); | 
|---|
| 109 | p.color.b = color.z(); | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | bool BulletDebugDrawer::frameStarted(const Ogre::FrameEvent& evt) | 
|---|
| 113 | { | 
|---|
| 114 | size_t now = Ogre::Root::getSingleton().getTimer()->getMilliseconds(); | 
|---|
| 115 | std::vector<ContactPoint>* newCP = mContactPoints == &mContactPoints1 ? &mContactPoints2 : &mContactPoints1; | 
|---|
| 116 | for (std::vector<ContactPoint>::iterator i = mContactPoints->begin(); i < mContactPoints->end(); i++ ) | 
|---|
| 117 | { | 
|---|
| 118 | ContactPoint& cp = *i; | 
|---|
| 119 | this->drawer_->drawLine(cp.from, cp.to, cp.color); | 
|---|
| 120 | if (now <= cp.dieTime) | 
|---|
| 121 | newCP->push_back(cp); | 
|---|
| 122 | } | 
|---|
| 123 | mContactPoints->clear(); | 
|---|
| 124 | mContactPoints = newCP; | 
|---|
| 125 |  | 
|---|
| 126 | // Right before the frame is rendered, call DebugDrawer::build(). | 
|---|
| 127 | this->drawer_->build(); | 
|---|
| 128 |  | 
|---|
| 129 | return true; | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | bool BulletDebugDrawer::frameEnded(const Ogre::FrameEvent& evt) | 
|---|
| 133 | { | 
|---|
| 134 | // After the frame is rendered, call DebugDrawer::clear() | 
|---|
| 135 | this->drawer_->clear(); | 
|---|
| 136 |  | 
|---|
| 137 | return true; | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | void BulletDebugDrawer::reportErrorWarning(const char* warningString) | 
|---|
| 141 | { | 
|---|
| 142 | orxout(internal_error) << warningString << endl; | 
|---|
| 143 | Ogre::LogManager::getSingleton().getDefaultLog()->logMessage(warningString); | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | void BulletDebugDrawer::draw3dText(const btVector3& location, const char* textString) | 
|---|
| 147 | { | 
|---|
| 148 |  | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | void BulletDebugDrawer::setDebugMode(int debugMode) | 
|---|
| 152 | { | 
|---|
| 153 | mDebugMode = (DebugDrawModes) debugMode; | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | int BulletDebugDrawer::getDebugMode() const | 
|---|
| 157 | { | 
|---|
| 158 | return mDebugMode; | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 | void BulletDebugDrawer::configure(bool bFill, float fillAlpha) | 
|---|
| 162 | { | 
|---|
| 163 | this->bFill_ = bFill; | 
|---|
| 164 | this->drawer_->setFillAlpha(fillAlpha); | 
|---|
| 165 | } | 
|---|
| 166 | } | 
|---|