/*
 * mytex - texture mapping surface shader for objects that
 *         consist of numerous patches.
 *
 * Aleksey Potashnik
 * cs184-bk
 *
 * Project 2
 *
 * DESCRIPTION:
 *   Locate the object in xz plane with the right surface pointing
 *   in the positive y direction.  Pass min and max values for
 *   x and z as parameters.
 *
 * PARAMETERS:
 *    fname             texture file
 *    Ka, Kd, Ks		The usual
 */
surface mytex

    (string     fname = "";
     float      xmin = 1.0126,
                xmax = 2.4649,
                zmin = 0.9148,
                zmax = 1.393280;
     float      Ka=1, Kd=1, Ks=0, roughness=.25;
	 color      specularcolor = 1
	)
{

    varying vector Nf, NI;
	varying color ctx;
	varying point PP;

	PP = transform("shader", P);
	
	Nf = faceforward( normalize(N), I);
	
	if (fname == "")
	    ctx = 1;
	else
		ctx = color texture(fname, (xcomp(PP) - xmin)/(xmax - xmin),
		                           (zcomp(PP) - zmin)/(zmax - zmin));

	Ci = (Ka*ambient() + Kd*diffuse(Nf)) * ctx;
	
	if (Ks != 0.0)	{
	    NI = normalize(-I);
		Ci += Ks*specularcolor*specular(Nf,NI,roughness);
	}
	
	Oi = Os;
	Ci = Ci * Oi;
}