ParticleSystem.cpp 15 KB

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