X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fshader.cpp;h=0c356f5f460826f7e97877881d15f35ba3db36f0;hp=ac604630904a6c8db9632dff8f8e715976f2d0cf;hb=0cacf19c2e6aaa182ae0fcc7dfaae345aedd0e74;hpb=1431f24f29bd6862d547b831c40b2686ff56d1ef diff --git a/source/shader.cpp b/source/shader.cpp index ac604630..0c356f5f 100644 --- a/source/shader.cpp +++ b/source/shader.cpp @@ -1,11 +1,10 @@ -/* $Id$ - -This file is part of libmspgl -Copyright © 2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - -#define GL_GLEXT_PROTOTYPES +#include +#include +#include +#include +#include +#include "error.h" +#include "misc.h" #include "shader.h" using namespace std; @@ -13,9 +12,31 @@ using namespace std; namespace Msp { namespace GL { -Shader::Shader(ShaderType t) +Shader::Shader(GLenum t) +{ + init(t); +} + +Shader::Shader(GLenum t, const string &src) { - id=glCreateShader(t); + init(t); + + source(src); + compile(); +} + +void Shader::init(GLenum t) +{ + compiled = false; + + if(t==GL_FRAGMENT_SHADER) + static Require _req(ARB_fragment_shader); + else if(t==GL_VERTEX_SHADER) + static Require _req(ARB_vertex_shader); + else if(t==GL_GEOMETRY_SHADER) + static Require _req(ARB_geometry_shader4); + + id = glCreateShader(t); } Shader::~Shader() @@ -23,7 +44,7 @@ Shader::~Shader() glDeleteShader(id); } -void Shader::source(sizei count, const char **str, const int *len) +void Shader::source(unsigned count, const char **str, const int *len) { glShaderSource(id, count, str, len); } @@ -38,27 +59,56 @@ void Shader::source(const char *str, int len) source(1, &str, &len); } -bool Shader::compile() +void Shader::compile() { glCompileShader(id); - compiled=get_param(GL_COMPILE_STATUS); - return compiled; -} + compiled = get_shader_i(id, GL_COMPILE_STATUS); + if(!compiled) + throw compile_error(get_info_log()); -int Shader::get_param(GLenum param) const -{ - int value; - glGetShaderiv(id, param, &value); - return value; +#ifdef DEBUG + string info_log = get_info_log(); + if(!info_log.empty()) + IO::print("Shader compile info log:\n%s", info_log); +#endif } string Shader::get_info_log() const { - sizei len=get_param(GL_INFO_LOG_LENGTH); - char log[len+1]; - glGetShaderInfoLog(id, len+1, reinterpret_cast(&len), log); - return string(log, len); + GLsizei len = get_shader_i(id, GL_INFO_LOG_LENGTH); + char *buf = new char[len+1]; + glGetShaderInfoLog(id, len+1, &len, buf); + string log(buf, len); + delete[] buf; + return log; } + +VertexShader::VertexShader(): + Shader(GL_VERTEX_SHADER) +{ } + +VertexShader::VertexShader(const string &src): + Shader(GL_VERTEX_SHADER, src) +{ } + + +FragmentShader::FragmentShader(): + Shader(GL_FRAGMENT_SHADER) +{ } + +FragmentShader::FragmentShader(const string &src): + Shader(GL_FRAGMENT_SHADER, src) +{ } + + +GeometryShader::GeometryShader(): + Shader(GL_GEOMETRY_SHADER) +{ } + +GeometryShader::GeometryShader(const string &src): + Shader(GL_GEOMETRY_SHADER, src) +{ } + } // namespace GL } // namespace Msp