/*
** Copyright (c) 1998 Mitsuharu Konishi.  All rights reserved.
** cs184-ak
** ______________________________________________________________________
*/

/*-------------------------------------------------------------------------
 *	plaid - creates a plaid surface (Kennedy) with variable colors.
 *
 *      color0 -- the color of the base layer(default is green)
 *      color1 -- the color of the layer for which the default is light blue
 *      color2 -- the color of the layer for which the default is red
 *      color3 -- the color of the layer for which the default is yellow
 *      color4 -- the color of the layer for which the default is dark blue
 *      color5 -- the color of the top layer (default is white)
 *      thickness -- the thickness of string (0.0 -- 1.0)
 *      maxX, maxY -- the number of columns or rows in the unit block
 *      xfreq, yfreq -- the number of blocks in the x or y direction
 *
 *	Kd, Ka - the usual meaning
 *-------------------------------------------------------------------------*/
surface
plaid( float Kd=0.9, Ka=.1,
              thickness = 0.25, maxX = 57, maxY = 57, xfreq = 2, yfreq = 2;
        color color0 = (0.398,0.598,0.199), color1 = (0.199,0.598,0.598),
              color2 = (0.598,0.199,0.199),  color3 = (0.797,0.598,0.199),
              color4 = (0.199,0.199,0.398),  color5 = (0.891,0.891,0.891)
      ) 
{
	varying point	Nf;
	varying float   ss,tt,x,y,xx,yy;
        varying color   currentColor;

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

        currentColor = color0;

	ss = mod(s*xfreq, 1);
	tt = mod(t*yfreq, 1);
	x = ss * maxX;
	y = tt * maxY;
        xx = mod(x,1);
        yy = mod(y,1);

	if(((1<=x && x<=3)||(maxX-3<=x && x<=maxX-1)) &&
           (xx-thickness<=yy && yy<=xx+thickness)) {  
          currentColor = color1;
        }
	else if(((1<=y && y<=3)||(maxY-3<=y && y<=maxY-1)) &&
           (yy < xx-thickness || xx+thickness < yy)) { 
          currentColor = color1;
        }
        if(((9<=x && x<=11)||(maxX-11<=x && x<=maxX-9)) &&
           (xx-thickness<=yy && yy<=xx+thickness)) {   
          currentColor = color2;
        }
	else if(((9<=y && y<=11)||(maxY-11<=y && y<=maxY-9)) &&
           (yy < xx-thickness || xx+thickness < yy)) { 
          currentColor = color2;
        }
        if(((14<=x && x<=15)||(maxX-15<=x && x<=maxX-14)||
            (16<=x && x<=17)||(maxX-17<=x && x<=maxX-16)) &&
           (xx-thickness<=yy && yy<=xx+thickness)) {   
          currentColor = color3;
        }
	else if(((14<=y && y<=15)||(maxY-15<=y && y<=maxY-14)||
            (16<=y && y<=17)||(maxY-17<=y && y<=maxY-16)) &&
           (yy < xx-thickness || xx+thickness < yy)) { 
          currentColor = color3;
        }
	if(((22<=x && x<=25)||(maxX-25<=x && x<=maxX-22)||
            (26<=x && x<=28)||(maxX-28<=x && x<=maxX-26)) &&
           (xx-thickness<=yy && yy<=xx+thickness)) {   
          currentColor = color4;
        }
	else if(((22<=y && y<=25)||(maxY-25<=y && y<=maxY-22)||
            (26<=y && y<=28)||(maxY-28<=y && y<=maxY-26)) &&
           (yy < xx-thickness || xx+thickness < yy)) { 
          currentColor = color4;
        }
	if(((28<=x && x<=29)||(maxX-29<=x && x<=maxX-28)) &&
           (xx-thickness<=yy && yy<=xx+thickness)) {  
          currentColor = color5;
        }
	else if(((28<=y && y<=29)||(maxY-29<=y && y<=maxY-28)) &&
           (yy < xx-thickness || xx+thickness < yy)) { 
          currentColor = color5;
        }

    /* get color */
	Oi = Os;
	Ci = Os * currentColor * (Ka*ambient() + Kd*diffuse(Nf));
}