tutorial.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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="left"><b><font color="#FFFFFF">Tutorial 12. Terrain Rendering</font></b></div>
  14. </div>
  15. </td>
  16. </tr>
  17. <tr bgcolor="#eeeeff">
  18. <td height="90" colspan="2">
  19. <div align="left">
  20. <p> This tutorial will briefly show how to use the terrain renderer of
  21. Irrlicht. It will also show the terrain renderer triangle selector to
  22. be able to do collision detection with terrain.</p>
  23. <p>The program which is described here will look like this:</p>
  24. <p align="center"><img src="../../media/012shot.jpg" width="258" height="202"></p>
  25. <p align="left"><br>
  26. Note that the terrain renderer in Irrlicht is based the terrain renderer
  27. by Soconne and the GeoMipMapSceneNode developed by Spinz, lots of thanks
  28. go to them.</p>
  29. </div>
  30. </td>
  31. </tr>
  32. </table>
  33. <br>
  34. <table width="95%" border="0" cellspacing="0" cellpadding="2" align="center">
  35. <tr>
  36. <td bgcolor="#666699"> <b><font color="#FFFFFF">Lets start!</font></b></td>
  37. </tr>
  38. <tr>
  39. <td height="90" bgcolor="#eeeeff" valign="top"> <div align="left">
  40. <div align="left">
  41. <p> In the beginning there is nothing special. We include the needed
  42. header files and create an event listener to listen if the user presses
  43. the 'W' key so we can switch to wireframe mode and if he presses 'D'
  44. we toggle to material between solid and detail mapped.</p>
  45. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  46. <tr>
  47. <td> <pre>#include &lt;irrlicht.h&gt;<br>#include &lt;iostream&gt;<br>using namespace irr;<br><br>#pragma comment(lib, &quot;Irrlicht.lib&quot;)<br><br>class MyEventReceiver : public IEventReceiver<br>{<br>public:<br> MyEventReceiver(scene::ISceneNode* terrain)<br> {<br><font color="#006600"> // store pointer to terrain so we can change its drawing mode<br></font> Terrain = terrain;<br> }<br><br> bool OnEvent(const SEvent& event)<br> {<br> <font color="#006600"> // check if user presses the key 'W' or 'D'</font><br> if (event.EventType == irr::EET_KEY_INPUT_EVENT &amp;&amp; !event.KeyInput.PressedDown)<br> {<br> switch (event.KeyInput.Key)<br> {<br> case irr::KEY_KEY_W:<font color="#006600"> // switch wire frame mode</font><br> Terrain-&gt;setMaterialFlag(video::EMF_WIREFRAME, !Terrain-&gt;getMaterial(0).Wireframe);<br> return true;<br> case irr::KEY_KEY_D:<font color="#006600"> // toggle detail map</font><br> Terrain-&gt;setMaterialType(<br> Terrain-&gt;getMaterial(0).MaterialType == video::EMT_SOLID ? <br> video::EMT_DETAIL_MAP : video::EMT_SOLID);<br> return true;<br> }<br> }<br> return false;<br> }<br><br>private:<br> scene::ISceneNode* Terrain;<br>};<br><br></pre></td>
  48. </tr>
  49. </table>
  50. <p>The start of the main function starts like in most other example.
  51. We ask the user for the desired renderer and start it up. </p>
  52. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  53. <tr>
  54. <td> <pre>int main()<br>{<br><font color="#006600"> // let user select driver type<br></font><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><br> 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><font color="#006600"><br> // create device<br></font> IrrlichtDevice* device = createDevice(driverType, core::dimension2d&lt;s32&gt;(640, 480));<br><br> if (device == 0)<br> return 1; <font color="#006600">// could not create selected driver.</font><br></pre></td>
  55. </tr>
  56. </table>
  57. <p> First, we add standard stuff to the scene: A nice irrlicht engine
  58. logo, a small help text, a user controlled camera, and we disable
  59. the mouse cursor.</p>
  60. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  61. <tr>
  62. <td> <pre>video::IVideoDriver* driver = device-&gt;getVideoDriver();<br>scene::ISceneManager* smgr = device-&gt;getSceneManager();<br>gui::IGUIEnvironment* env = device-&gt;getGUIEnvironment();<br><br>driver-&gt;setTextureCreationFlag(video::ETCF_ALWAYS_32_BIT, true);<br><br><font color="#006600">// add irrlicht logo<br></font>env-&gt;addImage(driver-&gt;getTexture(&quot;../../media/irrlichtlogoalpha.tga&quot;),<br> core::position2d&lt;s32&gt;(10,10));<br>
  63. <font color="#006600">// add some help text<br></font>gui::IGUIStaticText* text = env-&gt;addStaticText(<br> L&quot;Press 'W' to change wireframe mode\nPress 'D' to toggle detail map&quot;,<br> core::rect&lt;s32&gt;(10,453,200,475), true, true, 0, -1, true);<br>
  64. <font color="#006600">// add camera<br></font>scene::ICameraSceneNode* camera = <br> smgr-&gt;addCameraSceneNodeFPS(0,100.0f,1200.0f);<br>camera-&gt;setPosition(core::vector3df(1900*2,255*2,3700*2));<br>camera-&gt;setTarget(core::vector3df(2397*2,343*2,2700*2));<br>camera-&gt;setFarValue(12000.0f);<br><br><font color="#006600">// disable mouse cursor<br></font>device-&gt;getCursorControl()-&gt;setVisible(false);</pre></td>
  65. </tr>
  66. </table>
  67. <p> Here comes the terrain renderer scene node: We add it just like
  68. any other scene node to the scene using ISceneManager::addTerrainSceneNode().
  69. The only parameter we use is a file name to the heightmap we use.
  70. A heightmap is simply a gray scale texture. The terrain renderer loads
  71. it and creates the 3D terrain from it.<br>
  72. To make the terrain look more big, we change the scale factor of it
  73. to (40, 4.4, 40). Because we don't have any dynamic lights in the
  74. scene, we switch off the lighting, and we set the file terrain-texture.jpg
  75. as texture for the terrain and detailmap3.jpg as second texture, called
  76. detail map. At last, we set the scale values for the texture: The
  77. first texture will be repeated only one time over the whole terrain,
  78. and the second one (detail map) 20 times. </p>
  79. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  80. <tr>
  81. <td> <pre><font color="#006600">// add terrain scene node<br></font>scene::ITerrainSceneNode* terrain = smgr-&gt;addTerrainSceneNode( <br> &quot;../../media/terrain-heightmap.bmp&quot;);<br><br>terrain-&gt;setScale(core::vector3df(40, 4.4f, 40));<br>terrain-&gt;setMaterialFlag(video::EMF_LIGHTING, false);<br><br>terrain-&gt;setMaterialTexture(0, driver-&gt;getTexture(&quot;../../media/terrain-texture.jpg&quot;));<br>terrain-&gt;setMaterialTexture(1, driver-&gt;getTexture(&quot;../../media/detailmap3.jpg&quot;));<br> <br>terrain-&gt;setMaterialType(video::EMT_DETAIL_MAP);<br>terrain-&gt;scaleTexture(1.0f, 20.0f);<font color="#006600"><br></font></pre></td>
  82. </tr>
  83. </table>
  84. <p> To be able to do collision with the terrain, we create a triangle
  85. selector. If you want to know what triangle selectors do, just take
  86. a look into the collision tutorial. The terrain triangle selector
  87. works together with the terrain. To demonstrate this, we create a
  88. collision response animator and attach it to the camera, so that the
  89. camera will not be able to fly through the terrain. </p>
  90. </div>
  91. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  92. <tr>
  93. <td> <pre><font color="#006600">// create triangle selector for the terrain <br></font>scene::ITriangleSelector* selector =<br> smgr-&gt;createTerrainTriangleSelector(terrain, 0);<br>terrain-&gt;setTriangleSelector(selector);<br><br><font color="#006600">// create collision response animator and attach it to the camera<br></font>scene::ISceneNodeAnimator* anim = smgr-&gt;createCollisionResponseAnimator(<br> selector, camera, core::vector3df(60,100,60),<br> core::vector3df(0,0,0), <br> core::vector3df(0,50,0));<br>selector-&gt;drop();<br>camera-&gt;addAnimator(anim);<br>anim-&gt;drop();</pre></td>
  94. </tr>
  95. </table>
  96. <p> To make the user be able to switch between normal and wireframe mode,
  97. we create an instance of the event reciever from above and let Irrlicht
  98. know about it. In addition, we add the skybox which we already used
  99. in lots of Irrlicht examples.</p>
  100. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  101. <tr>
  102. <td><pre><font color="#006600">// create event receiver<br></font>MyEventReceiver receiver(terrain);<br>device-&gt;setEventReceiver(&amp;receiver);<br><br><font color="#006600">// create skybox<br></font>driver-&gt;setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false);<br><br>smgr-&gt;addSkyBoxSceneNode(<br> driver-&gt;getTexture(&quot;../../media/irrlicht2_up.jpg&quot;),<br> driver-&gt;getTexture(&quot;../../media/irrlicht2_dn.jpg&quot;),<br> driver-&gt;getTexture(&quot;../../media/irrlicht2_lf.jpg&quot;),<br> driver-&gt;getTexture(&quot;../../media/irrlicht2_rt.jpg&quot;),<br> driver-&gt;getTexture(&quot;../../media/irrlicht2_ft.jpg&quot;),<br> driver-&gt;getTexture(&quot;../../media/irrlicht2_bk.jpg&quot;));<br>
  103. driver-&gt;setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, true);<br></pre></td>
  104. </tr>
  105. </table>
  106. <p> That's it, draw everything. Now you know how to use terrain in Irrlicht.</p>
  107. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  108. <tr>
  109. <td><pre> int lastFPS = -1;<br><br> while(device-&gt;run())<br> if (device-&gt;isWindowActive())<br> {<br> driver-&gt;beginScene(true, true, 0 );<br><br> smgr-&gt;drawAll();<br> env-&gt;drawAll();<br><br> driver-&gt;endScene();
  110. <br><font color="#006600"> // display frames per second in window title
  111. </font> int fps = driver-&gt;getFPS();<br><br> if (lastFPS != fps)<br> {<br> core::stringw str = L&quot;Terrain Renderer - Irrlicht Engine [&quot;;<br> str += driver-&gt;getName();<br> str += &quot;] FPS:&quot;;<br> str += fps;<br> device-&gt;setWindowCaption(str.c_str());<br> lastFPS = fps;<br> }<br> }<br><br> device-&gt;drop();<br> <br> return 0;<br>}</pre></td>
  112. </tr>
  113. </table>
  114. <p>&nbsp; </p>
  115. </div>
  116. </td>
  117. </tr>
  118. </table>
  119. <p>&nbsp;</p>
  120. </body>
  121. </html>