// -- Variables and setup

// usage: #declare MyColor = GammaColor(rgb <R,G,B>, G)
// usage: #declare MyColor = GammaColor(rgbt <R,G,B,T>, G); 
// usage: #declare MyColor = GammaColor(rgbf <R,G,B,F>, G);

#macro GammaColor(Color,Gamma)
	rgbft<pow(Color.red,Gamma), pow(Color.green,Gamma),	pow(Color.blue,Gamma), Color.filter, Color.transmit>
#end

// -- Some Includes
#include "colors.inc"
//#include "grid.inc"
#include "woods.inc"

// -- Work Area
#declare UseGrid = no;
#declare WorkMode = no;

// -- Scene Complexity
#declare Detail = yes;
#declare DoObjects = yes;
#declare DoMedia = no;

// -- Lighting Types
#declare AreaLight = no;
#declare SpotLight = no;
#declare ParaLight = no;
#declare FocalBlur = no;

// 0=no rad, 1=load, 2=save
#declare UseRad = 0;

global_settings {
	max_trace_level 60
	noise_generator 3
	file_gamma 2.2
	#if (UseRad)
		radiosity {
			#if (UseRad=2)
				pretrace_start 0.08
				pretrace_end   0.01
				count 250
				nearest_count 20
				error_bound 0.1
				recursion_limit 2
				low_error_factor .9
				gray_threshold 0.5
				minimum_reuse 0.015
				brightness 1.0
				adc_bailout 0.01/2
				media on
			#else
				pretrace_start 1
				pretrace_end 1
				error_bound 0.1
				gray_threshold 0.5
				recursion_limit 2
				always_sample off
				media on
		  #end
			}
		ambient_light 0
	#end
	}

#default {finish {ambient 0 diffuse 1 specular 0.15 roughness 1e-4}}

/*#if (UseGrid)
	object {grid scale 1 translate y*0}
#end*/

#if (WorkMode)
	#declare CamPos = <0,0,-10>;
	#declare LookAt = <0,0,0>;
	#declare ZF = 1.85*z;
#else
	#declare CamPos = <10,6.5,5>;
	#declare LookAt = <-10,6.5,10>;
	#declare ZF = 2.25*z;
#end

// -- Camera & Lighting
camera {
	perspective
	location CamPos
	look_at  LookAt
	#if (FocalBlur)
		aperture 0.6
		blur_samples 128
		confidence 0.9
		variance 1/5000
		focal_point <0,-2.3,0>
	#end
	#if (WorkMode)
		rotate x*10
		rotate y*230
	#else
		rotate x*0
		//rotate y*330
	#end
	right  x*image_width/image_height
	direction ZF
	}

#declare LightPos = <0,0,-150>;
#declare LightColor = GammaColor(White+Gold, 2.2);
#declare LightDist = 150;

// --- Main Light
light_source {
	0,LightColor
	#if (ParaLight)
		parallel
	#end
	#if (SpotLight)
		spotlight
	#end
	#if (AreaLight)
		area_light x*10, y*10 5,5 adaptive 1 jitter orient circular
	#end
	fade_distance LightDist
	fade_power ln(1/(sqrt(2)-1))/ln(2)
	media_interaction on
	media_attenuation on
	translate LightPos
	rotate x*32
	rotate y*175
	#if (ParaLight)
		point_at <0,0,0>
	#end
	#if (SpotLight)	
		point_at <0,0,0>
		radius 9
		falloff 11
		tightness 0
	#end
	}

// A light source inside the room which does not
// interact with media so that we can see the room:
light_source { <14, 5, 2>, GammaColor( (White+Gold)*0.5, 2.2) media_interaction off }

background { GammaColor(rgb <0.3,0.6,0.9>, 2.2) }

// Textures and Materials
#declare Glass_Finish =
finish {
	specular 0.6
	roughness 0.002
	ambient 0
	diffuse 0.1
	brilliance 5
	reflection {
		0.1, 1.0
		fresnel on
		}
	conserve_energy 
	};
	
#declare Amethyst_Glass_Interior =
interior {
	ior 1.2
	caustics 1
	fade_distance 2
	fade_power 1001
	fade_color GammaColor (rgb <0.6980, 0.4902, 0.6235>, 2.2)
	};

#declare AmethystGlass =
material {
	texture {
		pigment { GammaColor (rgbf <0.6980, 0.4902, 0.6235, 1>, 2.2) }
		finish {Glass_Finish}
		normal {agate 0.25 scale 0.1 agate_turb 0.25}
		}
	interior {Amethyst_Glass_Interior}
	};

#declare Ruby_Glass_Interior =
interior {
	ior 1.2
	caustics 1
	fade_distance 2
	fade_power 1001
	fade_color GammaColor (rgb <0.8593, 0.0781, 0.2343>, 2.2)
	};

