Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/particles/particle_system.cc @ 6883

Last change on this file since 6883 was 6823, checked in by bensch, 19 years ago

orxonox/trunk: BoxEmitter

File size: 13.9 KB
RevLine 
[4597]1/*
[1853]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
[5357]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS
[1853]17
[3925]18#include "particle_system.h"
[1853]19
[3930]20#include "particle_emitter.h"
[4338]21
22#include "field.h"
[5511]23#include "model.h"
[4338]24
[5155]25#include "load_param.h"
[5652]26#include "factory.h"
[3942]27#include "material.h"
[4338]28#include "state.h"
[5446]29#include "shell_command.h"
[3930]30
[5944]31#include "parser/tinyxml/tinyxml.h"
[6612]32#include <algorithm>
[4338]33
[1856]34using namespace std;
[3245]35/**
[4836]36 *  standard constructor
37 * @param maxCount the Count of particles in the System
38 * @param type The Type of the ParticleSystem
[3245]39*/
[6621]40ParticleSystem::ParticleSystem (unsigned int maxCount)
[3365]41{
[4602]42  this->init();
[4597]43
[4727]44  this->setMaxCount(maxCount);
[3365]45}
[1853]46
[4677]47/**
[4836]48 *  standard deconstructor
[3245]49*/
[4176]50ParticleSystem::~ParticleSystem()
[3543]51{
[6625]52  // deleting all the living Particles
53  while (this->particles)
54  {
55    Particle* tmpDelPart = this->particles;
56    this->particles = this->particles->next;
57    delete tmpDelPart;
58  }
[4123]59
[6625]60  // deleting all the dead particles
61  while (this->deadList)
62  {
63    Particle* tmpDelPart = this->deadList;
64    this->deadList = this->deadList->next;
65    delete tmpDelPart;
66  }
[4176]67
[6625]68  while(!this->emitters.empty())
69  {
70    this->removeEmitter(this->emitters.front());
71  }
[6623]72
[3543]73}
[1853]74
[3945]75/**
[6629]76 * @brief initializes the ParticleSystem with default values
77 */
[4746]78void ParticleSystem::init()
[4602]79{
80  this->setClassID(CL_PARTICLE_SYSTEM, "ParticleSystem");
81
[4727]82  this->setMaxCount(PARTICLE_DEFAULT_MAX_COUNT);
[4602]83  this->count = 0;
84  this->particles = NULL;
85  this->deadList = NULL;
86  this->setConserve(1);
87  this->setLifeSpan(1);
[6612]88
89  this->toList(OM_ENVIRON);
[4602]90}
91
92
93/**
94 * loads Parameters from a TiXmlElement
95 * @param root the XML-element to load from.
96 */
97void ParticleSystem::loadParams(const TiXmlElement* root)
98{
[6512]99  WorldEntity::loadParams(root);
100  PhysicsInterface::loadParams(root);
[4602]101
[5671]102  LoadParam(root, "max-count", this, ParticleSystem, setMaxCount)
[6625]103  .describe("the maximal count of Particles, that can be emitted into this system");
[4727]104
[5671]105  LoadParam(root, "life-span", this, ParticleSystem, setLifeSpan)
[6625]106  .describe("sets the life-span of the Particles.");
[4602]107
[5671]108  LoadParam(root, "conserve", this, ParticleSystem, setConserve)
[6625]109  .describe("sets the Conserve factor of the Particles (1.0: they keep all their energy, 0.0:they keep no energy)");
[4725]110
[6623]111  LoadParamXML(root, "emitters", this, ParticleSystem, loadEmitters);
[6222]112
[5654]113  LOAD_PARAM_START_CYCLE(root, element);
[4727]114  {
[5654]115    element->ToText();
[6625]116    // PER-PARTICLE-ATTRIBUTES:
[5654]117    LoadParam_CYCLE(element, "radius", this, ParticleSystem, setRadius)
[6625]118    .describe("The Radius of each particle over time (TimeIndex [0-1], radius at TimeIndex, randomRadius at TimeIndex)");
[4725]119
[5654]120    LoadParam_CYCLE(element, "mass", this, ParticleSystem, setMass)
[6625]121    .describe("The Mass of each particle over time (TimeIndex: [0-1], mass at TimeIndex, randomMass at TimeIndex)");
[4725]122
[5654]123    LoadParam_CYCLE(element, "color", this, ParticleSystem, setColor)
[6625]124    .describe("The Color of each particle over time (TimeIndex: [0-1], red: [0-1], green: [0-1], blue: [0-1], alpha: [0-1])");
[4727]125  }
[5654]126  LOAD_PARAM_END_CYCLE(element);
[6629]127
128  LoadParam(root, "precache", this, ParticleSystem, precache)
129      .describe("Precaches the ParticleSystem for %1 seconds, %2 times per Second")
130      .defaultValues(2, 1, 25);
[4602]131}
[4727]132
[4725]133/**
[6623]134 * @brief loads the Emitters from An XML-Root
135 * @param root the XML-Element to load all emitters from
136 */
137void ParticleSystem::loadEmitters(const TiXmlElement* root)
138{
139  LOAD_PARAM_START_CYCLE(root, element);
140  {
141    BaseObject* emitter = Factory::fabricate(element);
[6823]142    if (emitter != NULL)
143    {
144      if (emitter->isA(CL_PARTICLE_EMITTER))
145        this->addEmitter(dynamic_cast<ParticleEmitter*>(emitter));
146      else
147      {
148        PRINTF(2)("Tried to load an Element of type '%s' that should be a ParticleEmitter onto '%s::%s'.\n",
149                  emitter->getClassName(), this->getClassName(), this->getName());
150        delete emitter;
151      }
152    }
[6623]153    else
154    {
[6823]155      PRINTF(2)("Could not Generate Emitter for system %s::%s (wrong type in XML-format)\n", this->getClassName(), getName());
[6623]156    }
157  }
158  LOAD_PARAM_END_CYCLE(element);
159}
160
161/**
[4836]162* @param maxCount the maximum count of particles that can be emitted
[4727]163 */
164void ParticleSystem::setMaxCount(int maxCount)
165{
166  this->maxCount = maxCount;
167}
168
[3932]169// setting properties
[4478]170/**
[4836]171 *  Sets the lifespan of newly created particles
[4597]172*/
[3932]173void ParticleSystem::setLifeSpan(float lifeSpan, float randomLifeSpan)
174{
[3934]175  this->lifeSpan = lifeSpan;
176  this->randomLifeSpan = randomLifeSpan;
[3932]177}
178
[3945]179/**
[4836]180 *  sets the conserve Factor of newly created particles
[3945]181*/
[4430]182void ParticleSystem::setConserve(float conserve)
[3932]183{
[4430]184  if (conserve > 1.0)
185    this->conserve = 1.0;
186  else if (conserve < 0.0)
187    this->conserve = 0.0;
188  else
189    this->conserve = conserve;
[3932]190}
191
[4430]192/////////////////////////////
193/* Per-Particle Attributes */
194/////////////////////////////
[3945]195/**
[4836]196 *  sets a key in the radius-animation on a per-particle basis
197 * @param lifeCycleTime the time (partilceLifeTime/particleAge) [0-1]
198 * @param radius the radius at this position
199 * @param randRadius the randRadius at this position
[4378]200*/
[4430]201void ParticleSystem::setRadius(float lifeCycleTime, float radius, float randRadius)
[4378]202{
[4649]203  this->radiusAnim.changeEntry(lifeCycleTime, radius);
204  this->randRadiusAnim.changeEntry(lifeCycleTime, randRadius);
[4378]205}
206
207/**
[4836]208 *  sets a key in the mass-animation on a per-particle basis
209 * @param lifeCycleTime the time (partilceLifeTime/particleAge) [0-1]
210 * @param mass the mass at this position
211 * @param randMass the randomMass at this position
[3945]212*/
[4430]213void ParticleSystem::setMass(float lifeCycleTime, float mass, float randMass)
[3932]214{
[4649]215  this->massAnim.changeEntry(lifeCycleTime, mass);
216  this->randMassAnim.changeEntry(lifeCycleTime, randMass);
[3932]217}
218
[3945]219/**
[4836]220 *  sets a key in the color-animation on a per-particle basis
221 * @param lifeCycleTime: the time (partilceLifeTime/particleAge) [0-1]
222 * @param red: red
223 * @param green: green
224 * @param blue: blue
225 * @param alpha: alpha
[4338]226*/
[4725]227void ParticleSystem::setColor(float lifeCycleTime, float red, float green, float blue, float alpha)
[4338]228{
[4649]229  this->colorAnim[0].changeEntry(lifeCycleTime, red);
230  this->colorAnim[1].changeEntry(lifeCycleTime, green);
231  this->colorAnim[2].changeEntry(lifeCycleTime, blue);
232  this->colorAnim[3].changeEntry(lifeCycleTime, alpha);
[4338]233}
234
[6629]235/**
236 * @brief adds an Emitter to this System.
237 * @param emitter the Emitter to add.
238 */
[6612]239void ParticleSystem::addEmitter(ParticleEmitter* emitter)
240{
[6619]241  assert (emitter != NULL);
242  if (emitter->getSystem() != NULL)
243    emitter->getSystem()->removeEmitter(emitter);
[6625]244  emitter->system = this;
[6612]245  this->emitters.push_back(emitter);
246}
247
[6629]248/**
249 * @brief removes a ParticleEmitter from this System
250 * @param emitter the Emitter to remove
251 */
[6612]252void ParticleSystem::removeEmitter(ParticleEmitter* emitter)
253{
[6625]254  assert (emitter != NULL);
255  emitter->system = NULL;
256  this->emitters.remove(emitter);
257  /*  std::list<ParticleEmitter*>::iterator it = std::find(this->emitters.begin(), this->emitters.end(), emitter);
[6612]258  if (it != this->emitters.end())
[6625]259    this->emitters.erase(it);*/
[6612]260}
261
[4338]262/**
[6629]263 * @brief does a Precaching, meaning, that the ParticleSystem(and its emitters) will be ticked force
264 * @param seconds: seconds
265 * @param ticksPerSeconds times per Second.
266 */
267void ParticleSystem::precache(unsigned int seconds, unsigned int ticksPerSecond)
268{
[6630]269
270  std::list<ParticleEmitter*>::iterator emitter;
271  for (emitter = this->emitters.begin(); emitter != this->emitters.end(); emitter++)
272    (*emitter)->updateNode(.1), (*emitter)->updateNode(.1);
273
[6629]274  PRINTF(4)("Precaching %s::%s %d seconds %d timesPerSecond\n", this->getClassName(), this->getName(), seconds, ticksPerSecond);
275  this->debug();
276  for (unsigned int i = 0; i < seconds*ticksPerSecond; i++)
277    this->tick(1.0/(float)ticksPerSecond);
278}
279
280
281/**
[4836]282 *  ticks the system.
283 * @param dt the time to tick all the Particles of the System
[3932]284
[3945]285   this is used to get all the particles some motion
286*/
[3931]287void ParticleSystem::tick(float dt)
288{
[3934]289  Particle* tickPart = particles;  // the particle to Tick
[4692]290  Particle* prevPart = NULL;
[3934]291  while (likely(tickPart != NULL))
[6625]292  {
293    // applying force to the System.
294    if (likely (tickPart->mass > 0.0))
295      tickPart->velocity += tickPart->extForce / tickPart->mass * dt;
[4687]296
[6625]297    tickPart->radius = radiusAnim.getValue(tickPart->lifeCycle)
298                       + randRadiusAnim.getValue(tickPart->lifeCycle) * tickPart->radiusRand;
[4434]299
[6625]300    tickPart->mass = massAnim.getValue(tickPart->lifeCycle)
301                     + randMassAnim.getValue(tickPart->lifeCycle) * tickPart->massRand;
[4597]302
[6625]303    tickPart->extForce = Vector(0,0,0);
[4338]304
[6625]305    // applying Color
306    tickPart->color[0] = this->colorAnim[0].getValue(tickPart->lifeCycle);
307    tickPart->color[1] = this->colorAnim[1].getValue(tickPart->lifeCycle);
308    tickPart->color[2] = this->colorAnim[2].getValue(tickPart->lifeCycle);
309    tickPart->color[3] = this->colorAnim[3].getValue(tickPart->lifeCycle);
[4431]310
[6757]311    // rendering new position.
312    tickPart->position += tickPart->velocity * dt;
313    tickPart->orientation += tickPart->momentum *dt;
314
[6625]315    // many more to come
[3932]316
[6625]317    if (this->conserve < 1.0)
318    {
319      tickPart->velocity *= this->conserve;
320      tickPart->momentum *= this->conserve;
321    }
322    // find out if we have to delete tickPart
323    if (unlikely((tickPart->lifeCycle += dt/tickPart->lifeTime) >= 1.0))
324    {
325      // remove the particle from the list
326      if (likely(prevPart != NULL))
[4691]327      {
[6625]328        prevPart->next = tickPart->next;
329        tickPart->next = this->deadList;
330        this->deadList = tickPart;
331        tickPart = prevPart->next;
[4691]332      }
[3934]333      else
[6625]334      {
335        prevPart = NULL;
336        this->particles = tickPart->next;
337        tickPart->next = this->deadList;
338        this->deadList = tickPart;
339        tickPart = this->particles;
340      }
341      --this->count;
[3932]342    }
[6625]343    else
344    {
345      prevPart = tickPart;
346      tickPart = tickPart->next;
347    }
348  }
[6612]349
[6625]350  std::list<ParticleEmitter*>::iterator emitter;
351  for (emitter = this->emitters.begin(); emitter != this->emitters.end(); emitter++)
352    (*emitter)->tick(dt);
[3932]353}
354
[4597]355/**
[4836]356  *  applies some force to a Particle.
357  * @param field the Field to apply.
[4338]358 */
[4395]359void ParticleSystem::applyField(Field* field)
[4338]360{
361  Particle* tickPart = particles;
362  while (tickPart)
[6625]363  {
364    tickPart->extForce += field->calcForce(tickPart->position);
365    tickPart = tickPart->next;
366  }
[4338]367}
368
369
[3945]370/**
[4836]371 * @returns the count of Faces of this ParticleSystem
[4677]372 */
[4746]373unsigned int ParticleSystem::getFaceCount() const
[4677]374{
[6621]375  return this->count;
[4677]376}
377
378
379/**
[4836]380 *  draws all the Particles of this System
[4338]381
382   The Cases in this Function all do the same:
383   Drawing all the particles with the appropriate Type.
384   This is just the fastest Way to do this, but will most likely be changed in the future.
[4663]385 */
[6621]386// void ParticleSystem::draw() const
387// {
388//   glPushAttrib(GL_ENABLE_BIT);
389//
390//   Particle* drawPart = particles;
391//
392//   switch (this->particleType)
393//   {
394//
395//     case PARTICLE_MODEL:
396//       {
397//       }
398//       break;
399//
400//     case PARTICLE_DOT:
401//       glDisable(GL_LIGHTING);
402//       glBegin(GL_POINTS);
403//       while (likely(drawPart != NULL))
404//       {
405//         glColor4fv(drawPart->color);
406//
407//         glLineWidth(drawPart->radius);
408//
409//         glVertex3f(drawPart->position.x, drawPart->position.y, drawPart->position.z);
410//         drawPart = drawPart->next;
411//       }
412//       glEnd();
413//       break;
414//   }
415//   glPopAttrib();
416// }
[4338]417
[3945]418/**
[4836]419 *  adds a new Particle to the System
420 * @param position the initial position, where the particle gets emitted.
421 * @param velocity the initial velocity of the particle.
422 * @param orientation the initial orientation of the Paritcle.
423 * @param momentum the initial momentum of the Particle (the speed of its rotation).
424 * @param data some more data given by the emitter
[3945]425*/
[4690]426void ParticleSystem::addParticle(const Vector& position, const Vector& velocity, const Quaternion& orientation, const Quaternion& momentum, unsigned int data)
[3932]427{
[3934]428  if (this->count <= this->maxCount)
[6625]429  {
430    // if it is the first Particle
431    if (unlikely(particles == NULL))
[3932]432    {
[6625]433      if (likely(deadList != NULL))
434      {
435        this->particles = this->deadList;
436        deadList = deadList->next;
437      }
[3934]438      else
[6625]439      {
440        PRINTF(5)("Generating new Particle\n");
441        this->particles = new Particle;
442      }
443      this->particles->next = NULL;
444    }
445    // filling the List from the beginning
446    else
447    {
448      Particle* tmpPart;
449      if (likely(deadList != NULL))
450      {
451        tmpPart = this->deadList;
452        deadList = deadList->next;
453      }
454      else
455      {
456        PRINTF(5)("Generating new Particle\n");
457        tmpPart = new Particle;
458      }
459      tmpPart->next = this->particles;
460      this->particles = tmpPart;
461    }
462    particles->lifeTime = this->lifeSpan + (float)(rand()/RAND_MAX)* this->randomLifeSpan;
463    particles->lifeCycle = 0.0;
464    particles->position = position;
465    particles->velocity = velocity;
[3951]466
[6625]467    particles->orientation = orientation;
468    particles->momentum = momentum;
[4687]469
[6625]470    //  particle->rotation = ; //! @todo rotation is once again something to be done.
471    particles->massRand = 2*(float)rand()/RAND_MAX -1;
472    particles->radiusRand = 2* (float)rand()/RAND_MAX -1;
473    particles->mass = this->massAnim.getValue(0.0) + this->randMassAnim.getValue(0.0)*particles->massRand;
474    particles->radius = this->radiusAnim.getValue(0.0) + this->randRadiusAnim.getValue(0.0)*particles->radiusRand;
[3934]475
[6625]476    ++this->count;
477  }
[3932]478  else
[4017]479    PRINTF(5)("maximum count of particles reached not adding any more\n");
[3932]480}
[3931]481
[3944]482/**
[4836]483 *  outputs some nice debug information
[3944]484*/
[4746]485void ParticleSystem::debug() const
[3944]486{
[6629]487  PRINT(0)("  ParticleCount: %d emitters: %d, maximumCount: %d :: filled %d%%\n", this->count, this->emitters.size(), this->maxCount, 100*this->count/this->maxCount);
[4123]488  if (deadList)
[6625]489  {
490    PRINT(0)("  - ParticleDeadList is used: ");
491    int i = 1;
492    Particle* tmpPart = this->deadList;
493    while (tmpPart = tmpPart->next) ++i;
494    PRINT(0)("count: %d\n", i);
495  }
[3944]496}
Note: See TracBrowser for help on using the repository browser.