1
0
Fork 0

Add shader program structure

This commit is contained in:
Meoweg 2015-11-09 15:06:34 +00:00
parent a0f76d35d7
commit 29c71cff88
6 changed files with 28 additions and 40 deletions

View File

@ -0,0 +1,3 @@
position
tex_coord
normal

View File

@ -11,8 +11,6 @@
#include <emscripten/emscripten.h>
#include <emscripten/html5.h>
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'])

View File

@ -1,23 +1,34 @@
#include "program.hpp"
Program::Program()
#include "shader.hpp"
#include <string>
#include <fstream>
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

View File

@ -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;