| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <osgParticle/ParticleEffect> |
|---|
| 15 | #include <osgParticle/ParticleSystemUpdater> |
|---|
| 16 | #include <osg/Geode> |
|---|
| 17 | |
|---|
| 18 | using namespace osgParticle; |
|---|
| 19 | |
|---|
| 20 | ParticleEffect::ParticleEffect(const ParticleEffect& copy, const osg::CopyOp& copyop): |
|---|
| 21 | osg::Group(copy,copyop), |
|---|
| 22 | _automaticSetup(copy._automaticSetup), |
|---|
| 23 | _useLocalParticleSystem(copy._useLocalParticleSystem), |
|---|
| 24 | _textureFileName(copy._textureFileName), |
|---|
| 25 | _defaultParticleTemplate(copy._defaultParticleTemplate), |
|---|
| 26 | _position(copy._position), |
|---|
| 27 | _scale(copy._scale), |
|---|
| 28 | _intensity(copy._intensity), |
|---|
| 29 | _startTime(copy._startTime), |
|---|
| 30 | _emitterDuration(copy._emitterDuration), |
|---|
| 31 | _wind(copy._wind) |
|---|
| 32 | { |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | void ParticleEffect::setUseLocalParticleSystem(bool local) |
|---|
| 36 | { |
|---|
| 37 | if (_useLocalParticleSystem==local) return; |
|---|
| 38 | |
|---|
| 39 | _useLocalParticleSystem = local; |
|---|
| 40 | |
|---|
| 41 | if (_automaticSetup) buildEffect(); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | void ParticleEffect::setTextureFileName(const std::string& filename) |
|---|
| 45 | { |
|---|
| 46 | _textureFileName = filename; |
|---|
| 47 | |
|---|
| 48 | if (_automaticSetup) setUpEmitterAndProgram(); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | void ParticleEffect::setDefaultParticleTemplate(const Particle& p) |
|---|
| 52 | { |
|---|
| 53 | _defaultParticleTemplate = p; |
|---|
| 54 | |
|---|
| 55 | if (_automaticSetup) setUpEmitterAndProgram(); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | void ParticleEffect::setPosition(const osg::Vec3& position) |
|---|
| 59 | { |
|---|
| 60 | if (_position==position) return; |
|---|
| 61 | |
|---|
| 62 | _position = position; |
|---|
| 63 | |
|---|
| 64 | if (_automaticSetup) setUpEmitterAndProgram(); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | void ParticleEffect::setScale(float scale) |
|---|
| 68 | { |
|---|
| 69 | if (_scale==scale) return; |
|---|
| 70 | |
|---|
| 71 | _scale = scale; |
|---|
| 72 | |
|---|
| 73 | if (_automaticSetup) setUpEmitterAndProgram(); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | void ParticleEffect::setIntensity(float intensity) |
|---|
| 77 | { |
|---|
| 78 | if (_intensity==intensity) return; |
|---|
| 79 | |
|---|
| 80 | _intensity = intensity; |
|---|
| 81 | |
|---|
| 82 | if (_automaticSetup) setUpEmitterAndProgram(); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | void ParticleEffect::setStartTime(double startTime) |
|---|
| 86 | { |
|---|
| 87 | if (_startTime==startTime) return; |
|---|
| 88 | |
|---|
| 89 | _startTime =startTime; |
|---|
| 90 | |
|---|
| 91 | if (_automaticSetup) setUpEmitterAndProgram(); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | void ParticleEffect::setEmitterDuration(double duration) |
|---|
| 95 | { |
|---|
| 96 | if (_emitterDuration==duration) return; |
|---|
| 97 | |
|---|
| 98 | _emitterDuration = duration; |
|---|
| 99 | |
|---|
| 100 | if (_automaticSetup) setUpEmitterAndProgram(); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | void ParticleEffect::setParticleDuration(double duration) |
|---|
| 104 | { |
|---|
| 105 | if (_defaultParticleTemplate.getLifeTime()==duration) return; |
|---|
| 106 | |
|---|
| 107 | _defaultParticleTemplate.setLifeTime(duration); |
|---|
| 108 | |
|---|
| 109 | if (_automaticSetup) setUpEmitterAndProgram(); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | void ParticleEffect::setWind(const osg::Vec3& wind) |
|---|
| 113 | { |
|---|
| 114 | if (_wind==wind) return; |
|---|
| 115 | |
|---|
| 116 | _wind = wind; |
|---|
| 117 | |
|---|
| 118 | if (_automaticSetup) setUpEmitterAndProgram(); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | void ParticleEffect::setParticleSystem(ParticleSystem* ps) |
|---|
| 122 | { |
|---|
| 123 | if (_particleSystem==ps) return; |
|---|
| 124 | |
|---|
| 125 | _particleSystem = ps; |
|---|
| 126 | |
|---|
| 127 | if (_automaticSetup) buildEffect(); |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | void ParticleEffect::setDefaults() |
|---|
| 131 | { |
|---|
| 132 | _useLocalParticleSystem = true; |
|---|
| 133 | _scale = 1.0f; |
|---|
| 134 | _intensity = 1.0f; |
|---|
| 135 | _startTime = 0.0; |
|---|
| 136 | _emitterDuration = 1.0; |
|---|
| 137 | _wind.set(0.0f,0.0f,0.0f); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | void ParticleEffect::buildEffect() |
|---|
| 141 | { |
|---|
| 142 | setUpEmitterAndProgram(); |
|---|
| 143 | |
|---|
| 144 | osg::ref_ptr<Emitter> emitter = getEmitter(); |
|---|
| 145 | osg::ref_ptr<Program> program = getProgram(); |
|---|
| 146 | osg::ref_ptr<ParticleSystem> particleSystem = getParticleSystem(); |
|---|
| 147 | |
|---|
| 148 | if (!emitter || !particleSystem || !program) return; |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | removeChildren(0,getNumChildren()); |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | addChild(emitter.get()); |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | addChild(program.get()); |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | osg::ref_ptr<osgParticle::ParticleSystemUpdater> psu = new osgParticle::ParticleSystemUpdater; |
|---|
| 162 | psu->addParticleSystem(particleSystem.get()); |
|---|
| 163 | addChild(psu.get()); |
|---|
| 164 | |
|---|
| 165 | if (_useLocalParticleSystem) |
|---|
| 166 | { |
|---|
| 167 | particleSystem->setParticleScaleReferenceFrame(ParticleSystem::LOCAL_COORDINATES); |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | osg::Geode* geode = new osg::Geode; |
|---|
| 171 | geode->addDrawable(particleSystem.get()); |
|---|
| 172 | addChild(geode); |
|---|
| 173 | } |
|---|
| 174 | } |
|---|