OpenMW
libs/openengine/bullet/BulletShapeLoader.h
Go to the documentation of this file.
00001 #ifndef _BULLET_SHAPE_LOADER_H_
00002 #define _BULLET_SHAPE_LOADER_H_
00003 
00004 #include <OgreResource.h>
00005 #include <OgreResourceManager.h>
00006 #include <btBulletCollisionCommon.h>
00007 #include <OgreVector3.h>
00008 
00009 namespace OEngine {
00010 namespace Physic
00011 {
00012 
00016 class BulletShape : public Ogre::Resource
00017 {
00018     Ogre::String mString;
00019 
00020 protected:
00021     void loadImpl();
00022     void unloadImpl();
00023     size_t calculateSize() const;
00024 
00025     void deleteShape(btCollisionShape* shape);
00026 
00027 public:
00028 
00029     BulletShape(Ogre::ResourceManager *creator, const Ogre::String &name,
00030         Ogre::ResourceHandle handle, const Ogre::String &group, bool isManual = false,
00031         Ogre::ManualResourceLoader *loader = 0);
00032 
00033     virtual ~BulletShape();
00034 
00035     btCollisionShape* mCollisionShape;
00036     btCollisionShape* mRaycastingShape;
00037 
00038     // Whether or not a NiRootCollisionNode was present in the .nif. If there is none, the collision behaviour
00039     // depends on object type, so we need to expose this variable.
00040     bool mHasCollisionNode;
00041 
00042     Ogre::Vector3 mBoxTranslation;
00043     Ogre::Quaternion mBoxRotation;
00044     //this flag indicate if the shape is used for collision or if it's for raycasting only.
00045     bool mCollide;
00046 };
00047 
00052 #if (OGRE_VERSION < ((1 << 16) | (9 << 8) | 0))
00053 class BulletShapePtr : public Ogre::SharedPtr<BulletShape>
00054 {
00055 public:
00056     BulletShapePtr() : Ogre::SharedPtr<BulletShape>() {}
00057     explicit BulletShapePtr(BulletShape *rep) : Ogre::SharedPtr<BulletShape>(rep) {}
00058     BulletShapePtr(const BulletShapePtr &r) : Ogre::SharedPtr<BulletShape>(r) {}
00059     BulletShapePtr(const Ogre::ResourcePtr &r) : Ogre::SharedPtr<BulletShape>()
00060     {
00061         if( r.isNull() )
00062             return;
00063         // lock & copy other mutex pointer
00064         OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
00065             OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
00066             pRep = static_cast<BulletShape*>(r.getPointer());
00067         pUseCount = r.useCountPointer();
00068         useFreeMethod = r.freeMethod();
00069         if (pUseCount)
00070         {
00071             ++(*pUseCount);
00072         }
00073     }
00074 
00076     BulletShapePtr& operator=(const Ogre::ResourcePtr& r)
00077     {
00078         if(pRep == static_cast<BulletShape*>(r.getPointer()))
00079             return *this;
00080         release();
00081         if( r.isNull() )
00082             return *this; // resource ptr is null, so the call to release above has done all we need to do.
00083         // lock & copy other mutex pointer
00084         OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
00085             OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
00086             pRep = static_cast<BulletShape*>(r.getPointer());
00087         pUseCount = r.useCountPointer();
00088         useFreeMethod = r.freeMethod();
00089         if (pUseCount)
00090         {
00091             ++(*pUseCount);
00092         }
00093         return *this;
00094     }
00095 };
00096 #else
00097 typedef Ogre::SharedPtr<BulletShape> BulletShapePtr;
00098 #endif
00099 
00118 class BulletShapeManager : public Ogre::ResourceManager
00119 {
00120 protected:
00121 
00122     // must implement this from ResourceManager's interface
00123     Ogre::Resource *createImpl(const Ogre::String &name, Ogre::ResourceHandle handle,
00124         const Ogre::String &group, bool isManual, Ogre::ManualResourceLoader *loader,
00125         const Ogre::NameValuePairList *createParams);
00126 
00127     static BulletShapeManager *sThis;
00128 
00129 private:
00131     BulletShapeManager(const BulletShapeManager &);
00132 
00134     BulletShapeManager& operator=(const BulletShapeManager &);
00135 
00136 
00137 public:
00138 
00139     BulletShapeManager();
00140     virtual ~BulletShapeManager();
00141 
00142 
00143 #if (OGRE_VERSION >= ((1 << 16) | (9 << 8) | 0))
00144 
00145 
00146     BulletShapePtr getByName(const Ogre::String& name, const Ogre::String& groupName = Ogre::ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME);
00147 
00150     BulletShapePtr create (const Ogre::String& name, const Ogre::String& group,
00151                         bool isManual = false, Ogre::ManualResourceLoader* loader = 0,
00152                         const Ogre::NameValuePairList* createParams = 0);
00153 #endif
00154 
00155     virtual BulletShapePtr load(const Ogre::String &name, const Ogre::String &group);
00156 
00157     static BulletShapeManager &getSingleton();
00158     static BulletShapeManager *getSingletonPtr();
00159 };
00160 
00161 class BulletShapeLoader : public Ogre::ManualResourceLoader
00162 {
00163 public:
00164 
00165     BulletShapeLoader(){};
00166     virtual ~BulletShapeLoader() {}
00167 
00168     virtual void loadResource(Ogre::Resource *resource);
00169 
00170     virtual void load(const std::string &name,const std::string &group);
00171 };
00172 
00173 }
00174 }
00175 
00176 #endif