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 | |
---|
24 | using 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 | */ |
---|
34 | ParticleSystem::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->setConserve(.8); |
---|
43 | this->setLifeSpan(.1); |
---|
44 | this->setInheritSpeed(0); |
---|
45 | |
---|
46 | ParticleEngine::getInstance()->addSystem(this); |
---|
47 | } |
---|
48 | |
---|
49 | |
---|
50 | /** |
---|
51 | \brief standard deconstructor |
---|
52 | |
---|
53 | */ |
---|
54 | ParticleSystem::~ParticleSystem() |
---|
55 | { |
---|
56 | // delete what has to be deleted here |
---|
57 | ParticleEngine::getInstance()->removeSystem(this); |
---|
58 | } |
---|
59 | |
---|
60 | // setting properties |
---|
61 | void ParticleSystem::setMaterial(Material* material) |
---|
62 | { |
---|
63 | this->material = material; |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | /** |
---|
68 | \brief how much of the speed from the ParticleEmitter should flow onto the ParticleSystem |
---|
69 | \param value a Value between zero and one |
---|
70 | |
---|
71 | |
---|
72 | if you want to change the value of this variable during emission time (to make it more dynamic) |
---|
73 | you may want to use the animation class |
---|
74 | */ |
---|
75 | void ParticleSystem::setInheritSpeed(float value) |
---|
76 | { |
---|
77 | if (unlikely(value > 1.0)) |
---|
78 | this->inheritSpeed = 1; |
---|
79 | else if (unlikely(value < 0.0)) |
---|
80 | this->inheritSpeed = 0; |
---|
81 | else |
---|
82 | this->inheritSpeed = value; |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | void ParticleSystem::setLifeSpan(float lifeSpan, float randomLifeSpan) |
---|
87 | { |
---|
88 | this->lifeSpan = lifeSpan; |
---|
89 | this->randomLifeSpan = randomLifeSpan; |
---|
90 | } |
---|
91 | |
---|
92 | void ParticleSystem::setRadius(float startRadius, float endRadius, float randomRadius) |
---|
93 | { |
---|
94 | this->startRadius = startRadius; |
---|
95 | this->endRadius = endRadius; |
---|
96 | this->randomRadius = randomRadius; |
---|
97 | } |
---|
98 | |
---|
99 | void ParticleSystem::setConserve(float conserve) |
---|
100 | { |
---|
101 | if (conserve > 1.0) |
---|
102 | this->conserve = 1.0; |
---|
103 | else if (conserve < 0.0) |
---|
104 | this->conserve = 0.0; |
---|
105 | else |
---|
106 | this->conserve = conserve; |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | |
---|
111 | void ParticleSystem::tick(float dt) |
---|
112 | { |
---|
113 | Particle* tickPart = particles; // the particle to Tick |
---|
114 | Particle* prevPart = NULL; // |
---|
115 | while (likely(tickPart != NULL)) |
---|
116 | { |
---|
117 | |
---|
118 | tickPart->position = tickPart->position + tickPart->velocity; |
---|
119 | tickPart->radius + tickPart->radiusIt * dt; |
---|
120 | // many more to come |
---|
121 | |
---|
122 | |
---|
123 | |
---|
124 | if (this->conserve < 1.0) |
---|
125 | tickPart->velocity = tickPart->velocity * this->conserve; |
---|
126 | // find out if we have to delete tickPart |
---|
127 | if ((tickPart->timeToLive -= dt) <= 0) |
---|
128 | { |
---|
129 | // remove the particle from the list |
---|
130 | if (likely(prevPart != NULL)) |
---|
131 | { |
---|
132 | prevPart->next = tickPart->next; |
---|
133 | delete tickPart; |
---|
134 | tickPart = prevPart->next; |
---|
135 | } |
---|
136 | else |
---|
137 | { |
---|
138 | prevPart = NULL; |
---|
139 | this->particles = tickPart->next; |
---|
140 | delete tickPart; |
---|
141 | tickPart = this->particles; |
---|
142 | } |
---|
143 | --this->count; |
---|
144 | } |
---|
145 | else |
---|
146 | { |
---|
147 | prevPart = tickPart; |
---|
148 | tickPart = tickPart->next; |
---|
149 | } |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | void ParticleSystem::draw(void) |
---|
154 | { |
---|
155 | Particle* drawPart = particles; |
---|
156 | if (likely(drawPart != NULL)) |
---|
157 | { |
---|
158 | glBegin(GL_POINTS); |
---|
159 | while (likely(drawPart != NULL)) |
---|
160 | { |
---|
161 | // draw in DOT mode |
---|
162 | glVertex3f(drawPart->position.x, drawPart->position.y, drawPart->position.z); |
---|
163 | |
---|
164 | |
---|
165 | drawPart = drawPart->next; |
---|
166 | } |
---|
167 | glEnd(); |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | void ParticleSystem::addParticle(Vector position, Vector velocity, unsigned int data) |
---|
173 | { |
---|
174 | if (this->count <= this->maxCount) |
---|
175 | { |
---|
176 | // if it is the first Particle |
---|
177 | if (unlikely(particles == NULL)) |
---|
178 | { |
---|
179 | this->particles = new Particle; |
---|
180 | this->particles->next = NULL; |
---|
181 | } |
---|
182 | // filling the List from the beginning |
---|
183 | else |
---|
184 | { |
---|
185 | Particle* tmpPart = new Particle; |
---|
186 | tmpPart->next = this->particles; |
---|
187 | this->particles = tmpPart; |
---|
188 | } |
---|
189 | |
---|
190 | particles->timeToLive = this->lifeSpan + (float)(random()/RAND_MAX)* this->randomLifeSpan; |
---|
191 | particles->position = position; |
---|
192 | particles->velocity = velocity; |
---|
193 | // particle->rotation = ; //! \todo rotation is once again something to be done. |
---|
194 | particles->mass = this->initialMass + (random()/RAND_MAX -.5)* this->randomInitialMass; |
---|
195 | particles->radius = this->startRadius + (random()/RAND_MAX-.5)*this->randomRadius; |
---|
196 | |
---|
197 | particles->radiusIt = (this->endRadius + (random()/RAND_MAX-.5)*this->randomRadius - particles->radius) / particles->timeToLive; |
---|
198 | |
---|
199 | ++this->count; |
---|
200 | } |
---|
201 | else |
---|
202 | PRINTF(4)("maximum count of particles reached not adding any more\n"); |
---|
203 | } |
---|
204 | |
---|