diff options
Diffstat (limited to 'src/render.cpp')
-rw-r--r-- | src/render.cpp | 71 |
1 files changed, 67 insertions, 4 deletions
diff --git a/src/render.cpp b/src/render.cpp index 0c92475..1b5ba62 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -27,8 +27,10 @@ void RenderSystem::configure([[maybe_unused]] entityx::EntityManager& entities, [[maybe_unused]] entityx::EventManager& events) { + events.subscribe<NewRenderEvent>(*this); events.subscribe<WorldMeshUpdateEvent>(*this); events.subscribe<entityx::ComponentAddedEvent<Player>>(*this); + init(); } @@ -221,11 +223,24 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities, glDrawArrays(GL_TRIANGLES, 0, worldVertex); } + glDisableVertexAttribArray(a); + glDisableVertexAttribArray(t); + /****************** * UI RENDERING * ******************/ - view = glm::lookAt(glm::vec3(0.0f, 0.0f, 0.0f), // Pos + static GLuint uiS = uiShader.getProgram(); + static GLuint uiS_v = uiShader.getUniform("view"); + static GLuint uiS_p = uiShader.getUniform("projection"); + static GLuint uiS_m = uiShader.getUniform("model"); + static GLuint uiS_a = uiShader.getAttribute("coord2d"); + static GLuint uiS_t = uiShader.getAttribute("tex_coord"); + static GLuint uiS_q = uiShader.getUniform("sampler"); + + glUseProgram(uiS); + + view = glm::lookAt(glm::vec3(0.0f, 0.0f, 10.0f), // Pos glm::vec3(0.0f, 0.0f, 0.0f), // Facing glm::vec3(0.0f, 1.0f, 0.0f)); // Up @@ -242,11 +257,40 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities, model = glm::mat4(1.0f); + glUniformMatrix4fv(uiS_v, 1, GL_FALSE, glm::value_ptr(view)); + glUniformMatrix4fv(uiS_p, 1, GL_FALSE, glm::value_ptr(projection)); + glUniformMatrix4fv(uiS_m, 1, GL_FALSE, glm::value_ptr(model)); + + glEnableVertexAttribArray(uiS_a); + glEnableVertexAttribArray(uiS_t); + + // Update all UI VBOs + for (auto& r : uiRenders) { + auto& render = r.second; + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, render.tex); + glUniform1i(uiS_q, 0); + + //glActiveTexture(GL_TEXTURE1); + //glBindTexture(GL_TEXTURE_2D, render.normal); + //glUniform1i(n, 1); + + glBindBuffer(GL_ARRAY_BUFFER, r.first); + + glVertexAttribPointer(uiS_a, 3, GL_FLOAT, GL_FALSE, + 6*sizeof(float), 0); + glVertexAttribPointer(uiS_t, 2, GL_FLOAT, GL_FALSE, + 6*sizeof(float), (void*)(3*sizeof(float))); + glDrawArrays(GL_TRIANGLES, 0, render.vertex); + } + + glDisableVertexAttribArray(uiS_a); + glDisableVertexAttribArray(uiS_t); + /************* * CLEANUP * *************/ - glDisableVertexAttribArray(a); - glDisableVertexAttribArray(t); glDisable(GL_POLYGON_OFFSET_FILL); glDisable(GL_CULL_FACE); @@ -315,8 +359,19 @@ int RenderSystem::init(void) worldShader.addUniform("AmbientLight"); worldShader.addUniform("Flipped"); + uiShader.createProgram("Shaders/ui.vert", "Shaders/ui.frag"); + + uiShader.addUniform("projection"); + uiShader.addUniform("view"); + uiShader.addUniform("model"); + + uiShader.addAttribute("coord2d"); + uiShader.addAttribute("tex_coord"); + + uiShader.addUniform("sampler"); + glEnableVertexAttribArray(worldShader.getAttribute("vertex")); - glUseProgram(worldShader.getProgram()); + glEnableVertexAttribArray(uiShader.getAttribute("coord2d")); // TODO //glPolygonOffset(1.0, 1.0); @@ -331,6 +386,13 @@ int RenderSystem::init(void) /************ * EVENTS * ************/ + +void RenderSystem::receive(const NewRenderEvent &nre) +{ + uiRenders.insert_or_assign(nre.vbo, + UIRenderData(nre.tex, nre.normal, nre.vertex)); +} + void RenderSystem::receive(const WorldMeshUpdateEvent &wmu) { worldVBO = wmu.worldVBO; @@ -343,3 +405,4 @@ void RenderSystem::receive(const entityx::ComponentAddedEvent<Player> &cae) { player = cae.entity; } + |