tutorial.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <html>
  2. <head>
  3. <title>Irrlicht Engine Tutorial</title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  5. </head>
  6. <body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
  7. <br>
  8. <table width="95%" border="0" cellspacing="0" cellpadding="2" align="center">
  9. <tr>
  10. <td bgcolor="#666699" width="10"><b><a href="http://irrlicht.sourceforge.net" target="_blank"><img src="../../media/irrlichtlogo.jpg" width="88" height="31" border="0"></a></b></td>
  11. <td bgcolor="#666699" width="100%">
  12. <div align="center">
  13. <div align="center"></div>
  14. <div align="left"><b><font color="#FFFFFF">Tutorial 4.Movement</font></b></div>
  15. </div>
  16. </td>
  17. </tr>
  18. <tr bgcolor="#eeeeff">
  19. <td height="90" colspan="2">
  20. <div align="left">
  21. <p>This Tutorial shows how to move and animate SceneNodes. The basic concept
  22. of SceneNodeAnimators is shown as well as manual movement of nodes using
  23. the keyboard.</p>
  24. <p>The program which is described here will look like this:</p>
  25. <p align="center"><img src="../../media/004shot.jpg" width="259" height="204"><br>
  26. </p>
  27. </div>
  28. </td>
  29. </tr>
  30. </table>
  31. <br>
  32. <table width="95%" border="0" cellspacing="0" cellpadding="2" align="center">
  33. <tr>
  34. <td bgcolor="#666699"> <div align="center"><b><font color="#FFFFFF"></font></b></div>
  35. <b><font color="#FFFFFF">Lets start!</font></b></td>
  36. </tr>
  37. <tr>
  38. <td height="90" bgcolor="#eeeeff" valign="top"> <div align="left">
  39. <p>As always, I include the header files, use the irr namespace, and tell
  40. the linker to link with the .lib file. </p>
  41. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  42. <tr>
  43. <td> <pre>#include &lt;stdio.h&gt;<br>#include &lt;wchar.h&gt;<br>#include &lt;irrlicht.h&gt;</pre>
  44. <pre>using namespace irr;</pre>
  45. <pre>#pragma comment(lib, &quot;Irrlicht.lib&quot;)</pre></td>
  46. </tr>
  47. </table>
  48. <p>In this tutorial, one of our goals is to move a scene node using some
  49. keys on the keyboard. We store a pointer to the scene node we want to
  50. move with the keys here.<br>
  51. The other pointer is a pointer to the Irrlicht Device, which we need
  52. int the EventReceiver to manipulate the scene node and to get the active
  53. camera.</p>
  54. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  55. <tr>
  56. <td> <pre>scene::ISceneNode* node = 0;<br>IrrlichtDevice* device = 0; </pre></td>
  57. </tr>
  58. </table>
  59. <p>To get events like mouse and keyboard input, or GUI events like &quot;the
  60. OK button has been clicked&quot;, we need an object wich is derived
  61. from the IEventReceiver object. There is only one method to override:
  62. OnEvent. This method will be called by the engine when an event happened.
  63. We will use this input to move the scene node with the keys W and S.</p>
  64. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  65. <tr>
  66. <td> <pre>class MyEventReceiver : public IEventReceiver<br>{<br>public:<br> virtual bool OnEvent(const SEvent&amp; event)<br> { </pre></td>
  67. </tr>
  68. </table>
  69. <p>If the key 'W' or 'S' was left up, we get the position of the scene
  70. node, and modify the Y coordinate a little bit. So if you press 'W',
  71. the node moves up, and if you press 'S' it moves down.</p>
  72. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  73. <tr>
  74. <td> <pre>if (node != 0 &amp;&amp; event.EventType == irr::EET_KEY_INPUT_EVENT&amp;&amp;<br> !event.KeyInput.PressedDown)<br>{<br> switch(event.KeyInput.Key)<br> {<br> case KEY_KEY_W:<br> case KEY_KEY_S:<br> {<br> core::vector3df v = node-&gt;getPosition();<br> v.Y += event.KeyInput.Key == KEY_KEY_W ? 2.0f : -2.0f;<br> node-&gt;setPosition(v);<br> }<br> return true;<br> }<br>} return false; <br> } <br> };</pre></td>
  75. </tr>
  76. </table>
  77. </div>
  78. <p>The event receiver for moving a scene node is ready. So lets just create
  79. an Irrlicht Device and the scene node we want to move. We also create
  80. some other additional scene nodes, to show that there are also some different
  81. possibilities to move and animate scene nodes.</p>
  82. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  83. <tr>
  84. <td><pre>int main()<br>{<br> MyEventReceiver receiver;
  85. device = createDevice(video::EDT_OPENGL, core::dimension2d&lt;s32&gt;(640, 480),
  86. 16, false, false, false, &amp;receiver);</pre>
  87. <pre> video::IVideoDriver* driver = device-&gt;getVideoDriver();
  88. scene::ISceneManager* smgr = device-&gt;getSceneManager();</pre>
  89. </td>
  90. </tr>
  91. </table>
  92. <p> Create the node for moving it with the 'W' and 'S' key. We create a
  93. sphere node, which is a built in geometric primitive scene node.
  94. We place the node at (0,0,30) and assign a texture to it to let it look
  95. a little bit more interesting.</p>
  96. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  97. <tr>
  98. <td><pre>node = smgr-&gt;addSphereSceneNode();
  99. node-&gt;setPosition(core::vector3df(0,0,30));
  100. node-&gt;setMaterialFlag(video::EMF_LIGHTING, false);
  101. node-&gt;setMaterialTexture(0, driver-&gt;getTexture(&quot;../../media/wall.bmp&quot;));</pre></td>
  102. </tr>
  103. </table>
  104. <p>Now we create another node, moving using a scene node animator. Scene
  105. node animators modify scene nodes and can be attached to any scene node
  106. like<br>
  107. mesh scene nodes, billboards, lights and even camera scene nodes. Scene
  108. node animators are not only able to modify the position of a scene node,
  109. they can<br>
  110. also animate the textures of an object for example. We create a test scene
  111. node again an attach a 'fly circle' scene node to it, letting this node
  112. fly around our first test scene node.</p>
  113. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  114. <tr>
  115. <td><pre>scene::ISceneNode* n = smgr-&gt;addCubeSceneNode();
  116. n-&gt;setMaterialTexture(0, driver-&gt;getTexture(&quot;../../media/t351sml.jpg&quot;));
  117. n-&gt;setMaterialFlag(video::EMF_LIGHTING, false);
  118. scene::ISceneNodeAnimator* anim =
  119. smgr-&gt;createFlyCircleAnimator(core::vector3df(0,0,30), 20.0f);
  120. n-&gt;addAnimator(anim);
  121. anim-&gt;drop();</pre></td>
  122. </tr>
  123. </table>
  124. <p>The last scene node we add to show possibilities of scene node animators
  125. is a md2 model, which uses a 'fly straight' animator to run between to
  126. points.</p>
  127. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  128. <tr>
  129. <td> <pre>scene::IAnimatedMeshSceneNode* anms = smgr-&gt;addAnimatedMeshSceneNode(<br> smgr-&gt;getMesh(&quot;../../media/sydney.md2&quot;));
  130. if (n)<br> {<br> anim = smgr-&gt;createFlyStraightAnimator(core::vector3df(100,0,60), <br> core::vector3df(-100,0,60), 10000, true);<br> anms-&gt;addAnimator(anim);<br> anim-&gt;drop();</pre>
  131. </td>
  132. </tr>
  133. </table>
  134. <p>To make to model look right we set the frames between which
  135. the animation should loop, rotate the model around 180 degrees, and adjust
  136. the animation speed and the texture.<br>
  137. To set the right animation (frames and speed), we would also be able to
  138. just call &quot;anms-&gt;setMD2Animation(scene::EMAT_RUN)&quot; for the
  139. 'run' animation instead of &quot;setFrameLoop&quot; and &quot;setAnimationSpeed&quot;,
  140. but this only works with MD2 animations, and so you know how to start
  141. other animations.</p>
  142. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  143. <tr>
  144. <td> <pre> anms-&gt;setMaterialFlag(video::EMF_LIGHTING, false);<br> anms-&gt;setFrameLoop(320, 360);
  145. anms-&gt;setAnimationSpeed(30);<br> anms-&gt;setRotation(core::vector3df(0,180.0f,0));<br> anms-&gt;setMaterialTexture(0, driver-&gt;getTexture(&quot;../../media/sydney.bmp&quot;));<br>}<br></pre></td>
  146. </tr>
  147. </table>
  148. <p>To be able to look at and move around in this scene, we create a first
  149. person shooter style camera and make the mouse cursor invisible.</p>
  150. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  151. <tr>
  152. <td> <pre>smgr-&gt;addCameraSceneNodeFPS(0, 100.0f, 100.0f);<br>device-&gt;getCursorControl()-&gt;setVisible(false); </pre></td>
  153. </tr>
  154. </table>
  155. <p>We have done everything, so lets draw it. We also write the current frames
  156. per second and the name of the driver to the caption of the window.</p>
  157. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  158. <tr>
  159. <td> <pre>int lastFPS = -1;</pre>
  160. <pre>while(device-&gt;run())
  161. {
  162. driver-&gt;beginScene(true, true, video::SColor(255,90,90,156));
  163. smgr-&gt;drawAll();
  164. driver-&gt;endScene();</pre>
  165. <pre> int fps = driver-&gt;getFPS();</pre>
  166. <pre> if (lastFPS != fps)
  167. {
  168. wchar_t tmp[1024];
  169. swprintf(tmp, 1024, L&quot;Movement Example - Irrlicht Engine (%ls)(fps:%d)&quot;,<br> driver-&gt;getName(), fps);</pre>
  170. <pre> device-&gt;setWindowCaption(tmp);
  171. lastFPS = fps;
  172. }
  173. }
  174. device-&gt;drop();<br>return 0;<br>}</pre></td>
  175. </tr>
  176. </table>
  177. <p>That's it. Compile and play around with the program. </p>
  178. <p>&nbsp;</p></td>
  179. </tr>
  180. </table>
  181. <p>&nbsp;</p>
  182. </body>
  183. </html>