#declare RubyGlass =
material {
	texture {
		pigment { GammaColor (rgbf <0.8593, 0.0781, 0.2343, 1>, 2.2) }
		finish {Glass_Finish}
		normal {agate 0.25 scale 0.1 agate_turb 0.25}
		}
	interior {Ruby_Glass_Interior}
	};

#declare WindowFrameSegment =
difference {
	union {
		box { <-3, 2.25, -0.35>, <3, 2.5, 0.35> }
		cylinder { <-3, 2.375, 0.35>, <3, 2.375, 0.35>, 0.075 }
		cylinder { <-3, 2.375, -0.35>, <3, 2.375, -0.35>, 0.075 }

		#if (Detail)
			texture { T_Wood29 rotate y*90 scale <1, 0.1, 0.35> }
		#else
			pigment { GammaColor(DarkWood, 2.2) }
		#end
		
		}

	cylinder { <-1.25, 0, 0>, <1.25, 0, 0>, 0.05 translate <0, 2.5, 0.35>  }
	cylinder { <-1.25, 0, 0>, <1.25, 0, 0>, 0.05 translate <0, 2.5, -0.35> }
	cylinder { <-1.25 ,0, 0>, <1.25, 0, 0>, 0.05 translate <0, 2.25, 0.35> }
	cylinder { <-1.25, 0, 0>, <1.25, 0, 0>, 0.05 translate <0, 2.25, -0.35> }

	box { <4, 0, 0.5>, <-4, -3, -0.5> rotate z*67.5 }
	box { <4, 0, 0.55>, <-4, -3, -0.55> rotate z*292.5 }

	
	/*#if (Detail)
			texture { T_Wood29 rotate y*90 scale <1, 0.1, 0.35> }
	#else
			pigment { GammaColor(DarkWood, 2.2) }
	#end*/

	cutaway_textures

	};

#declare WindowFrame =
union {
	#declare i = 0;
	#while (i < 360)
		object { WindowFrameSegment rotate z*i  }
		#declare i = i + 45;
	#end
	};

#declare GlassPiece =
difference {
	box { <-2.375, 0, -0.02>, <2.375, 2.375, 0.02> }
	box { <4, 0, 0.5>, <-4, -3, -0.5> rotate z*67.5 }
	box { <4, 0, 0.55>, <-4, -3, -0.55> rotate z*292.5 }
	};

#declare Window =
union {
	object { WindowFrame }

/*
	#declare i = 0;
	#while ( i < 360 )
		object { GlassPiece	hollow on	material { AmethystGlass } rotate z*i	}
		#declare i = i + 90;
	#end

	#declare j = 45;
	#while ( j < 360 )
		object { GlassPiece	hollow on	material { RubyGlass } rotate z*j }
		#declare j = j + 90;
	#end
*/

	// inner frame
	union {	
		#declare k = 22.5;
		#while (k < 360)
			cylinder { <0, 0, 0>, <0, 2.475, 0>, 0.05 pigment { GammaColor (rgb 0.25, 2.2) } rotate z*k }
			#declare k = k + 45;
		#end

		union {	
			difference {
				cylinder { <0, 0, -0.1>, <0, 0, 0.1>, 0.2 }
				torus { 0.2, 0.02 rotate x*90 translate z*0.1 }
				torus { 0.2, 0.02 rotate x*90 translate z*-0.1 }
				}
			sphere { 0, 0.18 scale <1, 1, 0.5> translate z*0.1 }
			sphere { 0, 0.18 scale <1, 1, 0.5> translate z*-0.1 }
			pigment { GammaColor (rgb 0.25, 2.2) }
			}
		}
	};

#declare WindowCutOut =
#declare i = 0;
difference {
	box { <-3, -2.5, -0.35>, <3, 2.5, 0.35> }
	#while (i < 360)
		box { <-3.1, 0, -0.375>, <3.1, 2.5, 0.375>  translate y*2.5 rotate z*i }
		#declare i = i + 45;
	#end
	};

// Room
#declare Room =
union {
	difference {
		box { <-11, -7, -11>, <16, 7, 10.5> }
		box { <-10, -6, -10>, <15, 6, 10> }
		object { WindowCutOut scale 1.5 translate <-1, 0.5, 10.25> }
		}

	object { Window translate <-1, 0.5, 10.25> }

	// scattering media box:
	#if (DoMedia)
		box { <-10.5, -6.5, -10.5>, <3, 6.5, 10.25>
			pigment { rgbt 1 }
			hollow on
			interior {
				media {
					scattering { 4, 0.035 extinction 0.1 }
					method 3
					samples 32 //256
					}
				}
		}
	#end

	pigment { GammaColor(Orange, 2.2) }

	};

#if (DoObjects)

	object { Room translate y*6 }

#end



