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