// geo-metric
// 11/2018 christian perle

#declare A = seed(443);

#declare HQ = 1;
//#declare HQ = 0;

camera {
	location <-5, 8, -15>
	direction 2*z
	look_at 0
	#if (HQ = 1)
	focal_point 0
	aperture 0.4
	blur_samples 20
	#end
}

light_source {
	<5, 8, -15> color rgb 1
	#if (HQ = 1)
	area_light 3*x, 3*y, 5, 5
	#end
}

#macro Cube(P)
superellipsoid {
	<.15, .15>
	scale .5
	hollow
	pigment { P }
	finish {
		phong .7 phong_size 18
		reflection .1
	}
	interior {
		ior 1.33
		fade_distance 5
		fade_power 1
		caustics 1
	}
	normal { ripples .1 scale .05 }
	rotate 90*x
}
#end

#macro Triangle(P)
prism {
	linear_sweep
	linear_spline
	0, 3, 4,
	<-7, 5.5>, <0, -6>, <7, 5.5>, <-7, 5.5>
	scale .1
	hollow
	pigment { P }
	finish {
		phong .7 phong_size 18
		reflection .1
	}
	interior {
		ior 1.33
		fade_distance 5
		fade_power 1
		caustics 1
	}
	normal { bozo .1 scale .1 }
	rotate 90*x
}
#end

#macro Sphere(P)
sphere {
	0, .5 hollow
	pigment { P }
	finish {
		phong .7 phong_size 18
		reflection .1
	}
	interior {
		ior 1.33
		fade_distance 5
		fade_power 1
		caustics 1
	}
	normal { spiral2 14 .1 }
	rotate 90*x
}
#end

#macro Torus(P)
torus {
	.5, .15 hollow
	pigment { P }
	finish {
		phong .7 phong_size 18
		reflection .1
	}
	interior {
		ior 1.33
		fade_distance 5
		fade_power 1
		caustics 1
	}
	normal { crackle .1 scale .05 }
	rotate 90*x
	translate .15*y
}
#end

#declare L = 0.04;

// plane with grid texture
plane {
	y, -.5
	texture {
		pigment { rgb 1 }
		finish { reflection .1 }
	}
	texture {
		pigment {
			gradient x
			color_map {
				[0 rgb 0]
				[L rgbf 1]
				[1-L rgbf 1]
				[1 rgb 0]
			}
		}
		translate -5000*x
	}
	texture {
		pigment {
			gradient x
			color_map {
				[0 rgb 0]
				[L rgbf 1]
				[1-L rgbf 1]
				[1 rgb 0]
			}
		}
		rotate 120*y
		translate -5000*z
	}
	texture {
		pigment {
			gradient x
			color_map {
				[0 rgb 0]
				[L rgbf 1]
				[1-L rgbf 1]
				[1 rgb 0]
			}
		}
		rotate 240*y
		translate -5000*z
	}
}

// number of objects
#declare N = 100;

// array for storing object locations
#declare Pos = array[N]
#declare C = 0;
#while (C < N)
	#declare Pos[C] = <1000, 0, 1000>;
	#declare C = C + 1;
#end

// random objects
#declare C = 0;
#while (C < N)
	#declare OK = 0;
	#while (OK = 0)
		// pick random location
		#declare Loc = <-10+20*rand(A), 0, -15+30*rand(A)>;
		// check distance to other objects
		#declare DOK = 1;
		#declare I = 0;
		#while (I < C)
			#declare D = vlength(Loc-Pos[I]);
			// too close
			#if (D < 2)
				#declare DOK = 0;
			#end
			#declare I = I + 1;
		#end
		// distance okay, exit loop
		#if (DOK = 1)
			#declare OK = 1;
		#end
	#end
	#declare Pos[C] = Loc;
	#declare Shape = int(4*rand(A));
	#if (HQ = 1)
	#declare objcolor = rgbf<rand(A), rand(A), rand(A), .7>;
	#else
	#declare objcolor = rgb<rand(A), rand(A), rand(A)>;
	#end
	object {
		#switch (Shape)
			#case (0)
				Cube(objcolor)
			#break
			#case (1)
				Triangle(objcolor)
			#break
			#case (2)
				Sphere(objcolor)
			#break
			#case (3)
				Torus(objcolor)
			#break
		#end
		rotate 180*rand(A)*y
		translate Loc
	}
	#declare C = C + 1;
#end