/*
 cake.pov
*/

#include "stdinc.inc"
#include "shapes.inc"
#include "shapesq.inc"

#declare quality = 3;

global_settings {
	assumed_gamma 1
 #if(quality>2)
	radiosity {
		pretrace_start 0.08
		pretrace_end 0.01
		count quality*20
		error_bound 1.2
		recursion_limit 3
		low_error_factor .2
                brightness .9 
		adc_bailout 0.01/2
	}
 #end
}

sky_sphere {
	pigment {gradient y/2+z
		color_map {
			[0 color rgb<0.04,.02,.01>]
			[1 color rgb<0.4,.2,.1>]
		}
	}
}

#default {finish {ambient 0 roughness 0.8}}

/* ---- camera */

#declare CamLoc = <5, 10,-10>;
camera {
	location CamLoc
	up y*image_height right x*image_width
	angle 45
	look_at 0
}

light_source {
	CamLoc+y+x rgb<1,.97,.9>*(quality<2 ? 1.2:0.4)
	area_light quality/6 quality/6 quality*2+1 quality*2+1 circular orient
	fade_distance 8 fade_power 2
}

light_source {
	<15, 9,28.8> rgb<1,.97,.9>
	area_light 1.5 1.5 quality+1 quality+1
	fade_distance 8 fade_power 2 
}

/* ---- boxes */

#declare wall_finish = texture {
	finish{diffuse 0.6}
	pigment {color rgb<0.9,0.85,0.8>}
	normal {crackle frequency .05 phase 0.5}
}

box { /* floor */
	<-20,-10,-30>,<50,-9, 30>
	texture{
		wall_finish
		pigment {color rgb<0.20,.15,.10>}
		finish{phong 1 phong_size .1 specular .5 reflection .2}
	}
}
box { /* right wall */
	< 1,-9,12>,<15,15,13>
	texture{wall_finish}
}
box { /* back wall */
	<-30,-9,29>,< 2,15,30>
	texture{wall_finish}
}

#declare box_finish = finish {
 diffuse 0.6 phong 0.2 phong_size 1
}

box { /* package 1 */
	<-15,-9,-5>,<-9,-4, 5>
	texture {
		pigment {color rgb<0.8,0.2,0.7>}
		finish{box_finish}
	}
}
box { /* package 2 */
	<-15,-9, 8>,<-9,-5,12>
	texture {
		pigment {color rgb<0.05,0.05,0.9>}
		finish{box_finish}
	}
}

box { /* table */
	<-9,-9,-9>,<9,-1,9>
	texture {
		pigment {color rgb<0.3, 0.6, 1>}
		//normal {granite 0.2 scale 0.02}
		finish{box_finish}
	}
}

/* ---- frosting piles (no actual cake) */
union {
	isosurface {
		function {y*.7 - f_snoise3d(x/7, x*.7, z)*.1}
		threshold 0
		contained_by {sphere {<0,-1,0>, 6.5}}
	}
	isosurface {
		function {y*.8+z*.03 - f_snoise3d(x/7, x, z*1.5)*.1}
		threshold 0
		contained_by {sphere {<0,-1,0>, 6.7}}
	}
	texture {
		pigment {color rgb<1.5,.1,.05>}
		finish{brilliance .5 specular 0.3}
	}
} 
 
/* ---- text */
#declare text_finish = texture {
pigment { color rgb <0.96, 0.96, 0.45> }
	finish {diffuse .8}
}

text { ttf "impact.ttf", "Big", .1, 0
	translate <-.6, 0.5, -.03>
	texture{text_finish}
	scale <5,2,1>
	rotate <88, -5, 0>
}
text { ttf "impact.ttf", "Fat", .1, 0
	translate <-.6, -0.6, -.06>
	texture{text_finish}
	scale <5,2,1>
	rotate <91, -5, 0>
}
text { ttf "impact.ttf", "60", .2, 0
	translate <-.5, -1.7, -.15>
	texture{text_finish}
	scale <8,2,1>
	rotate <90, -5, 0>
}

/* ---- candle */
#declare fy = .5;
#declare candleloc = <1.5,fy,-4.5>;
#declare flameloc = candleloc+y*5.1+x*.01;

#declare cr = .3;
#declare ch = 3;
#declare cp = 0;
union {
	blob {
		threshold .2
		cylinder { <0, 0, 0>, <0, ch, 0>, cr-.1 1}
	 #while(cp<=ch)
		cylinder { <0, cp, 0>, <.02, cp-.2, 0>, cr 2}
	  #declare cp = cp+.4;
	 #end
		/* melt drip */
		cylinder { <cr*.4, -.8*cr, cr*-.7>, <cr*.3, ch-cr*.5, cr*-.1>, cr*.4 1}
		pigment{rgbf<.05,.5,.05>}
		finish{specular .1 phong .1}
	}
	/* wick */
	intersection {
		torus {.95,.05 rotate <90,-45>
			translate<.65,ch,.65>}
		sphere{<0,ch-cr*.2,0> .4}
		pigment {
			gradient y+x/5 scale .5
			color_map {
				[.0 rgb 0]
				[.2 rgb 1]
				[.9 rgb 0]
				[1. rgb<.7,.1>]
			}
			translate (fy-.2)*y+x/5
		}
		finish{roughness 1 phong 0 specular 0}       
	}                                                           
	translate candleloc
}

/* ---- flame */
//#default {finish{roughness 0}}

#declare flame_part = object {
	Piriform
	sturm
	rotate -82*z
	scale <.19,1.9,.19>

	texture {
		pigment {
			gradient y scale 2
			color_map {
				[.00 color rgbt<1.0,0.02,.90,.9>]
				[.05 color rgbt<2.5,0.10,.05,.9>]
				[.15 color rgbt<7.2,5.90,.60,.9>]
				[.55 color rgbt<0.9,0.02,.01,.9>]
			}
			translate (fy-.3)*y
		}
		finish{ambient .7 brilliance 0}
	}
}

union {
	object{flame_part translate flameloc}
	/* fume */
	object{flame_part
		rotate 8*z+x*30
		pigment {
			gradient y scale 1
			color_map {
				[.0 color rgbt<0.10,0.05,.01,.95>]
				[.8 color rgbt<0.12,0.09,.05,1>]
			}
			translate (fy+1.6)*y
		}
		finish{ambient 0}
		interior{refraction 1}
		scale<1,3.5,1>
		translate flameloc
		translate <-.3,4.4,1>
	}
}
