// 0 = simple gradient skysphere
// 1 = emitting media
#declare sky_type = 1;
// Radiosity looks bad here, probably because I don't know how to use it.
#declare use_radiosity = 0;

// Arbaro
#include "palm.inc"

#include "functions.inc"
#include "rad_def.inc"
#declare invsquare = function { 1 / pow(2, sqrt(x*x + y*y + z*z + 0.01)) }
#declare sinewave = function { sin(x) + sin(y)/2 + 0.5 + sin(z) }
// Screw you, I'm defining my own gradient function... bastards...
// height = y, base = sqrt(x^2+z^2), angle = arctan(opp/adj) = arctan(height/base)
// This is in radians. Horizon is 0 rad, zenith is pi/2 rad.
// This is for a texture map, so let's get that into [0,1] instead of [0,pi/2]
#declare gah = function { atan2(y, sqrt(x*x+z*z)) / (3.14 / 2)  }

global_settings {
	adc_bailout 0.00392157
	assumed_gamma 1.5
	noise_generator 2
	#if (use_radiosity)
		radiosity { Rad_Settings(Radiosity_OutdoorLight, off, off) }
	#end
}

#if (sky_type = 0)
	sky_sphere {
		pigment {
			gradient y
			//granite
			color_map {
				[(1-cos(radians(  0)))/2 color rgb <0.5,0,0>]
				[(1-cos(radians(45)))/2 color rgb <0.5,0.4,0>]
				[(1-cos(radians(90)))/2 color rgb <0.5,0,0>]
			}
		}
	}
	#declare sun_vector = <64, 5, 90>;
	#declare sun = sphere {
		sun_vector * 4 - <0,12,0>, 20
		pigment { color rgbt 1 }
		interior { media {
			emission 0.05
			density { bozo density_map {
				[0 rgb <1,0.5,0>]
				//[0.6 rgb <0,0,0>]
				[1 rgb <0,0,0>] }
			}
		} }
		hollow
	}
	
	light_source {
		sun_vector, rgb <1, 1, 1>
		looks_like { sun }
	}
	object { sun }
#end



#if (sky_type = 1)
	#declare sun_vector = <64, 5, 90>;
	#declare sun = sphere {
		sun_vector*3 - <0,5,0>, 20
		pigment { color rgbt 1 }
		interior {
			media {
				emission 0.1
				density { bozo density_map {
					[0 rgb <1,0.5,0>]
					[1 rgb <0,0,0>] }
				}
			}
		}
		hollow
	}
	
	sky_sphere { pigment { color rgb <1,1,1> }  }
	
	light_source {
		sun_vector, rgb <1, 1, 1>
	}
	object { sun }

	#if(use_radiosity = 0)
		light_source {
			<0, 6, 0>, 0.1
			shadowless
			media_interaction off
		}
	#end
	
	#declare sky_red = density {
		bozo density_map {
			[0 rgb 0.6]
			[0.2 rgb <0.2, 0.5, 0.5>]
			[0.5 rgb <0.0, 0.3, 0.8>]
			[0.7 rgb <0.2, 0.3, 0.8>]
			[0.8 rgb <0.05, 0.8, 0.9>]
			[0.9 rgb <0.2, 0.3, 0.8>]
			[1 rgb 0.6]
		}
		scale <4, 0.8, 4>
	}
	#declare sky_red2 = density {
		bozo density_map {
			[0 rgb 0.7]
			[0.2 rgb <0.2, 0.6, 0.6>]
			[0.5 rgb <0.0, 0.4, 0.9>]
			[0.7 rgb <0.2, 0.4, 0.9>]
			[0.8 rgb <0.05, 0.9, 1.0>]
			[0.9 rgb <0.2, 0.4, 0.9>]
			[1 rgb 0.7]
		}
		scale <4, 1.5, 4>
	}
	#declare sky_yellow = density {
		bozo density_map {
			[0 rgb 0.2]
			[0.2 rgb <0.14, 0.11, 0.19>]
			[0.5 rgb <0.00, 0.06, 0.35>]
			[0.6 rgb <0.01, 0.16, 0.14>]
			[0.7 rgb <0.04, 0.08, 0.05>]
			[0.9 rgb <0.02, 0.12, 0.4>]
			[1 rgb 0.2]
		}
		scale <6, 0.53, 6>
	}
	#declare sky_yellow2 = density {
                bozo density_map {
                        [0 rgb 0.2]
                        [0.2 rgb <0.14, 0.21, 0.39>]
                        [0.5 rgb <0.05, 0.16, 0.45>]
                        [0.6 rgb <0.11, 0.26, 0.24>]
                        [0.7 rgb <0.14, 0.18, 0.15>]
                        [0.9 rgb <0.12, 0.22, 0.5>]
                        [1 rgb 0.2]
                }
                scale <9, 0.79, 9>
        }
	#declare sky_orange = density {
		bozo density_map {
			[0 rgb<0.05, 0.31, 0.90>]
			[0.5 rgb<0.05, 0.24, 0.93>]
			[1.0 rgb<0.05, 0.31, 0.90>]
		}
		scale <6, 0.4, 6>
	}
	#declare sky_orange_light = density {
		bozo density_map {
			[0 rgb<0.05, 0.18, 0.80>]
			[0.5 rgb<0.09, 0.11, 0.73>]
			[1.0 rgb<0.05, 0.18, 0.80>]
		}
		scale <21, 0.44, 22>
	}
	#declare sky_gradient = density {
		function { gah(x,y,z) } density_map {
		//gradient y density_map {
			[0 sky_red ]
			[0.1 sky_red2 ]
			[0.2 sky_orange ]
			[0.3 sky_yellow ]
			[1.0 sky_orange_light ]
		}
		scale <1,1,1>
	}
	sphere {
		<0,0,0> 60
		translate 15*y
		rotate 0
		pigment { rgbt 1 }
		hollow
		interior {
			media {
			absorption 0.07
			density { sky_gradient }
			}
		}
	}
