tutorial.html 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 13. Render to Texture</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 shows how to render to a texture using Irrlicht. Render
  21. to texture is a feature with which it is possible to create nice special
  22. effects. In addition, this tutorial shows how to enable specular highlights.</p>
  23. <p>The program which is described here will look like this:</p>
  24. <p align="center"><img src="../../media/013shot.jpg" width="256" height="200"><br>
  25. </p>
  26. </div>
  27. </td>
  28. </tr>
  29. </table>
  30. <br>
  31. <table width="95%" border="0" cellspacing="0" cellpadding="2" align="center">
  32. <tr>
  33. <td bgcolor="#666699"> <b><font color="#FFFFFF">Lets start!</font></b></td>
  34. </tr>
  35. <tr>
  36. <td height="90" bgcolor="#eeeeff" valign="top"> <div align="left">
  37. <div align="left">
  38. <p>In the beginning, everything as usual. Include the needed headers,
  39. ask the user for the rendering driver, create the Irrlicht Device:</p>
  40. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  41. <tr>
  42. <td><pre>#include &lt;irrlicht.h>
  43. #include &lt;iostream>
  44. using namespace irr;
  45. #pragma comment(lib, "Irrlicht.lib")
  46. int main()
  47. {
  48. // let user select driver type
  49. video::E_DRIVER_TYPE driverType = video::EDT_DIRECT3D9;
  50. 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;);
  51. char i;
  52. std::cin >> i;
  53. 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> }
  54. // create device and exit if creation failed
  55. IrrlichtDevice *device =
  56. createDevice(driverType, core::dimension2d<s32>(640, 480),
  57. 16, false, false);
  58. if (device == 0)
  59. return 1; // could not create selected driver.
  60. video::IVideoDriver* driver = device->getVideoDriver();
  61. scene::ISceneManager* smgr = device->getSceneManager();
  62. gui::IGUIEnvironment* env = device->getGUIEnvironment();</pre></td>
  63. </tr>
  64. </table>
  65. <p>Now, we load an animated mesh to be displayed. As in most examples,
  66. we'll take the fairy md2 model. The difference here: We set the shininess<br>
  67. of the model to a value other than 0 which is the default value. This
  68. enables specular highlights on the model if dynamic lighting is on.
  69. The value influences the size of the highlights.</p>
  70. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  71. <tr>
  72. <td><pre>// load and display animated fairy mesh
  73. scene::IAnimatedMeshSceneNode* fairy = smgr->addAnimatedMeshSceneNode(
  74. smgr->getMesh("../../media/faerie.md2"));
  75. if (fairy)
  76. {
  77. fairy->setMaterialTexture(0, driver->getTexture("../../media/faerie2.bmp")); // set diffuse texture
  78. fairy->setMaterialFlag(video::EMF_LIGHTING, true); // enable dynamic lighting
  79. fairy->getMaterial(0).Shininess = 20.0f; // set size of specular highlights
  80. fairy->setPosition(core::vector3df(-10,0,-100));
  81. }</pre></td>
  82. </tr>
  83. </table>
  84. <p> To make specular highlights appear on the model, we need a dynamic
  85. light in the scene. We add one directly in vicinity of the model.
  86. In addition, to make the model not that dark, we set the ambient light
  87. to gray. </p>
  88. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  89. <tr>
  90. <td><pre>
  91. // add white light
  92. scene::ILightSceneNode* light = smgr->addLightSceneNode(0,
  93. core::vector3df(-15,5,-105), video::SColorf(1.0f, 1.0f, 1.0f));
  94. // set ambient light
  95. driver->setAmbientLight(video::SColor(0,60,60,60));</pre></td>
  96. </tr>
  97. </table>
  98. <p>The next is just some standard stuff: Add a user controlled camera
  99. to the scene, disable mouse cursor, and add a test cube and let it
  100. rotate to make the scene more interesting.</p>
  101. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  102. <tr>
  103. <td><pre>
  104. // add fps camera
  105. scene::ICameraSceneNode* fpsCamera = smgr->addCameraSceneNodeFPS();
  106. fpsCamera->setPosition(core::vector3df(-50,50,-150));
  107. // disable mouse cursor
  108. device->getCursorControl()->setVisible(false);
  109. // create test cube
  110. scene::ISceneNode* test = smgr->addCubeSceneNode(60);
  111. // let the cube rotate and set some light settings
  112. scene::ISceneNodeAnimator* anim = smgr->createRotationAnimator(
  113. core::vector3df(0.3f, 0.3f,0));
  114. test->setPosition(core::vector3df(-100,0,-100));
  115. test->setMaterialFlag(video::EMF_LIGHTING, false); // disable dynamic lighting
  116. test->addAnimator(anim);
  117. anim->drop();
  118. // set window caption
  119. device->setWindowCaption(L"Irrlicht Engine - Render to Texture and Specular Highlights example");</pre></td>
  120. </tr>
  121. </table>
  122. <p> To test out the render to texture feature, we need a render target
  123. texture. These are not like standard textures, but need to be created
  124. first. To create one, we call IVideoDriver::createRenderTargetTexture()
  125. and specify the size of the texture. Please don't use sizes bigger
  126. than the frame buffer for this, because the render target shares the
  127. zbuffer with the frame buffer. And because we want to render the scene
  128. not from the user camera into the texture, we add another, fixed camera
  129. to the scene. But before we do all this, we check if the current running
  130. driver is able to render to textures. If it is not, we simply display
  131. a warning text.</p>
  132. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  133. <tr>
  134. <td><pre>// create render target
  135. video::ITexture* rt = 0;
  136. scene::ICameraSceneNode* fixedCam = 0;
  137. if (driver->queryFeature(video::EVDF_RENDER_TO_TARGET))
  138. {
  139. rt = driver->createRenderTargetTexture(core::dimension2d<s32>(256,256));
  140. test->setMaterialTexture(0, rt); // set material of cube to render target
  141. // add fixed camera
  142. fixedCam = smgr->addCameraSceneNode(0, core::vector3df(10,10,-80),
  143. core::vector3df(-10,10,-100));
  144. }
  145. else
  146. {
  147. // create problem text
  148. gui::IGUISkin* skin = env->getSkin();
  149. gui::IGUIFont* font = env->getFont("../../media/fonthaettenschweiler.bmp");
  150. if (font)
  151. skin->setFont(font);
  152. gui::IGUIStaticText* text = env->addStaticText(
  153. L"Your hardware or this renderer is not able to use the "\
  154. L"render to texture feature. RTT Disabled.",
  155. core::rect<s32>(150,20,470,60));
  156. text->setOverrideColor(video::SColor(100,255,255,255));
  157. }</pre></td>
  158. </tr>
  159. </table>
  160. <p> Nearly finished. Now we need to draw everything. Every frame, we
  161. draw the scene twice. Once from the fixed camera into the render target
  162. texture and once as usual. When rendering into the render target,
  163. we need to disable the visibilty of the test cube, because it has
  164. the render target texture applied to it.<br>
  165. That's, wasn't quite complicated I hope. :)</p>
  166. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  167. <tr>
  168. <td><pre>while(device->run())
  169. if (device->isWindowActive())
  170. {
  171. driver->beginScene(true, true, 0);
  172. if (rt)
  173. {
  174. // draw scene into render target
  175. // set render target texture
  176. driver->setRenderTarget(rt, true, true, video::SColor(0,0,0,255));
  177. // make cube invisible and set fixed camera as active camera
  178. test->setVisible(false);
  179. smgr->setActiveCamera(fixedCam);
  180. // draw whole scene into render buffer
  181. smgr->drawAll();
  182. // set back old render target
  183. driver->setRenderTarget(0);
  184. // make the cube visible and set the user controlled camera as active one
  185. test->setVisible(true);
  186. smgr->setActiveCamera(fpsCamera);
  187. }
  188. // draw scene normally
  189. smgr->drawAll();
  190. env->drawAll();
  191. driver->endScene();
  192. }
  193. if (rt)
  194. rt->drop(); // drop render target because we created if with a create() method
  195. device->drop(); // drop device
  196. return 0;
  197. }
  198. </pre></td>
  199. </tr>
  200. </table>
  201. <p>&nbsp;</p></div>
  202. </div>
  203. </td>
  204. </tr>
  205. </table>
  206. <p>&nbsp;</p>
  207. </body>
  208. </html>