/*
 * window.sl -- surface shader for window (but actually used for the entrance 
 * to castle from water)
 *
 * DESCRIPTION:
	Does texture mapping in the window which was displaced with shaders like
	wall_window.sl. 
 * 
 * PARAMETERS:
	 float
	 height 
	 width 
	 fake 
	 x_min
	 y_min 
 	 brightness

float Ks=.9, Kd=.8, Ka=.2, roughness=.3,
	scale=.02, nshades=4, exponent=2;
	    color specularcolor=1, graincolor=0

 * 
 *
 * AUTHOR: Mitsuharu Konishi and the author of stone.sl
 *
 * 
 */


surface
window (
	 float
	 height = 0.35,
	 width = 0.15,
	 fake = 0.02,
	 x_min = 0.425,
	 y_min = 0.2,
 	 brightness = 1.0;

         string fname = "door2.tif";

float Ks=.9, Kd=.8, Ka=.2, roughness=.3,
	scale=.02, nshades=4, exponent=2;
	    color specularcolor=1, graincolor=0

        ) {

  float x,y,x_center, y_center, xx, yy, r;

    point V, Nf;
    float tone;
    color grain;


  x = s - x_min;
  y = 1-t - y_min;
  x_center = width/2;
  y_center = height;
  xx = x - x_center;
  yy = y - y_center;
  r = sqrt(xx*xx + yy*yy);

  if(x < width-fake && y < height && x > fake && y > fake  ||
     r < width/2 - fake) {  
    Nf = faceforward( normalize(N), I);
    Ci = Os * texture(fname, x/width, 1-y/(height+width/2)) * (Ka*ambient() + Kd*diffuse(Nf)) ;

    Oi = Os;
  }


  else {  /* shade with stone */

 /* N and I are not automatically normalized in this implementation. */
    Nf = faceforward( normalize(N), I);
    V = -normalize(I) ;

 /* high-frequency noise */
    tone = noise( transform("shader",P) / scale);
 /* exponent sharpens peaks and pushes average toward darker end */
 /* round chops into nshades different intensity levels */
    tone = round(nshades * pow(tone, exponent)) / nshades;

    Oi = Os;
 /* interpolate between different colors using tone to get speckles */
    grain = mix(graincolor,Cs,tone);
    Ci = Os * (grain * (Ka*ambient() + Kd*diffuse(Nf)) +
    		specularcolor * Ks * specular(Nf,V,roughness) );
   }


}