1
0
Fork 0

Store uniform ids in executable

This commit is contained in:
Meoweg 2015-11-13 13:52:01 +00:00
parent 681c757777
commit 5106e7004b
9 changed files with 64 additions and 21 deletions

View File

@ -25,7 +25,10 @@ void Executable::use() const
glUseProgram(id); glUseProgram(id);
} }
GLuint Executable::get_uniform_location(const GLchar *name) const void Executable::get_uniforms(unsigned count, const GLchar *const names[])
{ {
return glGetUniformLocation(id, name); uniforms.resize(count);
for (unsigned index = 0; index < count; ++index)
uniforms[index] = glGetUniformLocation(id, names[index]);
} }

View File

@ -4,11 +4,13 @@
#include "gl.hpp" #include "gl.hpp"
#include "shader.hpp" #include "shader.hpp"
#include <vector>
struct Executable struct Executable
{ {
void use() const; void use() const;
GLuint get_uniform_location(const GLchar *name) const; inline GLuint uniform(unsigned index) const;
private: private:
friend struct Program; friend struct Program;
@ -18,7 +20,16 @@ private:
void bind_attrib_location(GLuint index, const GLchar *name); void bind_attrib_location(GLuint index, const GLchar *name);
void link(); void link();
void get_uniforms(unsigned count, const GLchar *const names[]);
GLuint id; GLuint id;
std::vector<GLuint> uniforms;
}; };
GLuint Executable::uniform(unsigned index) const
{
return uniforms.at(index);
}
#endif // _EXECUTABLE_HPP_ #endif // _EXECUTABLE_HPP_

View File

