/*
 * lab7.sl -- Surface shader for polka dots.
 *	
 * Johnny Lee - 12748961 - Login DP	
 *
 * DESCRIPTION:
 *   Polka-dot pattern with some controllable features as
 *   given below (and in the handout)
 * 
 * PARAMETERS:
 *	bgColor,dotColor 			colors (default is pink dots over blue back)
 *	radius						radius of dots
 *  spacing						spacing between center of each dot
 *								Note
 *
 * AUTHOR: written by Johnny Lee
 *
 * Date: 11-11-1998
 */


#define RADIUS 0.10
#define SPACING 0.30 

surface
lab7 ( 
	color bgColor = color "rgb" (.2,.15,.7);
	color dotColor = color "rgb" (.8,.2,.2);
	float radius = 0.10, spacing = 0.30;
        
    )
{
#define sqr(x) ((x)*(x))

  /* This makes sure dots don't overlap */
	if(2*radius >= spacing)
	{
		radius = RADIUS;
		spacing = SPACING;
	}

	normal Nf;
/*	float temps, tempt;

	temps = s - spacing*floor(s/spacing) - radius;
	tempt = t - spacing*floor(t/spacing) - radius;

	if( sqrt(sqr(temps)+sqr(tempt)) <= radius ) 
	{
		Cs = dotColor;
	}
	else
	{
		Cs = bgColor;
	}
*/	
	float tempu, tempv;

	tempu = u - spacing*floor(u/spacing) - radius;
	tempv = v - spacing*floor(v/spacing) - radius;

	if( sqrt(sqr(tempu)+sqr(tempv)) <= radius ) 
	{
		Cs = dotColor;
	}
	else
	{
		Cs = bgColor;
	}


    N = calculatenormal (P);
	Nf = faceforward (normalize(N),I);

	Oi = Os;
    Ci = Os * Cs * (ambient() + diffuse(Nf));

}