Sophie

Sophie

distrib > Mandriva > 2010.1 > x86_64 > by-pkgid > 58828b263d8f56d90ac336dea07a4586 > files > 746

irrlicht-doc-1.6.1-1mdv2010.1.x86_64.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>Irrlicht Engine: Tutorial 15: Loading Scenes from .irr Files</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<table class="irrlicht" >
  <tr valign="middle"> 
    <td><font size="2"><a class="qindex" href="index.html"><font color="#FFFFFF">Home</font></a> 
      | <a class="qindex" href="namespaces.html"><font color="#FFFFFF">Namespaces</font></a> 
      | <a class="qindex" href="hierarchy.html"><font color="#FFFFFF">Hierarchy</font></a> 
      | <a class="qindex" href="classes.html"><font color="#FFFFFF">Alphabetical 
      List</font></a> | <a class="qindex" href="annotated.html"><font color="#FFFFFF"> 
      Class list</font></a> | <a class="qindex" href="files.html"><font color="#FFFFFF">Files</font></a> 
      | <a class="qindex" href="namespacemembers.html"><font color="#FFFFFF"> 
      Namespace&nbsp;Members</font></a> | <a class="qindex" href="functions.html"><font color="#FFFFFF">Class 
      members</font></a> | <a class="qindex" href="globals.html"><font color="#FFFFFF">File 
      members</font></a> | <a class="qindex" href="pages.html"><font color="#FFFFFF">Tutorials</font></a></font> </td>
  </tr>
</table>
<!-- Generated by Doxygen 1.5.6 -->
<div class="contents">
<h1><a class="anchor" name="example015">Tutorial 15: Loading Scenes from .<a class="el" href="namespaceirr.html" title="Everything in the Irrlicht Engine can be found in this namespace.">irr</a> Files </a></h1><div align="center">
<img src="015shot.jpg" alt="015shot.jpg">
</div>
 <p>
Since version 1.1, Irrlicht is able to save and load the full scene graph into an .<a class="el" href="namespaceirr.html" title="Everything in the Irrlicht Engine can be found in this namespace.">irr</a> file, an xml based format. There is an editor available to edit those files, named irrEdit (<a href="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 .<a class="el" href="namespaceirr.html" title="Everything in the Irrlicht Engine can be found in this namespace.">irr</a> files.<p>
Lets start: Create an Irrlicht device and setup the window. <div class="fragment"><pre class="fragment"><span class="preprocessor">#include &lt;<a class="code" href="irrlicht_8h.html" title="Main header file of the irrlicht, the only file needed to include.">irrlicht.h</a>&gt;</span>
<span class="preprocessor">#include &lt;iostream&gt;</span>
<span class="keyword">using namespace </span>irr;

<span class="preprocessor">#ifdef _MSC_VER</span>
<span class="preprocessor"></span><span class="preprocessor">#pragma comment(lib, "Irrlicht.lib")</span>
<span class="preprocessor"></span><span class="preprocessor">#endif</span>
<span class="preprocessor"></span>
<span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>** argv)
{
        <span class="comment">// ask user for driver</span>

        <a class="code" href="namespaceirr_1_1video.html#e35a6de6d436c76107ad157fe42356d0" title="An enum for all types of drivers the Irrlicht Engine supports.">video::E_DRIVER_TYPE</a> driverType;

        printf(<span class="stringliteral">"Please select the driver you want for this example:\n"</span>\
                <span class="stringliteral">" (a) Direct3D 9.0c\n (b) Direct3D 8.1\n (c) OpenGL 1.5\n"</span>\
                <span class="stringliteral">" (d) Software Renderer\n (e) Burning's Software Renderer\n"</span>\
                <span class="stringliteral">" (f) NullDevice\n (otherKey) exit\n\n"</span>);

        <span class="keywordtype">char</span> i;
        std::cin &gt;&gt; i;

        <span class="keywordflow">switch</span>(i)
        {
                <span class="keywordflow">case</span> <span class="charliteral">'a'</span>: driverType = <a class="code" href="namespaceirr_1_1video.html#e35a6de6d436c76107ad157fe42356d04691ca314f9018f508dcf2c57dcaacec" title="Direct3D 9 device, only available on Win32 platforms.">video::EDT_DIRECT3D9</a>;<span class="keywordflow">break</span>;
                <span class="keywordflow">case</span> <span class="charliteral">'b'</span>: driverType = <a class="code" href="namespaceirr_1_1video.html#e35a6de6d436c76107ad157fe42356d08cc3807f6f28404f3424ad7e31b3142f" title="Direct3D8 device, only available on Win32 platforms.">video::EDT_DIRECT3D8</a>;<span class="keywordflow">break</span>;
                <span class="keywordflow">case</span> <span class="charliteral">'c'</span>: driverType = <a class="code" href="namespaceirr_1_1video.html#e35a6de6d436c76107ad157fe42356d02715182a79f1cb8e2826fd68a8150a53" title="OpenGL device, available on most platforms.">video::EDT_OPENGL</a>;   <span class="keywordflow">break</span>;
                <span class="keywordflow">case</span> <span class="charliteral">'d'</span>: driverType = <a class="code" href="namespaceirr_1_1video.html#e35a6de6d436c76107ad157fe42356d01598cd235a1a6bd052e2011b559e8995" title="The Irrlicht Engine Software renderer.">video::EDT_SOFTWARE</a>; <span class="keywordflow">break</span>;
                <span class="keywordflow">case</span> <span class="charliteral">'e'</span>: driverType = <a class="code" href="namespaceirr_1_1video.html#e35a6de6d436c76107ad157fe42356d0e85481da26159b967191ccc6de1e4a05" title="The Burning&amp;#39;s Software Renderer, an alternative software renderer.">video::EDT_BURNINGSVIDEO</a>;<span class="keywordflow">break</span>;
                <span class="keywordflow">case</span> <span class="charliteral">'f'</span>: driverType = <a class="code" href="namespaceirr_1_1video.html#e35a6de6d436c76107ad157fe42356d0cfdbd476cbfd4d05e72f9adffcc42210" title="Null driver, useful for applications to run the engine without visualisation.">video::EDT_NULL</a>;     <span class="keywordflow">break</span>;
                <span class="keywordflow">default</span>: <span class="keywordflow">return</span> 1;
        }

        <span class="comment">// create device and exit if creation failed</span>

        IrrlichtDevice* device =
                <a class="code" href="namespaceirr.html#baf4d8719cc26b0d30813abf85e47c76" title="Creates an Irrlicht device. The Irrlicht device is the root object for using the...">createDevice</a>(driverType, core::dimension2d&lt;u32&gt;(640, 480));

        <span class="keywordflow">if</span> (device == 0)
                <span class="keywordflow">return</span> 1; <span class="comment">// could not create selected driver.</span>

        device-&gt;setWindowCaption(L<span class="stringliteral">"Load .irr file example"</span>);

        video::IVideoDriver* driver = device-&gt;getVideoDriver();
        scene::ISceneManager* smgr = device-&gt;getSceneManager();
</pre></div><p>
Now load our .<a class="el" href="namespaceirr.html" title="Everything in the Irrlicht Engine can be found in this namespace.">irr</a> file. .<a class="el" href="namespaceirr.html" title="Everything in the Irrlicht Engine can be found in this namespace.">irr</a> 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(). <div class="fragment"><pre class="fragment">        <span class="comment">// load the scene</span>
        <span class="keywordflow">if</span> (argc&gt;1)
                smgr-&gt;loadScene(argv[1]);
        <span class="keywordflow">else</span>
                smgr-&gt;loadScene(<span class="stringliteral">"../../media/example.irr"</span>);
</pre></div><p>
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. <div class="fragment"><pre class="fragment">        scene::ICameraSceneNode * camera = smgr-&gt;addCameraSceneNodeFPS(0, 50.f, 0.1f);

        <span class="comment">// Create a meta triangle selector to hold several triangle selectors.</span>
        scene::IMetaTriangleSelector * meta = smgr-&gt;createMetaTriangleSelector();
</pre></div><p>
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. <div class="fragment"><pre class="fragment">        core::array&lt;scene::ISceneNode *&gt; nodes;
        smgr-&gt;getSceneNodesFromType(<a class="code" href="namespaceirr_1_1scene.html#cad3d7ef92a9807d391ba29120f3b7bda61d9ba5a5ec51a33600f83fb8bd71f5" title="Will match with any scene node when checking types.">scene::ESNT_ANY</a>, nodes); <span class="comment">// Find all nodes</span>

        <span class="keywordflow">for</span> (<a class="code" href="namespaceirr.html#0416a53257075833e7002efd0a18e804" title="32 bit unsigned variable.">u32</a> i=0; i &lt; nodes.size(); ++i)
        {
                scene::ISceneNode * node = nodes[i];
                scene::ITriangleSelector * selector = 0;

                <span class="keywordflow">switch</span>(node-&gt;getType())
                {
                <span class="keywordflow">case</span> <a class="code" href="namespaceirr_1_1scene.html#cad3d7ef92a9807d391ba29120f3b7bd44d66f5c284aed4d0698d6854b6a72e3" title="simple cube scene node">scene::ESNT_CUBE</a>:
                <span class="keywordflow">case</span> <a class="code" href="namespaceirr_1_1scene.html#cad3d7ef92a9807d391ba29120f3b7bd073d7fe9dfd49f24cb13bfae56d8d3b6" title="Animated Mesh Scene Node.">scene::ESNT_ANIMATED_MESH</a>:
                        <span class="comment">// Because the selector won't animate with the mesh,</span>
                        <span class="comment">// and is only being used for camera collision, we'll just use an approximate</span>
                        <span class="comment">// bounding box instead of ((scene::IAnimatedMeshSceneNode*)node)-&gt;getMesh(0)</span>
                        selector = smgr-&gt;createTriangleSelectorFromBoundingBox(node);
                <span class="keywordflow">break</span>;

                <span class="keywordflow">case</span> <a class="code" href="namespaceirr_1_1scene.html#cad3d7ef92a9807d391ba29120f3b7bd25998267ed8640ca0c432df23f1b71fe" title="Mesh Scene Node.">scene::ESNT_MESH</a>:
                <span class="keywordflow">case</span> <a class="code" href="namespaceirr_1_1scene.html#cad3d7ef92a9807d391ba29120f3b7bd46ad007c8d7d278a6a3769714c5dacdb" title="Sphere scene node.">scene::ESNT_SPHERE</a>: <span class="comment">// Derived from IMeshSceneNode</span>
                        selector = smgr-&gt;createTriangleSelector(((scene::IMeshSceneNode*)node)-&gt;getMesh(), node);
                        <span class="keywordflow">break</span>;

                <span class="keywordflow">case</span> <a class="code" href="namespaceirr_1_1scene.html#cad3d7ef92a9807d391ba29120f3b7bd699880de1c55e8ed4ae24b0e07df972a" title="Terrain Scene Node.">scene::ESNT_TERRAIN</a>:
                        selector = smgr-&gt;createTerrainTriangleSelector((scene::ITerrainSceneNode*)node);
                        <span class="keywordflow">break</span>;

                <span class="keywordflow">case</span> <a class="code" href="namespaceirr_1_1scene.html#cad3d7ef92a9807d391ba29120f3b7bd4c5badb04555328f19aef514e077ca5a" title="OctTree Scene Node.">scene::ESNT_OCT_TREE</a>:
                        selector = smgr-&gt;createOctTreeTriangleSelector(((scene::IMeshSceneNode*)node)-&gt;getMesh(), node);
                        <span class="keywordflow">break</span>;

                <span class="keywordflow">default</span>:
                        <span class="comment">// Don't create a selector for this node type</span>
                        <span class="keywordflow">break</span>;
                }

                <span class="keywordflow">if</span>(selector)
                {
                        <span class="comment">// Add it to the meta selector, which will take a reference to it</span>
                        meta-&gt;addTriangleSelector(selector);
                        <span class="comment">// And drop my reference to it, so that the meta selector owns it.</span>
                        selector-&gt;drop();
                }
        }
</pre></div><p>
Now that the mesh scene nodes have had triangle selectors created and added to the meta selector, create a collision response animator from that meta selector. <div class="fragment"><pre class="fragment">        scene::ISceneNodeAnimator* anim = smgr-&gt;createCollisionResponseAnimator(
                meta, camera, <a class="code" href="namespaceirr_1_1core.html#06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(5,5,5),
                <a class="code" href="namespaceirr_1_1core.html#06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(0,0,0));
        meta-&gt;drop(); <span class="comment">// I'm done with the meta selector now</span>

        camera-&gt;addAnimator(anim);
        anim-&gt;drop(); <span class="comment">// I'm done with the animator now</span>

        <span class="comment">// And set the camera position so that it doesn't start off stuck in the geometry</span>
        camera-&gt;setPosition(<a class="code" href="namespaceirr_1_1core.html#06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(0.f, 20.f, 0.f));

        <span class="comment">// Point the camera at the cube node, by finding the first node of type ESNT_CUBE</span>
        scene::ISceneNode * cube = smgr-&gt;getSceneNodeFromType(<a class="code" href="namespaceirr_1_1scene.html#cad3d7ef92a9807d391ba29120f3b7bd44d66f5c284aed4d0698d6854b6a72e3" title="simple cube scene node">scene::ESNT_CUBE</a>);
        <span class="keywordflow">if</span>(cube)
                camera-&gt;setTarget(cube-&gt;getAbsolutePosition());
</pre></div><p>
That's it. Draw everything and finish as usual. <div class="fragment"><pre class="fragment">        <span class="keywordtype">int</span> lastFPS = -1;

        <span class="keywordflow">while</span>(device-&gt;run())
        <span class="keywordflow">if</span> (device-&gt;isWindowActive())
        {
                driver-&gt;beginScene(<span class="keyword">true</span>, <span class="keyword">true</span>, video::SColor(0,200,200,200));
                smgr-&gt;drawAll();
                driver-&gt;endScene();

                <span class="keywordtype">int</span> fps = driver-&gt;getFPS();

                <span class="keywordflow">if</span> (lastFPS != fps)
                {
                        <a class="code" href="namespaceirr_1_1core.html#ef83fafbb1b36fcce44c07c9be23a7f2" title="Typedef for wide character strings.">core::stringw</a> str = L<span class="stringliteral">"Load Irrlicht File example - Irrlicht Engine ["</span>;
                        str += driver-&gt;getName();
                        str += <span class="stringliteral">"] FPS:"</span>;
                        str += fps;

                        device-&gt;setWindowCaption(str.c_str());
                        lastFPS = fps;
                }

        }

        device-&gt;drop();

        <span class="keywordflow">return</span> 0;
}
</pre></div> </div>
<hr size="1">
<address style="align: right;">
<small> </small>
</address>
<table width="100%" border="0" cellspacing="0" cellpadding="2">
  <tr> 
    <td width="0"> <div align="left"><small><a href="http://irrlicht.sourceforge.net" target="_blank"><img src="irrlicht.png" alt="The Irrlicht Engine" align="middle" border=0 width=88 height=31></a></small></div></td>
    <td> <div align="left"><small><em><font size="2">The <a href="http://irrlicht.sourceforge.net" target="_blank">Irrlicht 
        Engine</a> Documentation &copy; 2003-2009 by Nikolaus Gebhardt. Generated 
        on Sun Jan 10 09:24:07 2010 by <a href="http://www.doxygen.org" target="_blank">Doxygen</a> 
        (1.5.6)</font></em></small></div></td>
  </tr>
</table>
<address style="align: right;">
</address>
</body>
</html>