| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 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 | |
|---|
| 23 | using namespace osgParticle; |
|---|
| 24 | |
|---|
| 25 | SmokeEffect::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 | |
|---|
| 40 | SmokeEffect::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 | |
|---|
| 54 | SmokeEffect::SmokeEffect(const SmokeEffect& copy, const osg::CopyOp& copyop): |
|---|
| 55 | ParticleEffect(copy,copyop) |
|---|
| 56 | { |
|---|
| 57 | if (_automaticSetup) buildEffect(); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | void SmokeEffect::setDefaults() |
|---|
| 61 | { |
|---|
| 62 | ParticleEffect::setDefaults(); |
|---|
| 63 | |
|---|
| 64 | _textureFileName = "Images/smoke.rgb"; |
|---|
| 65 | _emitterDuration = 65.0; |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 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 | |
|---|
| 78 | void SmokeEffect::setUpEmitterAndProgram() |
|---|
| 79 | { |
|---|
| 80 | |
|---|
| 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; |
|---|
| 94 | |
|---|
| 95 | ptemplate.setLifeTime(_defaultParticleTemplate.getLifeTime()); |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 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 | |
|---|
| 105 | ptemplate.setRadius(radius); |
|---|
| 106 | ptemplate.setMass(density*radius*radius*radius*osg::PI*4.0f/3.0f); |
|---|
| 107 | |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 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 | |
|---|
| 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 | } |
|---|