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