Sirikata
|
00001 // Copyright (c) 2011 Sirikata Authors. All rights reserved. 00002 // Use of this source code is governed by a BSD-style license that can 00003 // be found in the LICENSE file. 00004 00005 #ifndef _SIRIKATA_OGRE_RESOURCE_LOADER_HPP_ 00006 #define _SIRIKATA_OGRE_RESOURCE_LOADER_HPP_ 00007 00008 #include <sirikata/mesh/Meshdata.hpp> 00009 #include <sirikata/core/transfer/URI.hpp> 00010 #include <sirikata/core/util/Time.hpp> 00011 #include <sirikata/core/service/Context.hpp> 00012 #include "ManualMaterialLoader.hpp" 00013 00014 namespace Sirikata { 00015 namespace Graphics { 00016 00034 class ResourceLoader { 00035 public: 00036 typedef std::tr1::function<void()> LoadedCallback; 00037 00044 ResourceLoader(Context* ctx, const Duration& per_frame_time); 00045 ~ResourceLoader(); 00046 00047 void loadMaterial(const String& name, Mesh::MeshdataPtr mesh, const Mesh::MaterialEffectInfo& mat, const Transfer::URI& uri, TextureBindingsMapPtr textureFingerprints, LoadedCallback cb); 00048 void loadBillboardMaterial(const String& name, const String& texuri, const Transfer::URI& uri, TextureBindingsMapPtr textureFingerprints, LoadedCallback cb); 00049 00050 void loadSkeleton(const String& name, Mesh::MeshdataPtr mesh, const std::set<String>& animationList, LoadedCallback cb); 00051 00052 void loadMesh(const String& name, Mesh::MeshdataPtr mesh, const String& skeletonName, TextureBindingsMapPtr textureFingerprints, LoadedCallback cb); 00053 00054 void loadTexture(const String& name, LoadedCallback cb); 00055 00056 void unloadResource(const String& name); 00057 00058 void tick(); 00059 00060 private: 00061 enum ResourceType { 00062 ResourceTypeMaterial, 00063 ResourceTypeTexture, 00064 ResourceTypeSkeleton, 00065 ResourceTypeMesh 00066 }; 00067 00068 void loadMaterialWork(const String& name, Mesh::MeshdataPtr mesh, const Mesh::MaterialEffectInfo& mat, const Transfer::URI& uri, TextureBindingsMapPtr textureFingerprints, LoadedCallback cb); 00069 00070 void loadBillboardMaterialWork(const String& name, const String& texuri, const Transfer::URI& uri, TextureBindingsMapPtr textureFingerprints, LoadedCallback cb); 00071 00072 void loadSkeletonWork(const String& name, Mesh::MeshdataPtr mesh, const std::set<String>& animationList, LoadedCallback cb); 00073 00074 void loadMeshWork(const String& name, Mesh::MeshdataPtr mesh, const String& skeletonName, TextureBindingsMapPtr textureFingerprints, LoadedCallback cb); 00075 00076 void loadTextureWork(const String& name, LoadedCallback cb); 00077 00078 void unloadResourceWork(const String& name, ResourceType type); 00079 00080 // Refcounting utilities: 00081 // Increment or start refcount at 1 00082 void incRefCount(const String& name, ResourceType type); 00083 // Decrement and possibly request unloading 00084 void decRefCount(const String& name); 00085 00086 00087 const Duration mPerFrameTime; 00088 TimeProfiler::Stage* mProfilerStage; 00089 00090 // This is just a task queue where tick() makes sure we stop when we've 00091 // passed our time threshold. 00092 typedef std::tr1::function<void()> Task; 00093 std::queue<Task> mTasks; 00094 00095 // Track ref counts for each object. To simplify management for 00096 // the users of this class, we track the type. 00097 struct ResourceData { 00098 ResourceData(ResourceType t) 00099 : type(t), refcount(1) 00100 {} 00101 00102 ResourceType type; 00103 int32 refcount; 00104 }; 00105 typedef std::map<String, ResourceData> RefCountMap; 00106 RefCountMap mRefCounts; 00107 00108 00109 // Ogre currently has a problem with removing materials 00110 // (specifically some internal state about Passes isn't properly 00111 // marked as dirty), which apparently has something to do with 00112 // multiple render systems (we have at least the main one and one 00113 // for overlays). When we remove a material we need to reset 00114 // render queues to force things back to a clean state. 00115 // (See http://www.ogre3d.org/forums/viewtopic.php?p=282082) 00116 bool mNeedRenderQueuesReset; 00117 void resetRenderQueues(); 00118 00119 }; 00120 00121 } // namespace Graphics 00122 } // namespace Sirikata 00123 00124 00125 #endif //_SIRIKATA_OGRE_RESOURCE_LOADER_HPP_