From e5a37a70232f43cd6bd3b76afcb26d0053fe56b5 Mon Sep 17 00:00:00 2001 From: scrawl Date: Thu, 18 Feb 2016 00:00:12 +0100 Subject: [PATCH] Add normal map code --- files/shaders/objects_fragment.glsl | 20 ++++++++++++++++++-- files/shaders/objects_vertex.glsl | 12 +++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/files/shaders/objects_fragment.glsl b/files/shaders/objects_fragment.glsl index f3a9aa766..6346aa499 100644 --- a/files/shaders/objects_fragment.glsl +++ b/files/shaders/objects_fragment.glsl @@ -20,9 +20,15 @@ uniform sampler2D emissiveMap; varying vec2 emissiveMapUV; #endif +#if @normalMap +uniform sampler2D normalMap; +varying vec2 normalMapUV; +varying vec3 viewTangent; +#endif + varying float depth; -#define PER_PIXEL_LIGHTING 0 +#define PER_PIXEL_LIGHTING @normalMap #if !PER_PIXEL_LIGHTING varying vec4 lighting; @@ -50,11 +56,21 @@ void main() gl_FragData[0].xyz *= texture2D(darkMap, darkMapUV).xyz; #endif +#if @normalMap + vec3 viewNormal = passViewNormal; + vec3 normalTex = texture2D(normalMap, normalMapUV).xyz; + + vec3 viewBinormal = cross(viewTangent, viewNormal); + mat3 tbn = mat3(viewTangent, viewBinormal, viewNormal); + + viewNormal = normalize(tbn * (normalTex * 2.0 - 1.0)); +#endif + #if !PER_PIXEL_LIGHTING gl_FragData[0] *= lighting; #else - gl_FragData[0] *= doLighting(passViewPos, passViewNormal, passColour); + gl_FragData[0] *= doLighting(passViewPos, normalize(viewNormal), passColour); #endif #if @emissiveMap diff --git a/files/shaders/objects_vertex.glsl b/files/shaders/objects_vertex.glsl index 7a6f0ca21..08fdb4ef8 100644 --- a/files/shaders/objects_vertex.glsl +++ b/files/shaders/objects_vertex.glsl @@ -16,9 +16,14 @@ varying vec2 detailMapUV; varying vec2 emissiveMapUV; #endif +#if @normalMap +varying vec2 normalMapUV; +varying vec3 viewTangent; +#endif + varying float depth; -#define PER_PIXEL_LIGHTING 0 +#define PER_PIXEL_LIGHTING @normalMap #if !PER_PIXEL_LIGHTING varying vec4 lighting; @@ -55,6 +60,11 @@ void main(void) emissiveMapUV = (gl_TextureMatrix[@emissiveMapUV] * gl_MultiTexCoord@emissiveMapUV).xy; #endif +#if @normalMap + normalMapUV = (gl_TextureMatrix[@normalMapUV] * gl_MultiTexCoord@normalMapUV).xy; + viewTangent = normalize(gl_NormalMatrix * gl_MultiTexCoord7.xyz); +#endif + #if !PER_PIXEL_LIGHTING lighting = doLighting(viewPos.xyz, viewNormal, gl_Color); #else