OpenMW
libs/openengine/bullet/BtOgreExtras.h
Go to the documentation of this file.
00001 /*
00002  * =====================================================================================
00003  *
00004  *       Filename:  BtOgreExtras.h
00005  *
00006  *    Description:  Contains the Ogre Mesh to Bullet Shape converters.
00007  *
00008  *        Version:  1.0
00009  *        Created:  27/12/2008 01:45:56 PM
00010  *
00011  *         Author:  Nikhilesh (nikki)
00012  *
00013  * =====================================================================================
00014  */
00015 
00016 #ifndef _BtOgreShapes_H_
00017 #define _BtOgreShapes_H_
00018 
00019 #include "btBulletDynamicsCommon.h"
00020 #include "OgreSimpleRenderable.h"
00021 #include "OgreCamera.h"
00022 #include "OgreHardwareBufferManager.h"
00023 #include "OgreMaterialManager.h"
00024 #include "OgreTechnique.h"
00025 #include "OgrePass.h"
00026 
00027 #include "OgreLogManager.h"
00028 
00029 namespace BtOgre
00030 {
00031 
00032 typedef std::vector<Ogre::Vector3> Vector3Array;
00033 
00034 //Converts from and to Bullet and Ogre stuff. Pretty self-explanatory.
00035 class Convert
00036 {
00037 public:
00038     Convert() {};
00039     ~Convert() {};
00040 
00041     static btQuaternion toBullet(const Ogre::Quaternion &q)
00042     {
00043         return btQuaternion(q.x, q.y, q.z, q.w);
00044     }
00045     static btVector3 toBullet(const Ogre::Vector3 &v)
00046     {
00047         return btVector3(v.x, v.y, v.z);
00048     }
00049 
00050     static Ogre::Quaternion toOgre(const btQuaternion &q)
00051     {
00052         return Ogre::Quaternion(q.w(), q.x(), q.y(), q.z());
00053     }
00054     static Ogre::Vector3 toOgre(const btVector3 &v)
00055     {
00056         return Ogre::Vector3(v.x(), v.y(), v.z());
00057     }
00058 };
00059 
00060 //From here on its debug-drawing stuff. ------------------------------------------------------------------
00061 
00062 class DynamicRenderable : public Ogre::SimpleRenderable
00063 {
00064 public:
00066   DynamicRenderable();
00068   virtual ~DynamicRenderable();
00069 
00078   void initialize(Ogre::RenderOperation::OperationType operationType,
00079                   bool useIndices);
00080 
00082   virtual Ogre::Real getBoundingRadius(void) const;
00084   virtual Ogre::Real getSquaredViewDepth(const Ogre::Camera* cam) const;
00085 
00086 protected:
00088   size_t mVertexBufferCapacity;
00090   size_t mIndexBufferCapacity;
00091 
00097   virtual void createVertexDeclaration() = 0;
00098 
00113   void prepareHardwareBuffers(size_t vertexCount, size_t indexCount);
00114 
00121   virtual void fillHardwareBuffers() = 0;
00122 };
00123 
00124 class DynamicLines : public DynamicRenderable
00125 {
00126   typedef Ogre::Vector3 Vector3;
00127   typedef Ogre::Quaternion Quaternion;
00128   typedef Ogre::Camera Camera;
00129   typedef Ogre::Real Real;
00130   typedef Ogre::RenderOperation::OperationType OperationType;
00131 
00132 public:
00134   DynamicLines(OperationType opType=Ogre::RenderOperation::OT_LINE_STRIP);
00135   virtual ~DynamicLines();
00136 
00138   void addPoint(const Ogre::Vector3 &p);
00140   void addPoint(Real x, Real y, Real z);
00141 
00143   void setPoint(unsigned short index, const Vector3 &value);
00144 
00146   const Vector3& getPoint(unsigned short index) const;
00147 
00149   unsigned short getNumPoints(void) const;
00150 
00152   void clear();
00153 
00155   void update();
00156 
00167   void setOperationType(OperationType opType);
00168   OperationType getOperationType() const;
00169 
00170 protected:
00172   virtual void createVertexDeclaration();
00174   virtual void fillHardwareBuffers();
00175 
00176 private:
00177   std::vector<Vector3> mPoints;
00178   bool mDirty;
00179 };
00180 
00181 class DebugDrawer : public btIDebugDraw
00182 {
00183 protected:
00184     Ogre::SceneNode *mNode;
00185     btDynamicsWorld *mWorld;
00186     DynamicLines *mLineDrawer;
00187     bool mDebugOn;
00188 
00189 public:
00190 
00191     DebugDrawer(Ogre::SceneNode *node, btDynamicsWorld *world)
00192         : mNode(node),
00193           mWorld(world),
00194           mDebugOn(true)
00195     {
00196         mLineDrawer = new DynamicLines(Ogre::RenderOperation::OT_LINE_LIST);
00197         mNode->attachObject(mLineDrawer);
00198 
00199                 if (!Ogre::ResourceGroupManager::getSingleton().resourceGroupExists("BtOgre"))
00200                     Ogre::ResourceGroupManager::getSingleton().createResourceGroup("BtOgre");
00201                 if (!Ogre::MaterialManager::getSingleton().resourceExists("BtOgre/DebugLines"))
00202                 {
00203                     Ogre::MaterialPtr mat = Ogre::MaterialManager::getSingleton().create("BtOgre/DebugLines", "BtOgre");
00204                     mat->setReceiveShadows(false);
00205                     mat->setSelfIllumination(1,1,1);
00206                 }
00207 
00208         mLineDrawer->setMaterial("BtOgre/DebugLines");
00209 
00210         //mLineDrawer->setVisibilityFlags (1024);
00211     }
00212 
00213     ~DebugDrawer()
00214     {
00215                 Ogre::MaterialManager::getSingleton().remove("BtOgre/DebugLines");
00216                 Ogre::ResourceGroupManager::getSingleton().destroyResourceGroup("BtOgre");
00217         delete mLineDrawer;
00218     }
00219 
00220     void step()
00221     {
00222         if (mDebugOn)
00223         {
00224             mWorld->debugDrawWorld();
00225             mLineDrawer->update();
00226             mNode->needUpdate();
00227             mLineDrawer->clear();
00228         }
00229         else
00230         {
00231             mLineDrawer->clear();
00232             mLineDrawer->update();
00233             mNode->needUpdate();
00234         }
00235     }
00236 
00237     void drawLine(const btVector3& from,const btVector3& to,const btVector3& color)
00238     {
00239         mLineDrawer->addPoint(Convert::toOgre(from));
00240         mLineDrawer->addPoint(Convert::toOgre(to));
00241     }
00242 
00243     void drawContactPoint(const btVector3& PointOnB,const btVector3& normalOnB,btScalar distance,int lifeTime,const btVector3& color)
00244     {
00245         mLineDrawer->addPoint(Convert::toOgre(PointOnB));
00246         mLineDrawer->addPoint(Convert::toOgre(PointOnB) + (Convert::toOgre(normalOnB) * distance * 20));
00247     }
00248 
00249     void reportErrorWarning(const char* warningString)
00250     {
00251         Ogre::LogManager::getSingleton().logMessage(warningString);
00252     }
00253 
00254     void draw3dText(const btVector3& location,const char* textString)
00255     {
00256     }
00257 
00258     //0 for off, anything else for on.
00259     void setDebugMode(int isOn)
00260     {
00261         mDebugOn = (isOn == 0) ? false : true;
00262 
00263         if (!mDebugOn)
00264             mLineDrawer->clear();
00265     }
00266 
00267     //0 for off, anything else for on.
00268     int getDebugMode() const
00269     {
00270         return mDebugOn;
00271     }
00272 
00273 };
00274 
00275 }
00276 
00277 #endif
00278 
00279 
00280 
00281 
00282