// Persistence of Vision Ray Tracer Scene Description File
// File: ?.pov
// Vers: 3.5
// Desc: Basic Scene Example
// Date: mm/dd/yy
// Auth: ?
//

#version 3.5;

#include "colors.inc"

// radiosity (global illumination) settings
global_settings { 
	ambient_light rgb .3
  radiosity {
    pretrace_start 0.08           // start pretrace at this size
    pretrace_end   0.04           // end pretrace at this size
    count 35                      // higher -> higher quality (1..1600) [35]
    nearest_count 5               // higher -> higher quality (1..10) [5]
    error_bound 1.8               // higher -> smoother, less accurate [1.8]
    recursion_limit 3             // how much interreflections are calculated (1..5+) [3]
    low_error_factor .5           // reduce error_bound during last pretrace step
    gray_threshold 0.0            // increase for weakening colors (0..1) [0]
    minimum_reuse 0.015           // reuse of old radiosity samples [0.015]
    brightness 1                  // brightness of radiosity effects (0..1) [1]

    adc_bailout 0.01/2
    //normal on                   // take surface normals into account [off]
    //media on                    // take media into account [off]
    //save_file "file_name"       // save radiosity data
    //load_file "file_name"       // load saved radiosity data
    //always_sample off           // turn sampling in final trace off [on]
    //max_sample 1.0              // maximum brightness of samples
  }
}


#declare A=100;
#declare B=100;


#debug "Setting up Camera\n"
// ----------------------------------------

camera {
  location  <0, 15, -B/2>
  direction 1.5*z
  right     x*image_width/image_height
  look_at   <0.0, 0.0,  0.0>
}

sky_sphere {
  pigment {
    gradient y
    color_map {
      [0.0 rgb <0.5,0.7,1.0>]
      [0.7 rgb <0.0,0.4,0.7>]
    }
  }
}

light_source {
  <0, 0, 0>            // light's position (translated below)
  color rgb 2  // light's color
  translate <-40, 80, -20>*1000
}

// ----------------------------------------
#debug "Creating Land Array\n"
#declare R=seed(1.23487);
#declare GC1=seed(1.63487);
#declare H=array[A][B];
#declare a=0;
#while (a<A)
	#declare b=0;
	#while(b<B)
		#declare D=sqrt( (A/2-a)*(A/2-a) + (B/2-b)*(B/2-b) );
		//#declare H[a][b]=sin(sqrt(a))+sin(a/2+b/2)+sin(sqrt(b))+cos(b/30);
		
		#declare H[a][b]=rand(R)*D/10;
		
		#if (a=0) #declare H[a][b]=0; #end
		#if (a=A-1) #declare H[a][b]=0; #end
		#if (b=0) #declare H[a][b]=0; #end
		#if (b=B-1) #declare H[a][b]=0; #end
		
		#declare b=b+1;
	#end
	#declare a=a+1;
#end


#debug "Averaging Land\n"
#declare a=1;
#while (a<A-1)
	#declare b=1;
	#while(b<B-1)		
		#declare H[a][b]=(H[a-1][b-1]+H[a-1][b]+H[a-1][b+1]+H[a][b-1]+H[a][b]+H[a][b+1]+H[a+1][b-1]+H[a+1][b]+H[a+1][b+1])/9;
		#declare b=b+1;
	#end
	#declare a=a+1;
#end



#debug "EnMeshing\n"
#declare MyMesh=mesh{
#declare a=1;
#while (a<A)
	#declare b=1;
	#while(b<B)
		triangle{<a,H[a][b],b>, <a-1,H[a-1][b],b>, <a,H[a][b-1],b-1>}
		triangle{<a-1,H[a-1][b-1],b-1>, <a-1,H[a-1][b],b>, <a,H[a][b-1],b-1>}
		#declare b=b+1;
	#end
	#declare a=a+1;
#end
	texture{
		pigment{color Tan}
		normal{granite 1 scale .1}
	}
}

#debug "Creating Grass Proto\n"
//vars for the grass blade
#declare A1=<-1,0,0>;
#declare B1=A1*<-1,1,1>;
#declare C1=<-.5,4,.5>;
#declare D1=C1*<-1,1,1>;
#declare E1=<-.25,6,2>;
#declare F1=E1*<-1,1,1>;
#declare G1=<0,7,4>;

#declare GrassMesh=	mesh{
		triangle{A1,B1,C1}
		triangle{B1,C1,D1}
		triangle{C1,E1,D1}
		triangle{E1,F1,D1}
		triangle{E1,F1,G1}	
		texture{
			pigment{color rgb<0,rand(GC1),0.5*rand(GC1)>}
			finish{phong 1}
		}
}

#macro Grassblade()
	object{GrassMesh
		scale <0.1,0.6+rand(R),0.8>*.25
		rotate < (rand(R)-0.25)*90,
		 (rand(R)-0.5)*90,
		 (rand(R)-0.75)*90>
	}
#end


union{

	#object{MyMesh}

#debug "Planting Grass\n"
//Grass
#declare a=0;
#while (a<A)
	union{
	#declare b=0;
	#while(b<B)
		#declare P=trace(MyMesh,<b,1000,a>,-y);
		#if (int(P.y)=int(int(P.y/2)*2)) 
			#object{Grassblade()
				translate P
			}		
		#end
		#declare b=b+rand(R)/2;
	#end
	
	#debug concat("Planting grass row ",str(a,0,1)," of ",str(A,0,1),"\n")
	bounded_by{box{<0,0,b><A,2,b+1>}}
	}
	#declare a=a+rand(R);
#end
	translate <-A/2,0,-B/2> 

}


//water
plane{y,0
	texture{
		pigment{color rgb .6}
		normal{bumps 7}
		finish{phong 0.75 reflection .3}
	}
	rotate <0,30,0>
	scale < 1.5,0,2>
	translate 0.5*y
}