From: Mikko Rasa Date: Fri, 11 Mar 2022 21:48:17 +0000 (+0200) Subject: Flip normal if a surface faces away from the camera X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=88c401224220010cb71426fafc5173661c4d48a3;p=libs%2Fgl.git Flip normal if a surface faces away from the camera This gives correct two-sided lighting for non-closed meshes, which may be used for tree leaves and such. --- diff --git a/shaderlib/common.glsl b/shaderlib/common.glsl index 7a57b067..557d4d76 100644 --- a/shaderlib/common.glsl +++ b/shaderlib/common.glsl @@ -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)