@ -10,7 +10,7 @@ enum Attrib : GLuint
normal, normal,
tex_coord, tex_coord,
__count __attrib_count
}; };
static const GLchar *const attribs[] = { static const GLchar *const attribs[] = {
@ -19,7 +19,17 @@ static const GLchar *const attribs[] = {
"tex_coord", "tex_coord",
}; };
extern GLuint mvp_uniform; enum Uniform : unsigned
extern GLuint local_modelview_uniform; {
mvp,
local_modelview,
__uniform_count
};
static const GLchar *const uniforms[] = {
"mvp",
"local_modelview",
};
#endif // _GL_HPP_ #endif // _GL_HPP_

View File

@ -1,3 +1,5 @@
#include "main.hpp"
#include "gl.hpp" #include "gl.hpp"
#include "program.hpp" #include "program.hpp"
#include "scene.hpp" #include "scene.hpp"
@ -14,6 +16,8 @@
#include <emscripten/emscripten.h> #include <emscripten/emscripten.h>
#include <emscripten/html5.h> #include <emscripten/html5.h>
const Executable *exe;
static Store store; static Store store;
static void iterate(); static void iterate();
@ -21,9 +25,6 @@ static void iterate();
static GLFWCALL void on_key(int key, int action); static GLFWCALL void on_key(int key, int action);
static EM_BOOL on_em_mousemove(int event_type, const EmscriptenMouseEvent *mouse_event, void *user_data); static EM_BOOL on_em_mousemove(int event_type, const EmscriptenMouseEvent *mouse_event, void *user_data);
GLuint mvp_uniform;
GLuint local_modelview_uniform;
static bool keys[GLFW_KEY_LAST]; static bool keys[GLFW_KEY_LAST];
static Scene scene; static Scene scene;
@ -72,10 +73,10 @@ int main()
glfwSetKeyCallback(on_key); glfwSetKeyCallback(on_key);
emscripten_set_mousemove_callback(nullptr, nullptr, false, on_em_mousemove); emscripten_set_mousemove_callback(nullptr, nullptr, false, on_em_mousemove);
const Executable *exe = Program("textured").build(__count, attribs); exe = Program("textured").build(
__attrib_count, attribs,
mvp_uniform = exe->get_uniform_location("mvp"); __uniform_count, uniforms
local_modelview_uniform = exe->get_uniform_location("local_modelview"); );
exe->use(); exe->use();

8
src/main.hpp Normal file
View File

@ -0,0 +1,8 @@
#ifndef _MAIN_HPP_
#define _MAIN_HPP_
#include "executable.hpp"
extern const Executable *exe;
#endif // _MAIN_HPP_

View File

@ -1,5 +1,7 @@
#include "raw.hpp" #include "raw.hpp"
#include "../main.hpp"
#include <cstdio> #include <cstdio>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
@ -39,10 +41,10 @@ Raw::Raw(__attribute__((unused)) Store &store, const std::string &name)
void Raw::draw(const glm::mat4 &mvp, const glm::mat4 &transformation) const void Raw::draw(const glm::mat4 &mvp, const glm::mat4 &transformation) const
{ {
const glm::mat4 transform = mvp * transformation; const glm::mat4 transform = mvp * transformation;
glUniformMatrix4fv(mvp_uniform, 1, GL_FALSE, glm::value_ptr(transform)); glUniformMatrix4fv(exe->uniform(Uniform::mvp), 1, GL_FALSE, glm::value_ptr(transform));
const glm::mat3 local_modelview = glm::transpose(glm::inverse(glm::mat3(transformation))); const glm::mat3 local_modelview = glm::transpose(glm::inverse(glm::mat3(transformation)));
glUniformMatrix3fv(local_modelview_uniform, 1, GL_FALSE, glm::value_ptr(local_modelview)); glUniformMatrix3fv(exe->uniform(Uniform::local_modelview), 1, GL_FALSE, glm::value_ptr(local_modelview));
glEnableVertexAttribArray(position); glEnableVertexAttribArray(position);
glBindBuffer(GL_ARRAY_BUFFER, positions_id); glBindBuffer(GL_ARRAY_BUFFER, positions_id);

View File

@ -1,5 +1,7 @@
#include "static.hpp" #include "static.hpp"
#include "../main.hpp"
#include "../mtllib.hpp" #include "../mtllib.hpp"
#include <vector> #include <vector>
@ -109,10 +111,10 @@ Static::Static(Store &store, const std::string &name)
void Static::draw(const glm::mat4 &mvp, const glm::mat4 &transformation) const void Static::draw(const glm::mat4 &mvp, const glm::mat4 &transformation) const
{ {
const glm::mat4 transform = mvp * transformation; const glm::mat4 transform = mvp * transformation;
glUniformMatrix4fv(mvp_uniform, 1, GL_FALSE, glm::value_ptr(transform)); glUniformMatrix4fv(exe->uniform(Uniform::mvp), 1, GL_FALSE, glm::value_ptr(transform));
const glm::mat3 local_modelview = glm::transpose(glm::inverse(glm::mat3(transformation))); const glm::mat3 local_modelview = glm::transpose(glm::inverse(glm::mat3(transformation)));
glUniformMatrix3fv(local_modelview_uniform, 1, GL_FALSE, glm::value_ptr(local_modelview)); glUniformMatrix3fv(exe->uniform(Uniform::local_modelview), 1, GL_FALSE, glm::value_ptr(local_modelview));
_material->use(); _material->use();

View File

@ -14,17 +14,21 @@ Program::Program(const std::string &name):
fragment_shader(GL_FRAGMENT_SHADER, filename(name) + "/fragment.glsl") fragment_shader(GL_FRAGMENT_SHADER, filename(name) + "/fragment.glsl")
{} {}
const Executable *Program::build(GLuint count, const GLchar *const names[]) const Executable *Program::build(
GLuint attrib_count, const GLchar *const attribs[],
unsigned uniform_count, const GLchar *const uniforms[])
{ {
Executable *exe = new Executable(); Executable *exe = new Executable();
exe->attach_shader(vertex_shader); exe->attach_shader(vertex_shader);
exe->attach_shader(fragment_shader); exe->attach_shader(fragment_shader);
for (GLuint index = 0; index < count; ++index) for (GLuint attrib = 0; attrib < attrib_count; ++attrib)
exe->bind_attrib_location(index, names[index]); exe->bind_attrib_location(attrib, attribs[attrib]);
exe->link(); exe->link();
exe->get_uniforms(uniform_count, uniforms);
return exe; return exe;
} }

View File

@ -9,7 +9,9 @@ struct Program
{ {
Program(const std::string &name); Program(const std::string &name);
const Executable *build(GLuint count, const GLchar *const names[]); const Executable *build(
GLuint attrib_count, const GLchar *const attribs[],
unsigned uniform_count, const GLchar *const uniforms[]);
private: private:
static const std::string filename(const std::string &name); static const std::string filename(const std::string &name);