root/OpenSceneGraph/trunk/examples/osgparticle/osgparticle.cpp
@
10827
| Revision 10827, 19.7 kB (checked in by robert, 3 years ago) | |
|---|---|
|
|
| Rev | Line | |
|---|---|---|
| [6941] | 1 | /* OpenSceneGraph example, osgparticle. |
| 2 | * | |
| 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 4 | * of this software and associated documentation files (the "Software"), to deal | |
| 5 | * in the Software without restriction, including without limitation the rights | |
| 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 7 | * copies of the Software, and to permit persons to whom the Software is | |
| 8 | * furnished to do so, subject to the following conditions: | |
| 9 | * | |
| 10 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 11 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 13 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 14 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 15 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 16 | * THE SOFTWARE. | |
| 17 | */ | |
| 18 | ||
| [5927] | 19 | #include <osgViewer/Viewer> |
| [10657] | 20 | #include <osgViewer/ViewerEventHandlers> |
| [1697] | 21 | |
| 22 | #include <osg/Group> | |
| 23 | #include <osg/Geode> | |
| 24 | ||
| 25 | #include <osgParticle/Particle> | |
| 26 | #include <osgParticle/ParticleSystem> | |
| 27 | #include <osgParticle/ParticleSystemUpdater> | |
| 28 | #include <osgParticle/ModularEmitter> | |
| 29 | #include <osgParticle/ModularProgram> | |
| 30 | #include <osgParticle/RandomRateCounter> | |
| 31 | #include <osgParticle/SectorPlacer> | |
| 32 | #include <osgParticle/RadialShooter> | |
| 33 | #include <osgParticle/AccelOperator> | |
| 34 | #include <osgParticle/FluidFrictionOperator> | |
| 35 | ||
| 36 | ||
| 37 | ||
| 38 | ////////////////////////////////////////////////////////////////////////////// | |
| 39 | // CUSTOM OPERATOR CLASS | |
| 40 | ////////////////////////////////////////////////////////////////////////////// | |
| 41 | ||
| 42 | // This class demonstrates Operator subclassing. This way you can create | |
| 43 | // custom operators to apply your motion effects to the particles. See docs | |
| 44 | // for more details. | |
| 45 | class VortexOperator: public osgParticle::Operator { | |
| 46 | public: | |
| [4805] | 47 | VortexOperator() |
| 48 | : osgParticle::Operator(), center_(0, 0, 0), axis_(0, 0, 1), intensity_(0.1f) {} | |
| [1697] | 49 | |
| [4805] | 50 | VortexOperator(const VortexOperator ©, const osg::CopyOp ©op = osg::CopyOp::SHALLOW_COPY) |
| 51 | : osgParticle::Operator(copy, copyop), center_(copy.center_), axis_(copy.axis_), intensity_(copy.intensity_) {} | |
| [1697] | 52 | |
| [4805] | 53 | META_Object(osgParticle, VortexOperator); |
| [1697] | 54 | |
| [4805] | 55 | void setCenter(const osg::Vec3 &c) |
| 56 | { | |
| 57 | center_ = c; | |
| 58 | } | |
| [1697] | 59 | |
| [4805] | 60 | void setAxis(const osg::Vec3 &a) |
| 61 | { | |
| 62 | axis_ = a / a.length(); | |
| 63 | } | |
| [1697] | 64 | |
| [4805] | 65 | // this method is called by ModularProgram before applying |
| 66 | // operators on the particle set via the operate() method. | |
| 67 | void beginOperate(osgParticle::Program *prg) | |
| 68 | { | |
| 69 | // we have to check whether the reference frame is RELATIVE_RF to parents | |
| 70 | // or it's absolute; in the first case, we must transform the vectors | |
| 71 | // from local to world space. | |
| 72 | if (prg->getReferenceFrame() == osgParticle::Program::RELATIVE_RF) { | |
| 73 | // transform the center point (full transformation) | |
| 74 | xf_center_ = prg->transformLocalToWorld(center_); | |
| 75 | // transform the axis vector (only rotation and scale) | |
| 76 | xf_axis_ = prg->rotateLocalToWorld(axis_); | |
| 77 | } else { | |
| 78 | xf_center_ = center_; | |
| 79 | xf_axis_ = axis_; | |
| 80 | } | |
| 81 | } | |
| [1697] | 82 | |
| [4805] | 83 | // apply a vortex-like acceleration. This code is not optimized, |
| 84 | // it's here only for demonstration purposes. | |
| 85 | void operate(osgParticle::Particle *P, double dt) | |
| 86 | { | |
| 87 | float l = xf_axis_ * (P->getPosition() - xf_center_); | |
| 88 | osg::Vec3 lc = xf_center_ + xf_axis_ * l; | |
| 89 | osg::Vec3 R = P->getPosition() - lc; | |
| 90 | osg::Vec3 v = (R ^ xf_axis_) * P->getMassInv() * intensity_; | |
| [1697] | 91 | |
| [4805] | 92 | // compute new position |
| 93 | osg::Vec3 newpos = P->getPosition() + v * dt; | |
| [1697] | 94 | |
| [4805] | 95 | // update the position of the particle without modifying its |
| 96 | // velocity vector (this is unusual, normally you should call | |
| 97 | // the Particle::setVelocity() or Particle::addVelocity() | |
| 98 | // methods). | |
| 99 | P->setPosition(newpos); | |
| 100 | } | |
| [1697] | 101 | |
| 102 | protected: | |
| [4805] | 103 | virtual ~VortexOperator() {} |
| [1697] | 104 | |
| 105 | private: | |
| [4805] | 106 | osg::Vec3 center_; |
| 107 | osg::Vec3 xf_center_; | |
| 108 | osg::Vec3 axis_; | |
| 109 | osg::Vec3 xf_axis_; | |
| 110 | float intensity_; | |
| [1697] | 111 | }; |
| 112 | ||
| 113 | ||
| 114 | ////////////////////////////////////////////////////////////////////////////// | |
| 115 | // SIMPLE PARTICLE SYSTEM CREATION | |
| 116 | ////////////////////////////////////////////////////////////////////////////// | |
| 117 | ||
| 118 | ||
| 119 | osgParticle::ParticleSystem *create_simple_particle_system(osg::Group *root) | |
| 120 | { | |
| 121 | ||
| [4805] | 122 | // Ok folks, this is the first particle system we build; it will be |
| 123 | // very simple, with no textures and no special effects, just default | |
| 124 | // values except for a couple of attributes. | |
| [1697] | 125 | |
| [4805] | 126 | // First of all, we create the ParticleSystem object; it will hold |
| 127 | // our particles and expose the interface for managing them; this object | |
| 128 | // is a Drawable, so we'll have to add it to a Geode later. | |
| [1697] | 129 | |
| [4805] | 130 | osgParticle::ParticleSystem *ps = new osgParticle::ParticleSystem; |
| [1697] | 131 | |
| [4805] | 132 | // As for other Drawable classes, the aspect of graphical elements of |
| 133 | // ParticleSystem (the particles) depends on the StateAttribute's we | |
| 134 | // give it. The ParticleSystem class has an helper function that let | |
| 135 | // us specify a set of the most common attributes: setDefaultAttributes(). | |
| 136 | // This method can accept up to three parameters; the first is a texture | |
| 137 | // name (std::string), which can be empty to disable texturing, the second | |
| 138 | // sets whether particles have to be "emissive" (additive blending) or not; | |
| 139 | // the third parameter enables or disables lighting. | |
| [1697] | 140 | |
| [4805] | 141 | ps->setDefaultAttributes("", true, false); |
| [1697] | 142 | |
| [4805] | 143 | // Now that our particle system is set we have to create an emitter, that is |
| 144 | // an object (actually a Node descendant) that generate new particles at | |
| 145 | // each frame. The best choice is to use a ModularEmitter, which allow us to | |
| 146 | // achieve a wide variety of emitting styles by composing the emitter using | |
| 147 | // three objects: a "counter", a "placer" and a "shooter". The counter must | |
| 148 | // tell the ModularEmitter how many particles it has to create for the | |
| 149 | // current frame; then, the ModularEmitter creates these particles, and for | |
| 150 | // each new particle it instructs the placer and the shooter to set its | |
| 151 | // position vector and its velocity vector, respectively. | |
| 152 | // By default, a ModularEmitter object initializes itself with a counter of | |
| 153 | // type RandomRateCounter, a placer of type PointPlacer and a shooter of | |
| 154 | // type RadialShooter (see documentation for details). We are going to leave | |
| 155 | // these default objects there, but we'll modify the counter so that it | |
| 156 | // counts faster (more particles are emitted at each frame). | |
| [1697] | 157 | |
| [4805] | 158 | osgParticle::ModularEmitter *emitter = new osgParticle::ModularEmitter; |
| [1697] | 159 | |
| [4805] | 160 | // the first thing you *MUST* do after creating an emitter is to set the |
| 161 | // destination particle system, otherwise it won't know where to create | |
| 162 | // new particles. | |
| [1697] | 163 | |
| [4805] | 164 | emitter->setParticleSystem(ps); |
| [1697] | 165 | |
| [4805] | 166 | // Ok, get a pointer to the emitter's Counter object. We could also |
| 167 | // create a new RandomRateCounter object and assign it to the emitter, | |
| 168 | // but since the default counter is already a RandomRateCounter, we | |
| 169 | // just get a pointer to it and change a value. | |
| [1697] | 170 | |
| [4805] | 171 | osgParticle::RandomRateCounter *rrc = |
| 172 | static_cast<osgParticle::RandomRateCounter *>(emitter->getCounter()); | |
| [1697] | 173 | |
| [4805] | 174 | // Now set the rate range to a better value. The actual rate at each frame |
| 175 | // will be chosen randomly within that range. | |
| [1697] | 176 | |
| [4805] | 177 | rrc->setRateRange(20, 30); // generate 20 to 30 particles per second |
| [1697] | 178 | |
| [4805] | 179 | // The emitter is done! Let's add it to the scene graph. The cool thing is |
| 180 | // that any emitter node will take into account the accumulated local-to-world | |
| 181 | // matrix, so you can attach an emitter to a transform node and see it move. | |
| [1697] | 182 | |
| [4805] | 183 | root->addChild(emitter); |
| [1697] | 184 | |
| [4805] | 185 | // Ok folks, we have almost finished. We don't add any particle modifier |
| 186 | // here (see ModularProgram and Operator classes), so all we still need is | |
| 187 | // to create a Geode and add the particle system to it, so it can be | |
| 188 | // displayed. | |
| [1697] | 189 | |
| [4805] | 190 | osg::Geode *geode = new osg::Geode; |
| 191 | geode->addDrawable(ps); | |
| [1697] | 192 | |
| [4805] | 193 | // add the geode to the scene graph |
| 194 | root->addChild(geode); | |
| [1697] | 195 | |
| [4805] | 196 | return ps; |
| [1697] | 197 | |
| 198 | } | |
| 199 | ||
| 200 | ||
| 201 | ||
| 202 | ////////////////////////////////////////////////////////////////////////////// | |
| 203 | // COMPLEX PARTICLE SYSTEM CREATION | |
| 204 | ////////////////////////////////////////////////////////////////////////////// | |
| 205 | ||
| 206 | ||
| 207 | osgParticle::ParticleSystem *create_complex_particle_system(osg::Group *root) | |
| 208 | { | |
| [4805] | 209 | // Are you ready for a more complex particle system? Well, read on! |
| [1697] | 210 | |
| [4805] | 211 | // Now we take one step we didn't before: create a particle template. |
| 212 | // A particle template is simply a Particle object for which you set | |
| 213 | // the desired properties (see documentation for details). When the | |
| 214 | // particle system has to create a new particle and it's been assigned | |
| 215 | // a particle template, the new particle will inherit the template's | |
| 216 | // properties. | |
| 217 | // You can even assign different particle templates to each emitter; in | |
| 218 | // this case, the emitter's template will override the particle system's | |
| 219 | // default template. | |
| [1697] | 220 | |
| [4805] | 221 | osgParticle::Particle ptemplate; |
| [1697] | 222 | |
| [4805] | 223 | ptemplate.setLifeTime(3); // 3 seconds of life |
| [1697] | 224 | |
| [4805] | 225 | // the following ranges set the envelope of the respective |
| 226 | // graphical properties in time. | |
| 227 | ptemplate.setSizeRange(osgParticle::rangef(0.75f, 3.0f)); | |
| 228 | ptemplate.setAlphaRange(osgParticle::rangef(0.0f, 1.5f)); | |
| 229 | ptemplate.setColorRange(osgParticle::rangev4( | |
| 230 | osg::Vec4(1, 0.5f, 0.3f, 1.5f), | |
| 231 | osg::Vec4(0, 0.7f, 1.0f, 0.0f))); | |
| [1697] | 232 | |
| [4805] | 233 | // these are physical properties of the particle |
| 234 | ptemplate.setRadius(0.05f); // 5 cm wide particles | |
| 235 | ptemplate.setMass(0.05f); // 50 g heavy | |
| [1697] | 236 | |
| [4805] | 237 | // As usual, let's create the ParticleSystem object and set its |
| 238 | // default state attributes. This time we use a texture named | |
| 239 | // "smoke.rgb", you can find it in the data distribution of OSG. | |
| 240 | // We turn off the additive blending, because smoke has no self- | |
| 241 | // illumination. | |
| 242 | osgParticle::ParticleSystem *ps = new osgParticle::ParticleSystem; | |
| 243 | ps->setDefaultAttributes("Images/smoke.rgb", false, false); | |
| [1697] | 244 | |
| [4805] | 245 | // assign the particle template to the system. |
| 246 | ps->setDefaultParticleTemplate(ptemplate); | |
| [1697] | 247 | |
| [4805] | 248 | // now we have to create an emitter; this will be a ModularEmitter, for which |
| 249 | // we define a RandomRateCounter as counter, a SectorPlacer as placer, and | |
| 250 | // a RadialShooter as shooter. | |
| 251 | osgParticle::ModularEmitter *emitter = new osgParticle::ModularEmitter; | |
| 252 | emitter->setParticleSystem(ps); | |
| [1697] | 253 | |
| [4805] | 254 | // setup the counter |
| 255 | osgParticle::RandomRateCounter *counter = new osgParticle::RandomRateCounter; | |
| 256 | counter->setRateRange(60, 60); | |
| 257 | emitter->setCounter(counter); | |
| [1697] | 258 | |
| [4805] | 259 | // setup the placer; it will be a circle of radius 5 (the particles will |
| 260 | // be placed inside this circle). | |
| 261 | osgParticle::SectorPlacer *placer = new osgParticle::SectorPlacer; | |
| 262 | placer->setCenter(8, 0, 10); | |
| 263 | placer->setRadiusRange(2.5, 5); | |
| 264 | placer->setPhiRange(0, 2 * osg::PI); // 360° angle to make a circle | |
| 265 | emitter->setPlacer(placer); | |
| [1697] | 266 | |
| [4805] | 267 | // now let's setup the shooter; we use a RadialShooter but we set the |
| 268 | // initial speed to zero, because we want the particles to fall down | |
| 269 | // only under the effect of the gravity force. Since we se the speed | |
| 270 | // to zero, there is no need to setup the shooting angles. | |
| 271 | osgParticle::RadialShooter *shooter = new osgParticle::RadialShooter; | |
| 272 | shooter->setInitialSpeedRange(0, 0); | |
| 273 | emitter->setShooter(shooter); | |
| [1697] | 274 | |
| [4805] | 275 | // add the emitter to the scene graph |
| 276 | root->addChild(emitter); | |
| [1697] | 277 | |
| [4805] | 278 | // WELL, we got our particle system and a nice emitter. Now we want to |
| 279 | // simulate the effect of the earth gravity, so first of all we have to | |
| 280 | // create a Program. It is a particle processor just like the Emitter | |
| 281 | // class, but it allows to modify particle properties *after* they have | |
| 282 | // been created. | |
| 283 | // The ModularProgram class can be thought as a sequence of operators, | |
| 284 | // each one performing some actions on the particles. So, the trick is: | |
| 285 | // create the ModularProgram object, create one or more Operator objects, | |
| 286 | // add those operators to the ModularProgram, and finally add the | |
| 287 | // ModularProgram object to the scene graph. | |
| 288 | // NOTE: since the Program objects perform actions after the particles | |
| 289 | // have been emitted by one or more Emitter objects, all instances of | |
| 290 | // Program (and its descendants) should be placed *after* the instances | |
| 291 | // of Emitter objects in the scene graph. | |
| [1697] | 292 | |
| [4805] | 293 | osgParticle::ModularProgram *program = new osgParticle::ModularProgram; |
| 294 | program->setParticleSystem(ps); | |
| [1697] | 295 | |
| [4805] | 296 | // create an operator that simulates the gravity acceleration. |
| 297 | osgParticle::AccelOperator *op1 = new osgParticle::AccelOperator; | |
| 298 | op1->setToGravity(); | |
| 299 | program->addOperator(op1); | |
| [1697] | 300 | |
| [4805] | 301 | // now create a custom operator, we have defined it before (see |
| 302 | // class VortexOperator). | |
| 303 | VortexOperator *op2 = new VortexOperator; | |
| 304 | op2->setCenter(osg::Vec3(8, 0, 0)); | |
| 305 | program->addOperator(op2); | |
| [1697] | 306 | |
| [4805] | 307 | // let's add a fluid operator to simulate air friction. |
| 308 | osgParticle::FluidFrictionOperator *op3 = new osgParticle::FluidFrictionOperator; | |
| 309 | op3->setFluidToAir(); | |
| 310 | program->addOperator(op3); | |
| [1697] | 311 | |
| [4805] | 312 | // add the program to the scene graph |
| 313 | root->addChild(program); | |
| [1697] | 314 | |
| [4805] | 315 | // create a Geode to contain our particle system. |
| 316 | osg::Geode *geode = new osg::Geode; | |
| 317 | geode->addDrawable(ps); | |
| [1697] | 318 | |
| [4805] | 319 | // add the geode to the scene graph. |
| 320 | root->addChild(geode); | |
| [1697] | 321 | |
| [4805] | 322 | return ps; |
| [1697] | 323 | } |
| 324 | ||
| 325 | ||
| [10827] | 326 | |
| [1697] | 327 | ////////////////////////////////////////////////////////////////////////////// |
| [10827] | 328 | // ANIMATED PARTICLE SYSTEM CREATION |
| 329 | ////////////////////////////////////////////////////////////////////////////// | |
| 330 | ||
| 331 | ||
| 332 | osgParticle::ParticleSystem *create_animated_particle_system(osg::Group *root) | |
| 333 | { | |
| 334 | ||
| 335 | // Now we will create a particle system that uses two emitters to | |
| 336 | // display two animated particles, one showing an explosion, the other | |
| 337 | // a smoke cloud. A particle system can only use one texture, so | |
| 338 | // the animations for both particles are stored in a single bitmap. | |
| 339 | // The frames of the animation are stored in tiles. For each particle | |
| 340 | // template, the start and end tile of their animation have to be given. | |
| 341 | // The example file used here has 64 tiles, stored in eight rows with | |
| 342 | // eight images each. | |
| 343 | ||
| 344 | // First create a prototype for the explosion particle. | |
| 345 | osgParticle::Particle pexplosion; | |
| 346 | ||
| 347 | // The frames of the explosion particle are played from birth to | |
| 348 | // death of the particle. So if lifetime is one second, all 16 images | |
| 349 | // of the particle are shown in this second. | |
| 350 | pexplosion.setLifeTime(1); | |
| 351 | ||
| 352 | // some other particle properties just as in the last example. | |
| 353 | pexplosion.setSizeRange(osgParticle::rangef(0.75f, 3.0f)); | |
| 354 | pexplosion.setAlphaRange(osgParticle::rangef(0.5f, 1.0f)); | |
| 355 | pexplosion.setColorRange(osgParticle::rangev4( | |
| 356 | osg::Vec4(1, 1, 1, 1), | |
| 357 | osg::Vec4(1, 1, 1, 1))); | |
| 358 | pexplosion.setRadius(0.05f); | |
| 359 | pexplosion.setMass(0.05f); | |
| 360 | ||
| 361 | // This command sets the animation tiles to be shown for the particle. | |
| 362 | // The first two parameters define the tile layout of the texture image. | |
| 363 | // 8, 8 means the texture has eight rows of tiles with eight columns each. | |
| 364 | // 0, 15 defines the start and end tile | |
| 365 | pexplosion.setTextureTileRange(8, 8, 0, 15); | |
| 366 | ||
| 367 | // The smoke particle is just the same, only plays another tile range. | |
| 368 | osgParticle::Particle psmoke = pexplosion; | |
| 369 | psmoke.setTextureTileRange(8, 8, 32, 45); | |
| 370 | ||
| 371 | // Create a single particle system for both particle types | |
| 372 | osgParticle::ParticleSystem *ps = new osgParticle::ParticleSystem; | |
| 373 | ||
| 374 | // Assign the tiled texture | |
| 375 | ps->setDefaultAttributes("Images/fireparticle8x8.png", false, false); | |
| 376 | ||
| 377 | // Create two emitters, one for the explosions, one for the smoke balls. | |
| 378 | osgParticle::ModularEmitter *emitter1 = new osgParticle::ModularEmitter; | |
| 379 | emitter1->setParticleSystem(ps); | |
| 380 | emitter1->setParticleTemplate(pexplosion); | |
| 381 | ||
| 382 | osgParticle::ModularEmitter *emitter2 = new osgParticle::ModularEmitter; | |
| 383 | emitter2->setParticleSystem(ps); | |
| 384 | emitter2->setParticleTemplate(psmoke); | |
| 385 | ||
| 386 | // create a counter each. We could reuse the counter for both emitters, but | |
| 387 | // then we could not control the ratio of smoke balls to explosions | |
| 388 | osgParticle::RandomRateCounter *counter1 = new osgParticle::RandomRateCounter; | |
| 389 | counter1->setRateRange(10, 10); | |
| 390 | emitter1->setCounter(counter1); | |
| 391 | ||
| 392 | osgParticle::RandomRateCounter *counter2 = new osgParticle::RandomRateCounter; | |
| 393 | counter2->setRateRange(3, 4); | |
| 394 | emitter2->setCounter(counter2); | |
| 395 | ||
| 396 | // setup a single placer for both emitters. | |
| 397 | osgParticle::SectorPlacer *placer = new osgParticle::SectorPlacer; | |
| 398 | placer->setCenter(-8, 0, 0); | |
| 399 | placer->setRadiusRange(2.5, 5); | |
| 400 | placer->setPhiRange(0, 2 * osg::PI); // 360° angle to make a circle | |
| 401 | emitter1->setPlacer(placer); | |
| 402 | emitter2->setPlacer(placer); | |
| 403 | ||
| 404 | // the shooter is reused for both emitters | |
| 405 | osgParticle::RadialShooter *shooter = new osgParticle::RadialShooter; | |
| 406 | shooter->setInitialSpeedRange(0, 0); | |
| 407 | ||
| 408 | // give particles a little spin | |
| 409 | shooter->setInitialRotationalSpeedRange(osgParticle::rangev3( | |
| 410 | osg::Vec3(0, 0, -1), | |
| 411 | osg::Vec3(0, 0, 1))); | |
| 412 | emitter1->setShooter(shooter); | |
| 413 | emitter2->setShooter(shooter); | |
| 414 | ||
| 415 | // add both emitters to the scene graph | |
| 416 | root->addChild(emitter1); | |
| 417 | root->addChild(emitter2); | |
| 418 | ||
| 419 | // create a program, just as before | |
| 420 | osgParticle::ModularProgram *program = new osgParticle::ModularProgram; | |
| 421 | program->setParticleSystem(ps); | |
| 422 | ||
| 423 | // create an operator that moves the particles upwards | |
| 424 | osgParticle::AccelOperator *op1 = new osgParticle::AccelOperator; | |
| 425 | op1->setAcceleration(osg::Vec3(0, 0, 2.0f)); | |
| 426 | program->addOperator(op1); | |
| 427 | ||
| 428 | // add the program to the scene graph | |
| 429 | root->addChild(program); | |
| 430 | ||
| 431 | // create a Geode to contain our particle system. | |
| 432 | osg::Geode *geode = new osg::Geode; | |
| 433 | geode->addDrawable(ps); | |
| 434 | ||
| 435 | // add the geode to the scene graph. | |
| 436 | root->addChild(geode); | |
| 437 | ||
| 438 | return ps; | |
| 439 | } | |
| 440 | ||
| 441 | ////////////////////////////////////////////////////////////////////////////// | |
| [1697] | 442 | // MAIN SCENE GRAPH BUILDING FUNCTION |
| 443 | ////////////////////////////////////////////////////////////////////////////// | |
| 444 | ||
| 445 | ||
| 446 | void build_world(osg::Group *root) | |
| 447 | { | |
| 448 | ||
| [4805] | 449 | // In this function we are going to create two particle systems; |
| 450 | // the first one will be very simple, based mostly on default properties; | |
| 451 | // the second one will be a little bit more complex, showing how to | |
| 452 | // create custom operators. | |
| 453 | // To avoid inserting too much code in a single function, we have | |
| 454 | // splitted the work into two functions which accept a Group node as | |
| 455 | // parameter, and return a pointer to the particle system they created. | |
| [1697] | 456 | |
| [4805] | 457 | osgParticle::ParticleSystem *ps1 = create_simple_particle_system(root); |
| 458 | osgParticle::ParticleSystem *ps2 = create_complex_particle_system(root); | |
| [10827] | 459 | osgParticle::ParticleSystem *ps3 = create_animated_particle_system(root); |
| [1697] | 460 | |
| [4805] | 461 | // Now that the particle systems and all other related objects have been |
| 462 | // created, we have to add an "updater" node to the scene graph. This node | |
| 463 | // will react to cull traversal by updating the specified particles system. | |
| [1697] | 464 | |
| [4805] | 465 | osgParticle::ParticleSystemUpdater *psu = new osgParticle::ParticleSystemUpdater; |
| 466 | psu->addParticleSystem(ps1); | |
| 467 | psu->addParticleSystem(ps2); | |
| [10827] | 468 | psu->addParticleSystem(ps3); |
| [1697] | 469 | |
| [4805] | 470 | // add the updater node to the scene graph |
| 471 | root->addChild(psu); | |
| [1697] | 472 | |
| 473 | } | |
| 474 | ||
| 475 | ||
| 476 | ////////////////////////////////////////////////////////////////////////////// | |
| 477 | // main() | |
| 478 | ////////////////////////////////////////////////////////////////////////////// | |
| 479 | ||
| 480 | ||
| [5927] | 481 | int main(int, char **) |
| [1697] | 482 | { |
| 483 | // construct the viewer. | |
| [5927] | 484 | osgViewer::Viewer viewer; |
| [1815] | 485 | |
| [1697] | 486 | osg::Group *root = new osg::Group; |
| 487 | build_world(root); | |
| 488 | ||
| [10657] | 489 | // add the stats handler |
| 490 | viewer.addEventHandler(new osgViewer::StatsHandler); | |
| 491 | ||
| [1697] | 492 | // add a viewport to the viewer and attach the scene graph. |
| 493 | viewer.setSceneData(root); | |
| 494 | ||
| [5927] | 495 | return viewer.run(); |
| [1697] | 496 | } |
Note: See TracBrowser
for help on using the browser.
