-/* $Id$
-
-This file is part of libmspgl
-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"
+#include "error.h"
#include "extension.h"
#include "program.h"
#include "shader.h"
"l!n", "varying vec3 v_normal;\n",
"l", "varying vec3 v_light_dir;\n",
"p", "varying vec3 v_eye_dir;\n",
+ "r", "vec4 transform_vertex(vec4);\n",
+ "lr", "vec3 transform_normal(vec3);\n",
0, "void main()\n",
0, "{\n",
- 0, "\tvec4 eye_pos = gl_ModelViewMatrix*gl_Vertex;\n",
+ "r", "\tvec4 eye_pos = transform_vertex(gl_Vertex);\n",
+ "!r", "\tvec4 eye_pos = gl_ModelViewMatrix*gl_Vertex;\n",
0, "\tgl_Position = gl_ProjectionMatrix*eye_pos;\n",
- "l", "\tvec3 eye_normal = gl_NormalMatrix*gl_Normal;\n",
+ "lr", "\tvec3 eye_normal = transform_normal(gl_Normal);\n",
+ "l!r", "\tvec3 eye_normal = gl_NormalMatrix*gl_Normal;\n",
"l!n", "\tv_normal = eye_normal;\n",
"n", "\tvec3 eye_tangent = gl_NormalMatrix*tangent;\n",
"n", "\tvec3 eye_binormal = gl_NormalMatrix*binormal;\n",
void Program::link()
{
for(list<Shader *>::iterator i=shaders.begin(); i!=shaders.end(); ++i)
- if(!(*i)->get_compiled())
+ if(!(*i)->is_compiled())
(*i)->compile();
- glLinkProgramARB(id);
- if(!(linked = get_param(GL_LINK_STATUS)))
- throw CompileError(get_info_log());
-}
+ uniforms.clear();
-int Program::get_param(GLenum param) const
-{
+ glLinkProgramARB(id);
int value;
- glGetObjectParameterivARB(id, param, &value);
- return value;
+ glGetObjectParameterivARB(id, GL_OBJECT_LINK_STATUS_ARB, &value);
+ if(!(linked = value))
+ throw compile_error(get_info_log());
+
+ glGetObjectParameterivARB(id, GL_OBJECT_ACTIVE_UNIFORMS_ARB, &value);
+ for(int i=0; i<value; ++i)
+ {
+ UniformInfo info;
+ char name[128];
+ int len = 0;
+ glGetActiveUniformARB(id, i, 128, &len, &info.size, &info.type, name);
+ if(len)
+ {
+ info.name = name;
+ info.location = glGetUniformLocationARB(id, name);
+ uniforms[name] = info;
+ }
+ }
}
string Program::get_info_log() const
{
- GLsizei len = get_param(GL_INFO_LOG_LENGTH);
+ GLsizei len = 0;
+ glGetObjectParameterivARB(id, GL_OBJECT_INFO_LOG_LENGTH_ARB, &len);
char log[len+1];
glGetInfoLogARB(id, len+1, &len, log);
return string(log, len);
void Program::bind() const
{
if(!linked)
- throw InvalidState("Program is not linked");
+ throw invalid_operation("Program::bind");
if(!set_current(this))
return;
int Program::get_uniform_location(const string &n) const
{
- return glGetUniformLocationARB(id, n.c_str());
+ map<string, UniformInfo>::const_iterator i = uniforms.find(n);
+ if(i==uniforms.end())
+ return -1;
+
+ return i->second.location;
}
void Program::unbind()
lighting(false),
specular(false),
normalmap(false),
- shadow(false)
+ shadow(false),
+ transform(false)
{ }
string Program::StandardFeatures::create_flags() const
}
if(shadow)
flags += 's';
+ if(transform)
+ flags += 'r';
return flags;
}
add("shadow", &StandardFeatures::shadow);
add("specular", &StandardFeatures::specular);
add("texture", &StandardFeatures::texture);
+ add("transform", &StandardFeatures::transform);
}
} // namespace GL