tutorial.html 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 11. Per pixel lighting</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 use one of the built in more complex materials
  21. in irrlicht: Per pixel lighted surfaces using normal maps and parallax
  22. mapping. It will also show how to use fog and moving particle systems.
  23. And don't panic: You dont need any experience with shaders to use these
  24. materials in Irrlicht.</p>
  25. <p>The program which is described here will look like this:</p>
  26. <p align="center"><img src="../../media/011shot.jpg" width="258" height="202"><br>
  27. </p>
  28. </div>
  29. </td>
  30. </tr>
  31. </table>
  32. <br>
  33. <table width="95%" border="0" cellspacing="0" cellpadding="2" align="center">
  34. <tr>
  35. <td bgcolor="#666699"> <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. <div align="left">
  40. <p>At first, we need to include all headers and do the stuff we always
  41. do, like in nearly all other tutorials.</p>
  42. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  43. <tr>
  44. <td> <pre>#include &lt;irrlicht.h&gt;<br>#include &lt;iostream&gt;<br><br>using namespace irr;<br><br>#pragma comment(lib, &quot;Irrlicht.lib&quot;)<br></pre></td>
  45. </tr>
  46. </table>
  47. <p>For this example, we need an event receiver, to make it possible
  48. for the user to switch between the three available material types.
  49. In addition, the event receiver will create some small GUI window
  50. which displays what material is currently being used. There is nothing
  51. special done in this class, so maybe you want to skip reading it.</p>
  52. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  53. <tr>
  54. <td><pre>class MyEventReceiver : public IEventReceiver
  55. {
  56. public:
  57. MyEventReceiver(scene::ISceneNode* room,
  58. gui::IGUIEnvironment* env, video::IVideoDriver* driver)
  59. {
  60. // store pointer to room so we can change its drawing mode
  61. Room = room;
  62. Driver = driver;
  63. // set a nicer font
  64. gui::IGUISkin* skin = env->getSkin();
  65. gui::IGUIFont* font = env->getFont("../../media/fonthaettenschweiler.bmp");
  66. if (font)
  67. skin->setFont(font);
  68. // add window and listbox
  69. gui::IGUIWindow* window = env->addWindow(
  70. core::rect<s32>(490,390,630,470), false, L"Use 'E' + 'R' to change");
  71. ListBox = env->addListBox(
  72. core::rect<s32>(2,22,135,78), window);
  73. ListBox->addItem(L"Diffuse");
  74. ListBox->addItem(L"Bump mapping");
  75. ListBox->addItem(L"Parallax mapping");
  76. ListBox->setSelected(1);
  77. // create problem text
  78. ProblemText = env->addStaticText(
  79. L"Your hardware or this renderer is not able to use the "\
  80. L"needed shaders for this material. Using fall back materials.",
  81. core::rect<s32>(150,20,470,60));
  82. ProblemText->setOverrideColor(video::SColor(100,255,255,255));
  83. // set start material (prefer parallax mapping if available)
  84. video::IMaterialRenderer* renderer =
  85. Driver->getMaterialRenderer(video::EMT_PARALLAX_MAP_SOLID);
  86. if (renderer && renderer->getRenderCapability() == 0)
  87. ListBox->setSelected(2);
  88. // set the material which is selected in the listbox
  89. setMaterial();
  90. }
  91. bool OnEvent(const SEvent& event)
  92. {
  93. // check if user presses the key 'E' or 'R'
  94. if (event.EventType == irr::EET_KEY_INPUT_EVENT &&
  95. !event.KeyInput.PressedDown && Room && ListBox)
  96. {
  97. // change selected item in listbox
  98. int sel = ListBox->getSelected();
  99. if (event.KeyInput.Key == irr::KEY_KEY_R)
  100. ++sel;
  101. else
  102. if (event.KeyInput.Key == irr::KEY_KEY_E)
  103. --sel;
  104. else
  105. return false;
  106. if (sel > 2) sel = 0;
  107. if (sel < 0) sel = 2;
  108. ListBox->setSelected(sel);
  109. // set the material which is selected in the listbox
  110. setMaterial();
  111. }
  112. return false;
  113. }
  114. private:
  115. // sets the material of the room mesh the the one set in the
  116. // list box.
  117. void setMaterial()
  118. {
  119. video::E_MATERIAL_TYPE type = video::EMT_SOLID;
  120. // change material setting
  121. switch(ListBox->getSelected())
  122. {
  123. case 0: type = video::EMT_SOLID;
  124. break;
  125. case 1: type = video::EMT_NORMAL_MAP_SOLID;
  126. break;
  127. case 2: type = video::EMT_PARALLAX_MAP_SOLID;
  128. break;
  129. }
  130. Room->setMaterialType(type);</pre>
  131. </td>
  132. </tr>
  133. </table>
  134. <p>We need to add a warning if the materials will not be able to be
  135. displayed 100% correctly. This is no problem, they will be renderered
  136. using fall back materials, but at least the user should know that
  137. it would look better on better hardware. We simply check if the material
  138. renderer is able to draw at full quality on the current hardware.
  139. The IMaterialRenderer::getRenderCapability() returns 0 if this is
  140. the case.<br>
  141. </p>
  142. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  143. <tr>
  144. <td> <pre>video::IMaterialRenderer* renderer = Driver->getMaterialRenderer(type);
  145. // display some problem text when problem
  146. if (!renderer || renderer->getRenderCapability() != 0)
  147. ProblemText->setVisible(true);
  148. else
  149. ProblemText->setVisible(false);
  150. }
  151. private:
  152. gui::IGUIStaticText* ProblemText;
  153. gui::IGUIListBox* ListBox;
  154. scene::ISceneNode* Room;
  155. video::IVideoDriver* Driver;
  156. };</pre></td>
  157. </tr>
  158. </table>
  159. <p><br>
  160. Now for the real fun. We create an Irrlicht Device and start to setup
  161. the scene.<br>
  162. </p>
  163. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  164. <tr>
  165. <td> <pre>int main()
  166. {
  167. // let user select driver type
  168. video::E_DRIVER_TYPE driverType = video::EDT_DIRECT3D9;
  169. <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>
  170. char i;
  171. std::cin >> i;
  172. 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 0;<br> }
  173. // create device
  174. IrrlichtDevice* device = createDevice(driverType, core::dimension2d<s32>(640, 480));
  175. if (device == 0)
  176. return 1; // could not create selected driver.
  177. </pre></td>
  178. </tr>
  179. </table>
  180. <br>
  181. Before we start with the interesting stuff, we do some simple things:
  182. Store pointers to the most important parts of the engine (video driver,<br>
  183. scene manager, gui environment) to safe us from typing too much, add
  184. an irrlicht engine logo to the window and a user controlled first person
  185. shooter style camera. Also, we let the engine now that it should store
  186. all textures in 32 bit. This necessary because for parallax mapping,
  187. we need 32 bit textures.<br>
  188. <br>
  189. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  190. <tr>
  191. <td> <pre>
  192. video::IVideoDriver* driver = device->getVideoDriver();
  193. scene::ISceneManager* smgr = device->getSceneManager();
  194. gui::IGUIEnvironment* env = device->getGUIEnvironment();
  195. driver->setTextureCreationFlag(video::ETCF_ALWAYS_32_BIT, true);
  196. // add irrlicht logo
  197. env->addImage(driver->getTexture("../../media/irrlichtlogoalpha.tga"),
  198. core::position2d<s32>(10,10));
  199. // add camera
  200. scene::ICameraSceneNode* camera =
  201. smgr->addCameraSceneNodeFPS(0,100.0f,300.0f);
  202. camera->setPosition(core::vector3df(-200,200,-200));
  203. // disable mouse cursor
  204. device->getCursorControl()->setVisible(false);</pre></td>
  205. </tr>
  206. </table>
  207. <br>
  208. Because we want the whole scene to look a little bit scarier, we add
  209. some fog to it. This is done by a call to IVideoDriver::setFog(). There
  210. you can set<br>
  211. various fog settings. In this example, we use pixel fog, because it
  212. will work well with the materials we'll use in this example. Please
  213. note that you will have to set the material flag EMF_FOG_ENABLE to 'true'
  214. in every scene node which should be affected by this fog.<br>
  215. <br>
  216. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  217. <tr>
  218. <td> <pre>driver-&gt;setFog(video::SColor(0,138,125,81), true, 250, 1000, 0, true);<br></pre></td>
  219. </tr>
  220. </table>
  221. <br>
  222. To be able to display something interesting, we load a mesh from a .3ds
  223. file which is a room I modeled with anim8or. It is the same room as
  224. <br>
  225. from the specialFX example. Maybe you remember from that tutorial, I
  226. am no good modeler at all and so I totally messed up the texture mapping
  227. in this model, but we can simply repair it with the IMeshManipulator::makePlanarTextureMapping()
  228. method.<br>
  229. <br>
  230. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  231. <tr>
  232. <td> <pre> scene::IAnimatedMesh* roomMesh = smgr->getMesh(
  233. "../../media/room.3ds");
  234. scene::ISceneNode* room = 0;
  235. if (roomMesh)
  236. {
  237. smgr->getMeshManipulator()->makePlanarTextureMapping(
  238. roomMesh->getMesh(0), 0.003f);</pre></td>
  239. </tr>
  240. </table>
  241. <br>
  242. Now for the first exciting thing: If we successfully loaded the mesh
  243. we need to apply textures to it. Because we want this room to be displayed
  244. with a very cool material, we have to do a little bit more than just
  245. set the textures. Instead of only loading a color map as usual, we also
  246. load a height map which is simply a grayscale texture. From this height
  247. map, we create a normal map which we will set as second texture of the
  248. room. If you already have a normal map, you could directly set it, but
  249. I simply didn&acute;t find a nice normal map for this texture. The normal
  250. map texture is being generated by the makeNormalMapTexture method<br>
  251. of the VideoDriver. The second parameter specifies the height of the
  252. heightmap. If you set it to a bigger value, the map will look more rocky.<br>
  253. <br>
  254. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  255. <tr>
  256. <td> <pre> video::ITexture* colorMap = driver->getTexture("../../media/rockwall.bmp");
  257. video::ITexture* normalMap = driver->getTexture("../../media/rockwall_height.bmp");
  258. driver->makeNormalMapTexture(normalMap, 9.0f);</pre></td>
  259. </tr>
  260. </table>
  261. <br>
  262. But just setting color and normal map is not everything. The material
  263. we want to use needs some additional informations per vertex like tangents
  264. and binormals.<br>
  265. Because we are too lazy to calculate that information now, we let Irrlicht
  266. do this for us. That's why we call IMeshManipulator::createMeshWithTangents().
  267. It<br>
  268. creates a mesh copy with tangents and binormals from any other mesh.
  269. After we've done that, we simply create a standard mesh scene node with
  270. this<br>
  271. mesh copy, set color and normal map and adjust some other material settings.
  272. Note that we set EMF_FOG_ENABLE to true to enable fog in the room.<br>
  273. <br>
  274. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  275. <tr>
  276. <td> <pre>scene::IMesh* tangentMesh = smgr-&gt;getMeshManipulator()-&gt;createMeshWithTangents(<br> roomMesh-&gt;getMesh(0));<br> <br> room = smgr-&gt;addMeshSceneNode(tangentMesh);<br> room-&gt;setMaterialTexture(0, colorMap);<br> room-&gt;setMaterialTexture(1, normalMap);<br> room-&gt;getMaterial(0).SpecularColor.set(0,0,0,0);<br> room-&gt;setMaterialFlag(video::EMF_FOG_ENABLE, true);<br> room-&gt;setMaterialType(video::EMT_PARALLAX_MAP_SOLID); <br> room-&gt;getMaterial(0).MaterialTypeParam = 0.02f; // adjust height for parallax effect<br> // drop mesh because we created it with a create.. call.<br> tangentMesh-&gt;drop();<br> }<br></pre></td>
  277. </tr>
  278. </table>
  279. <br>
  280. After we've created a room shaded by per pixel lighting, we add a sphere
  281. into it with the same material, but we'll make it transparent. In addition,<br>
  282. because the sphere looks somehow like a familiar planet, we make it
  283. rotate. The procedure is similar as before. The difference is that we
  284. are loading <br>
  285. the mesh from an .x file which already contains a color map so we do
  286. not need to load it manually. But the sphere is a little bit too small
  287. for our needs, so we scale it by the factor 50.<br>
  288. <br>
  289. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  290. <tr>
  291. <td> <pre>// add earth sphere
  292. scene::IAnimatedMesh* earthMesh = smgr->getMesh("../../media/earth.x");
  293. if (earthMesh)
  294. {
  295. // create mesh copy with tangent informations from original earth.x mesh
  296. scene::IMesh* tangentSphereMesh =
  297. smgr->getMeshManipulator()->createMeshWithTangents(earthMesh->getMesh(0));
  298. // set the alpha value of all vertices to 200
  299. smgr->getMeshManipulator()->setVertexColorAlpha(tangentSphereMesh, 200);
  300. // scale the mesh by factor 50
  301. smgr->getMeshManipulator()->scaleMesh(
  302. tangentSphereMesh, core::vector3df(50,50,50));
  303. // create mesh scene node
  304. scene::ISceneNode* sphere = smgr->addMeshSceneNode(tangentSphereMesh);
  305. sphere->setPosition(core::vector3df(-70,130,45));
  306. // load heightmap, create normal map from it and set it
  307. video::ITexture* earthNormalMap = driver->getTexture("../../media/earthbump.bmp");
  308. driver->makeNormalMapTexture(earthNormalMap, 20.0f);
  309. sphere->setMaterialTexture(1, earthNormalMap);
  310. // adjust material settings
  311. sphere->setMaterialFlag(video::EMF_FOG_ENABLE, true);
  312. sphere->setMaterialType(video::EMT_NORMAL_MAP_TRANSPARENT_VERTEX_ALPHA);
  313. // add rotation animator
  314. scene::ISceneNodeAnimator* anim =
  315. smgr->createRotationAnimator(core::vector3df(0,0.1f,0));
  316. sphere->addAnimator(anim);
  317. anim->drop();
  318. // drop mesh because we created it with a create.. call.
  319. tangentSphereMesh->drop();
  320. }</pre></td>
  321. </tr>
  322. </table>
  323. <br>
  324. Per pixel lighted materials only look cool when there are moving lights.
  325. So we add some. And because moving lights alone are so boring, we add
  326. billboards <br>
  327. to them, and a whole particle system to one of them. We start with the
  328. first light which is red and has only the billboard attached.<br>
  329. <br>
  330. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  331. <tr>
  332. <td> <pre>// add light 1 (nearly red)
  333. scene::ILightSceneNode* light1 =
  334. smgr->addLightSceneNode(0, core::vector3df(0,0,0),
  335. video::SColorf(0.5f, 1.0f, 0.5f, 0.0f), 200.0f);
  336. // add fly circle animator to light 1
  337. scene::ISceneNodeAnimator* anim =
  338. smgr->createFlyCircleAnimator (core::vector3df(50,300,0),190.0f, -0.003f);
  339. light1->addAnimator(anim);
  340. anim->drop();
  341. // attach billboard to the light
  342. scene::ISceneNode* bill =
  343. smgr->addBillboardSceneNode(light1, core::dimension2d<f32>(60, 60));
  344. bill->setMaterialFlag(video::EMF_LIGHTING, false);
  345. bill->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR);
  346. bill->setMaterialTexture(0, driver->getTexture("../../media/particlered.bmp"));</pre></td>
  347. </tr>
  348. </table>
  349. <br>
  350. Now the same again, with the second light. The difference is that we
  351. add a particle system to it too. And because the light moves, the particles
  352. of the particlesystem will follow. If you want to know more about how
  353. particle systems are created in Irrlicht, take a look at the specialFx
  354. example.<br>
  355. Maybe you will have noticed that we only add 2 lights, this has a simple
  356. reason: The low end version of this material was written in ps1.1 and
  357. vs1.1, which doesn't allow more lights. You could add a third light
  358. to the scene, but it won't be used to shade the walls. But of course,
  359. this will change in future versions of Irrlicht were higher versions
  360. of pixel/vertex shaders will be implemented too.<br>
  361. <br>
  362. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  363. <tr>
  364. <td> <pre>// add light 2 (gray)
  365. scene::ISceneNode* light2 =
  366. smgr->addLightSceneNode(0, core::vector3df(0,0,0),
  367. video::SColorf(1.0f, 0.2f, 0.2f, 0.0f), 200.0f);
  368. // add fly circle animator to light 2
  369. anim = smgr->createFlyCircleAnimator (core::vector3df(0,150,0),200.0f);
  370. light2->addAnimator(anim);
  371. anim->drop();
  372. // attach billboard to light
  373. bill = smgr->addBillboardSceneNode(light2, core::dimension2d<f32>(120, 120));
  374. bill->setMaterialFlag(video::EMF_LIGHTING, false);
  375. bill->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR);
  376. bill->setMaterialTexture(0, driver->getTexture("../../media/particlewhite.bmp"));
  377. // add particle system
  378. scene::IParticleSystemSceneNode* ps =
  379. smgr->addParticleSystemSceneNode(false, light2);
  380. ps->setParticleSize(core::dimension2d<f32>(30.0f, 40.0f));
  381. // create and set emitter
  382. scene::IParticleEmitter* em = ps->createBoxEmitter(
  383. core::aabbox3d<f32>(-3,0,-3,3,1,3),
  384. core::vector3df(0.0f,0.03f,0.0f),
  385. 80,100,
  386. video::SColor(0,255,255,255), video::SColor(0,255,255,255),
  387. 400,1100);
  388. ps->setEmitter(em);
  389. em->drop();
  390. // create and set affector
  391. scene::IParticleAffector* paf = ps->createFadeOutParticleAffector();
  392. ps->addAffector(paf);
  393. paf->drop();
  394. // adjust some material settings
  395. ps->setMaterialFlag(video::EMF_LIGHTING, false);
  396. ps->setMaterialTexture(0, driver->getTexture("../../media/fireball.bmp"));
  397. ps->setMaterialType(video::EMT_TRANSPARENT_VERTEX_ALPHA);
  398. MyEventReceiver receiver(room, env, driver);
  399. device->setEventReceiver(&receiver);</pre></td>
  400. </tr>
  401. </table>
  402. <br>
  403. Finally, draw everything. That's it.<br>
  404. <br>
  405. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  406. <tr>
  407. <td> <pre>int lastFPS = -1;
  408. while(device->run())
  409. if (device->isWindowActive())
  410. {
  411. driver->beginScene(true, true, 0);
  412. smgr->drawAll();
  413. env->drawAll();
  414. driver->endScene();
  415. int fps = driver->getFPS();
  416. if (lastFPS != fps)
  417. {
  418. core::stringw str = L"Per pixel lighting example - Irrlicht Engine [";
  419. str += driver->getName();
  420. str += "] FPS:";
  421. str += fps;
  422. device->setWindowCaption(str.c_str());
  423. lastFPS = fps;
  424. }
  425. }
  426. device->drop();
  427. return 0;
  428. }
  429. </pre></td>
  430. </tr>
  431. </table>
  432. <br>
  433. </div>
  434. </div>
  435. </td>
  436. </tr>
  437. </table>
  438. <p>&nbsp;</p>
  439. </body>
  440. </html>