KlassischeKeplerKriege/game/glclasses.cpp

222 lines
5.8 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-29 10:28:35 +00:00
#include <fstream>
#include <sstream>
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); }
void VAO::unbind() { glBindVertexArray(0); }
2016-09-15 15:23:02 +00:00
void VAO::fill(GLuint index, GLint size, GLenum type, GLboolean normalized,
GLsizei stride, const GLvoid *pointer) {
glEnableVertexAttribArray(index);
glVertexAttribPointer(index, size, type, normalized, stride, pointer);
}
2016-09-15 15:23:02 +00:00
Shader::Shader() : m_program(0) {}
2016-09-27 18:56:17 +00:00
Shader::~Shader() {}
2016-09-25 20:24:04 +00:00
void Shader::init() { m_program = glCreateProgram(); }
bool Shader::check() {
GLint len = 0;
GLint result = 0;
2016-09-28 22:50:14 +00:00
glGetProgramiv(m_program, GL_LINK_STATUS, &result);
glGetProgramiv(m_program, GL_INFO_LOG_LENGTH, &len);
2016-09-15 15:23:02 +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-27 19:28:26 +00:00
}
// std::cout << "checked program" << std::endl;
2016-09-27 19:28:26 +00:00
return (bool)result;
}
2016-09-29 00:55:04 +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-27 18:56:17 +00:00
if (result == GL_FALSE) {
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
char *error = (char *)malloc(len + 1);
glGetShaderInfoLog(shader, len, NULL, error);
std::string str(error, error + len);
std::cout << str << std::endl;
}
2016-09-25 20:24:04 +00:00
// std::cout << "checked shader" << std::endl;
return result != GL_FALSE;
}
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 &data, 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);
2016-09-28 22:50:14 +00:00
const char *shaderdata = data.c_str();
glShaderSource(shader, 1, &shaderdata, NULL);
glCompileShader(shader);
checkShader(shader);
2016-09-28 22:50:14 +00:00
glAttachShader(m_program, shader);
glLinkProgram(m_program);
check();
glDeleteShader(shader);
}
void Shader::loadFile(const std::string &path, GLenum shadertype) {
2016-09-29 10:28:35 +00:00
std::string content;
std::ifstream fileStream(path, std::ios::in);
if (!fileStream.is_open()) {
std::cerr << "Could not read file " << path << ". File does not exist."
<< std::endl;
2016-09-29 10:28:35 +00:00
return;
}
std::string line = "";
while (!fileStream.eof()) {
2016-09-29 10:28:35 +00:00
std::getline(fileStream, line);
content.append(line + "\n");
}
fileStream.close();
load(content, shadertype);
}
2016-09-29 10:28:35 +00:00
GLuint Shader::location(const std::string &name) {
return glGetUniformLocation(m_program, name.c_str());
}
Texture::Texture() { glGenTextures(1, &m_name); }
Texture::~Texture() { glDeleteTextures(1, &m_name); }
void Texture::bind() { glBindTexture(GL_TEXTURE_2D, m_name); }
void Texture::unbind() { glBindTexture(GL_TEXTURE_2D, 0); }
void Texture::fill(GLenum target, GLint level, GLint internalFormat,
GLsizei width, GLsizei height, GLint border, GLenum format,
GLenum type, const GLvoid *data) {
bind();
glTexImage2D(target, level, internalFormat, width, height, border, format,
type, data);
}
GLuint Texture::getName() { return m_name; }
Framebuffer::Framebuffer() { glGenFramebuffers(1, &m_name); }
Framebuffer::~Framebuffer() { glDeleteFramebuffers(1, &m_name); }
void Framebuffer::bind() { glBindFramebuffer(GL_FRAMEBUFFER, m_name); }
void Framebuffer::unbind() { glBindFramebuffer(GL_FRAMEBUFFER, 0); }
void Framebuffer::attachRenderbuffer(GLenum attachment, GLuint rbo) {
bind();
glFramebufferRenderbuffer(GL_FRAMEBUFFER, attachment, GL_RENDERBUFFER, rbo);
2016-09-28 01:33:01 +00:00
}
void Framebuffer::attachTexture(GLenum attachment, GLuint tex) {
bind();
glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, tex, 0);
}
Renderbuffer::Renderbuffer() { glGenRenderbuffers(1, &m_name); }
Renderbuffer::~Renderbuffer() { glDeleteRenderbuffers(1, &m_name); }
void Renderbuffer::bind() { glBindRenderbuffer(GL_RENDERBUFFER, m_name); }
void Renderbuffer::unbind() { glBindRenderbuffer(GL_RENDERBUFFER, 0); }
void Renderbuffer::create(GLenum internalformat, GLsizei width,
GLsizei height) {
bind();
glRenderbufferStorage(GL_RENDERBUFFER, internalformat, width, height);
}
}
void printGlError(GLenum err) {
if (err != GL_NO_ERROR) {
std::cout << "opengl error is: " << stringFromGlError(err) << std::endl;
}
}
void discardLastGlError(bool print) {
GLenum err = glGetError();
if (print) {
printGlError(err);
}
}
// return false if there's an error
bool checkAndPrintGlError() {
GLenum err = glGetError();
printGlError(err);
return true;
}
const char *stringFromGlError(GLenum err) {
switch (err) {
case GL_INVALID_ENUM:
return "GL_INVALID_ENUM";
break;
case GL_INVALID_VALUE:
return "GL_INVALID_VALUE";
break;
case GL_INVALID_OPERATION:
return "GL_INVALID_OPERATION";
break;
case GL_OUT_OF_MEMORY:
return "GL_OUT_OF_MEMORY";
break;
case GL_INVALID_FRAMEBUFFER_OPERATION:
return "GL_INVALID_FRAMEBUFFER_OPERATION";
break;
default:
return "<unknown>";
break;
}
}