Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/tools/BulletDebugDrawer.cc @ 10193

Last change on this file since 10193 was 10193, checked in by landauf, 9 years ago

details, made debug drawer more configurable

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