From: Mikko Rasa Date: Tue, 21 Aug 2007 13:51:07 +0000 (+0000) Subject: Add shaders X-Git-Tag: 0.9~51 X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=commitdiff_plain;h=1431f24f29bd6862d547b831c40b2686ff56d1ef Add shaders --- diff --git a/source/program.cpp b/source/program.cpp new file mode 100644 index 00000000..b7f8577d --- /dev/null +++ b/source/program.cpp @@ -0,0 +1,73 @@ +/* $Id$ + +This file is part of libmspgl +Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#define GL_GLEXT_PROTOTYPES +#include "program.h" +#include "shader.h" + +using namespace std; + +namespace Msp { +namespace GL { + +Program::Program() +{ + id=glCreateProgram(); +} + +Program::~Program() +{ + glDeleteProgram(id); +} + +void Program::attach_shader(Shader &shader) +{ + if(find(shaders.begin(), shaders.end(), &shader)==shaders.end()) + { + glAttachShader(id, shader.get_id()); + shaders.push_back(&shader); + } +} + +void Program::detach_shader(Shader &shader) +{ + list::iterator i=remove(shaders.begin(), shaders.end(), &shader); + if(i!=shaders.end()) + { + shaders.erase(i, shaders.end()); + glDetachShader(id, shader.get_id()); + } +} + +bool Program::link() +{ + for(list::iterator i=shaders.begin(); i!=shaders.end(); ++i) + if(!(*i)->get_compiled() && !(*i)->compile()) + return false; + + glLinkProgram(id); + linked=get_param(GL_LINK_STATUS); + return linked; +} + +int Program::get_param(GLenum param) const +{ + int value; + glGetProgramiv(id, param, &value); + return value; +} + +string Program::get_info_log() const +{ + sizei len=get_param(GL_INFO_LOG_LENGTH); + char log[len+1]; + glGetProgramInfoLog(id, len+1, reinterpret_cast(&len), log); + return string(log, len); +} + +} // namespace GL +} // namespace Msp diff --git a/source/program.h b/source/program.h new file mode 100644 index 00000000..682ac99f --- /dev/null +++ b/source/program.h @@ -0,0 +1,47 @@ +/* $Id$ + +This file is part of libmspgl +Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#ifndef MSP_GL_PROGRAM_H_ +#define MSP_GL_PROGRAM_H_ + +#include +#include +#include +#include "shader.h" +#include "types.h" + +namespace Msp { +namespace GL { + +class Shader; + +class Program +{ +public: + Program(); + ~Program(); + + void attach_shader(Shader &shader); + void detach_shader(Shader &shader); + bool link(); + int get_param(GLenum param) const; + std::string get_info_log() const; + void bind(); + + static void unbind(); +private: + uint id; + std::list shaders; + bool linked; + + static Program *cur_prog; +}; + +} // namespace GL +} // namespace Msp + +#endif diff --git a/source/shader.cpp b/source/shader.cpp new file mode 100644 index 00000000..ac604630 --- /dev/null +++ b/source/shader.cpp @@ -0,0 +1,64 @@ +/* $Id$ + +This file is part of libmspgl +Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#define GL_GLEXT_PROTOTYPES +#include "shader.h" + +using namespace std; + +namespace Msp { +namespace GL { + +Shader::Shader(ShaderType t) +{ + id=glCreateShader(t); +} + +Shader::~Shader() +{ + glDeleteShader(id); +} + +void Shader::source(sizei count, const char **str, const int *len) +{ + glShaderSource(id, count, str, len); +} + +void Shader::source(const string &str) +{ + source(str.data(), str.size()); +} + +void Shader::source(const char *str, int len) +{ + source(1, &str, &len); +} + +bool Shader::compile() +{ + glCompileShader(id); + compiled=get_param(GL_COMPILE_STATUS); + return compiled; +} + +int Shader::get_param(GLenum param) const +{ + int value; + glGetShaderiv(id, param, &value); + return value; +} + +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); +} + +} // namespace GL +} // namespace Msp diff --git a/source/shader.h b/source/shader.h new file mode 100644 index 00000000..4e02b084 --- /dev/null +++ b/source/shader.h @@ -0,0 +1,46 @@ +/* $Id$ + +This file is part of libmspgl +Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#ifndef MSP_GL_SHADER_H_ +#define MSP_GL_SHADER_H_ + +#include +#include +#include "types.h" + +namespace Msp { +namespace GL { + +enum ShaderType +{ + FRAGMENT_SHADER = GL_FRAGMENT_SHADER, + VERTEX_SHADER = GL_VERTEX_SHADER +}; + +class Shader +{ +public: + Shader(ShaderType t); + ~Shader(); + + void source(sizei count, const char **str, const int *len); + void source(const std::string &str); + void source(const char *str, int len); + bool compile(); + uint get_id() const { return id; } + bool get_compiled() const { return compiled; } + int get_param(GLenum param) const; + std::string get_info_log() const; +private: + uint id; + bool compiled; +}; + +} // namespace GL +} // namespace Msp + +#endif diff --git a/source/simpleprogram.cpp b/source/simpleprogram.cpp new file mode 100644 index 00000000..b7bc5edc --- /dev/null +++ b/source/simpleprogram.cpp @@ -0,0 +1,33 @@ +/* $Id$ + +This file is part of libmspgl +Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#include "simpleprogram.h" + +using namespace std; + +namespace Msp { +namespace GL { + +SimpleProgram::SimpleProgram(const string &vert_src, const string &frag_src): + vert_shader(VERTEX_SHADER), + frag_shader(FRAGMENT_SHADER) +{ + vert_shader.source(vert_src); + frag_shader.source(frag_src); + attach_shader(vert_shader); + attach_shader(frag_shader); + link(); +} + +SimpleProgram::~SimpleProgram() +{ + detach_shader(vert_shader); + detach_shader(frag_shader); +} + +} // namespace GL +} // namespace Msp diff --git a/source/simpleprogram.h b/source/simpleprogram.h new file mode 100644 index 00000000..aeebb88f --- /dev/null +++ b/source/simpleprogram.h @@ -0,0 +1,30 @@ +/* $Id$ + +This file is part of libmspgl +Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#ifndef MSP_GL_SIMPLEPROGRAM_H_ +#define MSP_GL_SIMPLEPROGRAM_H_ + +#include "program.h" +#include "shader.h" + +namespace Msp { +namespace GL { + +class SimpleProgram: public Program +{ +public: + SimpleProgram(const std::string &vert_src, const std::string &frag_src); + ~SimpleProgram(); +private: + Shader vert_shader; + Shader frag_shader; +}; + +} // namespace GL +} // namespace Msp + +#endif