diff options
author | Vegard Storheil Eriksen <zyp@jvnv.net> | 2010-05-24 16:17:18 +0200 |
---|---|---|
committer | Vegard Storheil Eriksen <zyp@jvnv.net> | 2010-05-24 16:17:18 +0200 |
commit | 0de601984c8af1d88affe1ac88d47cc9fe12de75 (patch) | |
tree | e0cfabeced7de96ee4a77a7b541529dcb542fdaa /engine | |
parent | 5c21b67a04bd847d7f04042a0ddc0bc4629d20e7 (diff) | |
parent | 42de6197a6b29ef1d922c82e5bab263f1348ec03 (diff) |
Merge branch 'master' of ssh://git.jvnv.net/srv/git/gardiner
Diffstat (limited to 'engine')
-rw-r--r-- | engine/shader.cpp | 76 | ||||
-rw-r--r-- | engine/shader.h | 18 |
2 files changed, 47 insertions, 47 deletions
diff --git a/engine/shader.cpp b/engine/shader.cpp index 04aa031..a0509ff 100644 --- a/engine/shader.cpp +++ b/engine/shader.cpp @@ -1,11 +1,21 @@ #include <fstream> +#include <stdexcept> + +#include <boost/format.hpp> + #include "shader.h" -bool GLBaseShader::shader_source(const char *filename) { +static void gl_check_error(const char *msg) { + GLenum error = glGetError(); + if(error != GL_NO_ERROR) { + throw(std::runtime_error((boost::format("%s:\n%s") % msg % gluErrorString(error)).str())); + } +} + +void GLBaseShader::shader_source(const char *filename) { std::ifstream inf(filename, std::ios_base::in); if(!inf.is_open()) { - std::cerr << "Failed to load shader " << filename << std::endl; - return false; + throw(std::runtime_error((boost::format("Failed to load shader from file %s") % filename).str())); } inf.seekg(0, std::ios_base::end); int length = inf.tellg(); @@ -15,77 +25,69 @@ bool GLBaseShader::shader_source(const char *filename) { inf.close(); glShaderSource(shader, 1, (const GLchar**)&buffer, &length); - print_check_ogl_error(); + gl_check_error("Failed to set shader source"); delete[] buffer; glCompileShader(shader); - print_check_ogl_error(); int p; glGetShaderiv(shader, GL_COMPILE_STATUS, &p); if(p == 0) { - std::cerr << "Failed to compile shader:" << std::endl; char log[0xffff]; int size; glGetShaderInfoLog(shader, 0xffff, &size, (GLchar*)&log); - std::cerr << log << std::endl; + throw(std::runtime_error((boost::format("Failed to compile shader:\n%s") % log).str())); } - return shader; } -bool GLBaseShader::shader_source(std::string& filename) { - return shader_source(filename.c_str()); +void GLBaseShader::shader_source(std::string& filename) { + shader_source(filename.c_str()); } GLBaseShader::GLBaseShader(GLenum type) { shader = glCreateShader(type); } +GLBaseShader::~GLBaseShader() { + glDeleteShader(shader); +} + +/** + * GLShaderProgram + */ GLShaderProgram::GLShaderProgram() { program = glCreateProgram(); } -bool GLShaderProgram::attach(GLBaseShader& shader) { +GLShaderProgram::~GLShaderProgram() { + glDeleteProgram(program); +} + +void GLShaderProgram::attach(GLBaseShader& shader) { glAttachShader(program, shader.shader); - return !print_check_ogl_error(); + + gl_check_error("Failed to attach shader to program"); } -bool GLShaderProgram::detach(GLBaseShader& shader) { +void GLShaderProgram::detach(GLBaseShader& shader) { glDetachShader(program, shader.shader); - return !print_check_ogl_error(); + + gl_check_error("Failed to detach shader from program"); } -bool GLShaderProgram::link() { +void GLShaderProgram::link() { glLinkProgram(program); - bool error = print_check_ogl_error(); + int p; glGetProgramiv(program, GL_LINK_STATUS, &p); if(p == 0) { - std::cerr << "Failed to link program:" << std::endl; char log[0xffff]; int size; glGetProgramInfoLog(program, 0xffff, &size, (GLchar*)&log); - printf(log); + throw(std::runtime_error((boost::format("Failed to link program:\n%s") % log).str())); } - return !error && p; } -bool GLShaderProgram::use() { - if(!glIsProgram(program)) program = glCreateProgram(); +void GLShaderProgram::use() { glUseProgram(program); - return !print_check_ogl_error(); -} - -void GLShaderProgram::remove() { - glDeleteProgram(program); -} -void print_ogl_error(GLenum error) { - unsigned char *buf = (unsigned char*)gluErrorString(error); - std::cerr << "OpenGL: " << buf << std::endl; -} - -bool print_check_ogl_error() { - GLenum error = glGetError(); - if(error != GL_NO_ERROR) - print_ogl_error(error); - return error != GL_NO_ERROR; + gl_check_error("Failed to use program"); } diff --git a/engine/shader.h b/engine/shader.h index 012165a..d4ad263 100644 --- a/engine/shader.h +++ b/engine/shader.h @@ -16,11 +16,12 @@ class GLBaseShader { friend class GLShaderProgram; protected: - bool shader_source(const char *filename); - bool shader_source(std::string& filename); + void shader_source(const char *filename); + void shader_source(std::string& filename); unsigned int shader; public: GLBaseShader(GLenum); + ~GLBaseShader(); }; class GLVertexShader : public GLBaseShader { @@ -42,15 +43,12 @@ class GLShaderProgram { unsigned int program; public: GLShaderProgram(); + ~GLShaderProgram(); - bool attach(GLBaseShader&); - bool detach(GLBaseShader&); - bool link(); - bool use(); - void remove(); + void attach(GLBaseShader&); + void detach(GLBaseShader&); + void link(); + void use(); }; -void print_ogl_error(GLenum); -bool print_check_ogl_error(); - #endif |