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

Revision 13041, 5.1 kB (checked in by robert, 3 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/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#include <osgParticle/SmokeEffect>
15
16#include <osgParticle/RandomRateCounter>
17#include <osgParticle/RadialShooter>
18#include <osgParticle/SectorPlacer>
19#include <osgParticle/ParticleSystemUpdater>
20
21#include <osg/Geode>
22
23using namespace osgParticle;
24
25SmokeEffect::SmokeEffect(bool automaticSetup):
26    ParticleEffect(automaticSetup)
27{
28    setDefaults();
29
30    _position.set(0.0f,0.0f,0.0f);
31    _scale = 1.0f;
32    _intensity = 1.0f;
33
34    _emitterDuration = 65.0;
35    _defaultParticleTemplate.setLifeTime(5.0*_scale);
36
37    if (_automaticSetup) buildEffect();
38}
39
40SmokeEffect::SmokeEffect(const osg::Vec3& position, float scale, float intensity)
41{
42    setDefaults();
43
44    _position = position;
45    _scale = scale;
46    _intensity = intensity;
47
48    _emitterDuration = 65.0;
49    _defaultParticleTemplate.setLifeTime(5.0*_scale);
50
51    if (_automaticSetup) buildEffect();
52}
53
54SmokeEffect::SmokeEffect(const SmokeEffect& copy, const osg::CopyOp& copyop):
55    ParticleEffect(copy,copyop)
56{
57    if (_automaticSetup) buildEffect();
58}
59
60void SmokeEffect::setDefaults()
61{
62    ParticleEffect::setDefaults();
63
64    _textureFileName = "Images/smoke.rgb";
65    _emitterDuration = 65.0;
66
67    // set up unit particle.
68    _defaultParticleTemplate.setLifeTime(5.0*_scale);
69    _defaultParticleTemplate.setSizeRange(osgParticle::rangef(0.75f, 2.0f));
70    _defaultParticleTemplate.setAlphaRange(osgParticle::rangef(0.1f, 1.0f));
71    _defaultParticleTemplate.setColorRange(osgParticle::rangev4(
72                                            osg::Vec4(1, 1.0f, 1.0f, 1.0f),
73                                            osg::Vec4(1, 1.0f, 1.f, 0.0f)));
74
75
76}
77
78void SmokeEffect::setUpEmitterAndProgram()
79{
80    // set up particle system
81    if (!_particleSystem)
82    {
83        _particleSystem = new osgParticle::ParticleSystem;
84    }
85
86    if (_particleSystem.valid())
87    {
88        _particleSystem->setDefaultAttributes(_textureFileName, false, false);
89
90        osgParticle::Particle& ptemplate = _particleSystem->getDefaultParticleTemplate();
91
92        float radius = 0.5f*_scale;
93        float density = 1.0f; // 1.0kg/m^3
94
95        ptemplate.setLifeTime(_defaultParticleTemplate.getLifeTime());
96
97        // the following ranges set the envelope of the respective
98        // graphical properties in time.
99        ptemplate.setSizeRange(osgParticle::rangef(radius*_defaultParticleTemplate.getSizeRange().minimum,
100                                                   radius*_defaultParticleTemplate.getSizeRange().maximum));
101        ptemplate.setAlphaRange(_defaultParticleTemplate.getAlphaRange());
102        ptemplate.setColorRange(_defaultParticleTemplate.getColorRange());
103
104        // these are physical properties of the particle
105        ptemplate.setRadius(radius);
106        ptemplate.setMass(density*radius*radius*radius*osg::PI*4.0f/3.0f);
107
108    }
109
110
111    // set up emitter
112    if (!_emitter)
113    {
114        _emitter = new osgParticle::ModularEmitter;
115        _emitter->setNumParticlesToCreateMovementCompensationRatio(1.5f);
116        _emitter->setCounter(new osgParticle::RandomRateCounter);
117        _emitter->setPlacer(new osgParticle::SectorPlacer);
118        _emitter->setShooter(new osgParticle::RadialShooter);
119    }
120
121    if (_emitter.valid())
122    {
123        _emitter->setParticleSystem(_particleSystem.get());
124        _emitter->setReferenceFrame(_useLocalParticleSystem?
125                                    osgParticle::ParticleProcessor::ABSOLUTE_RF:
126                                    osgParticle::ParticleProcessor::RELATIVE_RF);
127
128        _emitter->setStartTime(_startTime);
129        _emitter->setLifeTime(_emitterDuration);
130        _emitter->setEndless(false);
131
132        osgParticle::RandomRateCounter* counter = dynamic_cast<osgParticle::RandomRateCounter*>(_emitter->getCounter());
133        if (counter)
134        {
135            counter->setRateRange(3*_intensity,5*_intensity);
136        }
137
138        osgParticle::SectorPlacer* placer = dynamic_cast<osgParticle::SectorPlacer*>(_emitter->getPlacer());
139        if (placer)
140        {
141            placer->setCenter(_position);
142            placer->setRadiusRange(0.0f*_scale,0.25f*_scale);
143        }
144
145        osgParticle::RadialShooter* shooter = dynamic_cast<osgParticle::RadialShooter*>(_emitter->getShooter());
146        if (shooter)
147        {
148            shooter->setThetaRange(0.0f, osg::PI_4);
149            shooter->setInitialSpeedRange(0.0f*_scale,0.0f*_scale);
150        }
151    }
152
153    // set up program.
154    if (!_program)
155    {
156        _program = new osgParticle::FluidProgram;
157    }
158
159    if (_program.valid())
160    {
161        _program->setParticleSystem(_particleSystem.get());
162        _program->setWind(_wind);
163    }
164}
Note: See TracBrowser for help on using the browser.