/* test.sl - Nathan's silly playground.
 */



float BlinnBRDF( normal Normal; vector View, Light; float rough, fresnel )
{
	vector Half = normalize( View + Light );
	
	
	
	// D
	float D = Normal . Half;
	D = ((D*D) * ((rough*rough)-1)) + 1;
	
	if( D == 0 )
		D = 0.0001;
	D = (rough*rough) / D;
	D = D*D;
	
	
	
	// G
	float temp = View . Half;
	if( temp == 0 )
		temp = 0.0001;
		
	float Ga = 2 * (Normal.Half) * (Normal.View) / temp;
	float Gb = 2 * (Normal.Half) * (Normal.Light) / temp;
	
	float G = min( 1.0, Ga, Gb );
	
	
	
	// F
	float c = View . Half;
	float g = sqrt( (fresnel*fresnel) + (c*c) - 1 );
	
	temp = (g+c)*(g+c);
	if( temp == 0 )
		temp = 0.0001;
	
	float Fa = ((g-c)*(g-c)) / temp;
	
	temp = ((c*(g-c))+1);
	temp = temp*temp;
	if( temp == 0 )
		temp = 0.0001;
	
	float Fb = 1 + ((((c*(g+c))+1)*((c*(g+c))+1)) / temp );
	float F = Fa*Fb;
	
	
	
	return D*G*F;
}



surface test( float diff = 1.0,
                    spec = 1.0,
                    rough = 0.25,
                    fresnel = 2.0;
              color spec_color = color(1,1,1)  )
{
	normal Normal = faceforward( normalize(N), I, N );
	vector View = normalize( -I );
	point Po = transform("object",P);
	
	Oi = Os;
	Ci = color(0,0,0);

	illuminance( P, Normal, PI/2 )
	{
		vector Light = normalize( L );
		float r = (noise(Po*8) * 0.75) + (noise(Po*64) * 0.25);
		
		
		// Lambert diffuse
		Ci += color(1,1,1) * Cs * Cl * (Normal . Light) * diff * r;
		
		// Blinn specular
		float b = BlinnBRDF( Normal, View, Light, rough, fresnel );
		
		Ci += color(1,1,1) * spec_color * Cl * b * spec;
	}
}


