]> git.tdb.fi Git - libs/gl.git/blobdiff - source/program.cpp
Generalize VertexBuffer into Buffer with support for other types as well
[libs/gl.git] / source / program.cpp
index f057ea74aefbfe75d4aa447866d6a57a4cf05288..75f982d7f5dc8fa4c2235af999249cd23cae4146 100644 (file)
@@ -5,6 +5,7 @@ Copyright © 2007 Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
+#include <algorithm>
 #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)
@@ -76,6 +78,7 @@ void Program::set_del_shaders(bool ds)
 
 void Program::bind_attribute(uint index, const string &name)
 {
+       static RequireExtension _ext("GL_ARB_vertex_shader");
        glBindAttribLocationARB(id, index, name.c_str());
 }
 
@@ -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,41 +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::uniform4(int i, const float *v)
-{
-       glUniform4fvARB(i, 1, v);
-}
-
-void Program::uniform_matrix4(int i, const float *v)
-{
-       glUniformMatrix4fvARB(i, 1, false, v);
-}*/
-
 void Program::unbind()
 {
        if(cur_prog)
@@ -169,7 +137,7 @@ void Program::maybe_bind()
                bind();
 }
 
-Program *Program::cur_prog=0;
+const Program *Program::cur_prog=0;
 
 
 Program::Loader::Loader(Program &p):
@@ -182,11 +150,6 @@ Program::Loader::Loader(Program &p):
        add("attribute",       &Loader::attribute);
 }
 
-Program::Loader::~Loader()
-{
-       prog.link();
-}
-
 void Program::Loader::vertex_shader(const string &src)
 {
        prog.attach_shader(*new Shader(VERTEX_SHADER, src));
@@ -202,5 +165,10 @@ void Program::Loader::attribute(uint i, const string &n)
        prog.bind_attribute(i, n);
 }
 
+void Program::Loader::finish()
+{
+       prog.link();
+}
+
 } // namespace GL
 } // namespace Msp