Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ogre Resource Managers References: 1.http://www.ogre3d.org/tikiwiki/Resources+and+ResourceM anagers&structure=Tutorialshttp://www.ogre3d.org/tikiwiki/Resources+and+ResourceM.

Similar presentations


Presentation on theme: "Ogre Resource Managers References: 1.http://www.ogre3d.org/tikiwiki/Resources+and+ResourceM anagers&structure=Tutorialshttp://www.ogre3d.org/tikiwiki/Resources+and+ResourceM."— Presentation transcript:

1 Ogre Resource Managers References: 1.http://www.ogre3d.org/tikiwiki/Resources+and+ResourceM anagers&structure=Tutorialshttp://www.ogre3d.org/tikiwiki/Resources+and+ResourceM anagers&structure=Tutorials 2."Pro Ogre Programming", Junker, Springer-Verlag (2006)

2 Resources and ResourcePtr's Base classes – Resource is the resource itself (only one copy) – ResourcePtr is a (smart) pointer to a resource Might also contain parameters (e.g. what material) Eg. Mesh class – Contains all mesh data (indices, points, etc) Eg. MeshPtr: – Each entity has a MeshPtr attribute internally. – Each entity can change material settings.

3 ResourceManagers and ResourceGroupManager ResourceManager: a base class – Derived classes include: MeshManager, MaterialManager, etc. – Contains a map (hash table) of all Resources The one and only copy of each resource. ResourceGroup: – A named list of resources – [look in resources.cfg] ResrouceGroupManager: contains all ResourceManagers. – Owned by the root.

4 Ogre Resource Stages a.Undefined=>Declared i.ResourceGroupManager::declareResource ii.resources discovered during script loading (initResourceLocations). b.Declared=>Unloaded i.ResourceGroupManager::initialiseAllResourceGroups (initResources) ii.ResourceGroupManager::initialiseResourceGroup iii.ResourceManager::create + root::initialise() c.Unloaded=>Loaded i.ResourceGroupManager::loadResourceGroup() ii.Resource::load iii.when resource first used. 1. Undefined (Unknown) 2. Declared 3. Unloaded (Created) 4. Loaded a b d d e c d d.?=>Undefined i.ResourceGroupManager::clearResourceGroup() ii.Resource Manager::remove() e.Loaded=>Unloaded iv.ResourceManager::unload() v.ResourceGroupManager::unloadResourceGroup()

5 Why fit our resource manager's into Ogre's framework? Memory Budgets – Resource replacement (supposedly) thread-safe All resource scripts loaded during initialization. Ensures we only have one copy of a resource at a time. More practice integrating (in a C++ manner) our code into a different project.

6 Making a new Resource Manager 1.Derive a new resource class from Ogre::Resource – Re-define several methods 2.Create a new template-specialization for SharedPtr. 3.Derive a new manager class from Ogre::ResourceManager – Re-define several methods 4.Create an instance of your manager (in EngineApplication) 5.Create an EngineObjectComponent which includes a SharedPtr to a particular resource.

7 Using a 3D-sound resource manager (once it’s written) In a script file named mySounds.sound: [SoundFx] type = sound source = bubbles.wav [SoundFx2] type = sound source = magic.wav [LoadingMusic] type = music source = overture.ogg In initializeScene: // Create a sound for an arbitrary EngineObject (with “POS”) EngineObjectSoundComponent * snd = new EngineObjectSoundComponent(obj, “SoundFx”); // Register the player EngineObject (pobj) as a 3d listener EngineSoundManager::getSingleton().registerListener(pobj); In frameStarted: // Play (then switch) a sound (through EngineObject* obj) obj->handleMessage(“SOUND”, “play”); obj->handleMessage(“SOUND”, “setSource”, “SoundFx2”);

8 Detour: irrklang http://www.ambiera.com/irrklang/ Creating a sound engine (only one) ISoundEngine * createIrrKlangDevice() Setting listener position (probably player's pos): ISoundEngine::setListenerPosition(vec3df pos, vec3df up) Adding a sound source ISoundEngine::addSourceFromMemory(unsigned char * data, int dataSizeInBytes, char * name) – data is just a.wav or.ogg file's (binary) contents. Playing a 2d / 3d source: ISound* ISoundEnging::play3D(char * name, vec3df pos) Modifying a 3d position ISound::setPosition(vec3df pos) Stopping all sounds ISoundEngine::stopAllSounds()

9 Detailed Steps (EngineSound resource) Derive from Ogre::Resource – Add any attributes (filename, type (music,fx), irrklang sound source) Re-define these methods: EngineSound(EngineSoundManager * creator, const String &name, ResourceHandle handle, const String &group, const NameValuePairList *createParams, bool isManual = false, ManualResourceLoader * loader = 0); Pass all arguments except createParams as-is to the Resource constructor. createParams is a map of all script params (we'll construct this in the EngineSoundManager class). We should have "type" and "source" keys. – pick off elements and save them to attributes.

10 Detailed Steps (EngineSound resource) virtual void loadImpl() Will be (indirectly) called by Ogre when someone creates an instance of a sound for the first time. Actually load the data here. Read the binary contents of the (ogg or wav) file and create an irrklang sound source (probably in / through the SoundManager) set mSize (inherited from Resource) to the file size. virtual void unloadImpl() Will be (indirectly) called by Ogre when: – a) We've gone over memory budget and this res was picked for replacement – b) The EngineSoundManager is shutting down. De-register this sound as a sound source De-allocate the binary data in memory. set mSize to 0. virtual size_t calculateSize() const Just return mSize

