Method One: Particle systems
OSG particle systems have their own defined modules, such as osgparticle::explosioneffect (Explosion simulation), Osgparticle::smokeeffect (smoke simulation); Osgparticle:: Fireeffect (flare Simulation). I think the smoke simulation and flare simulation can be used to display the alarm effects. OSG Adding a osgparticle particle effect to the scene:
Add a custom Osgparticle instance to the scene to simulate the soot generated when the tank model moves on the terrain.
-----------------------------
Overview:
Adding a particle effect can effectively improve the appearance and authenticity of the emulator. Particle engines are typically used to simulate smoke, flames, dust, and other similar effects. If you want to add a particle effect to a OSG scene, you can usually use some of the following classes:
Particle systems (osgparticle::P Articlesystem)-maintains and manages the generation, updating, rendering, and destruction of a range of particles. The particle system class inherits from the Drawable class. The rendering control of particles is therefore similar to that of other drawable objects: control their rendering properties Stateattribute. OSG provides a convenient function to allow the user to control three commonly used rendering state properties. Method setdefaultattributes can be used to specify a material (or null to disable a material), allow/disallow additional image blending, and allow/disallow illumination.
Particle (osgparticle::P article)-The basic unit of the particle system. Particle classes have both physical and image properties. Its shape can be any point, quadrilateral (QUAD), Quad band (Quad_tripstrip), Hexagon (HEXAGON), or line. Each particle has its own life cycle. The life cycle is the number of seconds each particle can survive. (particles with a negative life span can survive indefinitely) all particles have a size (size), alpha value, and color property. Each group of particles can specify its maximum and minimum values. To facilitate the management of the particle life cycle, the particle system controls the rendering of individual particles by altering the maximum and minimum values of the life cycle. (linear interpolation between the minimum and maximum values based on the time that has been consumed)
The programmer can also specify an interpolation method of minimum to maximum value. (see Osgparticle::interpolator's code)
Placement Device (osgparticle::P lacer)-Sets the initial position of the particle. Users can use pre-defined placement or define their own placement device. The defined placement includes: Point placement pointplacer (all particles are born from the same point), fan placement sectorplacer (all particles from a specified center point, radius range and angle range of the sector are born), As well as a multi-segment placement Multisegmentplacer (the user specifies a series of points, the particles are born along the segments defined by these points).
Emitter (Osgparticle::shooter)-Specifies the initial velocity of the particle. The Radialshooter class allows the user to specify a speed range (M/s) and the direction in which the Radian value is represented. The direction is specified by two angles: theta Angle-angle to Z axis, phi angle-angle to xy plane.
Counter (osgparticle::counter)-controls the number of particles produced per frame. The Randomratecounter class allows the user to specify the maximum and minimum number of particles produced per frame.
Standard emitter (Osgparticle::modularemitter)-a standard emitter consists of a counter, a placement device and a transmitter. It provides a standard mechanism for users to control multiple elements in a particle system.
Particle System Updater (osgparticle::P articlesystemupdater)-Used to automatically update particles. When placed in a scene, it invokes the Update method of all "surviving" particles in the picking traversal.
Standard Programmer (Osgparticle::modularprogram)-in the lifetime of a single particle, the user can use the Modularprogram instance to control the particle's position. Modularprogram needs to be used in combination with operator objects.
Calculator (Osgparticle::operator)-Provides a way to control the motion characteristics of a particle during its life cycle. Users can change the parameters of an existing operator class instance, or define their own operator class. The operator classes offered by OSG include: acceloperator (applied constant acceleration), angularacceloperator (applied constant angular acceleration), Fluidfrictionoperator (calculated based on fluid motion with specified density and viscosity), and forceoperator (applied Changli).
Code:
To create a highly customized particle system using the above class, we can follow the steps below. (The steps in parentheses are optional and can be ignored if the particle system being established is relatively basic)
- Create a particle system instance and add it to the scene.
- (Sets the rendering state properties for the particle system.) )
- Creates a particle object and associates it to the particle system.
- (Sets the parameters for the particle.) )
- Create a particle system updater, associate it to a particle system instance, and add it to the scene.
- (Create standard emitter to define: counter-number of particles created per frame, placement-spawn position of particles, emitter-initial speed.) )
- (The standard emitter is associated to the particle system.) )
- (Create an instance of the Modularprogram standard programmer to control the movement of particles in the life cycle: first create and define the operator calculator; then add the calculator to the standard programmer.) )
The following code will complete the above steps. First, we need to build a basic tank and terrain model. (We'll add the tank model to the scene later.)
osg::group* RootNode =NewOsg::group (); Osg::node* Terrainnode =NewOsg::node (); Osgviewer::viewer Viewer;terrainnode= Osgdb::readnodefile ("//models//joedirt//joedirt.flt");if(!Terrainnode) {Std::cout<<"couldn ' t load models, quitting."<<Std::endl; return-1;} RootNode-AddChild (terrainnode); Osg::node* Tanknode = Osgdb::readnodefile ("//models//t72-tank//t72-tank_des.flt");if( !Tanknode) {Std::cout<<"No tank"<<Std::endl; return-1;}
The basic steps to build a particle system are to create a particle system object, an Updater object, and a particle object, while setting up the scene.
//Create and initialize the particle system. Osgparticle::P Articlesystem *dustparticlesystem =Newosgparticle::P articlesystem;//sets the material, whether to emit particles, and whether to use lighting. Dustparticlesystem->setdefaultattributes (Dust2.rgb", False, false);//since the particle system class inherits from the Drawable class, we can join the scene as a child of the Geode. Osg::geode *geode =NewOsg::geode;rootnode-AddChild (Geode); Geode-adddrawable (dustparticlesystem);//Add the updater to implement particle management for each frame. Osgparticle::P Articlesystemupdater *dustsystemupdater =Newosgparticle::P articlesystemupdater;//Associates an updater with a particle system object. Dustsystemupdater->Addparticlesystem (dustparticlesystem);//Add the Updater node to the scene. Rootnode->AddChild (dustsystemupdater);//creates a particle object, sets its properties, and is used by the particle system. osgparticle::P article Smokeparticle;smokeparticle.setsizerange (OSGPARTICLE::RANGEF (0.01,20.0));//Unit: MSmokeparticle.setlifetime (4);//units: SecondsSmokeparticle.setmass (0.01);//Unit: Kg//set as the default particle object for the particle system. Dustparticlesystem->setdefaultparticletemplate (smokeparticle);
The following code uses a standard emitter object to set some basic parameters for a particle: for example, the number of particles created per frame, where the new particles are generated, and the speed of the newborn particles.
//creates a standard emitter object. (includes default counter, placement and emitter)Osgparticle::modularemitter *emitter =NewOsgparticle::modularemitter;//associates a Emitter object with a particle system. Emitter->Setparticlesystem (dustparticlesystem);//gets the handle of the default counter in the emitter, adjusting the number of new particles added per frame. Osgparticle::randomratecounter *dustrate =static_cast<osgparticle::randomratecounter *> (emitter->getcounter ());d ustrate->setraterange (5,Ten);//5 to 10 new particles per second. //Customize a placement, where we create and initialize a multi-segment placement. osgparticle::multisegmentplacer* linesegment =Newosgparticle::multisegmentplacer ();//adds a vertex to the drop, that is, the position of the segment where the particle is generated. //(if the tank and the standard emitter are defined in the same position, we can implement a//The effect of the dust particles from the extension cord below the tank model. )Linesegment->addvertex (0,0,-2); LineSegment->addvertex (0,-2,-2); LineSegment->addvertex (0,- -,0);//set the placement device for the standard emitter. Emitter->Setplacer (linesegment);//to customize a transmitter, here we create and initialize a radialshooter radian emitter. osgparticle::radialshooter* Smokeshooter =NewOsgparticle::radialshooter ();//sets the properties of the emitter. Smokeshooter->setthetarange (0.0,3.14159/2);//The radian value, which is the angle of the z axis. Smokeshooter->setinitialspeedrange ( -, -);//Unit: M/S//set the emitter for the standard emitter. Emitter->Setshooter (smokeshooter); now we will add the emitter and tank model as child nodes of the Transform transformation node to the scene. Both the emitter and the tank are determined by the transformation node. The position of the particle will be arranged according to the parameters of the transformation as defined by the placement. Osg::matrixtransform* Tanktransform =Newosg::matrixtransform (); Tanktransform->setupdatecallback (NewOrbit ());//a callback function that causes the node to ring motion. //The emitter and tank models are added as child nodes of the transformation node. Tanktransform->AddChild (emitter); Tanktransform-AddChild (tanknode); RootNode-AddChild (tanktransform); The following code creates a standard programmer Modularprogram instance that controls how particles are updated in the life cycle. The standard programmer object uses the operator calculator to achieve control of particles. //creates a standard programmer object and associates it with a particle system. Osgparticle::modularprogram *movedustinair =NewOsgparticle::modularprogram;movedustinair-Setparticlesystem (dustparticlesystem);//Create a Calculator object that simulates the action of gravity, adjusts its parameters, and adds it to the programmer object. Osgparticle::acceloperator *accelup =NewOsgparticle::acceloperator;accelup->settogravity (-1);//sets the scaling factor for gravitational acceleration. Movedustinair->Addoperator (accelup);//Add a Calculator object to the programmer to calculate air resistance. Osgparticle::fluidfrictionoperator *airfriction =Newosgparticle::fluidfrictionoperator;airfriction-Setfluidtoair (); Movedustinair-Addoperator (airfriction);//Finally, add the programmer to the scene. Rootnode->AddChild (Movedustinair); The following code is the content of the simulation loop. Viewer.setcameramanipulator (Newosgga::trackballmanipulator ()); Viewer.setscenedata (RootNode); Viewer.realize (); while( !Viewer.done ()) {Viewer.frame ();}return 0;
OSG Alarm Effect Learning Summary