summaryrefslogtreecommitdiff
path: root/engine/shader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'engine/shader.cpp')
-rw-r--r--engine/shader.cpp76
1 files changed, 39 insertions, 37 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");
}