]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
made VBO rendering accessible
authorClyne Sullivan <clyne@bitgloo.com>
Tue, 17 Sep 2019 19:06:39 +0000 (15:06 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Tue, 17 Sep 2019 19:06:39 +0000 (15:06 -0400)
src/events/render.hpp [new file with mode: 0644]
src/render.cpp
src/render.hpp
src/text.cpp
src/text.hpp
src/world.cpp

diff --git a/src/events/render.hpp b/src/events/render.hpp
new file mode 100644 (file)
index 0000000..bcecac6
--- /dev/null
@@ -0,0 +1,16 @@
+#ifndef EVENTS_RENDER_HPP_
+#define EVENTS_RENDER_HPP_
+
+struct NewRenderEvent
+{
+    GLuint vbo;
+    GLuint tex;
+    GLuint normal;
+    unsigned int vertex;
+
+    NewRenderEvent(GLuint _vbo, GLuint _tex, GLuint _normal, unsigned int _vertex) :
+        vbo(_vbo), tex(_tex), normal(_normal), vertex(_vertex) {}
+};
+
+#endif // EVENTS_RENDER_HPP_
+
index b991b2f882bee59985a49b31802926e7594dcd6a..2a51b70742429784f845edd3b305d89d1e769592 100644 (file)
@@ -27,7 +27,7 @@
 void RenderSystem::configure([[maybe_unused]] entityx::EntityManager& entities,
                              [[maybe_unused]] entityx::EventManager& events)
 {
-    events.subscribe<WorldMeshUpdateEvent>(*this);
+    events.subscribe<NewRenderEvent>(*this);
     init();
 }
 
@@ -180,27 +180,25 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
         glDrawArrays(GL_TRIANGLES, 0, 6);
     });
 
-    // If we were given a world VBO render it
-    if (worldVBO) {
+    // Update all given VBOs
+    for (auto& r : renders) {
+        auto& render = r.second;
+
         glActiveTexture(GL_TEXTURE0);
-        glBindTexture(GL_TEXTURE_2D, worldTexture);
+        glBindTexture(GL_TEXTURE_2D, render.tex);
         glUniform1i(q, 0);
 
         glActiveTexture(GL_TEXTURE1);
-        glBindTexture(GL_TEXTURE_2D, worldNormal);
+        glBindTexture(GL_TEXTURE_2D, render.normal);
         glUniform1i(n, 1);
 
-        glBindBuffer(GL_ARRAY_BUFFER, worldVBO);
-        //glBufferData(GL_ARRAY_BUFFER, 
-        //             wm.size() * sizeof(WorldMeshData), 
-        //             &wm.front(), 
-        //             GL_STREAM_DRAW);
+        glBindBuffer(GL_ARRAY_BUFFER, r.first);
 
         glVertexAttribPointer(a, 3, GL_FLOAT, GL_FALSE,
                               6*sizeof(float), 0);
         glVertexAttribPointer(t, 2, GL_FLOAT, GL_FALSE, 
                               6*sizeof(float), (void*)(3*sizeof(float)));
-        glDrawArrays(GL_TRIANGLES, 0, worldVertex);
+        glDrawArrays(GL_TRIANGLES, 0, render.vertex);
     }
 
     /*************
@@ -292,10 +290,10 @@ int RenderSystem::init(void)
 /************
 *  EVENTS  *
 ************/
-void RenderSystem::receive(const WorldMeshUpdateEvent &wmu)
+
+void RenderSystem::receive(const NewRenderEvent &nre)
 {
-    worldVBO = wmu.worldVBO;
-    worldVertex = wmu.numVertex;
-    worldTexture = wmu.worldTexture;
-    worldNormal = wmu.worldNormal;
+    renders.insert_or_assign(nre.vbo,
+                             RenderData(nre.tex, nre.normal, nre.vertex));
 }
+
index 26e525b35e7cc99d7f2a3af54030458852372c2e..d74aca4d602b1da9bd983e7e5746e70a5621e790 100644 (file)
 
 #include "shader.hpp"
 #include "world.hpp"
+#include "events/render.hpp"
 #include "events/world.hpp"
 
+#include <map>
+
+struct RenderData
+{
+    GLuint tex;
+    GLuint normal;
+    unsigned int vertex;
+
+    RenderData(GLuint _tex, GLuint _normal, unsigned int _vertex) :
+        tex(_tex), normal(_normal), vertex(_vertex) {}
+};
+
 class RenderSystem : public entityx::System<RenderSystem>,
                      public entityx::Receiver<RenderSystem>
 {
@@ -52,10 +65,8 @@ private:
     Shader worldShader;
     glm::vec3 camPos;
 
-    GLuint worldVBO = 0;
-    unsigned int worldVertex = 0;
-    GLuint worldTexture = 0;
-    GLuint worldNormal = 0;
+    // Map of VBOs and their render data
+    std::map<GLuint, RenderData> renders;
 public:
     RenderSystem() :
         window(nullptr, SDL_DestroyWindow) {}
@@ -88,8 +99,9 @@ public:
     /************
     *  EVENTS  *
     ************/
-    void receive(const WorldMeshUpdateEvent &wmu);
+    //void receive(const WorldMeshUpdateEvent &wmu);
     
+    void receive(const NewRenderEvent &nre);
 };
 
 #endif // SYSTEM_RENDER_HPP_
index 8726b88e1c9973350c71080ec33cb398a3864e38..186907f61d2cd9f5ed8a782720e99942e2e6981d 100644 (file)
@@ -97,6 +97,17 @@ void TextSystem::loadFont(const std::string& name,
               << font.tex << ")" << std::endl;
 }
 
+void TextSystem::put(const std::string& font,
+                     float x,
+                     float y,
+                     const std::string& text)
+{
+    if (fontData.find(font) == fontData.end())
+        return;
+
+    fontData[font].text.emplace_back(text, x, y);
+}
+
 void TextSystem::updateVBOs(void)
 {
     for (auto& data : fontData) {
index f456500a276f8155a36f51e46ddb9633e85e7fe3..8efe3ae23b73c8fe3e9f303dd8d258557a08d564 100644 (file)
@@ -51,6 +51,9 @@ struct Text {
     float x;
     float y;
     float z;
+
+    Text(std::string _text, float _x, float _y, float _z = 0.0f) :
+        text(_text), x(_x), y(_y), z(_z) {}
 };
 
 // Stores texture and placement data for a font at a size.
@@ -80,6 +83,8 @@ public:
                 entityx::EventManager& events,
                 entityx::TimeDelta dt) final;
 
+    void put(const std::string& font, float x, float y, const std::string& text);
+
     void loadFont(const std::string& name, const std::string& file, int size);
 
 private:
index 7710ecc31f91601142cd45eee03eb5fbdaf39d6b..9f69c45d40a20d4f5e67343a8c2f5b2be92c7f59 100644 (file)
@@ -18,6 +18,7 @@
  */
 
 #include "world.hpp"
+#include "events/render.hpp"
 #include "events/world.hpp"
 
 /*****************
@@ -210,11 +211,11 @@ void WorldSystem::update([[maybe_unused]]entityx::EntityManager& entities,
     }
 
     if (currentWorld->meshUpdated) {
-        events.emit<WorldMeshUpdateEvent>(
+        events.emit<NewRenderEvent>(
             currentWorld->worldVBO, 
-            currentWorld->mesh.size(),
             currentWorld->getTexture(),
-            currentWorld->getNormal()
+            currentWorld->getNormal(),
+            currentWorld->mesh.size()
         );
     }
 }