2016-09-27 17:01:21 +00:00
|
|
|
#include "glclasses.hpp"
|
2016-09-15 15:23:02 +00:00
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
2016-09-15 15:23:02 +00:00
|
|
|
|
2016-09-27 15:57:41 +00:00
|
|
|
endofthejedi::VAO::VAO() { glGenVertexArrays(1, &m_name); }
|
2016-09-15 15:23:02 +00:00
|
|
|
|
2016-09-27 15:57:41 +00:00
|
|
|
endofthejedi::VAO::~VAO() { glDeleteVertexArrays(1, &m_name); }
|
2016-09-15 15:23:02 +00:00
|
|
|
|
2016-09-27 15:57:41 +00:00
|
|
|
void endofthejedi::VAO::bind() { glBindVertexArray(m_name); }
|
2016-09-15 15:23:02 +00:00
|
|
|
|
2016-09-27 15:57:41 +00:00
|
|
|
void endofthejedi::VAO::fill(GLuint index, GLint size, GLenum type,
|
|
|
|
GLboolean normalized, GLsizei stride,
|
|
|
|
const GLvoid *pointer) {
|
2016-09-27 18:56:17 +00:00
|
|
|
glEnableVertexAttribArray(index);
|
|
|
|
glVertexAttribPointer(index, size, GL_FLOAT, normalized, stride, pointer);
|
2016-09-15 15:23:02 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
endofthejedi::Shader::Shader() { m_program = glCreateProgram(); }
|
2016-09-15 15:23:02 +00:00
|
|
|
|
2016-09-27 15:57:41 +00:00
|
|
|
endofthejedi::Shader::~Shader() {}
|
2016-09-15 15:23:02 +00:00
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
bool endofthejedi::Shader::check() {
|
|
|
|
GLint len = 0;
|
|
|
|
GLint result = 0;
|
|
|
|
|
|
|
|
glGetProgramiv(m_program, GL_LINK_STATUS, &result);
|
|
|
|
glGetProgramiv(m_program, GL_INFO_LOG_LENGTH, &len);
|
|
|
|
if (len > 1) {
|
2016-09-27 19:28:26 +00:00
|
|
|
char *error = (char *)malloc(len);
|
2016-09-27 18:56:17 +00:00
|
|
|
glGetProgramInfoLog(m_program, len, NULL, error);
|
|
|
|
std::string str(error);
|
|
|
|
std::cout << str << "\n";
|
|
|
|
}
|
2016-09-27 19:28:26 +00:00
|
|
|
std::cout << "checked program"
|
|
|
|
<< "\n";
|
2016-09-25 20:24:04 +00:00
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
return (bool)result;
|
2016-09-25 20:24:04 +00:00
|
|
|
}
|
2016-09-15 15:23:02 +00:00
|
|
|
|
2016-09-27 19:28:26 +00:00
|
|
|
bool endofthejedi::Shader::checkShader(GLuint shader) {
|
|
|
|
GLint len = 0;
|
|
|
|
GLint result = 0;
|
|
|
|
|
2016-09-28 01:33:01 +00:00
|
|
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &result);
|
|
|
|
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
|
2016-09-27 19:28:26 +00:00
|
|
|
if (len > 1) {
|
2016-09-28 01:33:01 +00:00
|
|
|
char *error = (char *)malloc(len+1);
|
2016-09-27 19:28:26 +00:00
|
|
|
glGetShaderInfoLog(shader, 0, &len, error);
|
2016-09-28 01:33:01 +00:00
|
|
|
std::string str(error, error + len);
|
2016-09-27 19:28:26 +00:00
|
|
|
std::cout << str << "\n";
|
|
|
|
}
|
|
|
|
std::cout << "checked shader"
|
|
|
|
<< "\n";
|
|
|
|
|
|
|
|
return (bool)result;
|
2016-09-25 20:24:04 +00:00
|
|
|
}
|
2016-09-27 18:56:17 +00:00
|
|
|
|
|
|
|
void endofthejedi::Shader::bind() { glUseProgram(m_program); }
|
|
|
|
|
|
|
|
void endofthejedi::Shader::unbind() { glUseProgram(0); }
|
|
|
|
|
2016-09-27 15:57:41 +00:00
|
|
|
void endofthejedi::Shader::load(std::string path, GLenum shadertype) {
|
2016-09-27 18:56:17 +00:00
|
|
|
GLuint cheddar = glCreateShader(shadertype);
|
|
|
|
const char *cheddardata = path.c_str();
|
|
|
|
glShaderSource(cheddar, 1, &cheddardata, NULL);
|
|
|
|
glCompileShader(cheddar);
|
2016-09-27 19:28:26 +00:00
|
|
|
checkShader(cheddar);
|
2016-09-27 18:56:17 +00:00
|
|
|
glAttachShader(m_program, cheddar);
|
|
|
|
glLinkProgram(m_program);
|
|
|
|
check();
|
2016-09-27 19:28:26 +00:00
|
|
|
glDeleteShader(cheddar);
|
2016-09-25 20:24:04 +00:00
|
|
|
}
|
|
|
|
|
2016-09-28 01:33:01 +00:00
|
|
|
GLuint endofthejedi::Shader::location(std::string name) {
|
|
|
|
return glGetUniformLocation(m_program, name.c_str());
|
|
|
|
}
|