/* * Fragment shader for normal mapping and texture mapping. Basically, we * do texture shading as we did before, but we use the normal specified * in the normal map instead of the regular normal. Note that the normal * that's specified in the normal map is given in TANGENT SPACE, not world space -- * so instead of converting the normal back into eye space for lighting, * we converted the lighting direction into tangent space back in the vertex * shader. * * Alexandri Zavodny, Fall 2010 * for CSE 40166/60166 Computer Graphics, University of Notre Dame */ varying vec4 theColor; uniform sampler2D colorTex, bumpTex; varying vec3 dirToLight; void main() { vec2 uvCoords = gl_TexCoord[0].st; //grab the texture coordinate as usual vec4 texColor = texture2D(colorTex, uvCoords); //and grab the normal from the map at the same coordinates. vec3 normDist = vec3(texture2D(bumpTex, uvCoords)); //the normals are packed to the range of [0,1] because images don't //have negative values -- we need to expand the range to [0,2] and then //move it to [-1.0, 1.0] normDist = 2.0 * normDist - vec3(1.0,1.0,1.0); //compute lighting as usual! using the new normal, of course. float lambertianIntensity = max(dot(normalize(normDist), normalize(dirToLight)),0.0); gl_FragColor = lambertianIntensity * texColor; }