shader() example

This shader implements the diffuse part of the SGI lighting model discussed in class.
void
shader(float *color)
{
   float eye2Surf[3];
   int i;

   /* Compute vector from surface to eye. */
   vec_sub(eyePosition,surfacePosition,eye2Surf);
   vec_normalize(eye2Surf);

   /* For each light, compute its contribution */
   vec_set(color,0,0,0);

   for(i=0 ; i<numLights ; i++)
   {
      float light2Surf[3];
      float edotn;
      float tmp[3];

      /* Compute vector from light to surface. */
      vec_sub(lights[i].position,surfacePosition,light2Surf);
      vec_normalize(light2Surf);

      /* Add diffuse component. */
      edotn = vec_dot(light2Surf,surfaceNormal);
      if(edotn > 0)
      {
	 vec_scale2(edotn,diffuseColor,tmp);
	 vec_comp_mult(tmp,lights[i].color,tmp);
	 vec_add(color,tmp,color);
      }
   }

   CLAMP_COLOR(color);
}

This configuration file could be used with this shader:
#
# This stuff has nothing to do with the shader, only scene layout
#
string patchObject = models/patches/teapot.obj
int   loDice = 10
int   hiDice = 20
int   numLights = 2
vec   eyePosition = 0 0 20
vec   light0Pos = 0 20 40
color light0Color = 1 0 0
vec   light1Pos = 0 -20 40
color light1Color = 0 1 0
#
# Shader variables go here
#
string whichShader = diffuse
color diffuseColor = 0.2 0.2 0.2

hanrahan@cs.stanford.edu
beers@graphics.stanford.edu

Copyright © 1997 Pat Hanrahan and Andrew C. Beers