Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: loadParams is now virtual.
ALL THE CLASSES HAVE TO CALL

SuperClass::loadParams(root);

isntead of:
static_cast<SuperClass*>(this)→loadParams(root);

which was quite stupid anyways

File size: 17.7 KB
Line 
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.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS
17
18#include "particle_system.h"
19
20#include "particle_emitter.h"
21#include "particle_engine.h"
22
23#include "field.h"
24#include "model.h"
25
26#include "load_param.h"
27#include "factory.h"
28#include "material.h"
29#include "state.h"
30#include "shell_command.h"
31
32#include "parser/tinyxml/tinyxml.h"
33
34CREATE_FACTORY(ParticleSystem, CL_PARTICLE_SYSTEM);
35SHELL_COMMAND(texture, ParticleSystem, setMaterialTexture)
36    ->defaultValues(1, "maps/evil-flower.png");
37
38using namespace std;
39
40/**
41 *  standard constructor
42 * @param maxCount the Count of particles in the System
43 * @param type The Type of the ParticleSystem
44*/
45ParticleSystem::ParticleSystem (unsigned int maxCount, PARTICLE_TYPE type)
46{
47  this->init();
48
49  this->setMaxCount(maxCount);
50  this->setType(type, 1);
51}
52
53/**
54 * @brief creates a Particle System out of a XML-element
55 * @param root: the XML-element to load from
56 */
57ParticleSystem::ParticleSystem(const TiXmlElement* root)
58{
59  this->init();
60
61  this->loadParams(root);
62}
63
64/**
65 *  standard deconstructor
66*/
67ParticleSystem::~ParticleSystem()
68{
69  // delete what has to be deleted here
70   ParticleEngine::getInstance()->removeSystem(this);
71
72   // deleting all the living Particles
73   while (this->particles)
74     {
75       Particle* tmpDelPart = this->particles;
76       this->particles = this->particles->next;
77       delete tmpDelPart;
78     }
79
80   // deleting all the dead particles
81   while (this->deadList)
82     {
83       Particle* tmpDelPart = this->deadList;
84       this->deadList = this->deadList->next;
85       delete tmpDelPart;
86     }
87
88   if (this->material)
89     delete this->material;
90}
91
92/**
93  \brief initializes the ParticleSystem with default values
94*/
95void ParticleSystem::init()
96{
97  this->setClassID(CL_PARTICLE_SYSTEM, "ParticleSystem");
98
99  this->material = NULL;
100  this->setMaxCount(PARTICLE_DEFAULT_MAX_COUNT);
101  this->count = 0;
102  this->particles = NULL;
103  this->deadList = NULL;
104  this->setConserve(1);
105  this->setLifeSpan(1);
106  this->glID = NULL;
107  this->setType(PARTICLE_DEFAULT_TYPE, 1);
108  ParticleEngine::getInstance()->addSystem(this);
109}
110
111
112/**
113 * loads Parameters from a TiXmlElement
114 * @param root the XML-element to load from.
115 */
116void ParticleSystem::loadParams(const TiXmlElement* root)
117{
118  WorldEntity::loadParams(root);
119  PhysicsInterface::loadParams(root);
120
121  LoadParam(root, "max-count", this, ParticleSystem, setMaxCount)
122      .describe("the maximal count of Particles, that can be emitted into this system");
123
124  LoadParam(root, "life-span", this, ParticleSystem, setLifeSpan)
125      .describe("sets the life-span of the Particles.");
126
127  LoadParam(root, "conserve", this, ParticleSystem, setConserve)
128      .describe("sets the Conserve factor of the Particles (1.0: they keep all their energy, 0.0:they keep no energy)");
129
130  LoadParam(root, "type", this, ParticleSystem, setType)
131      .describe("sets the type of the Particles, (dot, spark, sprite or model)");
132
133  LoadParam(root, "texture", this, ParticleSystem, setMaterialTexture);
134
135  LOAD_PARAM_START_CYCLE(root, element);
136  {
137    element->ToText();
138  // PER-PARTICLE-ATTRIBUTES:
139    LoadParam_CYCLE(element, "radius", this, ParticleSystem, setRadius)
140        .describe("The Radius of each particle over time (TimeIndex [0-1], radius at TimeIndex, randomRadius at TimeIndex)");
141
142    LoadParam_CYCLE(element, "mass", this, ParticleSystem, setMass)
143        .describe("The Mass of each particle over time (TimeIndex: [0-1], mass at TimeIndex, randomMass at TimeIndex)");
144
145    LoadParam_CYCLE(element, "color", this, ParticleSystem, setColor)
146        .describe("The Color of each particle over time (TimeIndex: [0-1], red: [0-1], green: [0-1], blue: [0-1], alpha: [0-1])");
147  }
148  LOAD_PARAM_END_CYCLE(element);
149
150}
151
152/**
153* @param maxCount the maximum count of particles that can be emitted
154 */
155void ParticleSystem::setMaxCount(int maxCount)
156{
157  this->maxCount = maxCount;
158}
159
160
161/**
162 * @param particleType the type of particles in this System
163 * @param count how many particles (in PARTICLE_MULTI-mode)
164   @todo this will be different
165*/
166void ParticleSystem::setType(const char* particleType)
167{
168  if (!strcmp(particleType, "dot"))
169    this->setType(PARTICLE_DOT);
170  else if(!strcmp(particleType, "spark"))
171    this->setType(PARTICLE_SPARK);
172  else if(!strcmp(particleType, "model"))
173    this->setType(PARTICLE_MODEL);
174  else // if (strcmp(particleType, "SPRITE"))
175    this->setType(PARTICLE_SPRITE);
176}
177
178/**
179 * @param particleType the type of particles in this System
180 * @param count how many particles (in PARTICLE_MULTI-mode)
181   @todo this MUST be different
182*/
183void ParticleSystem::setType(PARTICLE_TYPE particleType, int count)
184{
185  this->particleType = particleType;
186  this->dialectCount = count;
187  //  if (glID != NULL)
188  //    delete glID;
189
190  //  glID = new GLuint[count];
191  //  for (int i = 0; i< count; i++)
192  //    glID[i] = 0;
193
194  //  glID[0] = glGenLists(count);
195  if (this->material)
196    delete this->material;
197  this->material = NULL;
198
199  switch (this->particleType)
200    {
201      case PARTICLE_SPRITE:
202        this->material = new Material("transperencyMap");
203        this->material->setDiffuseMap("maps/radialTransparency.png");
204      //  material->setTransparency(.5);
205        break;
206  }
207}
208
209// setting properties
210/**
211 *  sets the material to an external material
212 * @param material: the material to set this material to.
213
214   !! important if the extern material gets deleted it MUST be unregistered here or segfault !!
215*/
216void ParticleSystem::setMaterial(Material* material)
217{
218  this->material = material;
219}
220
221void ParticleSystem::setMaterialTexture(const char* textureFile)
222{
223  if (this->material != NULL)
224    this->material->setDiffuseMap(textureFile);
225}
226
227/**
228 *  Sets the lifespan of newly created particles
229*/
230void ParticleSystem::setLifeSpan(float lifeSpan, float randomLifeSpan)
231{
232  this->lifeSpan = lifeSpan;
233  this->randomLifeSpan = randomLifeSpan;
234}
235
236/**
237 *  sets the conserve Factor of newly created particles
238*/
239void ParticleSystem::setConserve(float conserve)
240{
241  if (conserve > 1.0)
242    this->conserve = 1.0;
243  else if (conserve < 0.0)
244    this->conserve = 0.0;
245  else
246    this->conserve = conserve;
247}
248
249/////////////////////////////
250/* Per-Particle Attributes */
251/////////////////////////////
252/**
253 *  sets a key in the radius-animation on a per-particle basis
254 * @param lifeCycleTime the time (partilceLifeTime/particleAge) [0-1]
255 * @param radius the radius at this position
256 * @param randRadius the randRadius at this position
257*/
258void ParticleSystem::setRadius(float lifeCycleTime, float radius, float randRadius)
259{
260  this->radiusAnim.changeEntry(lifeCycleTime, radius);
261  this->randRadiusAnim.changeEntry(lifeCycleTime, randRadius);
262}
263
264/**
265 *  sets a key in the mass-animation on a per-particle basis
266 * @param lifeCycleTime the time (partilceLifeTime/particleAge) [0-1]
267 * @param mass the mass at this position
268 * @param randMass the randomMass at this position
269*/
270void ParticleSystem::setMass(float lifeCycleTime, float mass, float randMass)
271{
272  this->massAnim.changeEntry(lifeCycleTime, mass);
273  this->randMassAnim.changeEntry(lifeCycleTime, randMass);
274}
275
276/**
277 *  sets a key in the color-animation on a per-particle basis
278 * @param lifeCycleTime: the time (partilceLifeTime/particleAge) [0-1]
279 * @param red: red
280 * @param green: green
281 * @param blue: blue
282 * @param alpha: alpha
283*/
284void ParticleSystem::setColor(float lifeCycleTime, float red, float green, float blue, float alpha)
285{
286  this->colorAnim[0].changeEntry(lifeCycleTime, red);
287  this->colorAnim[1].changeEntry(lifeCycleTime, green);
288  this->colorAnim[2].changeEntry(lifeCycleTime, blue);
289  this->colorAnim[3].changeEntry(lifeCycleTime, alpha);
290}
291
292/**
293 *  ticks the system.
294 * @param dt the time to tick all the Particles of the System
295
296   this is used to get all the particles some motion
297*/
298void ParticleSystem::tick(float dt)
299{
300  Particle* tickPart = particles;  // the particle to Tick
301  Particle* prevPart = NULL;
302  while (likely(tickPart != NULL))
303    {
304      // applying force to the System.
305      if (likely (tickPart->mass > 0.0))
306        tickPart->velocity += tickPart->extForce / tickPart->mass * dt;
307      // rendering new position.
308      tickPart->position += tickPart->velocity * dt;
309      tickPart->orientation += tickPart->momentum *dt;
310
311      tickPart->radius = radiusAnim.getValue(tickPart->lifeCycle)
312        + randRadiusAnim.getValue(tickPart->lifeCycle) * tickPart->radiusRand;
313
314      tickPart->mass = massAnim.getValue(tickPart->lifeCycle)
315        + randMassAnim.getValue(tickPart->lifeCycle) * tickPart->massRand;
316
317      tickPart->extForce = Vector(0,0,0);
318
319      // applying Color
320      tickPart->color[0] = this->colorAnim[0].getValue(tickPart->lifeCycle);
321      tickPart->color[1] = this->colorAnim[1].getValue(tickPart->lifeCycle);
322      tickPart->color[2] = this->colorAnim[2].getValue(tickPart->lifeCycle);
323      tickPart->color[3] = this->colorAnim[3].getValue(tickPart->lifeCycle);
324
325      // many more to come
326
327      if (this->conserve < 1.0)
328      {
329        tickPart->velocity *= this->conserve;
330        tickPart->momentum *= this->conserve;
331      }
332      // find out if we have to delete tickPart
333      if (unlikely((tickPart->lifeCycle += dt/tickPart->lifeTime) >= 1.0))
334        {
335          // remove the particle from the list
336          if (likely(prevPart != NULL))
337            {
338              prevPart->next = tickPart->next;
339              tickPart->next = this->deadList;
340              this->deadList = tickPart;
341              tickPart = prevPart->next;
342            }
343          else
344            {
345              prevPart = NULL;
346              this->particles = tickPart->next;
347              tickPart->next = this->deadList;
348              this->deadList = tickPart;
349              tickPart = this->particles;
350            }
351          --this->count;
352        }
353      else
354        {
355          prevPart = tickPart;
356          tickPart = tickPart->next;
357        }
358    }
359}
360
361/**
362  *  applies some force to a Particle.
363  * @param field the Field to apply.
364 */
365void ParticleSystem::applyField(Field* field)
366{
367  Particle* tickPart = particles;
368  while (tickPart)
369    {
370      tickPart->extForce += field->calcForce(tickPart->position);
371      tickPart = tickPart->next;
372    }
373}
374
375
376/**
377 * @returns the count of Faces of this ParticleSystem
378 */
379unsigned int ParticleSystem::getFaceCount() const
380{
381  switch (this->particleType)
382  {
383    case PARTICLE_SPRITE:
384      return this->count;
385      break;
386    case PARTICLE_MODEL:
387      if (this->getModel(0))
388        return this->count * this->getModel()->getTriangleCount();
389      break;
390  }
391}
392
393
394/**
395 *  draws all the Particles of this System
396
397   The Cases in this Function all do the same:
398   Drawing all the particles with the appropriate Type.
399   This is just the fastest Way to do this, but will most likely be changed in the future.
400 */
401void ParticleSystem::draw() const
402{
403  glPushAttrib(GL_ENABLE_BIT);
404
405  Particle* drawPart = particles;
406
407  switch (this->particleType)
408  {
409    default:
410    case PARTICLE_SPRITE:
411      glDisable(GL_LIGHTING);
412      glMatrixMode(GL_MODELVIEW);
413      glDepthMask(GL_FALSE);
414
415      material->select();
416      glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA);
417
418      //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_MODULATE);
419
420
421      while (likely(drawPart != NULL))
422      {
423        glColor4fv(drawPart->color);
424          //! @todo implement a faster code for the look-at Camera algorithm.
425
426        const PNode* camera = State::getCamera();  //!< @todo MUST be different
427        Vector cameraPos = camera->getAbsCoor();
428        Vector cameraTargetPos = State::getCameraTarget()->getAbsCoor();
429        Vector view = cameraTargetPos - cameraPos;
430        Vector up = Vector(0, 1, 0);
431        up = camera->getAbsDir().apply(up);
432        Vector h = up.cross(view);
433        Vector v = h.cross(view);
434        h.normalize();
435        v.normalize();
436        v *= .5 * drawPart->radius;
437        h *= .5 * drawPart->radius;
438
439        glBegin(GL_TRIANGLE_STRIP);
440        glTexCoord2i(1, 1);
441        glVertex3f(drawPart->position.x - h.x - v.x,
442                   drawPart->position.y - h.y - v.y,
443                   drawPart->position.z - h.z - v.z);
444        glTexCoord2i(0, 1);
445        glVertex3f(drawPart->position.x - h.x + v.x,
446                   drawPart->position.y - h.y + v.y,
447                   drawPart->position.z - h.z + v.z);
448        glTexCoord2i(1, 0);
449        glVertex3f(drawPart->position.x + h.x - v.x,
450                   drawPart->position.y + h.y - v.y,
451                   drawPart->position.z + h.z - v.z);
452        glTexCoord2i(0, 0);
453        glVertex3f(drawPart->position.x + h.x + v.x,
454                   drawPart->position.y + h.y + v.y,
455                   drawPart->position.z + h.z + v.z);
456
457        glEnd();
458
459        drawPart = drawPart->next;
460      }
461      glDepthMask(GL_TRUE);
462      break;
463
464    case PARTICLE_SPARK:
465      glDisable(GL_LIGHTING);
466      glDepthMask(GL_FALSE);
467      //glEnable(GL_LINE_SMOOTH);
468      glEnable(GL_BLEND);
469
470      glBegin(GL_LINES);
471      while (likely(drawPart != NULL))
472      {
473        glColor4fv(drawPart->color);
474        glVertex3f(drawPart->position.x, drawPart->position.y, drawPart->position.z);
475        glVertex3f(drawPart->position.x - drawPart->velocity.x * drawPart->radius,
476                   drawPart->position.y - drawPart->velocity.y * drawPart->radius,
477                   drawPart->position.z - drawPart->velocity.z * drawPart->radius);
478        drawPart = drawPart->next;
479      }
480      glEnd();
481      break;
482
483    case PARTICLE_MODEL:
484      {
485        GLfloat matrix[4][4];
486
487        if (likely(this->getModel() != NULL))
488          while (likely(drawPart != NULL))
489        {
490          glPushMatrix();
491          glMatrixMode(GL_MODELVIEW);
492          /* move */
493          glTranslatef(drawPart->position.x, drawPart->position.y, drawPart->position.z);
494          /* scale */
495          glScalef(drawPart->radius, drawPart->radius, drawPart->radius);
496          /* rotate */
497          drawPart->orientation.matrix (matrix);
498          glMultMatrixf((float*)matrix);
499
500          this->getModel()->draw();
501
502          glPopMatrix();
503          drawPart = drawPart->next;
504        }
505        else
506          PRINTF(2)("no model loaded onto ParticleSystem-%s\n", this->getName());
507      }
508      break;
509
510    case PARTICLE_DOT:
511      glDisable(GL_LIGHTING);
512      glBegin(GL_POINTS);
513      while (likely(drawPart != NULL))
514      {
515        glColor4fv(drawPart->color);
516
517        glLineWidth(drawPart->radius);
518
519        glVertex3f(drawPart->position.x, drawPart->position.y, drawPart->position.z);
520        drawPart = drawPart->next;
521      }
522      glEnd();
523      break;
524  }
525  glPopAttrib();
526}
527
528/**
529 *  adds a new Particle to the System
530 * @param position the initial position, where the particle gets emitted.
531 * @param velocity the initial velocity of the particle.
532 * @param orientation the initial orientation of the Paritcle.
533 * @param momentum the initial momentum of the Particle (the speed of its rotation).
534 * @param data some more data given by the emitter
535*/
536void ParticleSystem::addParticle(const Vector& position, const Vector& velocity, const Quaternion& orientation, const Quaternion& momentum, unsigned int data)
537{
538  if (this->count <= this->maxCount)
539    {
540      // if it is the first Particle
541      if (unlikely(particles == NULL))
542        {
543          if (likely(deadList != NULL))
544            {
545              this->particles = this->deadList;
546              deadList = deadList->next;
547            }
548          else
549            {
550              PRINTF(5)("Generating new Particle\n");
551              this->particles = new Particle;
552            }
553          this->particles->next = NULL;
554        }
555      // filling the List from the beginning
556      else
557        {
558          Particle* tmpPart;
559          if (likely(deadList != NULL))
560            {
561              tmpPart = this->deadList;
562              deadList = deadList->next;
563            }
564          else
565            {
566              PRINTF(5)("Generating new Particle\n");
567              tmpPart = new Particle;
568            }
569          tmpPart->next = this->particles;
570          this->particles = tmpPart;
571        }
572      particles->lifeTime = this->lifeSpan + (float)(rand()/RAND_MAX)* this->randomLifeSpan;
573      particles->lifeCycle = 0.0;
574      particles->position = position;
575      particles->velocity = velocity;
576
577      particles->orientation = orientation;
578      particles->momentum = momentum;
579
580      //  particle->rotation = ; //! @todo rotation is once again something to be done.
581      particles->massRand = 2*(float)rand()/RAND_MAX -1;
582      particles->radiusRand = 2* (float)rand()/RAND_MAX -1;
583      particles->mass = this->massAnim.getValue(0.0) + this->randMassAnim.getValue(0.0)*particles->massRand;
584      particles->radius = this->radiusAnim.getValue(0.0) + this->randRadiusAnim.getValue(0.0)*particles->radiusRand;
585
586      ++this->count;
587    }
588  else
589    PRINTF(5)("maximum count of particles reached not adding any more\n");
590}
591
592/**
593 *  outputs some nice debug information
594*/
595void ParticleSystem::debug() const
596{
597  PRINT(0)("  ParticleSystem %s, type: ", this->getName());
598  {
599      if (this->particleType == PARTICLE_MODEL)
600        PRINT(0)("model");
601      else if (this->particleType == PARTICLE_SPRITE)
602        PRINT(0)("sprite");
603      else if (this->particleType == PARTICLE_DOT)
604        PRINT(0)("dot");
605      else if (this->particleType == PARTICLE_SPARK)
606        PRINT(0)("spark");
607
608      PRINT(0)("\n");
609  }
610
611  PRINT(0)("  ParticleCount: %d, maximumCount: %d :: filled %d%%\n", this->count, this->maxCount, 100*this->count/this->maxCount);
612  if (deadList)
613    {
614      PRINT(0)("  - ParticleDeadList is used: ");
615      int i = 1;
616      Particle* tmpPart = this->deadList;
617      while (tmpPart = tmpPart->next) ++i;
618      PRINT(0)("count: %d\n", i);
619    }
620}
Note: See TracBrowser for help on using the repository browser.