/*
 * polkadot - polka-dot surface shader
 *
 * Aleksey Potashnik
 * cs184-bk
 *
 * Lab 7
 *
 * DESCRIPTION:
 *   creates a regular array of filled chircles.
 *
 * PARAMETERS:
 *    Ka, Kd			The usual
 *    dot_              Color of a circle
 *    background_       Background color
 *    radius_           Radius of a dot
 *    distance_         Distance between the centers of
 *                      neighboring circles in s and t dimensions
 */
 
#include "patterns.h"

#define dist(s1, t1, s2, t2) sqrt((s2-s1)*(s2-s1) + (t2-t1)*(t2-t1))

surface
polkadot (float Ka          = 1;
          float Kd          = 1;
          color dot_        = color "rgb" (1, 0, 0);
          color background_ = color "rgb" (1, 1, 1);
          float radius_     = 0.005;
          float distance_   = 0.1)
{

  color c;
  float stile, ttile, ss, tt, rtile;

  basictile (s, t, distance_, distance_, 0.0, 0.0, 0.0, 0.0,
	       stile, ttile, ss, tt);

  rtile = radius_ / distance_;
  
  normal Nf = faceforward(N, I);

  Oi = Os;

  if (dist(ss, tt, 0.5, 0.5) <= rtile)
    c = dot_;
  else
    c = background_;
    
  Ci = Os * c * (Ka*ambient() + Kd*diffuse(Nf));
}