[1853] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
[1855] | 10 | |
---|
| 11 | ### File Specific: |
---|
[3925] | 12 | main-programmer: Benjamin Grauer |
---|
[1855] | 13 | co-programmer: ... |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[3934] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_PARTICLE |
---|
[1853] | 17 | |
---|
[3925] | 18 | #include "particle_system.h" |
---|
[1853] | 19 | |
---|
[3930] | 20 | #include "particle_emitter.h" |
---|
| 21 | #include "particle_engine.h" |
---|
[3932] | 22 | #include "compiler.h" |
---|
[3930] | 23 | |
---|
[1856] | 24 | using namespace std; |
---|
[1853] | 25 | |
---|
[1856] | 26 | |
---|
[3245] | 27 | /** |
---|
| 28 | \brief standard constructor |
---|
[3930] | 29 | \param count the Count of particles in the System |
---|
| 30 | \param type The Type of the ParticleSystem |
---|
| 31 | |
---|
[3245] | 32 | \todo this constructor is not jet implemented - do it |
---|
| 33 | */ |
---|
[3934] | 34 | ParticleSystem::ParticleSystem (unsigned int maxCount, PARTICLE_TYPE type) |
---|
[3365] | 35 | { |
---|
[3925] | 36 | this->setClassName ("ParticleSystem"); |
---|
[3930] | 37 | |
---|
[3934] | 38 | this->maxCount = maxCount; |
---|
| 39 | this->count = 0; |
---|
[3930] | 40 | this->particleType = type; |
---|
[3932] | 41 | this->particles = NULL; |
---|
[3935] | 42 | this->setConserve(.8); |
---|
[3934] | 43 | this->setLifeSpan(.1); |
---|
[3938] | 44 | this->setInheritSpeed(0); |
---|
[3932] | 45 | |
---|
| 46 | ParticleEngine::getInstance()->addSystem(this); |
---|
[3365] | 47 | } |
---|
[1853] | 48 | |
---|
| 49 | |
---|
[3245] | 50 | /** |
---|
| 51 | \brief standard deconstructor |
---|
[1853] | 52 | |
---|
[3245] | 53 | */ |
---|
[3932] | 54 | ParticleSystem::~ParticleSystem() |
---|
[3543] | 55 | { |
---|
| 56 | // delete what has to be deleted here |
---|
[3935] | 57 | ParticleEngine::getInstance()->removeSystem(this); |
---|
[3543] | 58 | } |
---|
[1853] | 59 | |
---|
[3932] | 60 | // setting properties |
---|
| 61 | void ParticleSystem::setMaterial(Material* material) |
---|
| 62 | { |
---|
[3935] | 63 | this->material = material; |
---|
[3932] | 64 | } |
---|
[3931] | 65 | |
---|
[3938] | 66 | |
---|
| 67 | /** |
---|
| 68 | \brief how much of the speed from the ParticleEmitter should flow onto the ParticleSystem |
---|
| 69 | \param value a Value between zero and one |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | if you want to change the value of this variable during emission time (to make it more dynamic) |
---|
| 73 | you may want to use the animation class |
---|
| 74 | */ |
---|
| 75 | void ParticleSystem::setInheritSpeed(float value) |
---|
| 76 | { |
---|
| 77 | if (unlikely(value > 1.0)) |
---|
| 78 | this->inheritSpeed = 1; |
---|
| 79 | else if (unlikely(value < 0.0)) |
---|
| 80 | this->inheritSpeed = 0; |
---|
| 81 | else |
---|
| 82 | this->inheritSpeed = value; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | |
---|
[3932] | 86 | void ParticleSystem::setLifeSpan(float lifeSpan, float randomLifeSpan) |
---|
| 87 | { |
---|
[3934] | 88 | this->lifeSpan = lifeSpan; |
---|
| 89 | this->randomLifeSpan = randomLifeSpan; |
---|
[3932] | 90 | } |
---|
| 91 | |
---|
| 92 | void ParticleSystem::setRadius(float startRadius, float endRadius, float randomRadius) |
---|
| 93 | { |
---|
[3935] | 94 | this->startRadius = startRadius; |
---|
| 95 | this->endRadius = endRadius; |
---|
| 96 | this->randomRadius = randomRadius; |
---|
[3932] | 97 | } |
---|
| 98 | |
---|
| 99 | void ParticleSystem::setConserve(float conserve) |
---|
| 100 | { |
---|
[3935] | 101 | if (conserve > 1.0) |
---|
| 102 | this->conserve = 1.0; |
---|
| 103 | else if (conserve < 0.0) |
---|
| 104 | this->conserve = 0.0; |
---|
| 105 | else |
---|
| 106 | this->conserve = conserve; |
---|
[3932] | 107 | } |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | |
---|
[3931] | 111 | void ParticleSystem::tick(float dt) |
---|
| 112 | { |
---|
[3934] | 113 | Particle* tickPart = particles; // the particle to Tick |
---|
| 114 | Particle* prevPart = NULL; // |
---|
| 115 | while (likely(tickPart != NULL)) |
---|
[3932] | 116 | { |
---|
[3935] | 117 | |
---|
[3934] | 118 | tickPart->position = tickPart->position + tickPart->velocity; |
---|
[3936] | 119 | tickPart->radius + tickPart->radiusIt * dt; |
---|
[3932] | 120 | // many more to come |
---|
| 121 | |
---|
[3934] | 122 | |
---|
| 123 | |
---|
[3935] | 124 | if (this->conserve < 1.0) |
---|
| 125 | tickPart->velocity = tickPart->velocity * this->conserve; |
---|
[3934] | 126 | // find out if we have to delete tickPart |
---|
| 127 | if ((tickPart->timeToLive -= dt) <= 0) |
---|
| 128 | { |
---|
| 129 | // remove the particle from the list |
---|
| 130 | if (likely(prevPart != NULL)) |
---|
| 131 | { |
---|
| 132 | prevPart->next = tickPart->next; |
---|
| 133 | delete tickPart; |
---|
| 134 | tickPart = prevPart->next; |
---|
| 135 | } |
---|
| 136 | else |
---|
| 137 | { |
---|
| 138 | prevPart = NULL; |
---|
| 139 | this->particles = tickPart->next; |
---|
| 140 | delete tickPart; |
---|
| 141 | tickPart = this->particles; |
---|
| 142 | } |
---|
| 143 | --this->count; |
---|
| 144 | } |
---|
| 145 | else |
---|
| 146 | { |
---|
| 147 | prevPart = tickPart; |
---|
| 148 | tickPart = tickPart->next; |
---|
| 149 | } |
---|
[3932] | 150 | } |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | void ParticleSystem::draw(void) |
---|
| 154 | { |
---|
[3934] | 155 | Particle* drawPart = particles; |
---|
| 156 | if (likely(drawPart != NULL)) |
---|
[3932] | 157 | { |
---|
[3935] | 158 | glBegin(GL_POINTS); |
---|
[3934] | 159 | while (likely(drawPart != NULL)) |
---|
[3932] | 160 | { |
---|
| 161 | // draw in DOT mode |
---|
[3934] | 162 | glVertex3f(drawPart->position.x, drawPart->position.y, drawPart->position.z); |
---|
[3932] | 163 | |
---|
| 164 | |
---|
[3934] | 165 | drawPart = drawPart->next; |
---|
[3932] | 166 | } |
---|
| 167 | glEnd(); |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | |
---|
| 172 | void ParticleSystem::addParticle(Vector position, Vector velocity, unsigned int data) |
---|
| 173 | { |
---|
[3934] | 174 | if (this->count <= this->maxCount) |
---|
[3932] | 175 | { |
---|
[3934] | 176 | // if it is the first Particle |
---|
| 177 | if (unlikely(particles == NULL)) |
---|
| 178 | { |
---|
| 179 | this->particles = new Particle; |
---|
| 180 | this->particles->next = NULL; |
---|
| 181 | } |
---|
| 182 | // filling the List from the beginning |
---|
| 183 | else |
---|
| 184 | { |
---|
| 185 | Particle* tmpPart = new Particle; |
---|
| 186 | tmpPart->next = this->particles; |
---|
| 187 | this->particles = tmpPart; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | particles->timeToLive = this->lifeSpan + (float)(random()/RAND_MAX)* this->randomLifeSpan; |
---|
| 191 | particles->position = position; |
---|
| 192 | particles->velocity = velocity; |
---|
| 193 | // particle->rotation = ; //! \todo rotation is once again something to be done. |
---|
[3936] | 194 | particles->mass = this->initialMass + (random()/RAND_MAX -.5)* this->randomInitialMass; |
---|
| 195 | particles->radius = this->startRadius + (random()/RAND_MAX-.5)*this->randomRadius; |
---|
| 196 | |
---|
| 197 | particles->radiusIt = (this->endRadius + (random()/RAND_MAX-.5)*this->randomRadius - particles->radius) / particles->timeToLive; |
---|
[3934] | 198 | |
---|
| 199 | ++this->count; |
---|
[3932] | 200 | } |
---|
| 201 | else |
---|
[3934] | 202 | PRINTF(4)("maximum count of particles reached not adding any more\n"); |
---|
[3932] | 203 | } |
---|
[3931] | 204 | |
---|