2015-11-07 14:11:23 +00:00
|
|
|
#include "program.hpp"
|
|
|
|
|
2015-11-09 15:06:34 +00:00
|
|
|
#include "shader.hpp"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
Program::Program(const char *const name)
|
2015-11-07 14:11:23 +00:00
|
|
|
{
|
2015-11-09 15:06:34 +00:00
|
|
|
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());
|
|
|
|
|
2015-11-07 14:11:23 +00:00
|
|
|
_id = glCreateProgram();
|
|
|
|
|
2015-11-09 15:06:34 +00:00
|
|
|
glAttachShader(_id, vertex_shader.id());
|
|
|
|
glAttachShader(_id, fragment_shader.id());
|
2015-11-07 14:11:23 +00:00
|
|
|
|
2015-11-09 15:06:34 +00:00
|
|
|
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());
|
2015-11-07 14:11:23 +00:00
|
|
|
|
|
|
|
glLinkProgram(_id);
|
2015-11-09 15:06:34 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < index; ++i)
|
|
|
|
glEnableVertexAttribArray(i);
|
2015-11-07 14:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Program::use() const
|
|
|
|
{
|
|
|
|
glUseProgram(_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint Program::get_uniform_location(const GLchar *name) const
|
|
|
|
{
|
|
|
|
return glGetUniformLocation(_id, name);
|
|
|
|
}
|