|
1086
|
1 /**
|
|
|
2 * Copyright (c) 2006-2012 LOVE Development Team
|
|
|
3 *
|
|
|
4 * This software is provided 'as-is', without any express or implied
|
|
|
5 * warranty. In no event will the authors be held liable for any damages
|
|
|
6 * arising from the use of this software.
|
|
|
7 *
|
|
|
8 * Permission is granted to anyone to use this software for any purpose,
|
|
|
9 * including commercial applications, and to alter it and redistribute it
|
|
|
10 * freely, subject to the following restrictions:
|
|
|
11 *
|
|
|
12 * 1. The origin of this software must not be misrepresented; you must not
|
|
|
13 * claim that you wrote the original software. If you use this software
|
|
|
14 * in a product, an acknowledgment in the product documentation would be
|
|
|
15 * appreciated but is not required.
|
|
|
16 * 2. Altered source versions must be plainly marked as such, and must not be
|
|
|
17 * misrepresented as being the original software.
|
|
|
18 * 3. This notice may not be removed or altered from any source distribution.
|
|
|
19 **/
|
|
|
20
|
|
|
21 #include "ParticleSystem.h"
|
|
|
22
|
|
|
23 #include "common/math.h"
|
|
|
24
|
|
|
25 #include "GLee.h"
|
|
|
26 #include <cmath>
|
|
|
27 #include <cstdlib>
|
|
|
28
|
|
|
29 namespace love
|
|
|
30 {
|
|
|
31 namespace graphics
|
|
|
32 {
|
|
|
33 namespace opengl
|
|
|
34 {
|
|
|
35
|
|
|
36 namespace
|
|
|
37 {
|
|
|
38 Colorf colorToFloat(const Color &c)
|
|
|
39 {
|
|
|
40 return Colorf((GLfloat)c.r/255.0f, (GLfloat)c.g/255.0f, (GLfloat)c.b/255.0f, (GLfloat)c.a/255.0f);
|
|
|
41 }
|
|
|
42 }
|
|
|
43
|
|
|
44 float calculate_variation(float inner, float outer, float var)
|
|
|
45 {
|
|
|
46 float low = inner - (outer/2.0f)*var;
|
|
|
47 float high = inner + (outer/2.0f)*var;
|
|
|
48 float r = (rand() / (float(RAND_MAX)+1));
|
|
|
49 return low*(1-r)+high*r;
|
|
|
50 }
|
|
|
51
|
|
|
52
|
|
|
53 ParticleSystem::ParticleSystem(Image *sprite, unsigned int buffer)
|
|
|
54 : pStart(0)
|
|
|
55 , pLast(0)
|
|
|
56 , pEnd(0)
|
|
|
57 , active(true)
|
|
|
58 , emissionRate(0)
|
|
|
59 , emitCounter(0)
|
|
|
60 , lifetime(-1)
|
|
|
61 , life(0)
|
|
|
62 , particleLifeMin(0)
|
|
|
63 , particleLifeMax(0)
|
|
|
64 , direction(0)
|
|
|
65 , spread(0)
|
|
|
66 , relative(false)
|
|
|
67 , speedMin(0)
|
|
|
68 , speedMax(0)
|
|
|
69 , gravityMin(0)
|
|
|
70 , gravityMax(0)
|
|
|
71 , radialAccelerationMin(0)
|
|
|
72 , radialAccelerationMax(0)
|
|
|
73 , tangentialAccelerationMin(0)
|
|
|
74 , tangentialAccelerationMax(0)
|
|
|
75 , sizeVariation(0)
|
|
|
76 , rotationMin(0)
|
|
|
77 , rotationMax(0)
|
|
|
78 , spinStart(0)
|
|
|
79 , spinEnd(0)
|
|
|
80 , spinVariation(0)
|
|
|
81 , offsetX(sprite->getWidth()*0.5f)
|
|
|
82 , offsetY(sprite->getHeight()*0.5f)
|
|
|
83 {
|
|
|
84 this->sprite = sprite;
|
|
|
85 sprite->retain();
|
|
|
86 sizes.push_back(1.0f);
|
|
|
87 colors.push_back(Colorf(1.0f, 1.0f, 1.0f, 1.0f));
|
|
|
88 setBufferSize(buffer);
|
|
|
89 }
|
|
|
90
|
|
|
91 ParticleSystem::~ParticleSystem()
|
|
|
92 {
|
|
|
93 if (this->sprite != 0)
|
|
|
94 this->sprite->release();
|
|
|
95
|
|
|
96 if (pStart != 0)
|
|
|
97 delete [] pStart;
|
|
|
98 }
|
|
|
99
|
|
|
100 void ParticleSystem::add()
|
|
|
101 {
|
|
|
102 if (isFull()) return;
|
|
|
103
|
|
|
104 float min,max;
|
|
|
105
|
|
|
106 min = particleLifeMin;
|
|
|
107 max = particleLifeMax;
|
|
|
108 if (min == max)
|
|
|
109 pLast->life = min;
|
|
|
110 else
|
|
|
111 pLast->life = (rand() / (float(RAND_MAX)+1)) * (max - min) + min;
|
|
|
112 pLast->lifetime = pLast->life;
|
|
|
113
|
|
|
114 pLast->position[0] = position.getX();
|
|
|
115 pLast->position[1] = position.getY();
|
|
|
116
|
|
|
117 min = direction - spread/2.0f;
|
|
|
118 max = direction + spread/2.0f;
|
|
|
119 pLast->direction = (rand() / (float(RAND_MAX)+1)) * (max - min) + min;
|
|
|
120
|
|
|
121 pLast->origin = position;
|
|
|
122
|
|
|
123 min = speedMin;
|
|
|
124 max = speedMax;
|
|
|
125 float speed = (rand() / (float(RAND_MAX)+1)) * (max - min) + min;
|
|
|
126 pLast->speed = love::Vector(cos(pLast->direction), sin(pLast->direction));
|
|
|
127 pLast->speed *= speed;
|
|
|
128
|
|
|
129 min = gravityMin;
|
|
|
130 max = gravityMax;
|
|
|
131 pLast->gravity = (rand() / (float(RAND_MAX)+1)) * (max - min) + min;
|
|
|
132
|
|
|
133 min = radialAccelerationMin;
|
|
|
134 max = radialAccelerationMax;
|
|
|
135 pLast->radialAcceleration = (rand() / (float(RAND_MAX)+1)) * (max - min) + min;
|
|
|
136
|
|
|
137 min = tangentialAccelerationMin;
|
|
|
138 max = tangentialAccelerationMax;
|
|
|
139 pLast->tangentialAcceleration = (rand() / (float(RAND_MAX)+1)) * (max - min) + min;
|
|
|
140
|
|
|
141 pLast->sizeOffset = (rand() / (float(RAND_MAX)+1)) * sizeVariation; // time offset for size change
|
|
|
142 pLast->sizeIntervalSize = (1.0f - (rand() / (float(RAND_MAX)+1)) * sizeVariation) - pLast->sizeOffset;
|
|
|
143 pLast->size = sizes[(size_t)(pLast->sizeOffset - .5f) * (sizes.size() - 1)];
|
|
|
144
|
|
|
145 min = rotationMin;
|
|
|
146 max = rotationMax;
|
|
|
147 pLast->spinStart = calculate_variation(spinStart, spinEnd, spinVariation);
|
|
|
148 pLast->spinEnd = calculate_variation(spinEnd, spinStart, spinVariation);
|
|
|
149 pLast->rotation = (rand() / (float(RAND_MAX)+1)) * (max - min) + min;;
|
|
|
150
|
|
|
151 pLast->color = colors[0];
|
|
|
152
|
|
|
153 pLast++;
|
|
|
154 }
|
|
|
155
|
|
|
156 void ParticleSystem::remove(particle *p)
|
|
|
157 {
|
|
|
158 if (!isEmpty())
|
|
|
159 {
|
|
|
160 *p = *(--pLast);
|
|
|
161 }
|
|
|
162 }
|
|
|
163
|
|
|
164 void ParticleSystem::setSprite(Image *image)
|
|
|
165 {
|
|
|
166 if (sprite != 0)
|
|
|
167 sprite->release();
|
|
|
168
|
|
|
169 sprite = image;
|
|
|
170 sprite->retain();
|
|
|
171 }
|
|
|
172
|
|
|
173 void ParticleSystem::setBufferSize(unsigned int size)
|
|
|
174 {
|
|
|
175 // delete previous data
|
|
|
176 delete [] pStart;
|
|
|
177
|
|
|
178 pLast = pStart = new particle[size];
|
|
|
179
|
|
|
180 pEnd = pStart + size;
|
|
|
181 }
|
|
|
182
|
|
|
183 void ParticleSystem::setEmissionRate(int rate)
|
|
|
184 {
|
|
|
185 emissionRate = rate;
|
|
|
186 }
|
|
|
187
|
|
|
188 void ParticleSystem::setLifetime(float life)
|
|
|
189 {
|
|
|
190 this->life = lifetime = life;
|
|
|
191 }
|
|
|
192
|
|
|
193 void ParticleSystem::setParticleLife(float min, float max)
|
|
|
194 {
|
|
|
195 particleLifeMin = min;
|
|
|
196 if (max == 0)
|
|
|
197 particleLifeMax = min;
|
|
|
198 else
|
|
|
199 particleLifeMax = max;
|
|
|
200 }
|
|
|
201
|
|
|
202 void ParticleSystem::setPosition(float x, float y)
|
|
|
203 {
|
|
|
204 position = love::Vector(x, y);
|
|
|
205 }
|
|
|
206
|
|
|
207 void ParticleSystem::setDirection(float direction)
|
|
|
208 {
|
|
|
209 this->direction = direction;
|
|
|
210 }
|
|
|
211
|
|
|
212 void ParticleSystem::setSpread(float spread)
|
|
|
213 {
|
|
|
214 this->spread = spread;
|
|
|
215 }
|
|
|
216
|
|
|
217 void ParticleSystem::setRelativeDirection(bool relative)
|
|
|
218 {
|
|
|
219 this->relative = relative;
|
|
|
220 }
|
|
|
221
|
|
|
222 void ParticleSystem::setSpeed(float speed)
|
|
|
223 {
|
|
|
224 speedMin = speedMax = speed;
|
|
|
225 }
|
|
|
226
|
|
|
227 void ParticleSystem::setSpeed(float min, float max)
|
|
|
228 {
|
|
|
229 speedMin = min;
|
|
|
230 speedMax = max;
|
|
|
231 }
|
|
|
232
|
|
|
233 void ParticleSystem::setGravity(float gravity)
|
|
|
234 {
|
|
|
235 gravityMin = gravityMax = gravity;
|
|
|
236 }
|
|
|
237
|
|
|
238 void ParticleSystem::setGravity(float min, float max)
|
|
|
239 {
|
|
|
240 gravityMin = min;
|
|
|
241 gravityMax = max;
|
|
|
242 }
|
|
|
243
|
|
|
244 void ParticleSystem::setRadialAcceleration(float acceleration)
|
|
|
245 {
|
|
|
246 radialAccelerationMin = radialAccelerationMax = acceleration;
|
|
|
247 }
|
|
|
248
|
|
|
249 void ParticleSystem::setRadialAcceleration(float min, float max)
|
|
|
250 {
|
|
|
251 radialAccelerationMin = min;
|
|
|
252 radialAccelerationMax = max;
|
|
|
253 }
|
|
|
254
|
|
|
255 void ParticleSystem::setTangentialAcceleration(float acceleration)
|
|
|
256 {
|
|
|
257 tangentialAccelerationMin = tangentialAccelerationMax = acceleration;
|
|
|
258 }
|
|
|
259
|
|
|
260 void ParticleSystem::setTangentialAcceleration(float min, float max)
|
|
|
261 {
|
|
|
262 tangentialAccelerationMin = min;
|
|
|
263 tangentialAccelerationMax = max;
|
|
|
264 }
|
|
|
265
|
|
|
266 void ParticleSystem::setSize(float size)
|
|
|
267 {
|
|
|
268 sizes.resize(1);
|
|
|
269 sizes[0] = size;
|
|
|
270 }
|
|
|
271
|
|
|
272 void ParticleSystem::setSize(const std::vector<float> &newSizes, float variation)
|
|
|
273 {
|
|
|
274 sizes = newSizes;
|
|
|
275 sizeVariation = variation;
|
|
|
276 }
|
|
|
277
|
|
|
278 void ParticleSystem::setSizeVariation(float variation)
|
|
|
279 {
|
|
|
280 sizeVariation = variation;
|
|
|
281 }
|
|
|
282
|
|
|
283 void ParticleSystem::setRotation(float rotation)
|
|
|
284 {
|
|
|
285 rotationMin = rotationMax = rotation;
|
|
|
286 }
|
|
|
287
|
|
|
288 void ParticleSystem::setRotation(float min, float max)
|
|
|
289 {
|
|
|
290 rotationMin = min;
|
|
|
291 rotationMax = max;
|
|
|
292 }
|
|
|
293
|
|
|
294 void ParticleSystem::setSpin(float spin)
|
|
|
295 {
|
|
|
296 spinStart = spin;
|
|
|
297 spinEnd = spin;
|
|
|
298 }
|
|
|
299
|
|
|
300 void ParticleSystem::setSpin(float start, float end)
|
|
|
301 {
|
|
|
302 spinStart = start;
|
|
|
303 spinEnd = end;
|
|
|
304 }
|
|
|
305
|
|
|
306 void ParticleSystem::setSpin(float start, float end, float variation)
|
|
|
307 {
|
|
|
308 spinStart = start;
|
|
|
309 spinEnd = end;
|
|
|
310 spinVariation = variation;
|
|
|
311 }
|
|
|
312
|
|
|
313 void ParticleSystem::setSpinVariation(float variation)
|
|
|
314 {
|
|
|
315 spinVariation = variation;
|
|
|
316 }
|
|
|
317
|
|
|
318 void ParticleSystem::setColor(const Color &color)
|
|
|
319 {
|
|
|
320 colors.resize(1);
|
|
|
321 colors[0] = colorToFloat(color);
|
|
|
322 }
|
|
|
323
|
|
|
324 void ParticleSystem::setColor(const std::vector<Color> &newColors)
|
|
|
325 {
|
|
|
326 colors.resize(newColors.size());
|
|
|
327 for (size_t i = 0; i < newColors.size(); ++i)
|
|
|
328 colors[i] = colorToFloat(newColors[i]);
|
|
|
329 }
|
|
|
330
|
|
|
331 void ParticleSystem::setOffset(float x, float y)
|
|
|
332 {
|
|
|
333 offsetX = x;
|
|
|
334 offsetY = y;
|
|
|
335 }
|
|
|
336
|
|
|
337 float ParticleSystem::getX() const
|
|
|
338 {
|
|
|
339 return position.getX();
|
|
|
340 }
|
|
|
341
|
|
|
342 float ParticleSystem::getY() const
|
|
|
343 {
|
|
|
344 return position.getY();
|
|
|
345 }
|
|
|
346
|
|
|
347 const love::Vector &ParticleSystem::getPosition() const
|
|
|
348 {
|
|
|
349 return position;
|
|
|
350 }
|
|
|
351
|
|
|
352 float ParticleSystem::getDirection() const
|
|
|
353 {
|
|
|
354 return direction;
|
|
|
355 }
|
|
|
356
|
|
|
357 float ParticleSystem::getSpread() const
|
|
|
358 {
|
|
|
359 return spread;
|
|
|
360 }
|
|
|
361
|
|
|
362 float ParticleSystem::getOffsetX() const
|
|
|
363 {
|
|
|
364 return offsetX;
|
|
|
365 }
|
|
|
366
|
|
|
367 float ParticleSystem::getOffsetY() const
|
|
|
368 {
|
|
|
369 return offsetY;
|
|
|
370 }
|
|
|
371
|
|
|
372 int ParticleSystem::count() const
|
|
|
373 {
|
|
|
374 return (int)(pLast - pStart);
|
|
|
375 }
|
|
|
376
|
|
|
377 void ParticleSystem::start()
|
|
|
378 {
|
|
|
379 active = true;
|
|
|
380 }
|
|
|
381
|
|
|
382 void ParticleSystem::stop()
|
|
|
383 {
|
|
|
384 active = false;
|
|
|
385 life = lifetime;
|
|
|
386 emitCounter = 0;
|
|
|
387 }
|
|
|
388
|
|
|
389 void ParticleSystem::pause()
|
|
|
390 {
|
|
|
391 active = false;
|
|
|
392 }
|
|
|
393
|
|
|
394 void ParticleSystem::reset()
|
|
|
395 {
|
|
|
396 pLast = pStart;
|
|
|
397 life = lifetime;
|
|
|
398 emitCounter = 0;
|
|
|
399 }
|
|
|
400
|
|
|
401 bool ParticleSystem::isActive() const
|
|
|
402 {
|
|
|
403 return active;
|
|
|
404 }
|
|
|
405
|
|
|
406 bool ParticleSystem::isEmpty() const
|
|
|
407 {
|
|
|
408 return pStart == pLast;
|
|
|
409 }
|
|
|
410
|
|
|
411 bool ParticleSystem::isFull() const
|
|
|
412 {
|
|
|
413 return pLast == pEnd;
|
|
|
414 }
|
|
|
415
|
|
|
416 void ParticleSystem::draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const
|
|
|
417 {
|
|
|
418 if (sprite == 0) return; // just in case of failure
|
|
|
419
|
|
|
420 glPushMatrix();
|
|
|
421 glPushAttrib(GL_CURRENT_BIT);
|
|
|
422
|
|
|
423 Matrix t;
|
|
|
424 t.setTransformation(x, y, angle, sx, sy, ox, oy, kx, ky);
|
|
|
425 glMultMatrixf((const GLfloat *)t.getElements());
|
|
|
426
|
|
|
427 particle *p = pStart;
|
|
|
428 while (p != pLast)
|
|
|
429 {
|
|
|
430 glPushMatrix();
|
|
|
431
|
|
|
432 glColor4f(p->color.r, p->color.g, p->color.b, p->color.a);
|
|
|
433 sprite->draw(p->position[0], p->position[1], p->rotation, p->size, p->size, offsetX, offsetY, 0.0f, 0.0f);
|
|
|
434
|
|
|
435 glPopMatrix();
|
|
|
436 p++;
|
|
|
437 }
|
|
|
438
|
|
|
439 glPopAttrib();
|
|
|
440 glPopMatrix();
|
|
|
441 }
|
|
|
442
|
|
|
443 void ParticleSystem::update(float dt)
|
|
|
444 {
|
|
|
445 // Traverse all particles and update.
|
|
|
446 particle *p = pStart;
|
|
|
447
|
|
|
448 // Make some more particles.
|
|
|
449 if (active)
|
|
|
450 {
|
|
|
451 float rate = 1.0f / emissionRate; // the amount of time between each particle emit
|
|
|
452 emitCounter += dt;
|
|
|
453 while (emitCounter > rate)
|
|
|
454 {
|
|
|
455 add();
|
|
|
456 emitCounter -= rate;
|
|
|
457 }
|
|
|
458 /*int particles = (int)(emissionRate * dt);
|
|
|
459 for (int i = 0; i != particles; i++)
|
|
|
460 add();*/
|
|
|
461
|
|
|
462 life -= dt;
|
|
|
463 if (lifetime != -1 && life < 0)
|
|
|
464 stop();
|
|
|
465 }
|
|
|
466
|
|
|
467 while (p != pLast)
|
|
|
468 {
|
|
|
469 // Decrease lifespan.
|
|
|
470 p->life -= dt;
|
|
|
471
|
|
|
472 if (p->life > 0)
|
|
|
473 {
|
|
|
474
|
|
|
475 // Temp variables.
|
|
|
476 love::Vector radial, tangential, gravity(0, p->gravity);
|
|
|
477 love::Vector ppos(p->position[0], p->position[1]);
|
|
|
478
|
|
|
479 // Get vector from particle center to particle.
|
|
|
480 radial = ppos - p->origin;
|
|
|
481 radial.normalize();
|
|
|
482 tangential = radial;
|
|
|
483
|
|
|
484 // Resize radial acceleration.
|
|
|
485 radial *= p->radialAcceleration;
|
|
|
486
|
|
|
487 // Calculate tangential acceleration.
|
|
|
488 {
|
|
|
489 float a = tangential.getX();
|
|
|
490 tangential.setX(-tangential.getY());
|
|
|
491 tangential.setY(a);
|
|
|
492 }
|
|
|
493
|
|
|
494 // Resize tangential.
|
|
|
495 tangential *= p->tangentialAcceleration;
|
|
|
496
|
|
|
497 // Update position.
|
|
|
498 p->speed += (radial+tangential+gravity)*dt;
|
|
|
499
|
|
|
500 // Modify position.
|
|
|
501 ppos += p->speed * dt;
|
|
|
502
|
|
|
503 p->position[0] = ppos.getX();
|
|
|
504 p->position[1] = ppos.getY();
|
|
|
505
|
|
|
506 const float t = 1.0f - p->life / p->lifetime;
|
|
|
507
|
|
|
508 // Rotate.
|
|
|
509 p->rotation += (p->spinStart * (1.0f - t) + p->spinEnd * t)*dt;
|
|
|
510
|
|
|
511 // Change size according to given intervals:
|
|
|
512 // i = 0 1 2 3 n-1
|
|
|
513 // |-------|-------|------|--- ... ---|
|
|
|
514 // t = 0 1/(n-1) 3/(n-1) 1
|
|
|
515 //
|
|
|
516 // `s' is the interpolation variable scaled to the current
|
|
|
517 // interval width, e.g. if n = 5 and t = 0.3, then the current
|
|
|
518 // indices are 1,2 and s = 0.3 - 0.25 = 0.05
|
|
|
519 float s = p->sizeOffset + t * p->sizeIntervalSize; // size variation
|
|
|
520 s *= (float)(sizes.size() - 1); // 0 <= s < sizes.size()
|
|
|
521 size_t i = (size_t)s;
|
|
|
522 size_t k = (i == sizes.size() - 1) ? i : i + 1; // boundary check (prevents failing on t = 1.0f)
|
|
|
523 s -= (float)i; // transpose s to be in interval [0:1]: i <= s < i + 1 ~> 0 <= s < 1
|
|
|
524 p->size = sizes[i] * (1.0f - s) + sizes[k] * s;
|
|
|
525
|
|
|
526 // Update color according to given intervals (as above)
|
|
|
527 s = t * (float)(colors.size() - 1);
|
|
|
528 i = (size_t)s;
|
|
|
529 k = (i == colors.size() - 1) ? i : i + 1;
|
|
|
530 s -= (float)i; // 0 <= s <= 1
|
|
|
531 p->color = colors[i] * (1.0f - s) + colors[k] * s;
|
|
|
532
|
|
|
533 // Next particle.
|
|
|
534 p++;
|
|
|
535 }
|
|
|
536 else
|
|
|
537 {
|
|
|
538 remove(p);
|
|
|
539
|
|
|
540 if (p >= pLast)
|
|
|
541 return;
|
|
|
542 } // else
|
|
|
543 } // while
|
|
|
544 }
|
|
|
545
|
|
|
546 } // opengl
|
|
|
547 } // graphics
|
|
|
548 } // love
|