Explorar el Código

Activate shader programs

Sam Jaffe hace 6 años
padre
commit
96f62ec03b
Se han modificado 3 ficheros con 52 adiciones y 0 borrados
  1. 1 0
      graphics/src/helper.hpp
  2. 49 0
      graphics/src/opengl_helper.cxx
  3. 2 0
      graphics/src/shader_program.cpp

+ 1 - 0
graphics/src/helper.hpp

@@ -24,6 +24,7 @@ namespace graphics {
     unsigned int init(type, std::string const &);
     unsigned int init_program(shader const & fragmentShader,
                               shader const & vertexShader);
+    void activate(unsigned int id);
   }
 }
 

+ 49 - 0
graphics/src/opengl_helper.cxx

@@ -265,4 +265,53 @@ namespace graphics { namespace shaders {
     }
     return id;
   }
+
+  void activate(unsigned int id) {
+    // 100. Use the shader program ID to "turn it on" for all subsequent drawing
+    glUseProgram(id);
+
+    /*
+     // 101. Enable texturing and Bind texture(s) to GPU texture units
+     glActiveTexture(GL_TEXTURE3);
+     glEnable(GL_TEXTURE_2D);
+     glBindTexture(GL_TEXTURE_2D, emissiveTextureID);
+
+     glActiveTexture(GL_TEXTURE2);
+     glEnable(GL_TEXTURE_2D);
+     glBindTexture(GL_TEXTURE_2D, specularTextureID);
+
+     glActiveTexture(GL_TEXTURE1);
+     glEnable(GL_TEXTURE_2D);
+     glBindTexture(GL_TEXTURE_2D, normalTextureID);
+
+     glActiveTexture(GL_TEXTURE0);
+     glEnable(GL_TEXTURE_2D);
+     glBindTexture(GL_TEXTURE_2D, diffuseTextureID);
+     */
+
+    // 102. Get the location # of each named uniform you wish to pass in to the
+    // shader
+    int timeUniformLocation = glGetUniformLocation(id, "u_time");
+    int scale = glGetUniformLocation(id, "Scale");
+    int diffuseMapUniformLocation = glGetUniformLocation(id, "u_diffuseMap");
+    int normalMapUniformLocation = glGetUniformLocation(id, "u_normalMap");
+    int specularMapUniformLocation = glGetUniformLocation(id, "u_specularMap");
+    int emissiveMapUniformLocation = glGetUniformLocation(id, "u_emissiveMap");
+    int debugWave = glGetUniformLocation(id, "g_debugWave");
+
+    // 103. Set the uniform values, including the texture unit numbers for
+    // texture (sampler) uniforms
+    // Env::GetCurrentTimeSeconds()
+    glUniform1f(timeUniformLocation, (float)time(NULL));
+    glUniform1f(scale, 2.f);
+    glUniform1i(debugWave, 1); // TODO: m_waveEffectOn in ShaderProgram??
+    // for GL_TEXTURE0, texture unit 0
+    glUniform1i(diffuseMapUniformLocation, 0);
+    // for GL_TEXTURE1, texture unit 1
+    glUniform1i(normalMapUniformLocation, 1);
+    // for GL_TEXTURE2, texture unit 2
+    glUniform1i(specularMapUniformLocation, 2);
+    // for GL_TEXTURE3, texture unit 3
+    glUniform1i(emissiveMapUniformLocation, 3);
+  }
 }}

+ 2 - 0
graphics/src/shader_program.cpp

@@ -34,4 +34,6 @@ namespace graphics {
 
   shader_program::shader_program(unsigned int id)
       : identity<shader_program>(id) {}
+
+  void shader_program::activate() const { shaders::activate(id); }
 }