X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fshader.cpp;h=02dcc0dcff789fe142f9b47d4bde5ad2e48fe7fa;hp=ac604630904a6c8db9632dff8f8e715976f2d0cf;hb=bec07999d95b76f4b47cffcc564d0cd0afc0435e;hpb=1431f24f29bd6862d547b831c40b2686ff56d1ef diff --git a/source/shader.cpp b/source/shader.cpp index ac604630..02dcc0dc 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,32 @@ 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) +{ + static Require _req_base(ARB_shader_objects); + 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 +45,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 +60,55 @@ 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); + string log(len+1, 0); + glGetShaderInfoLog(id, len+1, &len, &log[0]); + log.erase(len); + 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