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