Sirikata
libogre/include/sirikata/ogre/Entity.hpp
Go to the documentation of this file.
00001 /*  Sirikata
00002  *  Entity.hpp
00003  *
00004  *  Copyright (c) 2009, Patrick Reiter Horn
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions are
00009  *  met:
00010  *  * Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  *  * Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in
00014  *    the documentation and/or other materials provided with the
00015  *    distribution.
00016  *  * Neither the name of Sirikata nor the names of its contributors may
00017  *    be used to endorse or promote products derived from this software
00018  *    without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00021  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00022  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00023  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00024  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00026  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  */
00032 #ifndef SIRIKATA_OGRE_ENTITY_HPP__
00033 #define SIRIKATA_OGRE_ENTITY_HPP__
00034 
00035 #include <sirikata/ogre/Platform.hpp>
00036 #include <sirikata/ogre/Util.hpp>
00037 #include <OgreMovableObject.h>
00038 #include <OgreRenderable.h>
00039 #include <OgreSceneManager.h>
00040 #include <sirikata/ogre/OgreConversions.hpp>
00041 #include <sirikata/core/network/IOTimer.hpp>
00042 #include <sirikata/core/transfer/URI.hpp>
00043 #include <sirikata/core/transfer/TransferData.hpp>
00044 
00045 #include <sirikata/core/util/ListenerProvider.hpp>
00046 #include <sirikata/core/util/Liveness.hpp>
00047 
00048 #include <sirikata/mesh/Meshdata.hpp>
00049 #include <sirikata/mesh/Billboard.hpp>
00050 
00051 namespace Sirikata {
00052 namespace Graphics {
00053 
00054 class OgreRenderer;
00055 class Entity;
00056 
00057 class SIRIKATA_OGRE_EXPORT EntityListener {
00058   public:
00059     virtual ~EntityListener();
00060 
00061     virtual void entityLoaded(Entity* ent, bool success) {}
00062 };
00063 
00065 class SIRIKATA_OGRE_EXPORT Entity : public Provider<EntityListener*>, public Liveness {
00066 public:
00067     typedef std::map<int, std::pair<String, Ogre::MaterialPtr> > ReplacedMaterialMap;
00068 protected:
00069     OgreRenderer *const mScene;
00070     String mName;
00071 
00072     Ogre::MovableObject* mOgreObject;
00073     Ogre::SceneNode *mSceneNode;
00074 
00075     std::list<Entity*>::iterator mMovingIter;    
00076 
00077     ReplacedMaterialMap mReplacedMaterials;
00078     TextureBindingsMap mTextureBindings;
00079 
00080     typedef std::vector<Ogre::Light*> LightList;
00081     LightList mLights;
00082 
00083     // We need to track this because Ogre doesn't seem to do the right
00084     // thing if you toggle visibility with cascading, then later add
00085     // more nodes.
00086     bool mVisible;
00087     bool mIsAggregate;
00088     float mAggregateRadius;
00089     Vector3d mAggregateOffset;
00090 
00091     // And we track this because it's more efficient than looping through all
00092     // available animations on the mesh.
00093     Ogre::AnimationState* mCurrentAnimation;
00094     String mInitialAnimationName;
00095     String mMeshName;
00096 
00097     std::set<String> mAnimationList;
00098     bool mMeshLoaded;
00099 
00100     void fixTextures();
00101 
00102     void init(Ogre::MovableObject *obj);
00103 
00104     void beginLoad();
00105     void unloadMesh();
00106     void unloadBillboard();
00107 
00108 
00109     // Note that this is forceful -- it doesn't do any checks. Generally you
00110     // should only use it from within checkDynamic().
00111     void setDynamic(bool isStatic);
00112     // Checks whether the object is dynamic and updates the renderer
00113     void checkDynamic();
00114 
00115     void updateScale(float scale);
00116     void updateVisibility();
00117 
00118     void setIsAggregate(bool isAgg);
00119 
00120 public:
00121     Entity(OgreRenderer *scene, const String& name);
00122     virtual ~Entity();
00123 
00124     // These should be overridden to allow this class to learn about the
00125     // properties of this Entity or override the basic behavior of Entity.
00126     virtual BoundingSphere3f bounds() = 0;
00127     virtual void tick(const Time& t, const Duration& deltaTime);
00128     // Check if the object is dynamic, i.e. needs updates every frame. The
00129     // Entity base class is only non-static during animations. Be sure to
00130     // include the base classes implementation when checking whether the object
00131     // is static.
00132     virtual bool isDynamic() const;
00133     // Check if the object is mobile.
00134     virtual bool isMobile() const = 0;
00135 
00136     static Entity *fromMovableObject(Ogre::MovableObject *obj);
00137 
00138     //note that we do not want to check if we're mobile or not if we're calling
00139     //removeFromScene from destructor of Entity because isMobile is purely
00140     //virtual from within Entity.  As a result, take an extra bool param.  If
00141     //true, then we check isMobile before removing from scene.  Otherwise, we do
00142     //not.
00143     void removeFromScene(bool *mobileVal=NULL);
00144     void addToScene(Ogre::SceneNode *newParent=NULL);
00145 
00146     OgreRenderer *getScene() const{
00147         return mScene;
00148     }
00149 
00150     Ogre::SceneNode *getSceneNode() {
00151         return mSceneNode;
00152     }
00153 
00154     Ogre::Entity* getOgreEntity() ;
00155     Ogre::BillboardSet* getOgreBillboard() const;
00156 
00157     void setOgrePosition(const Vector3d &pos);
00158     void setOgreOrientation(const Quaternion &orient);
00159 
00160     Vector3d getOgrePosition();
00161     Quaternion getOgreOrientation();
00162     float getRadius();
00163 
00164     void setSelected(bool selected);
00165 
00166     static std::string ogreMeshName(const String& name);
00167     std::string ogreMovableName()const;
00168 
00169     const String& id()const{
00170         return mName;
00171     }
00172 
00173     void setVisible(bool vis);
00174 
00175     void setAnimation(const String& name,bool* checkVisible = NULL);
00176     const std::vector<String> getAnimationList();
00177 
00178 
00179     void bindTexture(const std::string &textureName, const String& objId);
00180     void unbindTexture(const std::string &textureName);
00181 
00183     void loadEmpty();
00187     void loadMesh(Mesh::MeshdataPtr meshdata, const String& meshname, const std::set<String>& animations);
00191     void loadBillboard(Mesh::BillboardPtr bboard, const String& bbtexname);
00195     void loadFailed();
00196 
00197     void unload();
00198 };
00199 typedef std::tr1::shared_ptr<Entity> EntityPtr;
00200 
00201 }
00202 }
00203 
00204 #endif //SIRIKATA_OGRE_ENTITY_HPP__