/* * Fragment 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 mode == 0, we just apply the per-vertex colors from the vertex shader. * Otherwise, we compute the per-pixel colors using the normal and direction to light * vectors that got passed in and interpolated by the rasterizer. * * Alexandri Zavodny, Fall 2010 * for CSE 40166/60166 Computer Graphics, University of Notre Dame */ uniform int mode; uniform vec3 ambient; varying vec3 dirToLight, theNormal, theColor; void main() { float lambertianIntensity = max(dot(normalize(theNormal), normalize(dirToLight)),0.0); //if we have mode == 0, we're doing Gourad shading, which is per-vertex -- //the lighting has already been computed and interpolated in theColor, so just use that. if(mode == 0) gl_FragColor = vec4(theColor, 1.0); //if we have mode == 1, we're doing per-pixel Phong lighting, so use the intensity //that we computed at this pixel! if(mode == 1) gl_FragColor = lambertianIntensity * vec4(theColor,1.0) + vec4(ambient, 1.0); //if we have mode == 2, we're doing per-pixel cel shading: just use the intensity //as for phong shading, but clamp it to a set of ranges to give it a nice step! if(mode == 2) { //clamp the intensity values to a set of ranges... if(lambertianIntensity < 0.3) lambertianIntensity = 0.0; else if(lambertianIntensity < 0.6) lambertianIntensity = 0.3; else if(lambertianIntensity < 0.9) lambertianIntensity = 0.6; else lambertianIntensity = 1.0; gl_FragColor = lambertianIntensity * vec4(theColor, 1.0) + vec4(ambient, 1.0); } }