tutorial.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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%"> <div align="center"><b><font color="#FFFFFF"></font></b></div>
  12. <b><font color="#FFFFFF">Tutorial 2.Quake3Map</font></b></td>
  13. </tr>
  14. <tr bgcolor="#eeeeff">
  15. <td height="90" colspan="2"> <div align="left">
  16. <p>This Tutorial shows how to load a Quake 3 map into the engine, create
  17. a SceneNode for optimizing the speed of rendering and how to create
  18. a user controlled camera. Please note that you should know the basics
  19. of the engine before starting this tutorial, just take a short look
  20. at the first tutorial, 1.HelloWorld, if you haven't done this yet.<br>
  21. The result of this example will look like this:</p>
  22. <p align="center"><img src="../../media/002shot.jpg" width="259" height="202"><br>
  23. </p>
  24. </div></td>
  25. </tr>
  26. </table>
  27. <br>
  28. <table width="95%" border="0" cellspacing="0" cellpadding="2" align="center">
  29. <tr>
  30. <td bgcolor="#666699"> <div align="center"><b><font color="#000000"></font></b></div>
  31. <font color="#FFFFFF"><b>Lets start!</b></font></td>
  32. </tr>
  33. <tr>
  34. <td height="90" bgcolor="#eeeeff" valign="top"> <div align="left">
  35. <p>Lets start like the HelloWorld example: We include the irrlicht header
  36. files and an additional file to be able<br>
  37. to ask the user for a driver type using the console.</p>
  38. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  39. <tr>
  40. <td> <pre>#include &lt;irrlicht.h&gt;<br>#include &lt;iostream&gt;<br></pre></td>
  41. </tr>
  42. </table>
  43. <p>As already written in the HelloWorld example, in the Irrlicht Engine,
  44. everything can be found in the namespace 'irr'. To get rid of the irr::
  45. in front of the name of every class, we tell the compiler that we use
  46. that namespace from now on, and we will not have to write that 'irr::'.<br>
  47. There are 5 other sub namespaces 'core', 'scene', 'video', 'io' and
  48. 'gui'. Unlike in the HelloWorld example, we do not a 'using namespace'
  49. for these 5 other namespaces because in this way you will see what can
  50. be found in which namespace. But if you like, you can also include the
  51. namespaces like in the previous example. Code just like you want to.</p>
  52. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  53. <tr>
  54. <td> <pre>using namespace irr;</pre> </td>
  55. </tr>
  56. </table>
  57. <p>Again, to be able to use the Irrlicht.DLL file, we need to link with
  58. the Irrlicht.lib. We could set this option in the project settings,
  59. but to make it easy, we use a pragma comment lib:</p>
  60. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  61. <tr>
  62. <td> <pre>#pragma comment(lib, &quot;Irrlicht.lib&quot;)</pre> </td>
  63. </tr>
  64. </table>
  65. </div>
  66. <p>Ok, lets start. Again, we use the main() method as start, not the WinMain(),
  67. because its shorter to write.</p>
  68. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  69. <tr>
  70. <td> <pre>int main()<br>{</pre> </td>
  71. </tr>
  72. </table>
  73. <p> Like in the HelloWorld example, we create an IrrlichtDevice with createDevice().
  74. The difference now is that we ask the user to select which hardware accelerated
  75. driver to use. The Software device would be too slow to draw a huge Quake
  76. 3 map, but just for the fun of it, we make this decision possible too.</p>
  77. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  78. <tr>
  79. <td> <pre>// ask user for driver<br><br>video::E_DRIVER_TYPE driverType = video::EDT_DIRECT3D9;<br><br>printf(&quot;Please select the driver you want for this example:\n&quot;\<br> &quot; (a) Direct3D 9.0c\n (b) Direct3D 8.1\n (c) OpenGL 1.5\n&quot;\<br> &quot; (d) Software Renderer\n (e) Apfelbaum Software Renderer\n&quot;\<br> &quot; (f) NullDevice\n (otherKey) exit\n\n&quot;);<br>
  80. char i;<br>std::cin &gt;&gt; i;<br><br>switch(i)<br>{<br> case 'a': driverType = video::EDT_DIRECT3D9;break;<br> case 'b': driverType = video::EDT_DIRECT3D8;break;<br> case 'c': driverType = video::EDT_OPENGL; break;<br> case 'd': driverType = video::EDT_SOFTWARE; break;<br> case 'e': driverType = video::EDT_BURNINGSVIDEO;break;<br> case 'f': driverType = video::EDT_NULL; break;<br> default: return 1;<br>} <br><br>// create device and exit if creation failed<br><br>IrrlichtDevice *device =<br> createDevice(driverType, core::dimension2d&lt;s32&gt;(640, 480));<br><br>if (device == 0)<br> return 1;</pre></td>
  81. </tr>
  82. </table>
  83. <p>Get a pointer to the video driver and the SceneManager so that we do
  84. not always have to write device-&gt;getVideoDriver() and device-&gt;getSceneManager().</p>
  85. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  86. <tr>
  87. <td> <pre>video::IVideoDriver* driver = device-&gt;getVideoDriver();
  88. scene::ISceneManager* smgr = device-&gt;getSceneManager();</pre> </td>
  89. </tr>
  90. </table>
  91. <p>To display the Quake 3 map, we first need to load it. Quake 3 maps are
  92. packed into .pk3 files wich are nothing other than .zip files. So we add
  93. the .pk3 file to our FileSystem. After it was added, we are able to read
  94. from the files in that archive as they would directly be stored on disk.</p>
  95. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  96. <tr>
  97. <td> <pre>device-&gt;getFileSystem()-&gt;addZipFileArchive(&quot;../../media/map-20kdm2.pk3&quot;);</pre> </td>
  98. </tr>
  99. </table>
  100. <p>Now we can load the mesh by calling getMesh(). We get a pointer returned
  101. to a IAnimatedMesh. As you know, Quake 3 maps are not really animated,
  102. they are only a huge chunk of static geometry with some materials attached.
  103. Hence the IAnimated mesh consists of only one frame,<br>
  104. so we get the &quot;first frame&quot; of the &quot;animation&quot;, which
  105. is our quake level and create an OctTree scene node with it, using addOctTreeSceneNode().
  106. The OctTree optimizes the scene a little bit, trying to draw only geometry
  107. which is currently visible. An alternative to the OctTree would be a AnimatedMeshSceneNode,
  108. which would draw always the complete geometry of the mesh, without optimization.
  109. Try it out: Write addAnimatedMeshSceneNode instead of addOctTreeSceneNode
  110. and compare the primitives drawed by the video driver. (There is a getPrimitiveCountDrawed()
  111. method in the IVideoDriver class). Note that this optimization with the
  112. Octree is only useful when drawing huge meshes consiting of lots of geometry.</p>
  113. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  114. <tr>
  115. <td> <pre>scene::IAnimatedMesh* mesh = smgr-&gt;getMesh(&quot;20kdm2.bsp&quot;);<br>scene::ISceneNode* node = 0;
  116. if (mesh)<br> node = smgr-&gt;addOctTreeSceneNode(mesh-&gt;getMesh(0));</pre> </td>
  117. </tr>
  118. </table>
  119. <p>Because the level was modelled not around the origin (0,0,0), we translate
  120. the whole level a little bit.</p>
  121. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  122. <tr>
  123. <td> <pre>if (node)<br> node-&gt;setPosition(core::vector3df(-1300,-144,-1249));</pre> </td>
  124. </tr>
  125. </table>
  126. <p>Now we only need a Camera to look at the Quake 3 map. And we want to
  127. create a user controlled camera. There are some different cameras available
  128. in the Irrlicht engine. For example the Maya Camera which can be controlled
  129. compareable to the camera in Maya: Rotate with left mouse button pressed,
  130. Zoom with both buttons pressed,<br>
  131. translate with right mouse button pressed. This could be created with
  132. addCameraSceneNodeMaya(). But for this example, we want to create a camera
  133. which behaves like the ones in first person shooter games (FPS):</p>
  134. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  135. <tr>
  136. <td> <pre>smgr-&gt;addCameraSceneNodeFPS();</pre> </td>
  137. </tr>
  138. </table>
  139. <p>The mouse cursor needs not to be visible, so we make it invisible. </p>
  140. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  141. <tr>
  142. <td> <pre>device-&gt;getCursorControl()-&gt;setVisible(false);</pre> </td>
  143. </tr>
  144. </table>
  145. <p>We have done everything, so lets draw it. We also write the current frames
  146. per second and the drawn primitives to the caption of the window. The
  147. 'if (device-&gt;isWindowActive())' line is optional, but prevents the
  148. engine render to set the position of the mouse cursor after task switching
  149. when other program are active.</p>
  150. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  151. <tr>
  152. <td> <pre>int lastFPS = -1;</pre>
  153. <pre>while(device-&gt;run())
  154. {
  155. driver-&gt;beginScene(true, true, video::SColor(0,200,200,200));
  156. smgr-&gt;drawAll();
  157. driver-&gt;endScene();</pre>
  158. <pre> int fps = driver-&gt;getFPS();</pre>
  159. <pre> if (lastFPS != fps)
  160. {
  161. core::stringw str = L&quot;Irrlicht Engine - Quake 3 Map example [&quot;;<br> str += driver-&gt;getName();<br> str += &quot;] FPS:&quot;;<br> str += fps;<br> device-&gt;setWindowCaption(str.c_str());<br> lastFPS = fps;
  162. }
  163. }</pre> </td>
  164. </tr>
  165. </table>
  166. <p>In the end, delete the Irrlicht device.</p>
  167. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  168. <tr>
  169. <td> <pre> device-&gt;drop();<br> return 0;<br>}</pre> </td>
  170. </tr>
  171. </table>
  172. <p>That's it. Compile and play around with the program. </p></td>
  173. </tr>
  174. </table>
  175. <p>&nbsp;</p>
  176. <p>&nbsp;</p>
  177. </body>
  178. </html>