#end

#declare bark = pigment {
     onion
     //spiral1 1
     turbulence 0.7
     color_map {
	 [ 0 color rgb <0.50, 0.42, 0.25> ]
         [ 0.2 color rgb <0.55, 0.50, 0.22> ]
	 [ 0.25 color rgb <0.52, 0.35, 0.25> ]
         [ 0.4 color rgb <0.55, 0.50, 0.52> ]
	 [ 0.5 color rgb <0.52, 0.35, 0.25> ]
         [ 1 color rgb <0.50, 0.42, 0.25> ]       
     }
     rotate 100*x + 180*z
     scale <1, 0.2, 1>
}

#declare vine_texture = texture {
	finish {
		//ambient rgbft <0.188235, 0.196078, 0.156863, 0.1, 0.1>
		diffuse 0.9
		brilliance 1
		specular 0.5
		roughness 0.0029512
	}
	pigment {
		gradient <1, 1, 1>
		turbulence <0.4, 0.4, 0.4>
		pigment_map {
			[ 0   rgb <0.49, 0.50, 0.22> ]
			[ 0.6 bark ]
			[ 1.0 rgb <0.49, 0.50, 0.22> ]
		}
		//rotate z*130
		//scale 0.1
	}
	normal {
		gradient <1, 1, 1> 1
		turbulence <0.4, 0.4, 0.4>
		slope_map {
			[ 0 <0, 0> ]
			[ 0.6 <0.5, 2> ]
			[ 0.9 <0.2, 4> ]
		}
		rotate z*130
		scale 0.1
	}
}

#declare leaf = texture {
   finish {
     brilliance 3
     specular 0.4
     roughness 0.005
   }
   pigment {
     spotted
     turbulence 0.2
     color_map {
         [0 color rgb <0.10, 0.46, 0.09>]
         [0.5 color rgb <0.06, 0.27, 0.05>]
         [0.75 color rgb <0.63, 0.68, 0.08>]
         [1 color rgb <0.1, 0.46, 0.09>]
     }
   }
}

#declare palmtree = merge {
    object {
        palm_13_stems
        texture { vine_texture }
    }
    object {
        palm_13_leaves
        texture { leaf }
    }
}

#declare sand2 = texture {
   finish {
      ambient rgb <0.19, 0.19, 0.19>
      diffuse 0.8
   }
   //*PMRawBegin
   normal { bumps 0.12 scale 0.0001 }
   //*PMRawEnd
   scale 0.0001
   
   pigment {
      granite
      turbulence <0.6, 0.6, 0.6>
      
      color_map {
         [ 0 color rgb <0.65, 0.62, 0.45> ]
         [ 0.1 color rgb <0.19, 0.19, 0.19> ]
         [ 0.2 color rgb <0.82, 0.77, 0.70> ]
         [ 0.35 color rgb <1, 0.83, 0.64> ]
         [ 0.4 color rgb <0.50, 0.50, 0.42> ]
         [ 0.7 color rgb <0.25, 0.25, 0.25> ]
         [ 1 color rgb <0.47, 0.45, 0.42> ]
      }
   }
}

#declare sand3 = texture {
   finish {
      ambient rgb <0.19, 0.19, 0.19>
      diffuse 0.8
   }
   //normal { bumps 0.12 scale 0.0001 }
   scale 0.0001
   
   pigment {
      granite
      turbulence <0.6, 0.6, 0.6>
      
      color_map {
         [ 0 color rgb <0.65, 0.62, 0.45> ]
         [ 0.1 color rgb <0.39, 0.39, 0.39> ]
         [ 0.2 color rgb <0.82, 0.77, 0.70> ]
         [ 0.35 color rgb <1, 0.83, 0.64> ]
         [ 0.4 color rgb <0.60, 0.55, 0.42> ]
         [ 0.7 color rgb <0.65, 0.65, 0.65> ]
         [ 1 color rgb <0.57, 0.45, 0.42> ]
      }
   }
}

