1
0
Fork 0

Use program as executable builder

This commit is contained in:
Meoweg 2015-11-13 13:37:53 +00:00
parent 12834f76a5
commit 681c757777
5 changed files with 25 additions and 51 deletions

View File

@ -72,12 +72,12 @@ 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 Program *program = Program("textured").build(__count, attribs); const Executable *exe = Program("textured").build(__count, attribs);
program->use(); mvp_uniform = exe->get_uniform_location("mvp");
local_modelview_uniform = exe->get_uniform_location("local_modelview");
mvp_uniform = program->get_uniform_location("mvp"); exe->use();
local_modelview_uniform = program->get_uniform_location("local_modelview");
camera.projection = glm::perspective(45.0f, (float)640 / (float)480, 0.1f, 100.0f); camera.projection = glm::perspective(45.0f, (float)640 / (float)480, 0.1f, 100.0f);

View File

@ -9,45 +9,22 @@ const std::string Program::filename(const std::string &name)
return "/data/shaders/" + name; return "/data/shaders/" + name;
} }
Program::Program(const std::string &name) Program::Program(const std::string &name):
vertex_shader(GL_VERTEX_SHADER, filename(name) + "/vertex.glsl"),
fragment_shader(GL_FRAGMENT_SHADER, filename(name) + "/fragment.glsl")
{}
const Executable *Program::build(GLuint count, const GLchar *const names[])
{ {
const std::string path = filename(name) + '/'; Executable *exe = new Executable();
const Shader vertex_shader = Shader(GL_VERTEX_SHADER, (path + "vertex.glsl").c_str()); exe->attach_shader(vertex_shader);
const Shader fragment_shader = Shader(GL_FRAGMENT_SHADER, (path + "fragment.glsl").c_str()); exe->attach_shader(fragment_shader);
_id = glCreateProgram();
glAttachShader(_id, vertex_shader.id());
glAttachShader(_id, fragment_shader.id());
}
void Program::bind_attrib_location(GLuint index, const GLchar *name)
{
glBindAttribLocation(_id, index, name);
}
const Program *Program::build(GLuint count, const GLchar *const names[])
{
for (GLuint index = 0; index < count; ++index) for (GLuint index = 0; index < count; ++index)
bind_attrib_location(index, names[index]); exe->bind_attrib_location(index, names[index]);
link(); exe->link();
return this; return exe;
}
void Program::link()
{
glLinkProgram(_id);
}
void Program::use() const
{
glUseProgram(_id);
}
GLuint Program::get_uniform_location(const GLchar *name) const
{
return glGetUniformLocation(_id, name);
} }

View File

@ -1,7 +1,7 @@
#ifndef _PROGRAM_HPP_ #ifndef _PROGRAM_HPP_
#define _PROGRAM_HPP_ #define _PROGRAM_HPP_
#include "gl.hpp" #include "executable.hpp"
#include <string> #include <string>
@ -9,18 +9,13 @@ struct Program
{ {
Program(const std::string &name); Program(const std::string &name);
const Program *build(GLuint count, const GLchar *const names[]); const Executable *build(GLuint count, const GLchar *const names[]);
void use() const;
GLuint get_uniform_location(const GLchar *name) const;
private: private:
static const std::string filename(const std::string &name); static const std::string filename(const std::string &name);
void bind_attrib_location(GLuint index, const GLchar *name); const Shader vertex_shader;
void link(); const Shader fragment_shader;
GLuint _id;
}; };
#endif // _PROGRAM_HPP_ #endif // _PROGRAM_HPP_

View File

@ -3,9 +3,9 @@
#include <cstdlib> #include <cstdlib>
#include <cstdio> #include <cstdio>
Shader::Shader(const GLenum type, const char *const filename) Shader::Shader(const GLenum type, const std::string &filename)
{ {
FILE *file = fopen(filename, "r"); FILE *file = fopen(filename.c_str(), "r");
fseek(file, 0, SEEK_END); fseek(file, 0, SEEK_END);
const long size = ftell(file); const long size = ftell(file);

View File

@ -3,9 +3,11 @@
#include "gl.hpp" #include "gl.hpp"
#include <string>
struct Shader struct Shader
{ {
Shader(GLenum type, const char *filename); Shader(GLenum type, const std::string &filename);
inline GLuint id() const { return _id; }; inline GLuint id() const { return _id; };
private: private: