#version 3.5;
#include "library_500.lib"
#include "material_500.lib"

#include "colors.inc"

//constants from Anton Raves sample scenes
#declare Final_town    = 1;
#declare Final_space   = 2;
#declare Final_diver   = 3;
#declare Preview_town  = 4;
#declare Preview_space = 5;
#declare Preview_diver = 6;
#declare Building_test = 7;

#declare Quality=Preview_town;  

#declare setting=2;

global_settings {
  assumed_gamma 1.0
  max_trace_level 5
}

// ----------------------------------------

#declare basic_finish =
finish { ambient 0.4 diffuse 0.6 brilliance 1 phong 0 phong_size 40 reflection 0 }
#switch ( Quality )
  #case ( Final_town )
    light_source { <500, 500, 0> color white area_light 50*y, 50*z, 5, 5 adaptive 0 jitter rotate 105*y }
    #declare nice = color white;
    #declare nice2 = color rgb <.2, .2 ,.8>;
    #declare grad_tex =
    texture {
      pigment { gradient y color_map { [ 0.0 color nice ] [ 0.6 color nice2 ] } }
      finish { basic_finish }
      scale 200
    }
    #break
  #case ( Final_space )
    light_source { <500, 500, 0> color white area_light 50*y, 50*z, 5, 5 adaptive 0 jitter }
    #declare nice = color rgb <1, .8, .6>;
    #declare nice2 = color black;
    #declare grad_tex =
    texture {
      pigment { gradient y color_map { [ 0.0 color nice ] [ 0.4 color rood ] [ 0.8 color black ] } }
      finish { basic_finish }
      scale 200
    }
    #break
  #case ( Final_diver )
    light_source { <500, 500, 0> color white area_light 50*y, 50*z, 5, 5 adaptive 0 jitter }
    #declare nice = color t_cyan;
    #declare nice2= color _blue;
    #declare grad_tex =
    texture {
      pigment { gradient y color_map { [ 0.0 color t_cyan ] [ 0.4 color t_cyan ] [ 0.8 color _blue ] } }
      finish { basic_finish }
      scale 200
    }
    #break
  #case ( Preview_town )
    light_source { <500, 500, 0> color white rotate 105*y }
    #declare nice = color white;
    #declare nice2 = color rgb <.2, .2, .8>;
    #declare grad_tex =
    texture {
      pigment { gradient y color_map { [ 0.0 color white ] [ 0.6 color nice2 ] } }
      finish { basic_finish }
      scale 200
    }
    #break
  #case ( Preview_space )
    light_source { <500, 500, 0> color white }
    #declare nice = color rgb <1, .8, .6>;
    #declare nice2= color black;
    #declare grad_tex =
    texture {
      pigment { gradient y color_map { [ 0.0 color nice ] [ 0.4 color rood ] [ 0.8 color black ] } }
      finish { basic_finish }
      scale 200
    }
    #break
  #case ( Preview_diver )
    light_source { <500, 500, 0> color white }
    #declare nice = color t_cyan;
    #declare nice2= color _blue;
    #declare grad_tex =
    texture {
      pigment { gradient y color_map { [ 0.0 color t_cyan ] [ 0.4 color t_cyan ] [ 0.8 color _blue ] } }
      finish { basic_finish }
      scale 200
    }
    #break
  #case ( Building_test )
    light_source { <500, 500, 0> color white }
    #declare nice = color white;
    #declare nice2 = color rgb <.2, .2 ,.8>;
#end

camera {
  location  <0, 37.5, -160>
  direction 2*z
  up        y
  right     computer*x
  look_at   <0, 37.5, 0>
}

