October 31, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Cross-Platform Game Development for C++ Developers, Part III: The 3D Irrlicht Engine

  • August 24, 2005
  • By Victor Volkman
  • Send Email »
  • More Articles »

Particle System

The next special effect is a lot more interesting: a particle system. The particle system in the Irrlicht Engine is both modular and extensible, yet still easy to use. You simply put the particle emitters into a particle system scene node and thus the particles seem to appear from nothing. These emitters are quite configurable and usually have lots of parameters like particle direction, amount, and color.

Of course, emitter types differ (for example, a point emitter that lets particles pop out at a fixed point). If the particle emitters available in the engine are not enough for you, you can easily create your own. Simply derive a new class from the IParticleEmitter interface and attach it to the particle system using setEmitter().

The next example creates a box particle emitter. As you might expect, it creates particles randomly inside a bounding box. The parameters define the box, direction of the particles, minimal and maximal new particles per second, color, and minimal and maximal lifetime of the particles.

A particle system composed entirely of emitters would be both boring and lacking in realism. Therefore, Irrlicht supports particle affectors, which modify particles as they fly around. Once added to the particle system, they can simulate additional effects like gravity or wind. The particle affector in this example simply modifies the color of the particles to produce a fade-out effect.

As you might have guessed, particle affectors are implemented by deriving from IParticleAffector and adding them with addAffector(). After you set a nice material to the particle system, you have a cool-looking campfire. By adjusting material, texture, and particle emitter and affector parameters, you also can easily create smoke, rain, explosions, snow, and so on:

scene::IParticleSystemSceneNode* ps = 0;
ps = smgr->addParticleSystemSceneNode(false);
ps->setPosition(core::vector3df(-70,60,40));
ps->setScale(core::vector3df(2,2,2));
ps->setParticleSize(core::dimension2d(20.0f, 10.0f));

scene::IParticleEmitter* em = ps->createBoxEmitter(
   core::aabbox3d(-7,0,-7,7,1,7),
   core::vector3df(0.0f,0.03f,0.0f),
   80,100,
   video::SColor(0,255,255,255), video::SColor(0,255,255,255),
   800,2000);
ps->setEmitter(em);
em->drop();
scene::IParticleAffector* paf =
   ps->createFadeOutParticleAffector();

ps->addAffector(paf);
paf->drop();
ps->setMaterialFlag(video::EMF_LIGHTING, false);
ps->setMaterialTexture(0, driver->getTexture,"particle.bmp"));
ps->setMaterialType(video::EMT_TRANSPARENT_VERTEX_ALPHA);

Shadow Cast

Last but not least, you want a dynamic shadow cast from an animated character. For this, you load a Quake2 .md2 model file and place it into your world. To create the shadow, you just call addShadowVolumeSceneNode(). You control the color of shadows, which is adjustable only globally and affects all shadows, by calling ISceneManager::setShadowColor(). Voilà! Here is your dynamic shadow:

mesh = smgr->getMesh("../../media/faerie.md2");
scene::IAnimatedMeshSceneNode* anode = 0;
anode = smgr->addAnimatedMeshSceneNode(mesh);

anode->setPosition(core::vector3df(-50,45,-60));
anode->setMD2Animation(scene::EMAT_STAND);
anode->setMaterialTexture(0, driver->getTexture
                         ("../../media/Faerie5.BMP"));
anode->addShadowVolumeSceneNode();
smgr->setShadowColor(video::SColor(220,0,0,0));

Game Loop

Finally, you can enter the game loop controlled by the device->run() method. The loop will run until the device quits by getting a close window event (such as the ALT-F4 keystroke in Windows). Everything must be drawn between a beginScene() and an endScene() call. The beginScene() clears the screen with a color and also the depth buffer, if desired. Then you let the Scene Manager and the GUI environment draw their content. With the endScene() call, everything is presented on the screen. In this example, you also dynamically display the frames per second (FPS) figure, which is all-important to the serious gamer, on the titlebar:

scene::ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS();

camera->setPosition(core::vector3df(-50,50,-150));

int lastFPS = -1;
while(device->run())
{
   driver->beginScene(true, true, 0);
   smgr->drawAll();
   driver->endScene();
   int fps = driver->getFPS();
   if (lastFPS != fps)
   {
      core::stringw str = L"Campfire FX example [";
      str += driver->getName();
      str += "] FPS:";
      str += fps;
      device->setWindowCaption(str.c_str());
      lastFPS = fps;
   }
}

device->drop();

After you have finished, you must delete the Irrlicht Device created earlier with createDevice(). With the Irrlicht Engine, you should delete all objects you created with a method or function that starts with 'create'. The object is deleted simply by calling device->drop().

Irrlicht Add-ons You Might Like

As mentioned in the introduction, Irrlicht has a thriving community of independent developers producing add-ons and games. Many improvements by these people are re-integrated into a subsequent Irrlicht release. Here's a brief list of a few I thought would interest prospective developers:

  • OCTTools is a set of tools for Irrlicht created by Murphy McCauley for working with OCT-files: exporters, loaders, and much more.
  • ICE (Irrlicht Common Engine) Framework provides a skeleton project enhancing rapid production of new projects.
  • MIM by Murphy McCauley is a very useful XML-based file format for Irrlicht with loaders, converters, tools, and so on.
  • My3D is a toolkit enabling you to export light-mapped scenes from various 3D packages (3DStudio MAX, Giles, and the like) directly into Irrlicht.
  • Dusty Engine allows programmers to create "tasks" that do anything the programmer wants them to do. The tasks are then added to a general tree of tasks, where each task can have as many children tasks as the designer desires. The "grouping" of tasks allows the game designer to perform common operations such as pause, resume, or destroy on an entire tree.
  • Irrlicht RPG (Erring Light) is a 3D walk-around game engine being developed with RPG-style play in mind.
  • 2D Image and Sprite classes comprise a useful library that extends Irrlicht's 2D capabilities.
  • Zenprogramming is home to the first unofficial external terrain renderer for Irrlicht and a place for tutorials.

Stay Tuned!

Be sure to come back next week for Cross-Platform Game Development for C++ Developers, Part IV.

About the Author

Victor Volkman has been writing for C/C++ Users Journal and other programming journals since the late 1980s. He is a graduate of Michigan Tech and a faculty advisor board member for Washtenaw Community College CIS department. Volkman is the editor of numerous books, including C/C++ Treasure Chest and is the owner of Loving Healing Press. He can help you in your quest for open source tools and libraries, just drop an e-mail to sysop@HAL9K.com.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel