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

Revision 13041, 5.3 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/FireEffect>
15
16#include <osgParticle/ModularEmitter>
17#include <osgParticle/ModularProgram>
18#include <osgParticle/RandomRateCounter>
19#include <osgParticle/SectorPlacer>
20#include <osgParticle/RadialShooter>
21#include <osgParticle/AccelOperator>
22#include <osgParticle/FluidFrictionOperator>
23#include <osgParticle/ParticleSystemUpdater>
24
25#include <osg/Geode>
26
27using namespace osgParticle;
28
29FireEffect::FireEffect(bool automaticSetup):
30    ParticleEffect(automaticSetup)
31{
32    setDefaults();
33
34    _position.set(0.0f,0.0f,0.0f);
35    _scale = 1.0f;
36    _intensity = 1.0f;
37
38    _emitterDuration = 60.0;
39    _defaultParticleTemplate.setLifeTime(0.5+0.1*_scale);
40
41    if (_automaticSetup) buildEffect();
42}
43
44FireEffect::FireEffect(const osg::Vec3& position, float scale, float intensity)
45{
46    setDefaults();
47
48    _position = position;
49    _scale = scale;
50    _intensity = intensity;
51
52    _emitterDuration = 60.0;
53    _defaultParticleTemplate.setLifeTime(0.5+0.1*_scale);
54
55    if (_automaticSetup) buildEffect();
56}
57
58FireEffect::FireEffect(const FireEffect& copy, const osg::CopyOp& copyop):
59    ParticleEffect(copy,copyop)
60{
61    if (_automaticSetup) buildEffect();
62}
63
64void FireEffect::setDefaults()
65{
66    ParticleEffect::setDefaults();
67
68    _textureFileName = "Images/smoke.rgb";
69    _emitterDuration = 60.0;
70
71    // set up unit particle.
72    _defaultParticleTemplate.setLifeTime(0.5+0.1*_scale);
73    _defaultParticleTemplate.setSizeRange(osgParticle::rangef(0.75f, 3.0f));
74    _defaultParticleTemplate.setAlphaRange(osgParticle::rangef(0.1f, 1.0f));
75    _defaultParticleTemplate.setColorRange(osgParticle::rangev4(
76                                            osg::Vec4(1, 0.8f, 0.2f, 1.0f),
77                                            osg::Vec4(1, 0.3f, 0.2f, 0.0f)));
78}
79
80void FireEffect::setUpEmitterAndProgram()
81{
82
83    // set up particle system
84    if (!_particleSystem)
85    {
86        _particleSystem = new osgParticle::ParticleSystem;
87    }
88
89    if (_particleSystem.valid())
90    {
91        _particleSystem->setDefaultAttributes(_textureFileName, false, false);
92
93        osgParticle::Particle& ptemplate = _particleSystem->getDefaultParticleTemplate();
94
95        float radius = 0.25f*_scale;
96        float density = 0.5f; // 0.5kg/m^3
97
98        ptemplate.setLifeTime(_defaultParticleTemplate.getLifeTime());
99
100        // the following ranges set the envelope of the respective
101        // graphical properties in time.
102        ptemplate.setSizeRange(osgParticle::rangef(radius*_defaultParticleTemplate.getSizeRange().minimum,
103                                                   radius*_defaultParticleTemplate.getSizeRange().maximum));
104        ptemplate.setAlphaRange(_defaultParticleTemplate.getAlphaRange());
105        ptemplate.setColorRange(_defaultParticleTemplate.getColorRange());
106
107        // these are physical properties of the particle
108        // these are physical properties of the particle
109        ptemplate.setRadius(radius);
110        ptemplate.setMass(density*radius*radius*radius*osg::PI*4.0f/3.0f);
111
112    }
113
114
115    // set up emitter
116    if (!_emitter)
117    {
118        _emitter = new osgParticle::ModularEmitter;
119        _emitter->setNumParticlesToCreateMovementCompensationRatio(1.5f);
120        _emitter->setCounter(new osgParticle::RandomRateCounter);
121        _emitter->setPlacer(new osgParticle::SectorPlacer);
122        _emitter->setShooter(new osgParticle::RadialShooter);
123    }
124
125    if (_emitter.valid())
126    {
127        _emitter->setParticleSystem(_particleSystem.get());
128        _emitter->setReferenceFrame(_useLocalParticleSystem?
129                                    osgParticle::ParticleProcessor::ABSOLUTE_RF:
130                                    osgParticle::ParticleProcessor::RELATIVE_RF);
131
132        _emitter->setStartTime(_startTime);
133        _emitter->setLifeTime(_emitterDuration);
134        _emitter->setEndless(false);
135
136        osgParticle::RandomRateCounter* counter = dynamic_cast<osgParticle::RandomRateCounter*>(_emitter->getCounter());
137        if (counter)
138        {
139            counter->setRateRange(10*_intensity,15*_intensity);
140        }
141
142        osgParticle::SectorPlacer* placer = dynamic_cast<osgParticle::SectorPlacer*>(_emitter->getPlacer());
143        if (placer)
144        {
145            placer->setCenter(_position);
146            placer->setRadiusRange(0.0f*_scale,0.25f*_scale);
147        }
148
149        osgParticle::RadialShooter* shooter = dynamic_cast<osgParticle::RadialShooter*>(_emitter->getShooter());
150        if (shooter)
151        {
152            shooter->setThetaRange(0.0f, osg::PI_4);
153            shooter->setInitialSpeedRange(0.0f*_scale,0.0f*_scale);
154        }
155    }
156
157
158    // set up program.
159    if (!_program)
160    {
161        _program = new osgParticle::FluidProgram;
162    }
163
164    if (_program.valid())
165    {
166        _program->setParticleSystem(_particleSystem.get());
167        _program->setWind(_wind);
168    }
169}
Note: See TracBrowser for help on using the browser.