diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2017-01-06 13:30:30 -0500 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2017-01-06 13:30:30 -0500 |
commit | a77877d65dfa1ee74407d94e221f2c737fdf20dd (patch) | |
tree | 6192ffa41f08e4df4e0e62d5f8cf5151b3f2b624 /src/particle.cpp | |
parent | 1358acea95c821729f3770ea5968d9476b621936 (diff) |
particle VBOs
Diffstat (limited to 'src/particle.cpp')
-rw-r--r-- | src/particle.cpp | 65 |
1 files changed, 50 insertions, 15 deletions
diff --git a/src/particle.cpp b/src/particle.cpp index 8090e2d..3e140ae 100644 --- a/src/particle.cpp +++ b/src/particle.cpp @@ -12,9 +12,9 @@ ParticleSystem::ParticleSystem(int count, bool m) void ParticleSystem::add(const vec2& pos, const ParticleType& type) { - // TODO enforce max - //if (max && parts.size() >= std::end(parts)) - // return; + // TODO not enforce max + if (/*max &&*/ parts.size() + 1 >= parts.capacity()) + return; parts.emplace_back(pos, type); } @@ -28,16 +28,32 @@ void ParticleSystem::addMultiple(const int& count, const ParticleType& type, std void ParticleSystem::render(void) const { - static const GLfloat tex[12] = { - 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0 - }; + static GLuint particleVBO = 9999, texVBO = 0; + + if (particleVBO == 9999) { + glGenBuffers(1, &particleVBO); + glBindBuffer(GL_ARRAY_BUFFER, particleVBO); + glBufferData(GL_ARRAY_BUFFER, parts.capacity() * 18 * sizeof(GLfloat), nullptr, + GL_STREAM_DRAW); + + glGenBuffers(1, &texVBO); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, texVBO); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, parts.capacity() * 12 * sizeof(GLfloat), nullptr, + GL_STATIC_DRAW); + int top = (parts.capacity() - 1) * 12 * sizeof(GLfloat), i = 0; + unsigned char zero = 0; + while (i < top) { + glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, i, 1, &zero); + i++; + } + } - Render::worldShader.use(); - Render::worldShader.enable(); - Colors::blue.use(); + glBindBuffer(GL_ARRAY_BUFFER, particleVBO); + // copy data into VBO + int offset = 0; for (const auto& p : parts) { - GLfloat coord[18] = { + GLfloat coords[18] = { p.location.x, p.location.y, -1, p.location.x, p.location.y + 5, -1, p.location.x + 5, p.location.y + 5, -1, @@ -45,14 +61,33 @@ void ParticleSystem::render(void) const p.location.x + 5, p.location.y, -1, p.location.x, p.location.y, -1 }; - - glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 0, coord); - glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 0, tex); - glDrawArrays(GL_TRIANGLES, 0, 6); + + glBufferSubData(GL_ARRAY_BUFFER, offset, 18 * sizeof(GLfloat), coords); + offset += 18 * sizeof(GLfloat); } + + Render::worldShader.use(); + Render::worldShader.enable(); + Colors::blue.use(); + //glUniform4f(Render::worldShader.uniform[WU_tex_color], 0.0f, 0.0f, 1.0f, 1.0f); + + glEnableClientState(GL_VERTEX_ARRAY); + + glBindBuffer(GL_ARRAY_BUFFER, particleVBO); + glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 0, 0); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, texVBO); + glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 0, 0); + + glDrawArrays(GL_TRIANGLES, 0, parts.size() * 6); + + glDisableClientState(GL_VERTEX_ARRAY); Render::worldShader.disable(); Render::worldShader.unuse(); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } void ParticleSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) @@ -74,7 +109,7 @@ void ParticleSystem::update(entityx::EntityManager &en, entityx::EventManager &e // update movement switch (p.type) { case ParticleType::Drop: - if (p.velocity.y > -.5) + if (p.velocity.y > -.6) p.velocity.y -= 0.001f; break; case ParticleType::Confetti: |