]> git.tdb.fi Git - libs/gl.git/commitdiff
Flip normal if a surface faces away from the camera
authorMikko Rasa <tdb@tdb.fi>
Fri, 11 Mar 2022 21:48:17 +0000 (23:48 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 11 Mar 2022 21:48:17 +0000 (23:48 +0200)
This gives correct two-sided lighting for non-closed meshes, which may
be used for tree leaves and such.

shaderlib/common.glsl

index 7a57b0671eebf1a3b4850cb451785c66a52498ff..557d4d76a17cff24ef538946d8d95e531d5c099d 100644 (file)
@@ -78,10 +78,12 @@ 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)