shader() example
This shader implements the diffuse part of the
SGI lighting
model discussed in class.
void
shader(float *color)
{
int i;
/* 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);
}
}
vec_clamp(color, 0, 1);
}
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
Copyright © 1997--1998 Pat Hanrahan and Andrew C. Beers