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