]> git.tdb.fi Git - libs/gl.git/blobdiff - shaderlib/common.glsl
Move lighting calculations to world space
[libs/gl.git] / shaderlib / common.glsl
index bac82fc131150bf0895db30d975c71cb398ae56d..94237c7acfd3cec3626497c85761db24b00e9106 100644 (file)
@@ -1,58 +1,70 @@
 import msp_interface;
 import shadow;
 
+uniform EnvMap
+{
+       mat3 env_world_matrix;
+};
+
+uniform sampler2D normal_map;
+uniform samplerCube environment_map;
+
 layout(constant_id=auto) const bool use_normal_map = false;
 
 #pragma MSP stage(vertex)
-vec4 get_vertex_position()
+virtual vec4 get_vertex_position()
 {
        return vertex;
 }
 
-vec3 get_vertex_normal()
+virtual vec3 get_vertex_normal()
 {
        return normal;
 }
 
-vec4 transform_position(vec4 pos)
+virtual mat4 get_vertex_transform()
 {
-       return eye_obj_matrix*pos;
+       return world_obj_matrix;
 }
 
-vec3 transform_normal(vec3 nor)
+virtual mat3 get_normal_transform()
 {
-       return eye_obj_normal_matrix*nor;
+       return world_obj_normal_matrix;
 }
 
 void standard_transform()
 {
-       out vec4 eye_vertex = transform_position(get_vertex_position());
-       gl_Position = projection_matrix*eye_vertex;
+       mat4 vertex_tf = get_vertex_transform();
+       mat3 normal_tf = get_normal_transform();
 
-       out vec3 eye_normal = transform_normal(get_vertex_normal());
-       vec3 eye_tangent = transform_normal(tangent);
-       vec3 eye_binormal = transform_normal(binormal);
-       out mat3 eye_tbn_matrix = mat3(eye_tangent, eye_binormal, eye_normal);
+       vec4 world_vertex = vertex_tf*get_vertex_position();
+       vec4 eye_vertex = eye_world_matrix*world_vertex;
+       gl_Position = clip_eye_matrix*eye_vertex;
 
-       out vec3 eye_look_dir = normalize(eye_vertex.xyz);
-       out vec3 tbn_look_dir = eye_look_dir*eye_tbn_matrix;
+       out vec3 world_normal = normal_tf*get_vertex_normal();
+       vec3 world_tangent = normal_tf*tangent;
+       vec3 world_binormal = cross(world_normal, world_tangent);
+       out mat3 world_tbn_matrix = mat3(world_tangent, world_binormal, world_normal);
 
-       out vec3 eye_light_dir = normalize(light_sources[0].position.xyz-eye_vertex.xyz*light_sources[0].position.w);
-       out vec3 tbn_light_dir = eye_light_dir*eye_tbn_matrix;
+       vec3 eye_pos = (inverse(eye_world_matrix)*vec4(0.0, 0.0, 0.0, 1.0)).xyz;
+       out vec3 world_look_dir = normalize(world_vertex.xyz-eye_pos);
 
-       out vec3 eye_halfway_dir = normalize(eye_light_dir-eye_look_dir);
-       out vec3 tbn_halfway_dir = eye_halfway_dir*eye_tbn_matrix;
+       out vec3 world_light_dir = light_sources[0].position.xyz-world_vertex.xyz*light_sources[0].position.w;
+
+       out vec3 world_halfway_dir = normalize(world_light_dir-world_look_dir);
 
-       out vec3 tbn_zenith_dir = eye_zenith_dir*eye_tbn_matrix;
        out float fog_coord = eye_vertex.z;
 
-       for(int i=0; i<max_clip_planes; ++i)
-               gl_ClipDistance[i] = dot(eye_vertex, clip_planes[i].equation);
+       if(use_clipping)
+       {
+               for(int i=0; i<max_clip_planes; ++i)
+                       gl_ClipDistance[i] = dot(world_vertex, clip_planes[i].equation);
+       }
 
-       shadow_transform(eye_vertex);
+       shadow_transform(world_vertex);
 }
 
-void custom_transform()
+virtual void custom_transform()
 {
 }
 
@@ -64,30 +76,27 @@ void main()
 }
 
 #pragma MSP stage(fragment)
-vec3 get_fragment_normal()
+virtual vec3 get_fragment_normal()
 {
        if(use_normal_map)
-               return normalize(texture(normal_map, texcoord.xy).xyz*2.0-1.0);
+               return normalize(world_tbn_matrix*(texture(normal_map, texcoord.xy).xyz*2.0-1.0));
        else
-               return vec3(0.0, 0.0, 1.0);
+               return normalize(world_normal);
 }
 
-vec4 get_environment_sample(vec3 direction)
+virtual vec3 get_environment_sample(vec3 direction)
 {
-       return texture(environment_map, direction);
+       return texture(environment_map, env_world_matrix*direction).rgb;
 }
 
-vec3 get_reflection(vec3 normal, vec3 look)
+virtual vec3 get_reflection(vec3 normal, vec3 look)
 {
        vec3 reflect_dir = reflect(look, normal);
-       if(use_normal_map)
-               reflect_dir = eye_tbn_matrix*reflect_dir;
-
-       return get_environment_sample(env_eye_matrix*reflect_dir).rgb;
+       return get_environment_sample(reflect_dir);
 }
 
 vec3 apply_fog(vec3 color)
 {
        float fog_value = exp(fog_coord*fog_density);
-       return mix(fog_color, color, fog_value);
+       return mix(fog_color.rgb, color, fog_value);
 }