main.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /** Example 008 SpecialFX
  2. This tutorial describes how to do special effects. It shows how to use stencil
  3. buffer shadows, the particle system, billboards, dynamic light, and the water
  4. surface scene node.
  5. We start like in some tutorials before. Please note that this time, the
  6. 'shadows' flag in createDevice() is set to true, for we want to have a dynamic
  7. shadow cast from an animated character. If this example runs too slow,
  8. set it to false. The Irrlicht Engine also checks if your hardware doesn't
  9. support the stencil buffer, and then disables shadows by itself.
  10. */
  11. #include <irrlicht.h>
  12. #include <iostream>
  13. #include "driverChoice.h"
  14. #include "exampleHelper.h"
  15. using namespace irr;
  16. #ifdef _MSC_VER
  17. #pragma comment(lib, "Irrlicht.lib")
  18. #endif
  19. int main()
  20. {
  21. // ask if user would like shadows
  22. char i = 'y';
  23. printf("Please press 'y' if you want to use realtime shadows.\n");
  24. std::cin >> i;
  25. const bool shadows = (i == 'y');
  26. // ask user for driver
  27. video::E_DRIVER_TYPE driverType=driverChoiceConsole();
  28. if (driverType==video::EDT_COUNT)
  29. return 1;
  30. /*
  31. Create device and exit if creation failed. We make the stencil flag
  32. optional to avoid slow screen modes for runs without shadows.
  33. */
  34. IrrlichtDevice *device =
  35. createDevice(driverType, core::dimension2d<u32>(640, 480),
  36. 16, false, shadows);
  37. if (device == 0)
  38. return 1; // could not create selected driver.
  39. video::IVideoDriver* driver = device->getVideoDriver();
  40. scene::ISceneManager* smgr = device->getSceneManager();
  41. const io::path mediaPath = getExampleMediaPath();
  42. /*
  43. For our environment, we load a .3ds file. It is a small room I modeled
  44. with Anim8or and exported into the 3ds format because the Irrlicht
  45. Engine does not support the .an8 format. I am a very bad 3d graphic
  46. artist, and so the texture mapping is not very nice in this model.
  47. Luckily I am a better programmer than artist, and so the Irrlicht
  48. Engine is able to create a cool texture mapping for me: Just use the
  49. mesh manipulator and create a planar texture mapping for the mesh. If
  50. you want to see the mapping I made with Anim8or, uncomment this line. I
  51. also did not figure out how to set the material right in Anim8or, it
  52. has a specular light color which I don't really like. I'll switch it
  53. off too with this code.
  54. */
  55. scene::IAnimatedMesh* mesh = smgr->getMesh(mediaPath + "room.3ds");
  56. smgr->getMeshManipulator()->makePlanarTextureMapping(mesh->getMesh(0), 0.004f);
  57. scene::ISceneNode* node = 0;
  58. node = smgr->addAnimatedMeshSceneNode(mesh);
  59. node->setMaterialTexture(0, driver->getTexture(mediaPath + "wall.jpg"));
  60. node->getMaterial(0).SpecularColor.set(0,0,0,0);
  61. /*
  62. Now, for the first special effect: Animated water. It works like this:
  63. The WaterSurfaceSceneNode takes a mesh as input and makes it wave like
  64. a water surface. And if we let this scene node use a nice material like
  65. the EMT_REFLECTION_2_LAYER, it looks really cool. We are doing this
  66. with the next few lines of code. As input mesh, we create a hill plane
  67. mesh, without hills. But any other mesh could be used for this, you
  68. could even use the room.3ds (which would look really strange) if you
  69. want to.
  70. */
  71. mesh = smgr->addHillPlaneMesh( "myHill",
  72. core::dimension2d<f32>(20,20),
  73. core::dimension2d<u32>(40,40), 0, 0,
  74. core::dimension2d<f32>(0,0),
  75. core::dimension2d<f32>(10,10));
  76. node = smgr->addWaterSurfaceSceneNode(mesh->getMesh(0), 3.0f, 300.0f, 30.0f);
  77. node->setPosition(core::vector3df(0,7,0));
  78. node->setMaterialTexture(0, driver->getTexture(mediaPath + "stones.jpg"));
  79. node->setMaterialTexture(1, driver->getTexture(mediaPath + "water.jpg"));
  80. node->setMaterialType(video::EMT_REFLECTION_2_LAYER);
  81. /*
  82. The second special effect is very basic, I bet you saw it already in
  83. some Irrlicht Engine demos: A transparent billboard combined with a
  84. dynamic light. We simply create a light scene node, let it fly around,
  85. and to make it look more cool, we attach a billboard scene node to it.
  86. */
  87. // create light
  88. scene::ILightSceneNode * lightNode = smgr->addLightSceneNode(0, core::vector3df(0,0,0),
  89. video::SColorf(1.0f, 0.6f, 0.7f, 1.0f), 800.0f);
  90. scene::ISceneNodeAnimator* anim = 0;
  91. anim = smgr->createFlyCircleAnimator (core::vector3df(0,150,0),250.0f, 0.0005f);
  92. lightNode ->addAnimator(anim);
  93. anim->drop();
  94. // attach billboard to light
  95. node = smgr->addBillboardSceneNode(lightNode, core::dimension2d<f32>(50, 50));
  96. node->setMaterialFlag(video::EMF_LIGHTING, false);
  97. node->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR);
  98. node->setMaterialTexture(0, driver->getTexture(mediaPath + "particlewhite.bmp"));
  99. /*
  100. The next special effect is a lot more interesting: A particle system.
  101. The particle system in the Irrlicht Engine is quite modular and
  102. extensible, but yet easy to use. There is a particle system scene node
  103. into which you can put a particle emitter, which makes particles come out
  104. of nothing. These emitters are quite flexible and usually have lots of
  105. parameters like direction, amount, and color of the particles they
  106. create.
  107. There are different emitters, for example a point emitter which lets
  108. particles pop out at a fixed point. If the particle emitters available
  109. in the engine are not enough for you, you can easily create your own
  110. ones, you'll simply have to create a class derived from the
  111. IParticleEmitter interface and attach it to the particle system using
  112. setEmitter(). In this example we create a box particle emitter, which
  113. creates particles randomly inside a box. The parameters define the box,
  114. direction of the particles, minimal and maximal new particles per
  115. second, color, and minimal and maximal lifetime of the particles.
  116. Because only with emitters particle system would be a little bit
  117. boring, there are particle affectors which modify particles while
  118. they fly around. Affectors can be added to a particle system for
  119. simulating additional effects like gravity or wind.
  120. The particle affector we use in this example is an affector which
  121. modifies the color of the particles: It lets them fade out. Like the
  122. particle emitters, additional particle affectors can also be
  123. implemented by you, simply derive a class from IParticleAffector and
  124. add it with addAffector().
  125. After we set a nice material to the particle system, we have a cool
  126. looking camp fire. By adjusting material, texture, particle emitter,
  127. and affector parameters, it is also easily possible to create smoke,
  128. rain, explosions, snow, and so on.
  129. */
  130. // create a particle system
  131. scene::IParticleSystemSceneNode* ps =
  132. smgr->addParticleSystemSceneNode(false);
  133. if (ps)
  134. {
  135. scene::IParticleEmitter* em = ps->createBoxEmitter(
  136. core::aabbox3d<f32>(-7,0,-7,7,1,7), // emitter size
  137. core::vector3df(0.0f,0.06f,0.0f), // initial direction
  138. 80,100, // emit rate
  139. video::SColor(0,255,255,255), // darkest color
  140. video::SColor(0,255,255,255), // brightest color
  141. 800,2000,0, // min and max age, angle
  142. core::dimension2df(10.f,10.f), // min size
  143. core::dimension2df(20.f,20.f)); // max size
  144. ps->setEmitter(em); // this grabs the emitter
  145. em->drop(); // so we can drop it here without deleting it
  146. scene::IParticleAffector* paf = ps->createFadeOutParticleAffector();
  147. ps->addAffector(paf); // same goes for the affector
  148. paf->drop();
  149. ps->setPosition(core::vector3df(-70,60,40));
  150. ps->setScale(core::vector3df(2,2,2));
  151. ps->setMaterialFlag(video::EMF_LIGHTING, false);
  152. ps->setMaterialFlag(video::EMF_ZWRITE_ENABLE, false);
  153. ps->setMaterialTexture(0, driver->getTexture(mediaPath + "fire.bmp"));
  154. ps->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR);
  155. }
  156. /*
  157. Next we add a volumetric light node, which adds a glowing fake area light to
  158. the scene. Like with the billboards and particle systems we also assign a
  159. texture for the desired effect, though this time we'll use a texture animator
  160. to create the illusion of a magical glowing area effect.
  161. */
  162. scene::IVolumeLightSceneNode * n = smgr->addVolumeLightSceneNode(0, -1,
  163. 32, // Subdivisions on U axis
  164. 32, // Subdivisions on V axis
  165. video::SColor(0, 255, 255, 255), // foot color
  166. video::SColor(0, 0, 0, 0)); // tail color
  167. if (n)
  168. {
  169. n->setScale(core::vector3df(56.0f, 56.0f, 56.0f));
  170. n->setPosition(core::vector3df(-120,50,40));
  171. // load textures for animation
  172. core::array<video::ITexture*> textures;
  173. for (s32 g=7; g > 0; --g)
  174. {
  175. core::stringc tmp(mediaPath);
  176. tmp += "portal";
  177. tmp += g;
  178. tmp += ".bmp";
  179. video::ITexture* t = driver->getTexture( tmp.c_str() );
  180. textures.push_back(t);
  181. }
  182. // create texture animator
  183. scene::ISceneNodeAnimator* glow = smgr->createTextureAnimator(textures, 150);
  184. // add the animator
  185. n->addAnimator(glow);
  186. // drop the animator because it was created with a create() function
  187. glow->drop();
  188. }
  189. /*
  190. As our last special effect, we want a dynamic shadow be cast from an
  191. animated character. For this we load a DirectX .x model and place it
  192. into our world. For creating the shadow, we simply need to call
  193. addShadowVolumeSceneNode(). The color of shadows is only adjustable
  194. globally for all shadows, by calling ISceneManager::setShadowColor().
  195. Voila, here is our dynamic shadow.
  196. Because the character is a little bit too small for this scene, we make
  197. it bigger using setScale(). And because the character is lighted by a
  198. dynamic light, we need to normalize the normals to make the lighting on
  199. it correct. This is always necessary if the scale of a dynamic lighted
  200. model is not (1,1,1). Otherwise it would get too dark or too bright
  201. because the normals will be scaled too.
  202. */
  203. // add animated character
  204. mesh = smgr->getMesh(mediaPath + "dwarf.x");
  205. scene::IAnimatedMeshSceneNode* anode = 0;
  206. anode = smgr->addAnimatedMeshSceneNode(mesh);
  207. anode->setPosition(core::vector3df(-50,20,-60));
  208. anode->setAnimationSpeed(15);
  209. /*
  210. Shadows still have to be drawn even then the node causing them is not visible itself.
  211. We have to disable culling if the node is animated or it's transformations change
  212. as otherwise the shadow is not updated correctly.
  213. If you have many objects and this becomes a speed problem you will have to figure
  214. out some manual culling (for exampling hiding all objects beyond a certain distance).
  215. */
  216. anode->setAutomaticCulling(scene::EAC_OFF);
  217. // add shadow
  218. anode->addShadowVolumeSceneNode();
  219. smgr->setShadowColor(video::SColor(150,0,0,0));
  220. // make the model a bit bigger
  221. anode->setScale(core::vector3df(2,2,2));
  222. // because of the scaling we have to normalize its normals for correct lighting
  223. anode->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, true);
  224. // let the dwarf slowly rotate around it's y axis
  225. scene::ISceneNodeAnimator* ra = smgr->createRotationAnimator(irr::core::vector3df(0, 0.1f, 0));
  226. anode->addAnimator(ra);
  227. ra->drop();
  228. /*
  229. Finally we simply have to draw everything, that's all.
  230. */
  231. scene::ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS();
  232. camera->setPosition(core::vector3df(-50,50,-150));
  233. camera->setFarValue(10000.0f); // this increase a shadow visible range.
  234. // disable mouse cursor
  235. device->getCursorControl()->setVisible(false);
  236. s32 lastFPS = -1;
  237. while(device->run())
  238. if (device->isWindowActive())
  239. {
  240. driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, video::SColor(0));
  241. smgr->drawAll();
  242. driver->endScene();
  243. const s32 fps = driver->getFPS();
  244. if (lastFPS != fps)
  245. {
  246. core::stringw str = L"Irrlicht Engine - SpecialFX example [";
  247. str += driver->getName();
  248. str += "] FPS:";
  249. str += fps;
  250. device->setWindowCaption(str.c_str());
  251. lastFPS = fps;
  252. }
  253. }
  254. device->drop();
  255. return 0;
  256. }
  257. /*
  258. **/