====== LAB8: Particle Systems ====== In this lab we will be checking out particle systems, This lab will be roughly based on the "The Ogre 3D Startup Sequence" from the book "Ogre 3D 1.7 - Beginner's Guide" by Felix Kerger, Chapter 10. ===== Discussion ===== Discussion thread for this lab is here: [[https://piazza.com/class/hq7dlipqggr2qe?cid=34|Lab 8 Discussion Thread]] ===== Preperation ===== You will have to create the folder ''ParticleSystems'' where your application can see it. and add the folder into your resource file like you have done in previous labs. You can use your own project or start fresh with the base application {{:public:t-gede-14-1:lab6_handout.7z|code}} ===== Lab ===== - **Add the following code your MyApplication::createScene function** Here we are creating a particle system from the given examples that come with the ogre sdk, and attaching it to our Sinbad model. \\ You should see a "fireish" thingamajing emitting from the head of Sinbad. // Create a particle system Ogre::ParticleSystem* partSystem = _sceneManager->createParticleSystem("smoke", "Examples/Smoke"); // Attach the particle system to Sinbad _SinbadNode->attachObject(partSystem); - **Creating our own particle emitter** Now we will create our own particle system. \\ Particle systems are created in ''*.particle'' files so create a file ''MyParticleSystem.particle'' in your ParticleSystems folder. - **Adding particle code** Now we must add some code to it. This will create a particle system that can be acces via its name in the application. You can see more about particle script configuration at [[http://www.ogre3d.org/docs/manual/manual_34.html| The Ogre Particle System reference.]] particle_system MySmoke1 { // Make the particles use the "Examples/Smoke" material, and be 10x10 units large. material Examples/Smoke particle_width 10 particle_height 10 // Make the emitter limit the emitter to have at maximum 500 particles active. // And set the particle type as point, which always faces the camera. quota 500 billboard_type point // Make the emitters origin a point. emitter Point { // Make the emitter emit 3 particles per second. emission_rate 3 // Emitt the particles in the given direction, at a velocity of 20 units per second. direction 1 0 0 velocity 20 } } - **Now add additional properties to the Emitter**, Now lets make the particle emitter shoot the particles in a random fashion, and destroy them after 10 seconds alive. Additionally we will colour them red. // This will be the range of angle variation the particles will be fired at. angle 30 // Destroy the particle after 10 seconds. time_to_live 10 // Colour the particles red with a fully opaque alpha value. Colour 1 0 0 1 - **Now we will add some Additional variation** Lets make the time to live random, and give them colour a certain range. We do that by changing the time_to_live and colour attributes in the emitter block. Also change the emission rate to 30 increasing the rate of which the particles spawn. // Destroy the particle after 1 to 4 seconds. time_to_live_min 1 time_to_live_max 10 // Colour the particles linearly interpolated from red to blue. colour_range_start 1 0 0 colour_range_end 0 0 1 - **Additional attributes,** Now remove all the additional lines added in the previous steps from the particle emitter leaving only the ''emission_rate, direction and velocity''. And we will now modify the behaviour of the emitter, giving it a emission duration and restart delay. Lets make it emit particles for 1 second and then stop for one second before starting again. // Emit particles for 1 second at a time with a 1 second interval. duration 1 repeat_delay 1 - **Adding affectors** Now we will explore particle system affectors: Remove the attributes added in the last step. leaving again, only the ''emission rate, velocity and direction''. And set the ''time_to_live 10''. See: [[http://www.ogre3d.org/docs/manual/manual_40.html#DeflectorPlane-Affector|Affector Reference]]. - **Scaling affector:** Add the following line to the particle_system block, this will scale the particles by the given value per second : affector Scaler { rate 10 } - **Colour fading affector** Colour affector will add the given values to the particle colour. To use it replace the scalar affector with: // This affector will add the following values to the colour of the particles each second.รพ affector ColourFader { red 0.0 green -0.25 blue -0.25 } Color fader has some additional functionality, like changing the color in two stages. // This affector will add the following values to the colour of the particles each second. // Notice the suffix of the ColorFader(2), that is important. affector ColourFader2 { // Notice the suffix here also, red(1) this indicates the color change per second for state 1 red1 0.0 green1 -1 blue1 -1 // Set the second state when the particle has 2 second left to live. state_change 2 // And here is has the suffix 2, which in turn sets the change per second for state 2 red2 0 green2 +1 blue2 +1 // This is all works for the alpha channel to of course. alpha2 -1 } - And finally we have the ColorInterpelator. It works similarly to the ColorFader2 but with 1..6 number of states. It is used quite differently though. You provide the time on scale from 0-1 ''time# scale'' , and colours are given in the form ''colour# r g b a''. Lets create a jolly little stream: // ColourInterpolators can have up to 6 colour intervals indexed 0 to 5. affector ColourInterpolator { time0 0 colour0 1 0 0 0 time1 0.195 colour1 1 1 0 1 time2 0.39 colour2 0 1 1 1 time3 0.585 colour3 0 0 1 1 time4 0.78 colour4 1 0 1 1 time5 1 colour5 0 1 0 0 } **Note: **You have to define the default colour as ''colour 1 0 0 0'' in the point_emitter block to eliminate the stuttering when the particles spawn. Just remember to remove that for the next step. - **Direction Randomiser** We can make the particles change direction at random: // This will make the particles fly around like they have no tomorrow. affector DirectionRandomiser { // This indicates the magnitude of the random choices the particle makes. randomness 1000 // Scope governs the chance that a given particle takes a random direction change scale from 0..1 ~ 0-100% scope 1 // Here you can indicate if the particle should randomize its velocity or not. keep_velocity true } This could be useful for a pile of fireworks catching fire?, or a swarm of mosquitoes. - **Deflector Planes** Deflector planes can make the particles change direction when they hit the deflector plane, the plane is provided in point normal form. // Deflector planes bounce the particles of the given plane. affector DeflectorPlane { //The point of the plane, note: the plane is infinite plane_point 10 0 0 // The normal vector of the plane, the direction of the particles will be mirror around this normal. plane_normal -0.7 0.7 0 // The amount of velocity the particles retain after the collision. bounce 1.0 } // You can provide more than one plane. affector DeflectorPlane { plane_point 10 15 0 plane_normal -0.7 -0.7 0 bounce 1.0 } - **Different emitters** We can make the spawn point of the particles different, some of them have different parameters see: [[http://www.ogre3d.org/docs/manual/manual_38.html|Emitters]] - Point - Box - Cylinder - Ellipsoid - Hollow Ellipsoid - Ring - **Get your hands dirty.** Now create a particle system using the things you have learned. Don't just randomly assign everything to a particle effect, make something :) A fountain, bleeding wound, did Sinbad eat tacos for dinner, hint, anybody?, where will you be when #"/& strikes? What I want to see is at least: [[http://www.ogre3d.org/docs/manual/manual_36.html#Particle-Emitters|Any Emitter]], a [[http://www.ogre3d.org/docs/manual/manual_40.html#DeflectorPlane-Affector|DeflectorPlane]], any [[http://www.ogre3d.org/docs/manual/manual_40.html#ColourFader-Affector|Color Changer]], and a [[http://www.ogre3d.org/docs/manual/manual_40.html#Linear-Force-Affector|LinearForce affector]]: \\ //**A very simple fountain:**// {{ :public:t-gede-14-1:ogre_fountain.png?nolink |}} ===== Bonus ===== - **Using particle effects for something swell.** Add a glowing particle effect to Sinbads sword. Particle effects are often used for all sorts of [[https://www.youtube.com/watch?v=yLGWBKDSUVs&index=8&list=PLt5SL2R19SuJCLyqMdHMsfxZWAt0vGk2Q|special effects :)]]. You can use the same command as attaching the sword to the handle bone, just as well to attach a particle generator. //**Hint:** If Sinbad was holding a broom stick, the length of the broom would be the positive Z axis, and the y axis goes up into his arm.// // Attach a sword to Sinbads right hand. // Create the sword entity. Ogre::Entity* sword = _sceneManager->createEntity("Sword.mesh"); // Attach the sword to Sinbads right hand. _SinbadEnt->attachObjectToBone("Handle.R", sword); // Clasp Sinbads hands around the hilt of the sword. _SinbadEnt->getAnimationState("HandsClosed")->setEnabled(true); - **Celebrate Sinbads amazing sword effect.** If you followed the last point then Sinbad is surely a happy seadog, and a happy fellow deserves a show! Create some fireworks using the particle system Ogre provides. //**Hint:** Check out the [[http://www.ogre3d.org/docs/manual/manual_38.html#Emitting-Emitters|Emitter emitters]]. I'll provide you with some skeleton code.// // Use this as a base for your fireworks :) particle_system Fireworks { // Make the particles use the "Examples/Flare" material, and be 1x1 units large. material Examples/Flare particle_width 1 particle_height 1 // Make the emitter limit the emitter to have at maximum 2000 particles active. // And set the particle type as point, which always faces the camera. quota 2000 billboard_type point // The maximum amount of emitter emitters emit_emitter_quota 10 // Make the emitters origin a Point. emitter Point { // tell the particles to emitt the "bomb emitter" described below emit_emitter bomb // Fire one firework per second emission_rate 1 // Make the particles go up. direction 0 1 0 velocity 70 time_to_live 2 } // The emitter emitted by the particles above emitter Point { // Name the emitter to match the name in the emit_emitter block name bomb // Configure the explosion. emission_rate 200 velocity 30 // How long should the firework emit duration_min 0.1 duration_max 0.2 // This is used to make sure the particles in the emitter emitter dont fire while the particle is in mid flight. repeat_delay_min 1 repeat_delay_max 1.9 time_to_live_min 0.3 time_to_live_max 0.4 } // Add some pseudo gravity. affector LinearForce { force_vector 0 -50 0 } } **Note: ** Affectors affect both the emitter and the emitter emitter :){{ :public:t-gede-14-1:ogre_fireworks.png?nolink |}} - **Lastly** If you come up with a use for the particle system and think that it deserves an additional bonus point or half, then I might be generous :) \\ **Hint:** You could use the cylindrical effect from Lab 3 for interesting particle system effects :) ===== When You Are Finished ===== Upload your **commented source files** into Lab8 in MySchool (zip them up if more than one). The lab projects will not be graded, but their completion counts towards your participation grade.