Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added customized visualization for cylinders and cones to BulletDebugDrawer.
improved DebugDrawer by making circle, cylinder, and sphere rotatable. added rendering function for cones.

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