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