diff --git a/data/shaders/textured/attrib.txt b/data/shaders/textured/attrib.txt new file mode 100644 index 0000000..18c7975 --- /dev/null +++ b/data/shaders/textured/attrib.txt @@ -0,0 +1,3 @@ +position +tex_coord +normal diff --git a/data/shaders/fragment.glsl b/data/shaders/textured/fragment.glsl similarity index 100% rename from data/shaders/fragment.glsl rename to data/shaders/textured/fragment.glsl diff --git a/data/shaders/vertex.glsl b/data/shaders/textured/vertex.glsl similarity index 100% rename from data/shaders/vertex.glsl rename to data/shaders/textured/vertex.glsl diff --git a/src/main.cpp b/src/main.cpp index 66d84fb..2d07a2e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,8 +11,6 @@ #include #include -static Program build_program(); - static void iterate(); static GLFWCALL void on_key(int key, int action); @@ -56,7 +54,7 @@ int main() glfwSetKeyCallback(on_key); emscripten_set_mousemove_callback(nullptr, nullptr, false, on_em_mousemove); - Program program = build_program(); + Program program("textured"); program.use(); mvp_uniform = program.get_uniform_location("mvp"); @@ -94,26 +92,6 @@ int main() emscripten_set_main_loop(iterate, 0, 1); } -Program build_program() -{ - const Shader vertex_shader = Shader(GL_VERTEX_SHADER, "/data/shaders/vertex.glsl"); - const Shader fragment_shader = Shader(GL_FRAGMENT_SHADER, "/data/shaders/fragment.glsl"); - - Program program; - program.attach_shader(vertex_shader); - program.attach_shader(fragment_shader); - program.bind_attrib_location(0, "position"); - program.bind_attrib_location(1, "tex_coord"); - program.bind_attrib_location(2, "normal"); - program.link(); - - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - - return program; -} - void iterate() { if (keys['W']) diff --git a/src/program.cpp b/src/program.cpp index 7b38581..dd254ca 100644 --- a/src/program.cpp +++ b/src/program.cpp @@ -1,23 +1,34 @@ #include "program.hpp" -Program::Program() +#include "shader.hpp" + +#include +#include + +Program::Program(const char *const name) { + const std::string path = std::string("/data/shaders/") + name + '/'; + + const Shader vertex_shader = Shader(GL_VERTEX_SHADER, (path + "vertex.glsl").c_str()); + const Shader fragment_shader = Shader(GL_FRAGMENT_SHADER, (path + "fragment.glsl").c_str()); + _id = glCreateProgram(); -} -void Program::attach_shader(const Shader &shader) -{ - glAttachShader(_id, shader.id()); -} + glAttachShader(_id, vertex_shader.id()); + glAttachShader(_id, fragment_shader.id()); -void Program::bind_attrib_location(const GLuint index, const GLchar *const name) -{ - glBindAttribLocation(_id, index, name); -} + std::ifstream file(path + "attrib.txt", std::ios::in); + + std::string line; + int index = 0; + + while (std::getline(file, line)) + glBindAttribLocation(_id, index++, line.c_str()); -void Program::link() -{ glLinkProgram(_id); + + for (int i = 0; i < index; ++i) + glEnableVertexAttribArray(i); } void Program::use() const diff --git a/src/program.hpp b/src/program.hpp index bf6cdfe..0d7fd43 100644 --- a/src/program.hpp +++ b/src/program.hpp @@ -2,15 +2,11 @@ #define _PROGRAM_HPP_ #include "gl.hpp" -#include "shader.hpp" class Program { public: - Program(); - void attach_shader(const Shader &shader); - void bind_attrib_location(GLuint index, const GLchar *name); - void link(); + Program(const char *name); void use() const; GLuint get_uniform_location(const GLchar *name) const;