ParticleSystem.cpp 16 KB

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