X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Fshader.cpp;fp=source%2Fcore%2Fshader.cpp;h=02dcc0dcff789fe142f9b47d4bde5ad2e48fe7fa;hb=7aaec9a70b8d7733429bec043f8e33e02956f266;hp=0000000000000000000000000000000000000000;hpb=bec07999d95b76f4b47cffcc564d0cd0afc0435e;p=libs%2Fgl.git diff --git a/source/core/shader.cpp b/source/core/shader.cpp new file mode 100644 index 00000000..02dcc0dc --- /dev/null +++ b/source/core/shader.cpp @@ -0,0 +1,114 @@ +#include +#include +#include +#include +#include +#include "error.h" +#include "misc.h" +#include "shader.h" + +using namespace std; + +namespace Msp { +namespace GL { + +Shader::Shader(GLenum t) +{ + init(t); +} + +Shader::Shader(GLenum t, const string &src) +{ + 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() +{ + glDeleteShader(id); +} + +void Shader::source(unsigned 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); +} + +void Shader::compile() +{ + glCompileShader(id); + compiled = get_shader_i(id, GL_COMPILE_STATUS); + if(!compiled) + throw compile_error(get_info_log()); + +#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 +{ + 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