]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
particle VBOs
authorClyne Sullivan <tullivan99@gmail.com>
Fri, 6 Jan 2017 18:30:30 +0000 (13:30 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Fri, 6 Jan 2017 18:30:30 +0000 (13:30 -0500)
include/weather.hpp
src/particle.cpp

index f7b53f165bb8e9d9510b5ea49ccebe855be94e70..3aac2e651b522dcdd907da0ae69536762eb05e6b 100644 (file)
@@ -38,14 +38,18 @@ public:
                (void)dt;
 
                static auto& partSystem = *game::engine.getSystem<ParticleSystem>();
+               static int newPartDelay = 0; // TODO no
 
                switch (weather) {
                case Weather::Sunny:
                        break;
                case Weather::Rainy:
-                       partSystem.add(vec2(offset.x - game::SCREEN_WIDTH / 2 + randGet() % game::SCREEN_WIDTH,
-                               offset.y + game::SCREEN_HEIGHT / 2 + 100),
-                               ParticleType::Drop);
+                       if (newPartDelay++ == 4) {
+                               newPartDelay = 0;
+                               partSystem.add(vec2(offset.x - game::SCREEN_WIDTH / 2 + randGet() % game::SCREEN_WIDTH,
+                                       offset.y + game::SCREEN_HEIGHT / 2 + 100),
+                                       ParticleType::Drop);
+                       }
                        break; // TODO
                case Weather::Snowy:
                        break; // TODO
index 8090e2db0813217365d6f2818946a63492732927..3e140ae88616ba98a9b0550c351f7cbaccde65eb 100644 (file)
@@ -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: