/* * Vertex shader for normal mapping and texture mapping. We need to use the * tangent and binormal to transform our lighting direction into tangent * space, but other paramters are as usual. * * Alexandri Zavodny, Fall 2010 * for CSE 40166/60166 Computer Graphics, University of Notre Dame */ varying vec4 theColor; attribute vec3 tangent; attribute vec3 binormal; varying vec3 dirToLight; void main() { vec3 theNormal = normalize(gl_NormalMatrix * gl_Normal); vec3 theTangent = normalize(gl_NormalMatrix * tangent); vec3 theBinormal= normalize(gl_NormalMatrix * binormal); //this is the tricky part: since the normal from the normal map is //specified in tangent space, we have to put the direction to the light //in tangent space as well. since the normal, tangent, and binormal form //an orthogonal set of basis vectors for the tangent space, we can just //take the dot product of this vector with each of the axes. vec3 almostDirToLight = normalize(vec3(gl_LightSource[0].position - gl_ModelViewMatrix * gl_Vertex)); dirToLight.x = dot(almostDirToLight, theTangent); dirToLight.y = dot(almostDirToLight, theBinormal); dirToLight.z = dot(almostDirToLight, theNormal); dirToLight = normalize(dirToLight); gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1; gl_Position = ftransform(); }