Distributed under the LGPL
*/
+#include <algorithm>
#include "arb_shader_objects.h"
#include "arb_vertex_shader.h"
#include "except.h"
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)
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<Shader *>::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
return string(log, len);
}
-void Program::bind()
+void Program::bind() const
{
if(!linked)
throw InvalidState("Program is not linked");
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)
bind();
}
-Program *Program::cur_prog=0;
+const Program *Program::cur_prog=0;
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)
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