dungeon_crawler

diff prototype/sdr/mrt.p.glsl @ 35:d0e93b4d9ec9

normal mapping
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 28 Aug 2012 06:28:22 +0300
parents 85734f319626
children 80dab4000413
line diff
     1.1 --- a/prototype/sdr/mrt.p.glsl	Tue Aug 28 03:34:56 2012 +0300
     1.2 +++ b/prototype/sdr/mrt.p.glsl	Tue Aug 28 06:28:22 2012 +0300
     1.3 @@ -1,5 +1,5 @@
     1.4  /* MRT assignments
     1.5 - * 0 - RGB: position
     1.6 + * 0 - RGB: position, A: shininess
     1.7   * 1 - RGB: normal
     1.8   * 3 - RGB: diffuse color, A: shininess str.
     1.9   * 4 - unused
    1.10 @@ -7,19 +7,35 @@
    1.11  
    1.12  uniform sampler2D tex_dif, tex_norm;
    1.13  
    1.14 -varying vec3 pos, norm;
    1.15 +varying vec3 pos, norm, tang;
    1.16 +
    1.17 +const float fog_start = 3.0;
    1.18 +const float fog_end = 6.0;
    1.19  
    1.20  void main()
    1.21  {
    1.22  	vec3 n = normalize(norm);
    1.23 +	vec3 t = normalize(tang);
    1.24 +	vec3 b = cross(n, t);
    1.25 +
    1.26 +	mat3 tbn_mat = mat3(
    1.27 +			t.x, t.y, t.z,
    1.28 +			b.x, b.y, b.z,
    1.29 +			n.x, n.y, n.z);
    1.30 +
    1.31 +	// grab normal from the normal map, remap it and transform it to view space
    1.32 +	n = texture2D(tex_norm, gl_TexCoord[0].st).xyz * 2.0 - 1.0;
    1.33 +	n = normalize(tbn_mat * n);
    1.34 +
    1.35 +	float fog = clamp((fog_end + pos.z) / (fog_end - fog_start), 0.0, 1.0);
    1.36  
    1.37  	vec4 texel = texture2D(tex_dif, gl_TexCoord[0].st);
    1.38 -	vec3 diffuse = (gl_FrontMaterial.diffuse * texel).xyz;
    1.39 -	vec3 spec = gl_FrontMaterial.specular.xyz;
    1.40 +	vec3 diffuse = fog * (gl_FrontMaterial.diffuse * texel).xyz;
    1.41 +	vec3 spec = fog * gl_FrontMaterial.specular.xyz;
    1.42  	float sstr = (spec.x + spec.y + spec.z) / 3.0;
    1.43  
    1.44  	gl_FragData[0] = vec4(pos, gl_FrontMaterial.shininess);
    1.45 -	gl_FragData[1] = vec4(norm, 0.0);
    1.46 +	gl_FragData[1] = vec4(n, 0.0);
    1.47  	gl_FragData[2] = vec4(diffuse, sstr);
    1.48  	//gl_FragData[3] = vec4(0.0, 0.0, 0.0, 0.0);
    1.49  }