Jelajahi Sumber

Add activate material code.

Sam Jaffe 6 tahun lalu
induk
melakukan
d3e5227b99

+ 21 - 5
graphics/graphics.xcodeproj/project.pbxproj

@@ -102,13 +102,10 @@
 		CD3AC6E41D2C0364002B4BB0 /* src */ = {
 			isa = PBXGroup;
 			children = (
-				CD3AC6F01D2C03B7002B4BB0 /* material.cpp */,
-				CD3AC6FB1D2C06B5002B4BB0 /* shader.cpp */,
-				CD3AC7171D2C0950002B4BB0 /* shader_program.cpp */,
+				CD62FD1A22923B8E00376440 /* renderer */,
+				CD62FD1822923B8100376440 /* model */,
 				CD62FCF52290DC9000376440 /* helper.hpp */,
 				CD62FCF62290DC9000376440 /* opengl_helper.cxx */,
-				CD3AC6F61D2C0518002B4BB0 /* texture.cpp */,
-				CD3AC7241D2C0C63002B4BB0 /* object.cpp */,
 			);
 			path = src;
 			sourceTree = "<group>";
@@ -124,6 +121,25 @@
 			name = Products;
 			sourceTree = "<group>";
 		};
+		CD62FD1822923B8100376440 /* model */ = {
+			isa = PBXGroup;
+			children = (
+				CD3AC7241D2C0C63002B4BB0 /* object.cpp */,
+				CD3AC6F01D2C03B7002B4BB0 /* material.cpp */,
+				CD3AC6FB1D2C06B5002B4BB0 /* shader.cpp */,
+				CD3AC7171D2C0950002B4BB0 /* shader_program.cpp */,
+				CD3AC6F61D2C0518002B4BB0 /* texture.cpp */,
+			);
+			name = model;
+			sourceTree = "<group>";
+		};
+		CD62FD1A22923B8E00376440 /* renderer */ = {
+			isa = PBXGroup;
+			children = (
+			);
+			name = renderer;
+			sourceTree = "<group>";
+		};
 		CDA34D9822517A3D008036A7 /* Frameworks */ = {
 			isa = PBXGroup;
 			children = (

+ 6 - 6
graphics/include/game/graphics/material.hpp

@@ -15,13 +15,12 @@
 
 namespace graphics {
   class texture;
-  class material : public identity<material> {
-  public:
-    struct uniform {
-      flyweight<texture> texture;
-      int uniform_id; // TODO (sjaffe): use an enum and hide remapping?
-    };
+  struct uniform {
+    flyweight<texture> texture;
+    int uniform_id; // TODO (sjaffe): use an enum and hide remapping?
+  };
 
+  class material : public identity<material> {
   public:
     shader_program const shaders;
     std::vector<uniform> uniforms;
@@ -32,6 +31,7 @@ namespace graphics {
                                       std::string const & uniform);
 
     math::vec2i size() const;
+    void activate() const;
 
   private:
     material(unsigned int, shader_program const &);

+ 6 - 0
graphics/src/helper.hpp

@@ -17,6 +17,8 @@ template <typename> class flyweight;
 
 namespace graphics {
   class shader;
+  class shader_program;
+  struct uniform;
   namespace textures {
     enum class format { RGB, RGBA };
     unsigned int init(format, math::vec2i, void const *);
@@ -29,6 +31,10 @@ namespace graphics {
     void activate(unsigned int id);
     int uniform_location(unsigned int id, std::string const & uniform);
   }
+  namespace materials {
+    void activate(flyweight<shader_program> program,
+                  std::vector<uniform> const & uniforms);
+  }
 }
 
 namespace std {

+ 2 - 0
graphics/src/material.cpp

@@ -59,3 +59,5 @@ material::material(unsigned int id, shader_program const & sp)
 math::vec2i material::size() const {
   return uniforms.empty() ? ZERO : uniforms.front().texture.actual().size;
 }
+
+void material::activate() const { materials::activate(shaders, uniforms); }

+ 18 - 0
graphics/src/opengl_helper.cxx

@@ -18,6 +18,7 @@
 #include "scope_guard/scope_guard.hpp"
 #include "vector/vector.hpp"
 
+#include "game/graphics/material.hpp"
 #include "game/graphics/shader.hpp"
 #include "game/util/env.hpp"
 #include "game/util/files.hpp"
@@ -336,3 +337,20 @@ namespace graphics { namespace shaders {
     return glGetUniformLocation(id, uniform.c_str());
   }
 }}
+
+namespace graphics { namespace materials {
+  void activate(flyweight<shader_program> program,
+                std::vector<uniform> const & uniforms) {
+    glUseProgram(program.id);
+
+    for (unsigned int i = 0; i < uniforms.size(); i++) {
+      const uniform & uniform = 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);
+  }
+}}