]> git.tdb.fi Git - libs/gl.git/blobdiff - shaderlib/common.glsl
Flip normal if a surface faces away from the camera
[libs/gl.git] / shaderlib / common.glsl
index 33fefe8806deb4ddfa7240c5394f1778f082d1c1..557d4d76a17cff24ef538946d8d95e531d5c099d 100644 (file)
@@ -42,21 +42,18 @@ void standard_transform()
        vec4 eye_vertex = eye_world_matrix*world_vertex;
        gl_Position = clip_eye_matrix*eye_vertex;
 
-       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 world_normal = normalize(normal_tf*get_vertex_normal());
+       if(use_normal_map)
+       {
+               vec3 world_tangent = normalize(normal_tf*tangent);
+               vec3 world_binormal = normalize(cross(world_normal, world_tangent));
+               out mat3 world_tbn_matrix = mat3(world_tangent, world_binormal, world_normal);
+       }
 
-       vec3 eye_pos = (inverse(eye_world_matrix)*vec4(0.0, 0.0, 0.0, 1.0)).xyz;
+       vec3 eye_pos = world_eye_matrix[3].xyz;
        out vec3 world_look_dir = normalize(world_vertex.xyz-eye_pos);
 
        out float fog_coord = eye_vertex.z;
-
-       if(use_clipping)
-       {
-               for(int i=0; i<max_clip_planes; ++i)
-                       gl_ClipDistance[i] = dot(world_vertex, clip_planes[i].equation);
-       }
 }
 
 virtual void custom_transform()
@@ -77,12 +74,16 @@ struct IncomingLight
        vec3 color;
 };
 
+layout(location=0) out vec4 frag_color;
+
 virtual vec3 get_fragment_normal()
 {
+       vec3 normal;
+       float sgn = (gl_FrontFacing ? 1.0 : -1.0);
        if(use_normal_map)
-               return normalize(world_tbn_matrix*(texture(normal_map, texcoord.xy).xyz*2.0-1.0));
+               return sgn*normalize(world_tbn_matrix*(texture(normal_map, texcoord.xy).xyz*2.0-1.0));
        else
-               return normalize(world_normal);
+               return sgn*normalize(world_normal);
 }
 
 virtual IncomingLight get_incoming_light(int index, vec3 world_pos)