<title>Tutorial 15: Loading Scenes from .irr Files</title>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<!-- Wanted to avoid copying .css to each folder, so copied default .css from doxyen in here, kicked out most stuff we don't need for examples and modified some a little bit.
Target was having a single html in each example folder which is created from the main.cpp files and needs no files besides some images below media folder.
<p>Since version 1.1, Irrlicht is able to save and load the full scene graph into an .irr file, an xml based format. There is an editor available to edit those files, named irrEdit (<ahref="http://www.ambiera.com/irredit">http://www.ambiera.com/irredit</a>) which can also be used as world and particle editor. This tutorial shows how to use .irr files.</p>
<p>Lets start: Create an Irrlicht device and setup the window. </p><divclass="fragment"><divclass="line"><spanclass="preprocessor">#include <irrlicht.h></span></div><divclass="line"><spanclass="preprocessor">#include "driverChoice.h"</span></div><divclass="line"><spanclass="preprocessor">#include "exampleHelper.h"</span></div><divclass="line"></div><divclass="line"><spanclass="keyword">using namespace </span>irr;</div><divclass="line"></div><divclass="line"><spanclass="preprocessor">#ifdef _MSC_VER</span></div><divclass="line"><spanclass="preprocessor">#pragma comment(lib, "Irrlicht.lib")</span></div><divclass="line"><spanclass="preprocessor">#endif</span></div><divclass="line"></div><divclass="line"><spanclass="keywordtype">int</span> main(<spanclass="keywordtype">int</span> argc, <spanclass="keywordtype">char</span>** argv)</div><divclass="line">{</div><divclass="line"><spanclass="comment">// ask user for driver</span></div><divclass="line"> video::E_DRIVER_TYPE driverType=driverChoiceConsole();</div><divclass="line"><spanclass="keywordflow">if</span> (driverType==video::EDT_COUNT)</div><divclass="line"><spanclass="keywordflow">return</span> 1;</div><divclass="line"></div><divclass="line"><spanclass="comment">// create device and exit if creation failed</span></div><divclass="line"></div><divclass="line"> IrrlichtDevice* device =</div><divclass="line"> createDevice(driverType, core::dimension2d<u32>(640, 480));</div><divclass="line"></div><divclass="line"><spanclass="keywordflow">if</span> (device == 0)</div><divclass="line"><spanclass="keywordflow">return</span> 1; <spanclass="comment">// could not create selected driver.</span></div><divclass="line"></div><divclass="line"> device->setWindowCaption(L<spanclass="stringliteral">"Load .irr file example"</span>);</div><divclass="line"></div><divclass="line"> video::IVideoDriver* driver = device->getVideoDriver();</div><divclass="line"> scene::ISceneManager* smgr = device->getSceneManager();</div></div><!-- fragment --><p> Now load our .irr file. .irr files can store the whole scene graph including animators, materials and particle systems. And there is also the possibility to store arbitrary user data for every scene node in that file. To keep this example simple, we are simply loading the scene here. See the documentation at ISceneManager::loadScene and ISceneManager::saveScene for more information. So to load and display a complicated huge scene, we only need a single call to loadScene(). </p><divclass="fragment"><divclass="line"><spanclass="comment">// load the scene</span></div></div><!-- fragment --><p> You might have to work around some minor problems in current .irr loader:</p><ul>
<li>It can't load meshes relative to the .irr file, but only relative to the working directory. So you might have to change your working directory to the path where the .irr file is in.</li>
<li>When passing a custom parent node to loadScene then irr_scene attributes will be passed to that. Usually not a problem, but for example AmbientLight will not change that way unless you create a custom SceneNode type which can interpret those attributes. <divclass="fragment"><divclass="line"><spanclass="keywordflow">if</span> (argc>1)</div><divclass="line"> smgr->loadScene(argv[1]);</div><divclass="line"><spanclass="keywordflow">else</span></div><divclass="line"> smgr->loadScene(getExampleMediaPath() + <spanclass="stringliteral">"example.irr"</span>);</div></div><!-- fragment --> Now we'll create a camera, and give it a collision response animator that's built from the mesh nodes in the scene we just loaded. <divclass="fragment"><divclass="line">scene::ICameraSceneNode * camera = smgr->addCameraSceneNodeFPS(0, 50.f, 0.1f);</div><divclass="line"></div><divclass="line"><spanclass="comment">// Create a meta triangle selector to hold several triangle selectors.</span></div><divclass="line">scene::IMetaTriangleSelector * meta = smgr->createMetaTriangleSelector();</div></div><!-- fragment --> Now we will find all the nodes in the scene and create triangle selectors for all suitable nodes. Typically, you would want to make a more informed decision about which nodes to performs collision checks on; you could capture that information in the node name or Id. <divclass="fragment"><divclass="line">core::array<scene::ISceneNode *> nodes;</div><divclass="line">smgr->getSceneNodesFromType(scene::ESNT_ANY, nodes); <spanclass="comment">// Find all nodes</span></div><divclass="line"></div><divclass="line"><spanclass="keywordflow">for</span> (u32 i=0; i < nodes.size(); ++i)</div><divclass="line">{</div><divclass="line"> scene::ISceneNode * node = nodes[i];</div><divclass="line"> scene::ITriangleSelector * selector = 0;</div><divclass="line"></div><divclass="line"><spanclass="keywordflow">switch</span>(node->getType())</div><divclass="line"> {</div><divclass="line"><spanclass="keywordflow">case</span> scene::ESNT_CUBE:</div><divclass="line"><spanclass="keywordflow">case</span> scene::ESNT_ANIMATED_MESH:</div><divclass="line"><spanclass="comment">// Because the selector won't animate with the mesh,</span></div><divclass="line"><spanclass="comment">// and is only being used for camera collision, we'll just use an approximate</span></div><divclass="line"><spanclass="comment">// bounding box instead of ((scene::IAnimatedMeshSceneNode*)node)->getMesh(0)</span></div><divclass="line"> selector = smgr->createTriangleSelectorFromBoundingBox(node);</div><divclass="line"><spanclass="keywordflow">break</span>;</div><divclass="line"></div><divclass="line"><spanclass="keywordflow">case</span> scene::ESNT_MESH:</div><divclass="line"><spanclass="keywordflow">case</span> scene::ESNT_SPHERE: <spanclass="comment">// Derived from IMeshSceneNode</span></div><divclass="line"> selector = smgr->createTriangleSelector(((scene::IMeshSceneNode*)node)->getMesh(), node);</div><divclass="line"><spanclass="keywordflow">break</span>;</div><divclass="line"></div><divclass="line"><spanclass="keywordflow">case</span> scene::ESNT_TERRAIN:</div><divclass="line"> selector = smgr->createTerrainTriangleSelector((scene::ITerrainSceneNode*)node);</div><divclass="line"><spanclass="keywordflow">break</span>;</div><divclass="line"></div><divclass="line"><spanclass="keywordflow">case</span> scene::ESNT_OCTREE:</div><divclass="line"> selector = smgr->createOctreeTriangleSelector(((scene::IMeshSceneNode*)node)->getMesh(), node);</div><divclass="line"><spanclass="keywordflow">break</span>;</div><divclass="line"></div><divclass="line"><spanclass="keywordflow">default</span>:</div><divclass="line"><spanclass="comment">// Don't create a selector for this node type</span></div><divclass="line"><spanclass="key