X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fprogram.cpp;h=75f982d7f5dc8fa4c2235af999249cd23cae4146;hb=76e338af116120d93d082ad247591ec9adad9233;hp=f34133faf767b1e905f7706e1caae938f606c6a4;hpb=5318aa4fd553be4ce0bc428e73592b787842cdea;p=libs%2Fgl.git diff --git a/source/program.cpp b/source/program.cpp index f34133fa..75f982d7 100644 --- a/source/program.cpp +++ b/source/program.cpp @@ -5,6 +5,7 @@ Copyright © 2007 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ +#include #include "arb_shader_objects.h" #include "arb_vertex_shader.h" #include "except.h" @@ -18,28 +19,29 @@ namespace Msp { namespace GL { Program::Program(): - del_shaders(false), - linked(false) + del_shaders(false) { - require_extension("GL_ARB_shader_objects"); - require_extension("GL_ARB_vertex_shader"); - - id=glCreateProgramObjectARB(); + init(); } Program::Program(const string &vert, const string &frag): - del_shaders(true), - linked(false) + del_shaders(true) { - require_extension("GL_ARB_shader_objects"); - require_extension("GL_ARB_vertex_shader"); + init(); - id=glCreateProgramObjectARB(); attach_shader(*new Shader(VERTEX_SHADER, vert)); attach_shader(*new Shader(FRAGMENT_SHADER, frag)); link(); } +void Program::init() +{ + static RequireExtension _ext("GL_ARB_shader_objects"); + + linked=false; + id=glCreateProgramObjectARB(); +} + Program::~Program() { if(del_shaders) @@ -74,20 +76,21 @@ void Program::set_del_shaders(bool ds) del_shaders=ds; } -void Program::bind_attribute(int index, const string &name) +void Program::bind_attribute(uint index, const string &name) { + static RequireExtension _ext("GL_ARB_vertex_shader"); glBindAttribLocationARB(id, index, name.c_str()); } -bool Program::link() +void Program::link() { for(list::iterator i=shaders.begin(); i!=shaders.end(); ++i) - if(!(*i)->get_compiled() && !(*i)->compile()) - return false; + if(!(*i)->get_compiled()) + (*i)->compile(); glLinkProgramARB(id); - linked=get_param(GL_LINK_STATUS); - return linked; + if(!(linked=get_param(GL_LINK_STATUS))) + throw CompileError(get_info_log()); } int Program::get_param(GLenum param) const @@ -105,7 +108,7 @@ string Program::get_info_log() const return string(log, len); } -void Program::bind() +void Program::bind() const { if(!linked) throw InvalidState("Program is not linked"); @@ -119,36 +122,6 @@ int Program::get_uniform_location(const string &n) const return glGetUniformLocationARB(id, n.c_str()); } -void Program::uniform(int i, int v) -{ - glUniform1iARB(i, v); -} - -void Program::uniform(int i, float x) -{ - glUniform1fARB(i, x); -} - -void Program::uniform(int i, float x, float y) -{ - glUniform2fARB(i, x, y); -} - -void Program::uniform(int i, float x, float y, float z) -{ - glUniform3fARB(i, x, y, z); -} - -void Program::uniform(int i, float x, float y, float z, float w) -{ - glUniform4fARB(i, x, y, z, w); -} - -void Program::uniform_matrix4(int i, const float *v) -{ - glUniformMatrix4fvARB(i, 1, false, v); -} - void Program::unbind() { if(cur_prog) @@ -164,7 +137,7 @@ void Program::maybe_bind() bind(); } -Program *Program::cur_prog=0; +const Program *Program::cur_prog=0; Program::Loader::Loader(Program &p): @@ -174,11 +147,7 @@ Program::Loader::Loader(Program &p): add("vertex_shader", &Loader::vertex_shader); add("fragment_shader", &Loader::fragment_shader); -} - -Program::Loader::~Loader() -{ - prog.link(); + add("attribute", &Loader::attribute); } void Program::Loader::vertex_shader(const string &src) @@ -191,5 +160,15 @@ void Program::Loader::fragment_shader(const string &src) prog.attach_shader(*new Shader(FRAGMENT_SHADER, src)); } +void Program::Loader::attribute(uint i, const string &n) +{ + prog.bind_attribute(i, n); +} + +void Program::Loader::finish() +{ + prog.link(); +} + } // namespace GL } // namespace Msp