[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" |
---|
[3942] | 23 | #include "material.h" |
---|
[3930] | 24 | |
---|
[1856] | 25 | using namespace std; |
---|
[1853] | 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"); |
---|
[3944] | 37 | this->name = NULL; |
---|
[3934] | 38 | this->maxCount = maxCount; |
---|
| 39 | this->count = 0; |
---|
[3930] | 40 | this->particleType = type; |
---|
[3932] | 41 | this->particles = NULL; |
---|
[4123] | 42 | this->deadList = NULL; |
---|
[3935] | 43 | this->setConserve(.8); |
---|
[3934] | 44 | this->setLifeSpan(.1); |
---|
[3938] | 45 | this->setInheritSpeed(0); |
---|
[3942] | 46 | this->glID = NULL; |
---|
| 47 | this->setRadius(1.0, 1.0, 0.0); |
---|
| 48 | this->setType(PARTICLE_SPRITE, 1); |
---|
[3932] | 49 | ParticleEngine::getInstance()->addSystem(this); |
---|
[3365] | 50 | } |
---|
[1853] | 51 | |
---|
| 52 | |
---|
[3245] | 53 | /** |
---|
| 54 | \brief standard deconstructor |
---|
| 55 | */ |
---|
[3932] | 56 | ParticleSystem::~ParticleSystem() |
---|
[3543] | 57 | { |
---|
| 58 | // delete what has to be deleted here |
---|
[3935] | 59 | ParticleEngine::getInstance()->removeSystem(this); |
---|
[4123] | 60 | |
---|
| 61 | // deleting all the living Particles |
---|
| 62 | while (this->particles) |
---|
| 63 | { |
---|
| 64 | Particle* tmpDelPart = this->particles; |
---|
| 65 | this->particles = this->particles->next; |
---|
| 66 | delete tmpDelPart; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | // deleting all the dead particles |
---|
| 70 | while (this->deadList) |
---|
| 71 | { |
---|
| 72 | Particle* tmpDelPart = this->deadList; |
---|
| 73 | this->deadList = this->deadList->next; |
---|
| 74 | delete tmpDelPart; |
---|
| 75 | } |
---|
[3543] | 76 | } |
---|
[1853] | 77 | |
---|
[3945] | 78 | /** |
---|
| 79 | \brief sets the Name of the Particle System |
---|
| 80 | \param name the Name of the System |
---|
| 81 | */ |
---|
[3944] | 82 | void ParticleSystem::setName(const char* name) |
---|
| 83 | { |
---|
| 84 | if (this->name) |
---|
| 85 | delete this->name; |
---|
| 86 | this->name = new char[strlen(name)+1]; |
---|
| 87 | strcpy(this->name, name); |
---|
| 88 | } |
---|
[3942] | 89 | |
---|
[3945] | 90 | /** |
---|
| 91 | \returns the Name of the ParticleSystem |
---|
| 92 | */ |
---|
[3944] | 93 | const char* ParticleSystem::getName(void) const |
---|
| 94 | { |
---|
| 95 | return this->name; |
---|
| 96 | } |
---|
| 97 | |
---|
[3945] | 98 | /** |
---|
| 99 | \todo this will be different |
---|
| 100 | */ |
---|
[3942] | 101 | void ParticleSystem::setType(PARTICLE_TYPE particleType, int count) |
---|
| 102 | { |
---|
| 103 | this->particleType = particleType; |
---|
| 104 | this->dialectCount = count; |
---|
| 105 | if (glID != NULL) |
---|
| 106 | delete glID; |
---|
| 107 | |
---|
| 108 | glID = new GLuint[count]; |
---|
| 109 | for (int i = 0; i< count; i++) |
---|
| 110 | glID[i] = 0; |
---|
| 111 | |
---|
| 112 | glID[0] = glGenLists(count); |
---|
| 113 | |
---|
| 114 | material = new Material("transperencyMap"); |
---|
[3948] | 115 | material->setDiffuseMap("pictures/radialTransparency.png"); |
---|
[3946] | 116 | // material->setTransparency(.5); |
---|
| 117 | |
---|
[3942] | 118 | glNewList(glID[0], GL_COMPILE); |
---|
| 119 | glBegin(GL_TRIANGLE_STRIP); |
---|
| 120 | glTexCoord2f(1, 1); |
---|
[3945] | 121 | glVertex3f(0.0, .5, .5); |
---|
[3942] | 122 | glTexCoord2f(1, 0); |
---|
[3946] | 123 | glVertex3f(0.0, -.5, .5); |
---|
| 124 | glTexCoord2f(0, 1); |
---|
[3945] | 125 | glVertex3f(0.0, .5, -.5); |
---|
[3942] | 126 | glTexCoord2f(0, 0); |
---|
[3945] | 127 | glVertex3f(0.0, -.5, -.5); |
---|
[3942] | 128 | glEnd(); |
---|
| 129 | glEndList(); |
---|
| 130 | } |
---|
| 131 | |
---|
[3932] | 132 | // setting properties |
---|
| 133 | void ParticleSystem::setMaterial(Material* material) |
---|
| 134 | { |
---|
[3935] | 135 | this->material = material; |
---|
[3932] | 136 | } |
---|
[3931] | 137 | |
---|
[3938] | 138 | |
---|
[3942] | 139 | |
---|
[3938] | 140 | /** |
---|
| 141 | \brief how much of the speed from the ParticleEmitter should flow onto the ParticleSystem |
---|
| 142 | \param value a Value between zero and one |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | if you want to change the value of this variable during emission time (to make it more dynamic) |
---|
| 146 | you may want to use the animation class |
---|
| 147 | */ |
---|
| 148 | void ParticleSystem::setInheritSpeed(float value) |
---|
| 149 | { |
---|
| 150 | if (unlikely(value > 1.0)) |
---|
| 151 | this->inheritSpeed = 1; |
---|
| 152 | else if (unlikely(value < 0.0)) |
---|
| 153 | this->inheritSpeed = 0; |
---|
| 154 | else |
---|
| 155 | this->inheritSpeed = value; |
---|
| 156 | } |
---|
| 157 | |
---|
[3945] | 158 | /** |
---|
| 159 | \brief Sets the lifespan of newly created particles |
---|
| 160 | */ |
---|
[3932] | 161 | void ParticleSystem::setLifeSpan(float lifeSpan, float randomLifeSpan) |
---|
| 162 | { |
---|
[3934] | 163 | this->lifeSpan = lifeSpan; |
---|
| 164 | this->randomLifeSpan = randomLifeSpan; |
---|
[3932] | 165 | } |
---|
| 166 | |
---|
[3945] | 167 | /** |
---|
| 168 | \brief sets the radius of newly created particles |
---|
| 169 | */ |
---|
[3942] | 170 | void ParticleSystem::setRadius(float startRadius, float endRadius, float randomStartRadius, float randomEndRadius) |
---|
[3932] | 171 | { |
---|
[3935] | 172 | this->startRadius = startRadius; |
---|
| 173 | this->endRadius = endRadius; |
---|
[3942] | 174 | this->randomStartRadius = randomStartRadius; |
---|
| 175 | this->randomEndRadius = randomEndRadius; |
---|
[3932] | 176 | } |
---|
| 177 | |
---|
[3945] | 178 | /** |
---|
| 179 | \brief sets the conserve Factor of newly created particles |
---|
| 180 | */ |
---|
[3932] | 181 | void ParticleSystem::setConserve(float conserve) |
---|
| 182 | { |
---|
[3935] | 183 | if (conserve > 1.0) |
---|
| 184 | this->conserve = 1.0; |
---|
| 185 | else if (conserve < 0.0) |
---|
| 186 | this->conserve = 0.0; |
---|
| 187 | else |
---|
| 188 | this->conserve = conserve; |
---|
[3932] | 189 | } |
---|
| 190 | |
---|
[3945] | 191 | /** |
---|
| 192 | \brief ticks the system. |
---|
| 193 | \param dt the time to tick all the Particles of the System |
---|
[3932] | 194 | |
---|
[3945] | 195 | this is used to get all the particles some motion |
---|
| 196 | */ |
---|
[3931] | 197 | void ParticleSystem::tick(float dt) |
---|
| 198 | { |
---|
[3934] | 199 | Particle* tickPart = particles; // the particle to Tick |
---|
| 200 | Particle* prevPart = NULL; // |
---|
| 201 | while (likely(tickPart != NULL)) |
---|
[3932] | 202 | { |
---|
[3934] | 203 | tickPart->position = tickPart->position + tickPart->velocity; |
---|
[3942] | 204 | tickPart->radius += tickPart->radiusIt * dt; |
---|
| 205 | |
---|
[3932] | 206 | // many more to come |
---|
| 207 | |
---|
[3934] | 208 | |
---|
[3935] | 209 | if (this->conserve < 1.0) |
---|
| 210 | tickPart->velocity = tickPart->velocity * this->conserve; |
---|
[3934] | 211 | // find out if we have to delete tickPart |
---|
| 212 | if ((tickPart->timeToLive -= dt) <= 0) |
---|
| 213 | { |
---|
| 214 | // remove the particle from the list |
---|
| 215 | if (likely(prevPart != NULL)) |
---|
| 216 | { |
---|
| 217 | prevPart->next = tickPart->next; |
---|
[4123] | 218 | tickPart->next = this->deadList; |
---|
| 219 | this->deadList = tickPart; |
---|
[3934] | 220 | tickPart = prevPart->next; |
---|
| 221 | } |
---|
| 222 | else |
---|
| 223 | { |
---|
| 224 | prevPart = NULL; |
---|
| 225 | this->particles = tickPart->next; |
---|
[4123] | 226 | tickPart->next = this->deadList; |
---|
| 227 | this->deadList = tickPart; |
---|
[3934] | 228 | tickPart = this->particles; |
---|
| 229 | } |
---|
| 230 | --this->count; |
---|
| 231 | } |
---|
| 232 | else |
---|
| 233 | { |
---|
| 234 | prevPart = tickPart; |
---|
| 235 | tickPart = tickPart->next; |
---|
| 236 | } |
---|
[3932] | 237 | } |
---|
| 238 | } |
---|
| 239 | |
---|
[3945] | 240 | /** |
---|
| 241 | \brief draws all the Particles of this System |
---|
| 242 | */ |
---|
[3932] | 243 | void ParticleSystem::draw(void) |
---|
| 244 | { |
---|
[3942] | 245 | // material->select(); |
---|
| 246 | |
---|
| 247 | |
---|
| 248 | glMatrixMode(GL_MODELVIEW); |
---|
[3946] | 249 | // glDisable(GL_LIGHTING); |
---|
[4017] | 250 | material->select(); |
---|
| 251 | glDisable(GL_DEPTH_TEST); |
---|
| 252 | Particle* drawPart = particles; |
---|
[3934] | 253 | if (likely(drawPart != NULL)) |
---|
[3932] | 254 | { |
---|
[4017] | 255 | //draw in DOT mode |
---|
| 256 | // glBegin(GL_POINTS); |
---|
[3934] | 257 | while (likely(drawPart != NULL)) |
---|
[3932] | 258 | { |
---|
[3942] | 259 | glPushMatrix(); |
---|
| 260 | glTranslatef(drawPart->position.x, drawPart->position.y, drawPart->position.z); |
---|
| 261 | glScalef(drawPart->radius, drawPart->radius, drawPart->radius); |
---|
| 262 | glCallList(*this->glID); |
---|
[3932] | 263 | |
---|
[4017] | 264 | //glVertex3f(drawPart->position.x, drawPart->position.y, drawPart->position.z); |
---|
[3934] | 265 | drawPart = drawPart->next; |
---|
[3942] | 266 | glPopMatrix(); |
---|
[3932] | 267 | } |
---|
[4017] | 268 | // glEnd(); |
---|
[3932] | 269 | } |
---|
[4017] | 270 | // glEnable(GL_LIGHTING); |
---|
| 271 | glEnable(GL_DEPTH_TEST); |
---|
[3932] | 272 | } |
---|
| 273 | |
---|
[3945] | 274 | /** |
---|
| 275 | \brief adds a new Particle to the System |
---|
| 276 | \param position the position where the particle gets emitted. |
---|
| 277 | \param velocity the Starting velocity of the particle. |
---|
| 278 | \param data some more data given by the emitter |
---|
| 279 | */ |
---|
[3951] | 280 | void ParticleSystem::addParticle(const Vector& position, const Vector& velocity, unsigned int data) |
---|
[3932] | 281 | { |
---|
[3934] | 282 | if (this->count <= this->maxCount) |
---|
[3932] | 283 | { |
---|
[3934] | 284 | // if it is the first Particle |
---|
| 285 | if (unlikely(particles == NULL)) |
---|
| 286 | { |
---|
[4123] | 287 | if (likely(deadList != NULL)) |
---|
| 288 | { |
---|
| 289 | this->particles = this->deadList; |
---|
| 290 | deadList = deadList->next; |
---|
| 291 | } |
---|
| 292 | else |
---|
[4129] | 293 | { |
---|
| 294 | PRINTF(5)("Generating new Particle\n"); |
---|
| 295 | this->particles = new Particle; |
---|
| 296 | } |
---|
[3934] | 297 | this->particles->next = NULL; |
---|
| 298 | } |
---|
| 299 | // filling the List from the beginning |
---|
| 300 | else |
---|
| 301 | { |
---|
[4123] | 302 | Particle* tmpPart; |
---|
| 303 | if (likely(deadList != NULL)) |
---|
| 304 | { |
---|
| 305 | tmpPart = this->deadList; |
---|
| 306 | deadList = deadList->next; |
---|
| 307 | } |
---|
| 308 | else |
---|
[4129] | 309 | { |
---|
| 310 | PRINTF(5)("Generating new Particle\n"); |
---|
| 311 | tmpPart = new Particle; |
---|
| 312 | } |
---|
[3934] | 313 | tmpPart->next = this->particles; |
---|
| 314 | this->particles = tmpPart; |
---|
| 315 | } |
---|
| 316 | |
---|
[4017] | 317 | particles->timeToLive = this->lifeSpan + (float)(rand()/RAND_MAX)* this->randomLifeSpan; |
---|
[3934] | 318 | particles->position = position; |
---|
| 319 | particles->velocity = velocity; |
---|
[3951] | 320 | |
---|
[3934] | 321 | // particle->rotation = ; //! \todo rotation is once again something to be done. |
---|
[4017] | 322 | particles->mass = this->initialMass + (rand()/RAND_MAX -.5)* this->randomInitialMass; |
---|
| 323 | particles->radius = this->startRadius + (rand()/RAND_MAX-.5)*this->randomStartRadius; |
---|
[3936] | 324 | |
---|
[4017] | 325 | particles->radiusIt = (this->endRadius + (rand()/RAND_MAX-.5)*this->randomEndRadius - particles->radius) / particles->timeToLive; |
---|
[3934] | 326 | |
---|
| 327 | ++this->count; |
---|
[3932] | 328 | } |
---|
| 329 | else |
---|
[4017] | 330 | PRINTF(5)("maximum count of particles reached not adding any more\n"); |
---|
[3932] | 331 | } |
---|
[3931] | 332 | |
---|
[3944] | 333 | /** |
---|
| 334 | \brief outputs some nice debug information |
---|
| 335 | */ |
---|
| 336 | void ParticleSystem::debug(void) |
---|
| 337 | { |
---|
| 338 | PRINT(0)(" ParticleSystem %s\n", this->name); |
---|
| 339 | PRINT(0)(" ParticleCount: %d, maximumCount: %d :: filled %d%%\n", this->count, this->maxCount, 100*this->count/this->maxCount); |
---|
[4123] | 340 | if (deadList) |
---|
| 341 | { |
---|
| 342 | PRINT(0)(" - ParticleDeadList is used: "); |
---|
| 343 | int i = 1; |
---|
| 344 | Particle* tmpPart = this->deadList; |
---|
| 345 | while (tmpPart = tmpPart->next) ++i; |
---|
| 346 | PRINT(0)("count: %d\n", i); |
---|
| 347 | } |
---|
[3944] | 348 | } |
---|