Browse Source

Migrate the activation of materials into renderer.
TODO: Have manager/renderer code take ownership of certain characteristics such as uniform_id, and consider caching the outcomes of glUniformLocation() call(s).

Sam Jaffe 6 years ago
parent
commit
dcddecb8da
2 changed files with 20 additions and 1 deletions
  1. 16 1
      graphics/src/openGL/opengl_renderer.cxx
  2. 4 0
      graphics/src/openGL/opengl_renderer.h

+ 16 - 1
graphics/src/openGL/opengl_renderer.cxx

@@ -62,7 +62,7 @@ void opengl_renderer::draw(::identity<material> material_id,
                            math::matr4 const & object_to_world,
                            std::vector<vertex> const & vertices) {
   material const & mat = manager()->get(material_id);
-  if (material_id != active_material) { mat.activate(); }
+  activate(mat);
   // TODO: Attatch shader-id to material-id
   unsigned int const id = mat.program.id;
 
@@ -99,6 +99,21 @@ void opengl_renderer::draw(::identity<material> material_id,
   glDisableVertexAttribArray(texCoordsLocation);
 }
 
+void opengl_renderer::activate(material const & mat) {
+  if (mat.id == active_material) return;
+  glUseProgram(mat.program.id);
+
+  for (unsigned int i = 0; i < mat.uniforms.size(); i++) {
+    const uniform & uniform = mat.uniforms[i];
+    glActiveTexture(i + GL_TEXTURE0);
+    //      glEnable(GL_TEXTURE_2D);
+    glBindTexture(GL_TEXTURE_2D, uniform.texture.id);
+    glUniform1i(uniform.uniform_id, i);
+  }
+
+  glActiveTexture(GL_TEXTURE0);
+}
+
 template <> renderer_impl * graphics::get_renderer_impl<driver::openGL>() {
   static opengl_renderer impl;
   return &impl;

+ 4 - 0
graphics/src/openGL/opengl_renderer.h

@@ -9,6 +9,7 @@
 #pragma once
 
 #include "game/graphics/manager.hpp"
+#include "game/util/identity.hpp"
 #include "renderer_impl.hpp"
 
 #include "matrix/matrix.hpp"
@@ -36,6 +37,9 @@ namespace graphics {
       return mgr;
     }
 
+  private:
+    void activate(material const & mat);
+
   private:
     const math::matr4 identity{math::matrix::identity<float, 4>()};