//macro to create fullblown minifig using parts from Anton Raves LEGO library
//Parameters:
//head_tex - texture applied to the head (including face image map)
//headrotation - how much to rotate head
//usehair - put hair on minifig (1) or not (0)
//hair - hair object
//arm_tex - texture for minifig arms
//rightarmrotation - how much to rotate right arm
//leftarmrotation - how much to rotate left arm
//body_tex - texture applied to minifig body
//bodypaint_tex - paint texture (image map) for minifig body
//bodyrotation - how much to rotate body
//leg_tex - texture for minifig legs and leg joiner piece
//rightlegrotation - how much to rotate right leg
//leftlegrotation - how much to rotate left leg
//use_held_object - put object into minifig right hand (1) or not (0)
//held_object - object for the minifig to hold
#macro minifig(head_tex, headrotation,
                usehair, hair,
                arm_tex, rightarmrotation,
                leftarmrotation,  
                hand_tex, righthandrotation,
                lefthandrotation,
                body_tex, bodypaint_tex, bodyrotation,
                leg_tex, rightlegrotation,
                leftlegrotation,
                use_held_object,
                held_object)
	                       
    //normalize head rotation	                       
    #if (headrotation > 45)
	    #declare headrotation=45;
    #end
    #if (headrotation < -45)
	    #declare headrotation=-45;
	#end	   

    union 
    {
        object { left_leg(leg_tex) rotate rightlegrotation*x }
        object { right_leg(leg_tex) rotate leftlegrotation*x }
                
        union 
        {
            //leg joiner and body with its paint
            object { leg_joiner(leg_tex) }
            merge 
            {
                object { body(body_tex) translate <0, 5.05, 0> }
                object { body_paint() texture { bodypaint_tex }
                translate <0, 5.05, 0> }
            } //end merge                     
      
            //left arm and hand
            union 
            {
                object { left_arm(arm_tex) }
                object { hand(hand_tex) rotate lefthandrotation*z rotate -45*x translate <2.15, -8, -3.5> }
                rotate leftarmrotation*x rotate 10*z translate <6.15, 14.05, 0>
            }                       
      
            //right arm and hand
            union 
            {
                object { right_arm(arm_tex) }  
        
                #if(!use_held_object)
                    object {hand(hand_tex) rotate righthandrotation*z rotate -45*x translate <-2.15, -8, -3.5>}
                #else        
                    union
                    {                                                         
                        object {hand(hand_tex)}
                        object {held_object }                                    
                        rotate righthandrotation*z rotate -45*x translate <-2.15, -8, -3.5> 
                    }    
                #end

                rotate rightarmrotation*x rotate -10*z translate <-6.15, 14.05, 0>
            }                   
      
            //head with hair
            union 
            {
                object { head(head_tex ,1) translate <0, 17.55, 0>}        
                #if(usehair)
                    object { hair rotate -90*y translate <0, 24.05, 0> }
                #end
                rotate headrotation*y
            }
            rotate bodyrotation*x
        }
    }	
#end //macro minifig	   

// macro: test_minifig(p1, p2, p3, p4, p5, p6, p7, p8)
//    p1: headrotation
//    p2: rightarmrotation
//    p3: leftarmrotation
//    p4: leftlegrotation
//    p5: rightlegrotation
//    p6: lefthandrotation
//    p7: righthandrotation
//    p8: bodyrotation
	           
#macro test_minifig(headrotation, rightarmrotation, leftarmrotation, leftlegrotation, rightlegrotation, lefthandrotation, righthandrotation, bodyrotation)
    //define minifig face
    #local head_tex=
    texture 
    {
        material_map 
        {
            png "face_1.png" map_type 2 interpolate 4
            texture { black_tex }
            texture { yellow_tex }
        }
    
        translate <0, -0.1, 0>
        rotate -90*y
        scale <1, 12, 1>
    };
  
    //minifig "shirt"
    #local body_paint_texture=
    texture 
    { 
        material_map 
        { 
            png "shirt_21.png" once interpolate 4
            texture { white_tex }
            texture { red_tex }
            texture { white_tex }
        }
    
        translate <-0.5, 0, 0>
        scale <15, 12.4, 1>
    };
                         
    //different objects to hold (with transformations to line them up with hand part)
    //#local held_object=object {tool_hammer(grey_tex) rotate y*-90 rotate x*-8 translate z*-0.4*wide translate y*-4 }
    #local held_object=object {tool_powerdrill(grey_tex) rotate z*12 translate x*-0.78*wide rotate y*-90 translate 1.2*y*high}
    //#local held_object=object{axe(grey_tex) rotate y*-90 rotate x*-10 translate <0, -5, -2.5>}
  
    object
    {
        minifig(head_tex,
                headrotation,
                1, //render hair              
                male_hair(black_tex),
                white_tex, //arm texture
                rightarmrotation,
                leftarmrotation,
                yellow_tex, //hand texture
                righthandrotation,
                lefthandrotation,
                white_tex, //body texture
                body_paint_texture, //body paint
                bodyrotation,
                red_tex, //leg texture
                rightlegrotation,
                leftlegrotation,
                1, //render held_object
                held_object)
          
    }
		

#end //macro test_minifig  

// the floor
box { <-800, -2, -800> <800, 0, 900> texture { pigment { color nice } } finish { basic_finish } }
#if(Quality < 7)
// the "enclosing world"
box 
{ 
    <-750, -1, -750> <750, 750, 850> hollow
    texture { pigment { color white } } finish { basic_finish } 
}
// the background
union 
{
    box { <-800, -2, 849> <800, 800, 851> texture { finish { basic_finish } pigment { color nice2 } } }
    difference 
    {
        box { <-800, 0, 649> <800, 200, 849> }
        cylinder { -805*x, 805*x, 200 translate <0, 200, 649> }
        texture { grad_tex }
        bounded_by { box { <-805, -1, 648> <805, 201, 850> } }
    }
}               
#end


object {test_minifig(0, 45, 0, 0, 0, 0, 0, 0) translate y*high rotate y*45}