11 Detailed Steps (EngineSoundPtr class) class EngineSoundPtr : public Ogre::SharedPtr { public: /// Basic Constructor. EngineSoundPtr() : Ogre::SharedPtr () {} /// This constructor creates a new pointer to a /// EngineSound object. Remember: we can have multiple /// smart pointers (instances) pointing to one /// EngineSound object. explicit EngineSoundPtr(EngineSound *sound) : Ogre::SharedPtr (sound) {} /// This constructor takes a reference (instead of a /// pointer) to a EngineSound object which it should /// point to. EngineSoundPtr(const EngineSoundPtr &s) : Ogre::SharedPtr (s) {}

12 Detailed Steps (EngineSoundPtr) /// The constructor takes a reference to a resource (which had /// better be an EngineSound resource). EngineSoundPtr(const Ogre::ResourcePtr &s) : Ogre::SharedPtr () { if(s.isNull()) return; // Lock and copy other mutext pointer. This is necessary // because resource loading CAN be done on its own // thread. This ensures we are acquiring a lock on this // resource. No one else can access it until it's fully // loaded (at which point we release the lock). OGRE_LOCK_MUTEX(*s.OGRE_AUTO_MUTEX_NAME); OGRE_COPY_AUTO_SHARED_MUTEX(s.OGRE_AUTO_MUTEX_NAME); pRep = static_cast (s.getPointer()); pUseCount = s.useCountPointer(); useFreeMethod = s.freeMethod(); if (pUseCount) ++(*pUseCount); }

13 Detailed Steps (EngineSoundPtr) /// Converts a resource pointer to an EngineSound resource. /// Basically does what EngineSound(const Ogre:ResourcePtr & s) /// does, but also releases the resource we're converting from. EngineSoundPtr& operator = (const Ogre::ResourcePtr& s) { if (pRep == static_cast (s.getPointer())) return *this; release(); if (s.isNull()) return *this; //if the resource ptr is null, the // call to release above has done // what we needed OGRE_LOCK_MUTEX(*s.OGRE_AUTO_MUTEX_NAME); OGRE_COPY_AUTO_SHARED_MUTEX(s.OGRE_AUTO_MUTEX_NAME); pRep = static_cast (s.getPointer()); pUseCount = s.useCountPointer(); useFreeMethod = s.freeMethod(); if (pUseCount) ++(*pUseCount); return *this; } };

14 Detailed Steps (EngineSoundManager) Derived from Ogre::ResrouceManager – Additional attribues: at least the irrklang::ISoundEngine object. – Should be a singleton (can derive from Ogre::Singleton (a template) class – or do it yourself) Re-define these methods: – Constructor: Add to mScriptPaterns a "*.sound" string Register this with the ResourceGroupManager Register this as a ScriptLoader (with ResourceGroupManager) create the irrklang sound engine. – Destructor: Call removeAll (destroys all resources) Unregister yourself as a resource manager and a script loader. Call ISoundEngine->drop()

15 Detailed Steps (EngineSoundManager) Ogre::Resource *createImpl(const String &name, ResourceHandle handle, const String &group, bool isManual, ManualResourceLoader *loader, const NameValuePairList *createParams) – Called automatically by ogre any time a new resource is created (probably as part of script parsing) EngineSoundPtr EngineSoundManager::load(const String &name, const String &resourceName, const String &group) – Get an EngineSoundPtr to resourceName. – If it isNull, create it. – Regardless, load it and return it. Ogre::Resource *EngineSoundManager::createImpl(const String &name, ResourceHandle handle, const String &group, bool isManual, ManualResourceLoader *loader, const NameValuePairList *createParams) – Just return a new EngineSound object (pass all arguments to its constructor)

16 Detailed Steps (EngineSoundManager) void EngineSoundManager::parseScript(DataStreamPtr &stream, const String &groupName) – Ogre is telling you to parse a.sound script. – Use our initializeResourceLocations as a guide. – For each "section" (e.g. [SoundFx1]), declare a new resource of type "sound" with the ResourceGroupManager. – Also create a new NameValuePairList object, adding all key-value for the current "section" (pass this to the declareResource function call above).

17 Finding a file in our resource path(s) Given: “bubbles.wav” (a ‘local’ file name) Find: The full-fname ( relative to our debug directory ), such as “..\\..\..\\media\\sounds\\bubbles.wav” String fullName; FileInfoListPtr fptr = ResourceGroupManager::getSingleton().findResourceFileInfo(group, “bubbles.wav”); if (fptr->size() > 0) { FileInfo fi = fptr->at(0); String path = fi.archive->getName(); if (path[path.size()-1] != '/' && path[path.size()-1] != '\\') fullName = path + "/" + fi.filename; else fullName = path + fi.filename; } else fullName = "";


Download ppt "Ogre Resource Managers References: 1.http://www.ogre3d.org/tikiwiki/Resources+and+ResourceM anagers&structure=Tutorialshttp://www.ogre3d.org/tikiwiki/Resources+and+ResourceM."

Similar presentations


Ads by Google