/*
 * CS 184
 * LAB 7 (shader)
 * Daniel Lim - bu
 * 
 *
 * PARAMETERS:
 *    Ka, Kd, Ks, Kr
 *    speccolor, dcolor, backcolor
 *    dsize
 *    dspacing
 *
 */

surface lab7 ( float Ka = 1, Kd = 1, Ks = 1, Kr = 1;
	       color speccolor = 1;
	       color dcolor = color "rgb" (.6,.6,.1);
	       color backcolor = color "rgb" (.1,.1,.6);
	       float dsize = 1;
	       float dspacing = .2; )
{
#define sqr(x) ((x)*(x))
    float nx, ny, nz;
    float trans;
    normal Nf;               /* Forward facing normal vector */
    vector IN;               /* normalized incident vector */
    color ev;                /* Color of the reflections */
    
    /* Construct a forward facing surface normal */
    Nf = faceforward (normalize(N), I);
    IN = normalize (I);
    
    Oi = Os;

    /*
     * check to see if the coordinate is within a d
     */
    trans = dsize + dspacing / 2;
    nx = mod(xcomp(P), (trans*2));
    ny = mod(ycomp(P), (trans*2));
    nz = mod(zcomp(P), (trans*2));

    if((sqr(nx-trans) + sqr(ny-trans) + sqr(nz-trans)) < sqr(dsize)) {
      ev = dcolor;
    }
    else {
      ev = backcolor;
    }

    Ci = Os * (ev * (Ka*ambient() + Kd*diffuse(Nf)) +
		Ks * speccolor * specular(Nf,-IN,.2));

}