diff options
author | Clyne Sullivan <clyne@bitgloo.com> | 2019-09-17 15:06:39 -0400 |
---|---|---|
committer | Clyne Sullivan <clyne@bitgloo.com> | 2019-09-17 15:06:39 -0400 |
commit | 927348d9d8eb16b469c23ef32486bea5f94c5469 (patch) | |
tree | 58caa1e9eff92c1e6bafe9483c01b746bbecbeaf | |
parent | ceda39e4bd2e3a7794f0cb4f96df1de6ebee47d2 (diff) |
made VBO rendering accessible
-rw-r--r-- | src/events/render.hpp | 16 | ||||
-rw-r--r-- | src/render.cpp | 30 | ||||
-rw-r--r-- | src/render.hpp | 22 | ||||
-rw-r--r-- | src/text.cpp | 11 | ||||
-rw-r--r-- | src/text.hpp | 5 | ||||
-rw-r--r-- | src/world.cpp | 7 |
6 files changed, 67 insertions, 24 deletions
diff --git a/src/events/render.hpp b/src/events/render.hpp new file mode 100644 index 0000000..bcecac6 --- /dev/null +++ b/src/events/render.hpp @@ -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_ + diff --git a/src/render.cpp b/src/render.cpp index b991b2f..2a51b70 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -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)); } + diff --git a/src/render.hpp b/src/render.hpp index 26e525b..d74aca4 100644 --- a/src/render.hpp +++ b/src/render.hpp @@ -36,8 +36,21 @@ #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_ diff --git a/src/text.cpp b/src/text.cpp index 8726b88..186907f 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -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) { diff --git a/src/text.hpp b/src/text.hpp index f456500..8efe3ae 100644 --- a/src/text.hpp +++ b/src/text.hpp @@ -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: diff --git a/src/world.cpp b/src/world.cpp index 7710ecc..9f69c45 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -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() ); } } |