Use program as executable builder
This commit is contained in:
parent
12834f76a5
commit
681c757777
|
@ -72,12 +72,12 @@ int main()
|
|||
glfwSetKeyCallback(on_key);
|
||||
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");
|
||||
local_modelview_uniform = program->get_uniform_location("local_modelview");
|
||||
exe->use();
|
||||
|
||||
camera.projection = glm::perspective(45.0f, (float)640 / (float)480, 0.1f, 100.0f);
|
||||
|
||||
|
|
|
@ -9,45 +9,22 @@ const std::string Program::filename(const std::string &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());
|
||||
const Shader fragment_shader = Shader(GL_FRAGMENT_SHADER, (path + "fragment.glsl").c_str());
|
||||
exe->attach_shader(vertex_shader);
|
||||
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)
|
||||
bind_attrib_location(index, names[index]);
|
||||
exe->bind_attrib_location(index, names[index]);
|
||||
|
||||
link();
|
||||
exe->link();
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
void Program::link()
|
||||
{
|
||||
glLinkProgram(_id);
|
||||
}
|
||||
|
||||
void Program::use() const
|
||||
{
|
||||
glUseProgram(_id);
|
||||
}
|
||||
|
||||
GLuint Program::get_uniform_location(const GLchar *name) const
|
||||
{
|
||||
return glGetUniformLocation(_id, name);
|
||||
return exe;
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef _PROGRAM_HPP_
|
||||
#define _PROGRAM_HPP_
|
||||
|
||||
#include "gl.hpp"
|
||||
#include "executable.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
|
@ -9,18 +9,13 @@ struct Program
|
|||
{
|
||||
Program(const std::string &name);
|
||||
|
||||
const Program *build(GLuint count, const GLchar *const names[]);
|
||||
|
||||
void use() const;
|
||||
GLuint get_uniform_location(const GLchar *name) const;
|
||||
const Executable *build(GLuint count, const GLchar *const names[]);
|
||||
|
||||
private:
|
||||
static const std::string filename(const std::string &name);
|
||||
|
||||
void bind_attrib_location(GLuint index, const GLchar *name);
|
||||
void link();
|
||||
|
||||
GLuint _id;
|
||||
const Shader vertex_shader;
|
||||
const Shader fragment_shader;
|
||||
};
|
||||
|
||||
#endif // _PROGRAM_HPP_
|
||||
|
|
|
@ -3,9 +3,9 @@
|
|||
#include <cstdlib>
|
||||
#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);
|
||||
const long size = ftell(file);
|
||||
|
|
|
@ -3,9 +3,11 @@
|
|||
|
||||
#include "gl.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
struct Shader
|
||||
{
|
||||
Shader(GLenum type, const char *filename);
|
||||
Shader(GLenum type, const std::string &filename);
|
||||
inline GLuint id() const { return _id; };
|
||||
|
||||
private:
|
||||
|
|
Reference in New Issue