Sophie

Sophie

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

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 3: Custom SceneNode</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="example003">Tutorial 3: Custom SceneNode </a></h1><div align="center">
<img src="003shot.jpg" alt="003shot.jpg">
</div>
 <p>
This Tutorial is more advanced than the previous ones. If you are currently just playing around with the Irrlicht engine, you may want to look at other examples first. This tutorials shows how to create a custom scene node and how to use it in the engine. A custom scene node is needed if you want to implement a render technique the Irrlicht Engine currently does not support. For example, you can write an indoor portal based renderer or an advanced terrain scene node with it. By creating custom scene nodes, you can easily extend the Irrlicht Engine and adapt it to your own needs.<p>
I will keep the tutorial simple: Keep everything very short, everything in one .cpp file, and I'll use the engine here as in all other tutorials.<p>
To start, I include the header files, use the <a class="el" href="namespaceirr.html" title="Everything in the Irrlicht Engine can be found in this namespace.">irr</a> namespace, and tell the linker to link with the .lib file. <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">#endif</span>
</pre></div><p>
Here comes the more sophisticated part of this tutorial: The class of our very own custom scene node. To keep it simple, our scene node will not be an indoor portal renderer nor a terrain scene node, but a simple tetraeder, a 3d object consisting of 4 connected vertices, which only draws itself and does nothing more. Note that this scenario does not require a custom scene node in Irrlicht. Instead one would create a mesh from the geometry and pass it to a <a class="el" href="classirr_1_1scene_1_1_i_mesh_scene_node.html" title="A scene node displaying a static mesh.">irr::scene::IMeshSceneNode</a>. This example just illustrates creation of a custom scene node in a very simple setting.<p>
To let our scene node be able to be inserted into the Irrlicht Engine scene, the class we create needs to be derived from the <a class="el" href="classirr_1_1scene_1_1_i_scene_node.html" title="Scene node interface.">irr::scene::ISceneNode</a> class and has to override some methods. <div class="fragment"><pre class="fragment"><span class="keyword">class </span>CSampleSceneNode : <span class="keyword">public</span> scene::ISceneNode
{
</pre></div><p>
First, we declare some member variables: The bounding box, 4 vertices, and the material of the tetraeder. <div class="fragment"><pre class="fragment">        core::aabbox3d&lt;f32&gt; Box;
        video::S3DVertex Vertices[4];
        video::SMaterial Material;
</pre></div><p>
The parameters of the constructor specify the parent of the scene node, a pointer to the scene manager, and an id of the scene node. In the constructor we call the parent class' constructor, set some properties of the material, and create the 4 vertices of the tetraeder we will draw later. <div class="fragment"><pre class="fragment"><span class="keyword">public</span>:

        CSampleSceneNode(scene::ISceneNode* parent, scene::ISceneManager* mgr, <a class="code" href="namespaceirr.html#c66849b7a6ed16e30ebede579f9b47c6" title="32 bit signed variable.">s32</a> <span class="keywordtype">id</span>)
                : scene::ISceneNode(parent, mgr, id)
        {
                Material.Wireframe = <span class="keyword">false</span>;
                Material.Lighting = <span class="keyword">false</span>;

                Vertices[0] = video::S3DVertex(0,0,10, 1,1,0,
                                video::SColor(255,0,255,255), 0, 1);
                Vertices[1] = video::S3DVertex(10,0,-10, 1,0,0,
                                video::SColor(255,255,0,255), 1, 1);
                Vertices[2] = video::S3DVertex(0,20,0, 0,1,1,
                                video::SColor(255,255,255,0), 1, 0);
                Vertices[3] = video::S3DVertex(-10,0,-10, 0,0,1,
                                video::SColor(255,0,255,0), 0, 0);
</pre></div><p>
The Irrlicht Engine needs to know the bounding box of a scene node. It will use it for automatic culling and other things. Hence, we need to create a bounding box from the 4 vertices we use. If you do not want the engine to use the box for automatic culling, and/or don't want to create the box, you could also call <a class="el" href="classirr_1_1scene_1_1_i_scene_node.html#cd24dd56505a4c98bdb939d2db28c5b8" title="Enables or disables automatic culling based on the bounding box.">irr::scene::ISceneNode::setAutomaticCulling()</a> with <a class="el" href="namespaceirr_1_1scene.html#cabb2772476aa3706e65a7dc77fd9ccecf8b41fd7d45781cfb2eb7039ba6b09a">irr::scene::EAC_OFF</a>. <div class="fragment"><pre class="fragment">                Box.reset(Vertices[0].Pos);
                <span class="keywordflow">for</span> (<a class="code" href="namespaceirr.html#c66849b7a6ed16e30ebede579f9b47c6" title="32 bit signed variable.">s32</a> i=1; i&lt;4; ++i)
                        Box.addInternalPoint(Vertices[i].Pos);
        }
</pre></div><p>
Before it is drawn, the <a class="el" href="classirr_1_1scene_1_1_i_scene_node.html#c9795bfcb88dcaf8cba6ea3296e5d8d0" title="This method is called just before the rendering process of the whole scene.">irr::scene::ISceneNode::OnRegisterSceneNode()</a> method of every scene node in the scene is called by the scene manager. If the scene node wishes to draw itself, it may register itself in the scene manager to be drawn. This is necessary to tell the scene manager when it should call <a class="el" href="classirr_1_1scene_1_1_i_scene_node.html#ff530cc4856792101d0aedee51ce35fa" title="Renders the node.">irr::scene::ISceneNode::render()</a>. For example, normal scene nodes render their content one after another, while stencil buffer shadows would like to be drawn after all other scene nodes. And camera or light scene nodes need to be rendered before all other scene nodes (if at all). So here we simply register the scene node to render normally. If we would like to let it be rendered like cameras or light, we would have to call SceneManager-&gt;registerNodeForRendering(this, SNRT_LIGHT_AND_CAMERA); After this, we call the actual <a class="el" href="classirr_1_1scene_1_1_i_scene_node.html#c9795bfcb88dcaf8cba6ea3296e5d8d0" title="This method is called just before the rendering process of the whole scene.">irr::scene::ISceneNode::OnRegisterSceneNode()</a> method of the base class, which simply lets also all the child scene nodes of this node register themselves. <div class="fragment"><pre class="fragment">        <span class="keyword">virtual</span> <span class="keywordtype">void</span> OnRegisterSceneNode()
        {
                <span class="keywordflow">if</span> (IsVisible)
                        SceneManager-&gt;registerNodeForRendering(<span class="keyword">this</span>);

                ISceneNode::OnRegisterSceneNode();
        }
</pre></div><p>
In the render() method most of the interesting stuff happens: The Scene node renders itself. We override this method and draw the tetraeder. <div class="fragment"><pre class="fragment">        <span class="keyword">virtual</span> <span class="keywordtype">void</span> render()
        {
                <a class="code" href="namespaceirr.html#e9f8ec82692ad3b83c21f555bfa70bcc" title="16 bit unsigned variable.">u16</a> indices[] = {       0,2,3, 2,1,3, 1,0,3, 2,0,1      };
                video::IVideoDriver* driver = SceneManager-&gt;getVideoDriver();

                driver-&gt;setMaterial(Material);
                driver-&gt;setTransform(<a class="code" href="namespaceirr_1_1video.html#15b57657a320243be03ae6f66fcff43d843cf42adb3fa9caf61c9e228cf14e85" title="World transformation.">video::ETS_WORLD</a>, AbsoluteTransformation);
                driver-&gt;drawIndexedTriangleList(&amp;Vertices[0], 4, &amp;indices[0], 4);
        }
</pre></div><p>
And finally we create three small additional methods. <a class="el" href="classirr_1_1scene_1_1_i_scene_node.html#223f718fc2f4944b5ad28c592f6cc8c6" title="Get the axis aligned, not transformed bounding box of this node.">irr::scene::ISceneNode::getBoundingBox()</a> returns the bounding box of this scene node, <a class="el" href="classirr_1_1scene_1_1_i_scene_node.html#fa904bf3742941087aaee56b0b4cdfe2" title="Get amount of materials used by this scene node.">irr::scene::ISceneNode::getMaterialCount()</a> returns the amount of materials in this scene node (our tetraeder only has one material), and <a class="el" href="classirr_1_1scene_1_1_i_scene_node.html#1f44d8cf753b2e4c17c90d4fc2ed05b2" title="Returns the material based on the zero based index i.">irr::scene::ISceneNode::getMaterial()</a> returns the material at an index. Because we have only one material here, we can return the only one material, assuming that no one ever calls getMaterial() with an index greater than 0. <div class="fragment"><pre class="fragment">        <span class="keyword">virtual</span> <span class="keyword">const</span> core::aabbox3d&lt;f32&gt;&amp; getBoundingBox()<span class="keyword"> const</span>
<span class="keyword">        </span>{
                <span class="keywordflow">return</span> Box;
        }

        <span class="keyword">virtual</span> <a class="code" href="namespaceirr.html#0416a53257075833e7002efd0a18e804" title="32 bit unsigned variable.">u32</a> getMaterialCount()<span class="keyword"> const</span>
<span class="keyword">        </span>{
                <span class="keywordflow">return</span> 1;
        }

        <span class="keyword">virtual</span> video::SMaterial&amp; getMaterial(<a class="code" href="namespaceirr.html#0416a53257075833e7002efd0a18e804" title="32 bit unsigned variable.">u32</a> i)
        {
                <span class="keywordflow">return</span> Material;
        }       
};
</pre></div><p>
That's it. The Scene node is done. Now we simply have to start the engine, create the scene node and a camera, and look at the result. <div class="fragment"><pre class="fragment"><span class="keywordtype">int</span> main()
{
        <span class="comment">// let user select driver type</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> 0;
        }

        <span class="comment">// create device</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), 16, <span class="keyword">false</span>);
                
        <span class="keywordflow">if</span> (device == 0)
                <span class="keywordflow">return</span> 1; <span class="comment">// could not create selected driver.</span>

        <span class="comment">// create engine and camera</span>

        device-&gt;setWindowCaption(L<span class="stringliteral">"Custom Scene Node - Irrlicht Engine Demo"</span>);

        video::IVideoDriver* driver = device-&gt;getVideoDriver();
        scene::ISceneManager* smgr = device-&gt;getSceneManager();

        smgr-&gt;addCameraSceneNode(0, <a class="code" href="namespaceirr_1_1core.html#06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(0,-40,0), <a class="code" href="namespaceirr_1_1core.html#06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(0,0,0));
</pre></div><p>
Create our scene node. I don't check the result of calling new, as it should throw an exception rather than returning 0 on failure. Because the new node will create itself with a reference count of 1, and then will have another reference added by its parent scene node when it is added to the scene, I need to drop my reference to it. Best practice is to drop it only *after* I have finished using it, regardless of what the reference count of the object is after creation. <div class="fragment"><pre class="fragment">        CSampleSceneNode *myNode =
                <span class="keyword">new</span> CSampleSceneNode(smgr-&gt;getRootSceneNode(), smgr, 666);
</pre></div><p>
To animate something in this boring scene consisting only of one tetraeder, and to show that you now can use your scene node like any other scene node in the engine, we add an animator to the scene node, which rotates the node a little bit. <a class="el" href="classirr_1_1scene_1_1_i_scene_manager.html#29efe9505de4e5dc2218283ef0c2a64d" title="Creates a rotation animator, which rotates the attached scene node around itself...">irr::scene::ISceneManager::createRotationAnimator()</a> could return 0, so should be checked. <div class="fragment"><pre class="fragment">        scene::ISceneNodeAnimator* anim =
                smgr-&gt;createRotationAnimator(<a class="code" href="namespaceirr_1_1core.html#06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(0.8f, 0, 0.8f));

        <span class="keywordflow">if</span>(anim)
        {
                myNode-&gt;addAnimator(anim);
</pre></div><p>
I'm done referring to anim, so must <a class="el" href="classirr_1_1_i_reference_counted.html#fb169a857e0d2cdb96b8821cb9bff17a" title="Drops the object. Decrements the reference counter by one.">irr::IReferenceCounted::drop()</a> this reference now because it was produced by a createFoo() function. As I shouldn't refer to it again, ensure that I can't by setting to 0. <div class="fragment"><pre class="fragment">                anim-&gt;drop();
                anim = 0;
        }
</pre></div><p>
I'm done with my CSampleSceneNode object, and so must drop my reference. This won't delete the object, yet, because it is still attached to the scene graph, which prevents the deletion until the graph is deleted or the custom scene node is removed from it. <div class="fragment"><pre class="fragment">        myNode-&gt;drop();
        myNode = 0; <span class="comment">// As I shouldn't refer to it again, ensure that I can't</span>
</pre></div><p>
Now draw everything and finish. <div class="fragment"><pre class="fragment">        <a class="code" href="namespaceirr.html#0416a53257075833e7002efd0a18e804" title="32 bit unsigned variable.">u32</a> frames=0;
        <span class="keywordflow">while</span>(device-&gt;run())
        {
                driver-&gt;beginScene(<span class="keyword">true</span>, <span class="keyword">true</span>, video::SColor(0,100,100,100));

                smgr-&gt;drawAll();

                driver-&gt;endScene();
                <span class="keywordflow">if</span> (++frames==100)
                {
                        <a class="code" href="namespaceirr_1_1core.html#ef83fafbb1b36fcce44c07c9be23a7f2" title="Typedef for wide character strings.">core::stringw</a> str = L<span class="stringliteral">"Irrlicht Engine ["</span>;
                        str += driver-&gt;getName();
                        str += L<span class="stringliteral">"] FPS: "</span>;
                        str += (<a class="code" href="namespaceirr.html#c66849b7a6ed16e30ebede579f9b47c6" title="32 bit signed variable.">s32</a>)driver-&gt;getFPS();

                        device-&gt;setWindowCaption(str.c_str());
                        frames=0;
                }
        }

        device-&gt;drop();
        
        <span class="keywordflow">return</span> 0;
}
</pre></div><p>
That's it. Compile and play around with the program. </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:06 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>