KlassischeKeplerKriege/game/glclasses.cpp

108 lines
2.9 KiB
C++
Raw Normal View History

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
namespace endofthejedi {
VAO::VAO() { glGenVertexArrays(1, &m_name); }
2016-09-15 15:23:02 +00:00
VAO::~VAO() { glDeleteVertexArrays(1, &m_name); }
2016-09-15 15:23:02 +00:00
void VAO::bind() { glBindVertexArray(m_name); }
2016-09-15 15:23:02 +00:00
void VAO::fill(GLuint index, GLint size, GLenum type,
GLboolean normalized, GLsizei stride,
const GLvoid *pointer)
{
(void) type;
2016-09-15 15:23:02 +00:00
glEnableVertexAttribArray(index);
glVertexAttribPointer(index, size, GL_FLOAT, normalized, stride, pointer);
}
2016-09-15 15:23:02 +00:00
Shader::Shader() : m_program(0) { }
2016-09-15 15:23:02 +00:00
Shader::~Shader() {}
2016-09-27 18:56:17 +00:00
void Shader::init() { m_program = glCreateProgram(); }
2016-09-25 20:24:04 +00:00
bool Shader::check() {
GLint len = 0;
GLint result = 0;
glGetProgramiv(m_program, GL_LINK_STATUS, &result);
glGetProgramiv(m_program, GL_INFO_LOG_LENGTH, &len);
2016-09-28 22:50:14 +00:00
if (result == GL_FALSE) {
std::cout<<"getting error log:" << std::endl;
char *error = (char *)malloc(len+1);
glGetProgramInfoLog(m_program, len, NULL, error);
std::string str(error);
std::cout << str << std::endl;
}
2016-09-28 22:50:14 +00:00
//std::cout << "checked program" << std::endl;
2016-09-15 15:23:02 +00:00
return (bool)result;
2016-09-27 19:28:26 +00:00
}
bool Shader::checkShader(GLuint shader) {
GLint len = 0;
GLint result = 0;
2016-09-27 18:56:17 +00:00
glGetShaderiv(shader, GL_COMPILE_STATUS, &result);
2016-09-28 22:50:14 +00:00
if (result == GL_FALSE) {
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
char *error = (char *)malloc(len+1);
2016-09-28 22:50:14 +00:00
glGetShaderInfoLog(shader, len, NULL, error);
std::string str(error, error + len);
std::cout << str << std::endl;
}
2016-09-27 18:56:17 +00:00
2016-09-28 22:50:14 +00:00
//std::cout << "checked shader" << std::endl;
2016-09-27 18:56:17 +00:00
2016-09-28 22:50:14 +00:00
return result != GL_FALSE;
}
2016-09-25 20:24:04 +00:00
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);
}
2016-09-28 22:50:14 +00:00
GLuint Shader::program() { return 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;
}
2016-09-28 22:50:14 +00:00
GLuint shader = glCreateShader(shadertype);
const char *shaderdata = path.c_str();
glShaderSource(shader, 1, &shaderdata, NULL);
glCompileShader(shader);
checkShader(shader);
glAttachShader(m_program, shader);
glLinkProgram(m_program);
check();
2016-09-28 22:50:14 +00:00
glDeleteShader(shader);
}
GLuint Shader::location(const std::string &name) {
return glGetUniformLocation(m_program, name.c_str());
}
2016-09-28 01:33:01 +00:00
}