]> git.tdb.fi Git - libs/gl.git/blobdiff - source/program.cpp
Rename a variable in shader code to avoid conflict with a keyword
[libs/gl.git] / source / program.cpp
index 3c3a78b7c298a15bb6e55f8b9e75268fd788c4c7..8511b8c15833c3f5f62e2e707e56279092952a97 100644 (file)
@@ -1,7 +1,7 @@
 #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"
@@ -20,11 +20,15 @@ const char *standard_vertex_src[] =
        "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",
@@ -57,8 +61,8 @@ const char *standard_fragment_src[] =
        "n",   "\tvec3 n_normal = texture2D(normalmap, v_texcoord).xyz*2.0-1.0;\n",
        "l!n", "\tvec3 n_normal = normalize(v_normal);\n",
        "l",   "\tfloat l_diffuse = max(dot(n_normal, normalize(v_light_dir)), 0.0);\n",
-       "p",   "\tvec3 half = normalize(v_eye_dir+v_light_dir);\n",
-       "p",   "\tfloat l_specular = pow(max(dot(half, n_normal), 0.0), gl_FrontMaterial.shininess);\n",
+       "p",   "\tvec3 half_dir = normalize(v_eye_dir+v_light_dir);\n",
+       "p",   "\tfloat l_specular = pow(max(dot(half_dir, n_normal), 0.0), gl_FrontMaterial.shininess);\n",
        "s",   "\tfloat l_shadow = shadow2D(shadow, v_shadowcoord).r;\n",
         0,    "\tgl_FragColor = ",
        "!t!l!m", "vec4(1.0, 1.0, 1.0, 1.0)",
@@ -214,7 +218,7 @@ void Program::link()
        int value;
        glGetObjectParameterivARB(id, GL_OBJECT_LINK_STATUS_ARB, &value);
        if(!(linked = value))
-               throw CompileError(get_info_log());
+               throw compile_error(get_info_log());
 
        glGetObjectParameterivARB(id, GL_OBJECT_ACTIVE_UNIFORMS_ARB, &value);
        for(int i=0; i<value; ++i)
@@ -244,7 +248,7 @@ string Program::get_info_log() const
 void Program::bind() const
 {
        if(!linked)
-               throw InvalidState("Program is not linked");
+               throw invalid_operation("Program::bind");
 
        if(!set_current(this))
                return;
@@ -276,7 +280,8 @@ Program::StandardFeatures::StandardFeatures():
        lighting(false),
        specular(false),
        normalmap(false),
-       shadow(false)
+       shadow(false),
+       transform(false)
 { }
 
 string Program::StandardFeatures::create_flags() const
@@ -296,6 +301,8 @@ string Program::StandardFeatures::create_flags() const
        }
        if(shadow)
                flags += 's';
+       if(transform)
+               flags += 'r';
 
        return flags;
 }
@@ -349,6 +356,7 @@ Program::StandardFeatures::Loader::Loader(StandardFeatures &f):
        add("shadow",    &StandardFeatures::shadow);
        add("specular",  &StandardFeatures::specular);
        add("texture",   &StandardFeatures::texture);
+       add("transform", &StandardFeatures::transform);
 }
 
 } // namespace GL