/* * Vertex shader for some simple lighting formulae. The 'mode' variable determines * whether we do Gourad (mode == 0), Phong (mode == 1), or Cel shading (mode == 2). * If we do Gourad shading, we compute the lighting equation here in the vertex * shader and pass it along to the fragment shader; otherwise, lighting gets * computed per-pixel in the fragment shader. * * Alexandri Zavodny, Fall 2010 * for CSE 40166/60166 Computer Graphics, University of Notre Dame */ varying vec3 dirToLight, theNormal, theColor; uniform int mode; uniform vec3 ambient; void main() { //transform the normal into eye space and make sure it stays normalized //using normalize() here is like using glEnable(GL_NORMALIZE)! theNormal = normalize(gl_NormalMatrix * gl_Normal); //calculate the direction to the light as usual; the light source is in eye space //but gl_Vertex is in object space, so be sure to transform it first! dirToLight = normalize(vec3(gl_LightSource[0].position - gl_ModelViewMatrix * gl_Vertex)); //pass the color along to the fragment shader theColor = vec3(gl_Color); //and compute lambertian intensity using the simple formula. note that we only //use this intensity if mode == 0 (which is gourad shading). float lambertianIntensity = max(dot(normalize(theNormal), normalize(dirToLight)),0.0); if(mode == 0) theColor = lambertianIntensity * theColor + ambient; gl_Position = ftransform(); }