OSG Example parsing osgparticle (1)

Source: Internet
Author: User
Tags addchild constant
Introduction

The osgparticle example simply demonstrates the effect of using the particle system in OSG, using the related classes in the Osgparticle library, which are mainly in osgparticle:

(The following part of the material extracted from OSG to the scene to add osgparticle particle effect of the article, Google did not find the original author, posted the reprint address, in this thanked the original author ~)

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- -----

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.

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). --------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- -----

Next look at the code:

First, a subclass of operator is created in the code to simulate the effects of eddy currents, which are added to the program to make a difference to each particle in the particle system:

    void Operate (osgparticle::P article *p, double dt)
    {
        float L = xf_axis_ * (p->getposition ()-xf_center_);
        OSG::VEC3 LC = Xf_center_ + Xf_axis_ * l;
        OSG::VEC3 R = p->getposition ()-LC;
        OSG::VEC3 v = (R ^ xf_axis_) * P->GETMASSINV () * INTENSITY_;

        OSG::VEC3 Newpos = p->getposition () + v * DT;
        P->setposition (Newpos);    
    }
Because operator is a pure virtual class, subclasses must overload its operator method. Subclasses can also overload the Beginoperator method to accomplish some preparatory work before manipulating particles.

Let's take a look at the Create_simple_particle_system function, which shows the simplest steps to create a particle effect:

    First create the Particle system Object
	osgparticle::P articlesystem *ps = new osgparticle::P Articlesystem;
    Ps->setdefaultattributes ("", true, false);
   Set emitter parameters (set number of launches)
    osgparticle::modularemitter *emitter = new Osgparticle::modularemitter;
    Emitter->setparticlesystem (PS);
    Osgparticle::randomratecounter *RRC = 
        static_cast<osgparticle::randomratecounter *> (emitter-> Getcounter ());
    Rrc->setraterange (+);
    Root->addchild (emitter);
   The ion system is added to the leaf node
    osg::geode *geode = new Osg::geode;    
    Geode->adddrawable (PS);
    Root->addchild (Geode);
In Create_complex_particle_system, just set more parameters, add a program that acts on the particle system, and add a lot of operator in it, the whole setup process is the same.
   Set emitter more parameters Osgparticle::modularemitter *emitter = new Osgparticle::modularemitter;

    Emitter->setparticlesystem (PS);
    Osgparticle::randomratecounter *counter = new Osgparticle::randomratecounter;
    Counter->setraterange (60, 60);

    Emitter->setcounter (counter);
    Osgparticle::sectorplacer *placer = new Osgparticle::sectorplacer;
    Placer->setcenter (8, 0, 10);
    Placer->setradiusrange (2.5, 5);
    Placer->setphirange (0, 2 * OSG::P i);

    Emitter->setplacer (placer);
    Osgparticle::radialshooter *shooter = new Osgparticle::radialshooter;
    Shooter->setinitialspeedrange (0, 0);

    Emitter->setshooter (shooter);

	Root->addchild (emitter);
    Add operator to program Osgparticle::modularprogram *program = new Osgparticle::modularprogram;

    Program->setparticlesystem (PS);
    Osgparticle::acceloperator *op1 = new Osgparticle::acceloperator;
    Op1->settogravity ();

    Program->addoperator (OP1); Vortexoperator *OP2 = new Vortexoperator;
    Op2->setcenter (OSG::VEC3 (8, 0, 0));

    Program->addoperator (OP2);
    Osgparticle::fluidfrictionoperator *op3 = new Osgparticle::fluidfrictionoperator;
    Op3->setfluidtoair ();

    Program->addoperator (OP3);

    Root->addchild (program);
    Osg::geode *geode = new Osg::geode;

    Geode->adddrawable (PS); Root->addchild (Geode);
Create_animated_particle_system creates a particle effect animation that toggles the texture:

The texture of the particles is switched on and off in these texture slices:

	Texture slices used for setting textures
    pexplosion.settexturetilerange (8, 8, 0, +);
    Psmoke.settexturetilerange (8, 8, 32, 45);
The rest of the settings are identical to the ones above.

In the final step, we need to add these particle systems to a updater and update the particle system by picking traversal of the updater scene.

    Osgparticle::P Articlesystem *ps1 = Create_simple_particle_system (root);
    Osgparticle::P Articlesystem *ps2 = Create_complex_particle_system (root);
    Osgparticle::P Articlesystem *ps3 = Create_animated_particle_system (root);

    Osgparticle::P articlesystemupdater *psu = new osgparticle::P articlesystemupdater;
    Psu->addparticlesystem (PS1);
    Psu->addparticlesystem (PS2);
    Psu->addparticlesystem (PS3);

    Root->addchild (PSU);






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.