+ added glclasses for texture, fbo & rbo

This commit is contained in:
end 2016-10-02 11:25:53 +02:00
parent 0b71aad1f7
commit 415465df71
2 changed files with 203 additions and 106 deletions

View file

@ -6,28 +6,26 @@
#include <sstream> #include <sstream>
namespace endofthejedi { namespace endofthejedi {
VAO::VAO() { glGenVertexArrays(1, &m_name); } VAO::VAO() { glGenVertexArrays(1, &m_name); }
VAO::~VAO() { glDeleteVertexArrays(1, &m_name); } VAO::~VAO() { glDeleteVertexArrays(1, &m_name); }
void VAO::bind() { glBindVertexArray(m_name); } void VAO::bind() { glBindVertexArray(m_name); }
void VAO::unbind() { glBindVertexArray(0); } void VAO::unbind() { glBindVertexArray(0); }
void VAO::fill(GLuint index, GLint size, GLenum type, void VAO::fill(GLuint index, GLint size, GLenum type, GLboolean normalized,
GLboolean normalized, GLsizei stride, GLsizei stride, const GLvoid *pointer) {
const GLvoid *pointer)
{
glEnableVertexAttribArray(index); glEnableVertexAttribArray(index);
glVertexAttribPointer(index, size, type, normalized, stride, pointer); glVertexAttribPointer(index, size, type, normalized, stride, pointer);
} }
Shader::Shader() : m_program(0) { } Shader::Shader() : m_program(0) {}
Shader::~Shader() {} Shader::~Shader() {}
void Shader::init() { m_program = glCreateProgram(); } void Shader::init() { m_program = glCreateProgram(); }
bool Shader::check() { bool Shader::check() {
GLint len = 0; GLint len = 0;
GLint result = 0; GLint result = 0;
@ -35,18 +33,18 @@ namespace endofthejedi {
glGetProgramiv(m_program, GL_INFO_LOG_LENGTH, &len); glGetProgramiv(m_program, GL_INFO_LOG_LENGTH, &len);
if (result == GL_FALSE) { if (result == GL_FALSE) {
std::cout<<"getting error log:" << std::endl; std::cout << "getting error log:" << std::endl;
char *error = (char *)malloc(len+1); char *error = (char *)malloc(len + 1);
glGetProgramInfoLog(m_program, len, NULL, error); glGetProgramInfoLog(m_program, len, NULL, error);
std::string str(error); std::string str(error);
std::cout << str << std::endl; std::cout << str << std::endl;
} }
//std::cout << "checked program" << std::endl; // std::cout << "checked program" << std::endl;
return (bool)result; return (bool)result;
} }
bool Shader::checkShader(GLuint shader) { bool Shader::checkShader(GLuint shader) {
GLint len = 0; GLint len = 0;
GLint result = 0; GLint result = 0;
@ -54,19 +52,18 @@ namespace endofthejedi {
if (result == GL_FALSE) { if (result == GL_FALSE) {
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len); glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
char *error = (char *)malloc(len+1); char *error = (char *)malloc(len + 1);
glGetShaderInfoLog(shader, len, NULL, error); glGetShaderInfoLog(shader, len, NULL, error);
std::string str(error, error + len); std::string str(error, error + len);
std::cout << str << std::endl; std::cout << str << std::endl;
} }
//std::cout << "checked shader" << std::endl; // std::cout << "checked shader" << std::endl;
return result != GL_FALSE; return result != GL_FALSE;
} }
void Shader::bind() void Shader::bind() {
{
if (m_program == 0) { if (m_program == 0) {
std::cerr << "error: invalid to bind invalid program (0)! " std::cerr << "error: invalid to bind invalid program (0)! "
"use unbind() if that was your purpose!" << std::endl; "use unbind() if that was your purpose!" << std::endl;
@ -76,15 +73,16 @@ namespace endofthejedi {
} }
glUseProgram(m_program); glUseProgram(m_program);
} }
GLuint Shader::program() { return m_program; } GLuint Shader::program() { return m_program; }
void Shader::unbind() { glUseProgram(0); } void Shader::unbind() { glUseProgram(0); }
void Shader::load(const std::string &data, GLenum shadertype) { void Shader::load(const std::string &data, GLenum shadertype) {
if (m_program == 0) { if (m_program == 0) {
std::cout<<"[shader] error: shader program is invalid (0)!" << std::endl; std::cout << "[shader] error: shader program is invalid (0)!"
<< std::endl;
exit(-1); exit(-1);
return; return;
} }
@ -101,41 +99,89 @@ namespace endofthejedi {
check(); check();
glDeleteShader(shader); glDeleteShader(shader);
} }
void Shader::loadFile(const std::string &path, GLenum shadertype) { void Shader::loadFile(const std::string &path, GLenum shadertype) {
std::string content; std::string content;
std::ifstream fileStream(path, std::ios::in); std::ifstream fileStream(path, std::ios::in);
if(!fileStream.is_open()) { if (!fileStream.is_open()) {
std::cerr << "Could not read file " << path << ". File does not exist." << std::endl; std::cerr << "Could not read file " << path << ". File does not exist."
<< std::endl;
return; return;
} }
std::string line = ""; std::string line = "";
while(!fileStream.eof()) { while (!fileStream.eof()) {
std::getline(fileStream, line); std::getline(fileStream, line);
content.append(line + "\n"); content.append(line + "\n");
} }
fileStream.close(); fileStream.close();
load(content, shadertype); load(content, shadertype);
}
GLuint Shader::location(const std::string &name) {
return glGetUniformLocation(m_program, name.c_str());
}
} }
void printGlError(GLenum err) 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);
}
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) { if (err != GL_NO_ERROR) {
std::cout << "opengl error is: " << stringFromGlError(err) << std::endl; std::cout << "opengl error is: " << stringFromGlError(err) << std::endl;
} }
} }
void discardLastGlError(bool print) void discardLastGlError(bool print) {
{
GLenum err = glGetError(); GLenum err = glGetError();
if (print) { if (print) {
printGlError(err); printGlError(err);
@ -143,8 +189,7 @@ void discardLastGlError(bool print)
} }
// return false if there's an error // return false if there's an error
bool checkAndPrintGlError() bool checkAndPrintGlError() {
{
GLenum err = glGetError(); GLenum err = glGetError();
printGlError(err); printGlError(err);
@ -152,15 +197,25 @@ bool checkAndPrintGlError()
return true; return true;
} }
const char *stringFromGlError(GLenum err) const char *stringFromGlError(GLenum err) {
{ switch (err) {
switch(err) { case GL_INVALID_ENUM:
case GL_INVALID_ENUM: return "GL_INVALID_ENUM"; break; return "GL_INVALID_ENUM";
case GL_INVALID_VALUE: return "GL_INVALID_VALUE"; break; break;
case GL_INVALID_OPERATION: return "GL_INVALID_OPERATION"; break; case GL_INVALID_VALUE:
case GL_OUT_OF_MEMORY: return "GL_OUT_OF_MEMORY"; break; return "GL_INVALID_VALUE";
case GL_INVALID_FRAMEBUFFER_OPERATION: return "GL_INVALID_FRAMEBUFFER_OPERATION"; break; break;
default: return "<unknown>"; 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;
} }
} }

View file

@ -57,10 +57,53 @@ class Shader {
void bind(); void bind();
void unbind(); void unbind();
void load(const std::string &data, GLenum shadertype); void load(const std::string &data, GLenum shadertype);
void loadFile(const std::string& data, GLenum shadertype); void loadFile(const std::string &data, GLenum shadertype);
GLuint location(const std::string &name); GLuint location(const std::string &name);
GLuint program(); GLuint program();
}; };
class Texture {
private:
GLuint m_name;
protected:
public:
Texture();
~Texture();
void bind();
void unbind();
void fill(GLenum target, GLint level, GLint internalFormat, GLsizei width,
GLsizei height, GLint border, GLenum format, GLenum type,
const GLvoid *data);
GLuint getName();
};
class Framebuffer {
private:
GLuint m_name;
protected:
public:
Framebuffer();
~Framebuffer();
void bind();
void unbind();
void attachRenderbuffer(GLenum attachment, GLuint rbo);
void attachTexture(GLenum attachment, GLuint tex);
};
class Renderbuffer {
private:
GLuint m_name;
protected:
public:
Renderbuffer();
~Renderbuffer();
void bind();
void unbind();
void create(GLenum internalformat, GLsizei width, GLsizei height);
};
} }
#define TBufferObject_(pre, post) \ #define TBufferObject_(pre, post) \
@ -94,7 +137,7 @@ TBufferObject(void, unmap)() {
// todo // todo
} }
void discardLastGlError(bool print=false); void discardLastGlError(bool print = false);
// return false if there's an error // return false if there's an error
bool checkAndPrintGlError(); bool checkAndPrintGlError();
@ -102,4 +145,3 @@ bool checkAndPrintGlError();
void printGlError(GLenum err); void printGlError(GLenum err);
const char *stringFromGlError(GLenum err); const char *stringFromGlError(GLenum err);