KlassischeKeplerKriege/game/glclasses.cpp

100 lines
2.7 KiB
C++

#include "glclasses.hpp"
#include <iostream>
#include <vector>
namespace endofthejedi {
VAO::VAO() { glGenVertexArrays(1, &m_name); }
VAO::~VAO() { glDeleteVertexArrays(1, &m_name); }
void VAO::bind() { glBindVertexArray(m_name); }
void VAO::fill(GLuint index, GLint size, GLenum type,
GLboolean normalized, GLsizei stride,
const GLvoid *pointer)
{
(void) type;
glEnableVertexAttribArray(index);
glVertexAttribPointer(index, size, GL_FLOAT, normalized, stride, pointer);
}
Shader::Shader() : m_program(0) { }
Shader::~Shader() {}
void Shader::init() { m_program = glCreateProgram(); }
bool 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) {
char *error = (char *)malloc(len);
glGetProgramInfoLog(m_program, len, NULL, error);
std::string str(error);
std::cout << str << std::endl;
}
std::cout << "checked program" << std::endl;
return (bool)result;
}
bool Shader::checkShader(GLuint shader) {
GLint len = 0;
GLint result = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &result);
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
if (len > 1) {
char *error = (char *)malloc(len+1);
glGetShaderInfoLog(shader, 0, &len, error);
std::string str(error, error + len);
std::cout << str << std::endl;
}
std::cout << "checked shader" << std::endl;
return (bool)result;
}
void Shader::bind()
{
if (m_program == 0) {
std::cerr << "error: invalid to bind invalid program (0)! "
"use unbind() if that was your purpose!" << std::endl;
exit(-1);
return;
}
glUseProgram(m_program);
}
void Shader::unbind() { glUseProgram(0); }
void Shader::load(const std::string &path, GLenum shadertype) {
if (m_program == 0) {
std::cout<<"[shader] error: shader program is invalid (0)!" << std::endl;
exit(-1);
return;
}
GLuint cheddar = glCreateShader(shadertype);
const char *cheddardata = path.c_str();
glShaderSource(cheddar, 1, &cheddardata, NULL);
glCompileShader(cheddar);
checkShader(cheddar);
glAttachShader(m_program, cheddar);
glLinkProgram(m_program);
check();
glDeleteShader(cheddar);
}
GLuint Shader::location(const std::string &name) {
return glGetUniformLocation(m_program, name.c_str());
}
}