stk-code_catmod/data/shaders/normalmap.frag

31 lines
787 B
GLSL
Raw Normal View History

2014-01-17 17:50:06 -05:00
uniform sampler2D normalMap;
#if __VERSION__ >= 130
in vec3 tangent;
in vec3 bitangent;
in vec2 uv;
out vec2 EncodedNormal;
#else
varying vec3 tangent;
varying vec3 bitangent;
varying vec2 uv;
#define EncodedNormal gl_FragColor.xy
#endif
vec2 EncodeNormal(vec3 n);
void main()
{
// normal in Tangent Space
2014-04-06 19:03:14 -04:00
vec3 TS_normal = 2.0 * pow(texture(normalMap, uv).rgb, vec3(1./2.2)) - 1.0;
// Because of interpolation, we need to renormalize
vec3 Frag_tangent = normalize(tangent);
vec3 Frag_normal = normalize(cross(Frag_tangent, bitangent));
vec3 Frag_bitangent = cross(Frag_normal, Frag_tangent);
vec3 FragmentNormal = TS_normal.x * Frag_tangent + TS_normal.y * Frag_bitangent - TS_normal.z * Frag_normal;
EncodedNormal = 0.5 * EncodeNormal(normalize(FragmentNormal)) + 0.5;
}