Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/particleEngine/src/lib/graphics/particles/particle_system.cc @ 3934

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

orxonox/branches/particleEngine: particles get deleted after theit life is over

File size: 3.8 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_PARTICLE
17
18#include "particle_system.h"
19
20#include "particle_emitter.h"
21#include "particle_engine.h"
22#include "compiler.h"
23
24using namespace std;
25
26
27/**
28   \brief standard constructor
29   \param count the Count of particles in the System
30   \param type The Type of the ParticleSystem
31
32   \todo this constructor is not jet implemented - do it
33*/
34ParticleSystem::ParticleSystem (unsigned int maxCount, PARTICLE_TYPE type)
35{
36   this->setClassName ("ParticleSystem");
37
38   this->maxCount = maxCount;
39   this->count = 0;
40   this->particleType = type;
41   this->particles = NULL;
42   this->conserve = 1.0;
43   this->setLifeSpan(.1);
44
45   ParticleEngine::getInstance()->addSystem(this);
46}
47
48
49/**
50   \brief standard deconstructor
51
52*/
53ParticleSystem::~ParticleSystem() 
54{
55  // delete what has to be deleted here
56}
57
58// setting properties
59void ParticleSystem::setMaterial(Material* material)
60{
61 
62}
63
64void ParticleSystem::setLifeSpan(float lifeSpan, float randomLifeSpan)
65{
66  this->lifeSpan = lifeSpan;
67  this->randomLifeSpan = randomLifeSpan;
68}
69
70void ParticleSystem::setRadius(float startRadius, float endRadius, float randomRadius)
71{
72
73}
74
75void ParticleSystem::setConserve(float conserve)
76{
77  this->conserve = conserve;
78}
79
80
81
82void ParticleSystem::tick(float dt)
83{
84  Particle* tickPart = particles;  // the particle to Tick
85  Particle* prevPart = NULL;       //
86  while (likely(tickPart != NULL))
87    {
88         
89      tickPart->position = tickPart->position + tickPart->velocity;
90      // many more to come
91     
92
93
94
95
96
97      // find out if we have to delete tickPart
98      if ((tickPart->timeToLive -= dt) <= 0)
99        {
100          // remove the particle from the list
101          if (likely(prevPart != NULL))
102            {
103              prevPart->next = tickPart->next;
104              delete tickPart;
105              tickPart = prevPart->next;
106            }
107          else
108            {
109              prevPart = NULL;
110              this->particles = tickPart->next;
111              delete tickPart;
112              tickPart = this->particles;
113            }
114          --this->count;
115          printf("deleted particle: count %d\n", count);
116        }
117      else
118        {     
119          prevPart = tickPart;
120          tickPart = tickPart->next;
121        }
122    }
123}
124
125void ParticleSystem::draw(void)
126{
127  Particle* drawPart = particles;
128  if (likely(drawPart != NULL))
129    {
130      glBegin(GL_TRIANGLES);
131      while (likely(drawPart != NULL))
132        {
133          // draw in DOT mode
134          glVertex3f(drawPart->position.x, drawPart->position.y, drawPart->position.z);
135         
136         
137          drawPart = drawPart->next;
138        }
139      glEnd();
140    }
141}
142
143
144void ParticleSystem::addParticle(Vector position, Vector velocity, unsigned int data)
145{
146  if (this->count <= this->maxCount)
147    {
148      // if it is the first Particle
149      if (unlikely(particles == NULL))
150        {
151          this->particles = new Particle;
152          this->particles->next = NULL;
153        }
154      // filling the List from the beginning
155      else
156        {
157          Particle* tmpPart = new Particle;
158          tmpPart->next = this->particles;
159          this->particles = tmpPart;
160        }
161     
162      particles->timeToLive = this->lifeSpan + (float)(random()/RAND_MAX)* this->randomLifeSpan;
163      particles->position = position;
164      particles->velocity = velocity;
165      //  particle->rotation = ; //! \todo rotation is once again something to be done.
166      particles->mass = this->initialMass + (random()/RAND_MAX)* this->randomInitialMass;
167      particles->radius = this->startRadius + (random()/RAND_MAX)*this->randomRadius;
168
169      ++this->count;
170    }
171  else
172    PRINTF(4)("maximum count of particles reached not adding any more\n");
173}
174
Note: See TracBrowser for help on using the repository browser.