Skip to main content

History: Loading Meshes From Any Path

Source of version: 6

Copy to clipboard
            Usually the paths of resources - including meshes - should be defined in the ''resource.cfg'' file. However, sometimes we need to load mesh files just like "open file" from any local path, which is not predefined in the resource file.

The following code snippet shows to accomplish that:

''(for a similar example using FileStreamDataStream see http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Loading+skeletons+from+any+path&structure=Cookbook)''

{CODE(wrap="1", colors="c++")}// "source" should contain the pathname to your mesh file 
Ogre::String source;

/* 
   An alternate (better) way of doing the following would be to 
   use the FileStreamDataStream class, which avoids having to use
   the more esoteric "stat" struct and stdio APIs. For more, see
   http://stderr.org/doc/ogre-doc/api/classOgre_1_1FileStreamDataStream.html

   This prevents having to create and fill a MemoryDataStream instance, as 
   the FileStreamDataStream can be used directly in the "stream" c'tor below.
*/
FILE* pFile = fopen( source.c_str(), "rb" );
if (!pFile)
    OGRE_EXCEPT(Exception::ERR_FILE_NOT_FOUND,"File " + source + " not found.", "OgreMeshLoaded");

struct stat tagStat;
stat( source.c_str(), &tagStat );
MemoryDataStream* memstream = new MemoryDataStream(source, tagStat.st_size, true);
fread( (void*)memstream->getPtr(), tagStat.st_size, 1, pFile );
fclose( pFile );

// give the resource a name -- it can be the full pathname if you like, since it's 
// just going to be the key in an STL associative tree container
MeshPtr pMesh = MeshManager::getSingleton().createManual("LocalMesh",ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);

// this part does the actual load into the live Mesh object created above
MeshSerializer meshSerializer;
DataStreamPtr stream(memstream);
meshSerializer.importMesh(stream, pMesh.getPointer());

// and finally, now that we have a named Mesh resource, we can use it
// in our createEntity() call...
Entity* pF35_1 = m_pSceneMgr->createEntity("LocalMesh_Ent", "LocalMesh");
{CODE}

---
Alias: (alias(Loading_Meshes_From_Any_Path))

        

History

Information Version
Sat 15 of Nov, 2014 23:15 GMT-0000 spacegaier fixed interwiki linking 7
Sat 15 of Nov, 2014 22:43 GMT-0000 loath 6
Wed 11 of Aug, 2010 13:10 GMT-0000 jacmoe 5
Sat 02 of Jan, 2010 23:40 GMT-0000 jacmoe 4
Fri 01 of Jan, 2010 22:40 GMT-0000 jacmoe 3
Fri 25 of Dec, 2009 20:04 GMT-0000 jacmoe 2
Thu 02 of Jul, 2009 09:12 GMT-0000 Spacegaier code tag, category, language 1