root/OpenSceneGraph/trunk/src/osgParticle/ModularEmitter.cpp @ 13041

Revision 13041, 3.9 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osgParticle/ModularEmitter>
2#include <osgParticle/Emitter>
3#include <osgParticle/ConnectedParticleSystem>
4#include <osg/Notify>
5
6osgParticle::ModularEmitter::ModularEmitter()
7:    Emitter(),
8    _numParticleToCreateMovementCompensationRatio(0.0f),
9    _counter(new RandomRateCounter),
10    _placer(new PointPlacer),
11    _shooter(new RadialShooter)
12{
13}
14
15osgParticle::ModularEmitter::ModularEmitter(const ModularEmitter& copy, const osg::CopyOp& copyop):
16    Emitter(copy, copyop),
17    _numParticleToCreateMovementCompensationRatio(copy._numParticleToCreateMovementCompensationRatio),
18    _counter(static_cast<Counter *>(copyop(copy._counter.get()))),
19    _placer(static_cast<Placer *>(copyop(copy._placer.get()))),
20    _shooter(static_cast<Shooter *>(copyop(copy._shooter.get())))
21{
22}
23
24void osgParticle::ModularEmitter::emitParticles(double dt)
25{
26    ConnectedParticleSystem* cps = dynamic_cast<ConnectedParticleSystem*>(getParticleSystem());
27
28    osg::Matrix worldToPs;
29    osg::MatrixList worldMats = getParticleSystem()->getWorldMatrices();
30    if (!worldMats.empty())
31    {
32        const osg::Matrix psToWorld = worldMats[0];
33        worldToPs = osg::Matrix::inverse(psToWorld);
34    }
35
36    if (getReferenceFrame() == RELATIVE_RF)
37    {
38        const osg::Matrix& ltw = getLocalToWorldMatrix();
39        const osg::Matrix& previous_ltw = getPreviousLocalToWorldMatrix();
40        const osg::Matrix emitterToPs = ltw * worldToPs;
41        const osg::Matrix prevEmitterToPs = previous_ltw * worldToPs;
42
43        int n = _counter->numParticlesToCreate(dt);
44
45        if (_numParticleToCreateMovementCompensationRatio>0.0f)
46        {
47            // compute the distance moved between frames
48            const osg::Vec3d controlPosition
49                = osg::Vec3d(_placer->getControlPosition());
50            osg::Vec3d previousPosition = controlPosition * previous_ltw;
51            osg::Vec3d currentPosition = controlPosition * ltw;
52            float distance = (currentPosition-previousPosition).length();
53
54            float size = getUseDefaultTemplate() ?
55                        getParticleSystem()->getDefaultParticleTemplate().getSizeRange().minimum :
56                        getParticleTemplate().getSizeRange().minimum;
57
58            float num_extra_samples = _numParticleToCreateMovementCompensationRatio*distance/size;
59            float rounded_down = floor(num_extra_samples);
60            float remainder = num_extra_samples-rounded_down;
61
62            n = osg::maximum(n, int(rounded_down) +  (((float) rand() < remainder * (float)RAND_MAX) ? 1 : 0));
63        }
64
65        for (int i=0; i<n; ++i)
66        {
67            Particle* P = getParticleSystem()->createParticle(getUseDefaultTemplate()? 0: &getParticleTemplate());
68            if (P)
69            {
70                _placer->place(P);
71                _shooter->shoot(P);
72
73                // Now need to transform the position and velocity because we having a moving model.
74                float r = ((float)rand()/(float)RAND_MAX);
75                P->transformPositionVelocity(emitterToPs, prevEmitterToPs, r);
76                //P->transformPositionVelocity(ltw);
77
78                if (cps) P->setUpTexCoordsAsPartOfConnectedParticleSystem(cps);
79
80            }
81            else
82            {
83                OSG_NOTICE<<"run out of particle"<<std::endl;
84            }
85        }
86    }
87    else
88    {
89        int n = _counter->numParticlesToCreate(dt);
90        for (int i=0; i<n; ++i)
91        {
92            Particle* P = getParticleSystem()->createParticle(getUseDefaultTemplate()? 0: &getParticleTemplate());
93            if (P)
94            {
95                _placer->place(P);
96                P->setPosition(P->getPosition() * worldToPs);
97                _shooter->shoot(P);
98                P->setVelocity(osg::Matrix::transform3x3(P->getVelocity(),
99                                                         worldToPs));
100
101                if (cps) P->setUpTexCoordsAsPartOfConnectedParticleSystem(cps);
102            }
103        }
104    }
105}
Note: See TracBrowser for help on using the browser.