ParticleSystem.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * Copyright (c) 2006-2016 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. //LOVE
  21. #include "common/config.h"
  22. #include "ParticleSystem.h"
  23. #include "graphics/Graphics.h"
  24. #include "OpenGL.h"
  25. // STD
  26. #include <algorithm>
  27. #include <cmath>
  28. #include <cstdlib>
  29. namespace love
  30. {
  31. namespace graphics
  32. {
  33. namespace opengl
  34. {
  35. ParticleSystem::ParticleSystem(Texture *texture, uint32 size)
  36. : love::graphics::ParticleSystem(texture, size)
  37. , particleVerts(nullptr)
  38. , quadIndices(size)
  39. {
  40. createVertices(size);
  41. }
  42. ParticleSystem::ParticleSystem(const ParticleSystem &p)
  43. : love::graphics::ParticleSystem(p)
  44. , particleVerts(nullptr)
  45. , quadIndices(p.quadIndices)
  46. {
  47. createVertices(maxParticles);
  48. }
  49. ParticleSystem::~ParticleSystem()
  50. {
  51. delete[] particleVerts;
  52. }
  53. void ParticleSystem::createVertices(size_t numparticles)
  54. {
  55. try
  56. {
  57. Vertex *pverts = new Vertex[numparticles * 4];
  58. delete[] particleVerts;
  59. particleVerts = pverts;
  60. }
  61. catch (std::exception &)
  62. {
  63. throw love::Exception("Out of memory.");
  64. }
  65. }
  66. ParticleSystem *ParticleSystem::clone()
  67. {
  68. return new ParticleSystem(*this);
  69. }
  70. void ParticleSystem::setBufferSize(uint32 size)
  71. {
  72. love::graphics::ParticleSystem::setBufferSize(size);
  73. quadIndices = QuadIndices(size);
  74. createVertices(size);
  75. }
  76. void ParticleSystem::draw(Graphics *gfx, const Matrix4 &m)
  77. {
  78. uint32 pCount = getCount();
  79. if (pCount == 0 || texture.get() == nullptr || pMem == nullptr || particleVerts == nullptr)
  80. return;
  81. gfx->flushStreamDraws();
  82. OpenGL::TempDebugGroup debuggroup("ParticleSystem draw");
  83. Graphics::TempTransform transform(gfx, m);
  84. const Vertex *textureVerts = texture->getVertices();
  85. Vertex *pVerts = particleVerts;
  86. Particle *p = pHead;
  87. bool useQuads = !quads.empty();
  88. Matrix3 t;
  89. // set the vertex data for each particle (transformation, texcoords, color)
  90. while (p)
  91. {
  92. if (useQuads)
  93. textureVerts = quads[p->quadIndex]->getVertices();
  94. // particle vertices are image vertices transformed by particle info
  95. t.setTransformation(p->position.x, p->position.y, p->angle, p->size, p->size, offset.x, offset.y, 0.0f, 0.0f);
  96. t.transform(pVerts, textureVerts, 4);
  97. // Particle colors are stored as floats (0-1) but vertex colors are
  98. // unsigned bytes (0-255).
  99. Color c = toColor(p->color);
  100. // set the texture coordinate and color data for particle vertices
  101. for (int v = 0; v < 4; v++)
  102. {
  103. pVerts[v].s = textureVerts[v].s;
  104. pVerts[v].t = textureVerts[v].t;
  105. pVerts[v].color = c;
  106. }
  107. pVerts += 4;
  108. p = p->next;
  109. }
  110. gl.bindTextureToUnit(texture, 0, false);
  111. gl.prepareDraw();
  112. gl.useVertexAttribArrays(ATTRIBFLAG_POS | ATTRIBFLAG_TEXCOORD | ATTRIBFLAG_COLOR);
  113. gl.bindBuffer(BUFFER_VERTEX, 0);
  114. glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), &particleVerts[0].color.r);
  115. glVertexAttribPointer(ATTRIB_POS, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), &particleVerts[0].x);
  116. glVertexAttribPointer(ATTRIB_TEXCOORD, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), &particleVerts[0].s);
  117. GLsizei count = (GLsizei) quadIndices.getIndexCount(pCount);
  118. GLenum gltype = quadIndices.getType();
  119. // We use a client-side index array instead of an Index Buffers, because
  120. // at least one graphics driver (the one for Kepler nvidia GPUs in OS X
  121. // 10.11) fails to render geometry if an index buffer is used with
  122. // client-side vertex arrays.
  123. gl.bindBuffer(BUFFER_INDEX, 0);
  124. gl.drawElements(GL_TRIANGLES, count, gltype, quadIndices.getIndices(0));
  125. }
  126. } // opengl
  127. } // graphics
  128. } // love