#declare sand4_wet = texture {
   finish {
      ambient 0.02
      diffuse 0.4
      specular 0.8 roughness 0.0005
      reflection <0.14, 0.10, 0.06>
   }
   normal { bumps 0.12 scale 0.0001 }
   scale 0.0001
   
   pigment {
      granite
      turbulence <0.6, 0.6, 0.6>
      
      color_map {
         [ 0 color rgb <0.60, 0.57, 0.40> ]
         [ 0.1 color rgb <0.34, 0.34, 0.34> ]
         [ 0.2 color rgb <0.77, 0.72, 0.66> ]
         [ 0.35 color rgb <0.95, 0.79, 0.60> ]
         [ 0.4 color rgb <0.55, 0.51, 0.39> ]
         [ 0.7 color rgb <0.61, 0.60, 0.61> ]
         [ 1 color rgb <0.52, 0.41, 0.39> ]
      }
   }
}

#declare sand4 = texture {
   finish {
      ambient 0.1
      diffuse 0.8
      specular 0.05 roughness 0.0005
      //reflection <0.14, 0.10, 0.06>
   }
   //normal { bumps 0.12 scale 0.0001 }
   scale 0.0001
   
   pigment {
      granite
      turbulence <0.6, 0.6, 0.6>
      
      color_map {
         [ 0 color rgb <0.65, 0.62, 0.45> ]
         [ 0.1 color rgb <0.39, 0.39, 0.39> ]
         [ 0.2 color rgb <0.82, 0.77, 0.70> ]
         [ 0.35 color rgb <1, 0.83, 0.64> ]
         [ 0.4 color rgb <0.60, 0.55, 0.42> ]
         [ 0.7 color rgb <0.65, 0.65, 0.65> ]
         [ 1 color rgb <0.57, 0.45, 0.42> ]
      }
   }
}

#declare sand = texture {
   gradient x + z
   texture_map {
     [ 0.0 sand4 ]
     [ 0.5 sand4 ]
     [ 0.65 sand4_wet ]
     [ 1.0 sand4_wet ]
   }
      // Tune these so it starts with the dry sand and gets to its max near
      // the water
      scale <20,1,20>
      translate <-3,0,-3>
      
}

#declare sky1 = texture {
   pigment {
      bozo
      turbulence <1, 0, 1>
      scale <2, 1, 0.5>
      
      pigment_map {
         [ 0 color rgbt <0.19216, 0.14902, 0.21569, 0.8> ]
         [ 0.3 color rgbt <0.533333, 0.301961, 0.690196, 0.2> ]
         [ 0.6 color rgbt <0.21569, 0.16863, 0.23922, 0.8> ]
      }
   }
   finish {
      ambient rgb <0.24314, 0.24314, 0.24314>
      diffuse 0
   }
}

#declare water =  material {
    texture {
      pigment {
        color rgbf <0.7, 0.4, 0.9, 0.6>
      }
      finish {
        ambient 0.0
        diffuse 0.0

        reflection {
          0.0, 1.0
          fresnel on
        }

        specular 0.4
        roughness 0.003
      }
      normal {
        function {
          f_ridged_mf(x, y, z, 0.1, 3.0, 7, 0.7, 0.7, 2)
        } 0.8
        scale 0.5
      }
    }
    interior {
      ior 1.3
    }
  }





#declare xmin = -8;
#declare zmin = -8;
#declare xscale = 64;
#declare zscale = 64;
#declare xcount = 8;
#declare zcount = 8;
#declare r = <6,0,-5.5>;
#declare height = 6;
// Y start
#declare y1 = -2;

#declare sandfloor = height_field {
   png "/home/hodapp/geomorph/floor1.png"
   smooth
}

#declare cx = 0;
#while (cx < xcount)
   #declare cz = 0;
   #while (cz < zcount)
      object {
         sandfloor
         scale <xscale, height, zscale>
         translate <xmin + cx * xscale, y1, zmin + cz * zscale>
         rotate r
         texture { sand }
      }
      #declare cz = cz + 1;
   #end
   #declare cx = cx + 1;
#end

box {
    <xmin, -0.75, zmin>,
    <xmin + xscale * xcount, -3, zmin + zscale * zcount>
    material { water }
}

object {
    palmtree
    rotate 90*y
    scale 0.25
    translate <0.5,0.5,-0.5>
}

light_source {
   <-0.5, 0.25, -1.5>, 0.1
   shadowless
   media_interaction off
}

camera {
   perspective
   location <-2.5, 3, -2.5>
   sky <0, 1, 0>
   direction <0, 0, 1>
   right <1.33333, 0, 0>
   up <0, 1, 0>
   look_at <0, 2.5, 0>
}