/** Demo of using gbuffer rendering pass. Fragment shader */ #version 110 uniform sampler2D normalMap; uniform sampler2D colourMap; uniform float tileScale; uniform vec2 planes; varying vec3 vpos; varying vec3 vnormal; varying vec3 vtangent; varying vec3 vbinormal; vec4 float_to_color(float f) { vec4 color = vec4(0.0, 0.0, 0.0, 1.0); f *= 256.0; color.r = floor(f); f = (f - color.r) * 256.0; color.g = floor(f); color.b = f - color.g; color.rg *= 0.00390625; // *= 1.0/256 return color; } void main(){ vec2 tx_coord = gl_TexCoord[0].xy * tileScale; // look up the normal map vec3 normal = texture2D(normalMap, tx_coord).xyz; // transform the normal to view space normal -= 0.5; normal = normalize(normal.x * vtangent + normal.y * vbinormal + normal.z * vnormal); // Now back to colour space of [0, 1] normal = normal * 0.5 + 0.5; // look up the colour map vec4 colour = texture2D(colourMap, tx_coord); float d = ((planes.x * vpos.z + planes.y) / -vpos.z); // Finally fill out the buffer passes gl_FragData[0] = vec4(normal, 1); gl_FragData[1] = vec4((colour * gl_FrontMaterial.diffuse).rgb, 1.0); gl_FragData[2] = gl_FrontMaterial.specular; gl_FragData[3] = float_to_color(d); }