// Exercicio 7 - MC930
// Nilton Volpato - RA 003243
// A rebimboca da parafuseta

#include "colors.inc"
#include "textures.inc"
#include "stones.inc"
#include "metals.inc"


#default { finish { ambient 0.25 diffuse 0.8 } }


//----------------------------------------------
// Parâmetros da Animação

#declare vcam = <0,0,0>;
#declare vlook = <0,0,0>;
#declare va = <-5,10,0>;
#declare vb = <0,5,0>;
#declare vc = <0,0,0>;
#declare vseta = <0,0,0>;
#declare sreb = 1;
#declare rab = 0;

#declare usaseta = 0;

#switch (clock)
  // A -> B
  #range (0, 0.25)
    #declare clk = clock/0.25;
    #declare va = va - clk*<-5,5,0>;
  #break
  
  // rebimboca
  #range(0.25, 0.6)
    #declare clk = (clock - 0.25)/(0.6-0.25);
    #declare usaseta = 1;
    #declare vseta = sin(clk*2*pi)*x;
    #declare sreb = (1 -clk*(1 - 4.5/6))*x + y + z;
    #declare va = va - <-5,5,0>;
  #break
  
  // C -> AB e rotação
  #range(0.6, 0.9)
    #declare clk = (clock - 0.6)/(0.9-0.6);
    #declare sreb = (4.5/6)*x + y + z;
    #declare va = <0,5,0> - clk*<0,5,0>;
    #declare vb = vb - clk*vb;
    #if (clk <= 0.8)
      #declare rab = <15*(-cos(2*pi*(clk/0.8))+1),0,0>;
    #end
  #break
  
  // fim
  #range(0.9, 1.0)
    #declare clk = (clock - 0.9)/(1-0.9);
    #declare sreb = (4.5/6)*x + y + z;
    #declare va = <0,0,0>;
    #declare vb = <0,0,0>;
    #declare vcam = clk*<30, -5, 8>;
    #declare vlook = <0,-3*clk, 0>;
  #break
  
  #else
    #error "Oops!"
#end


//----------------------------------------------
// Luzes, Câmeras, ...

light_source {
  < -25.00, 40.00, -30.00 >
  color rgb 1.6*< 1, 1, 1 >
  rotate <0,20,0>
}

camera {
  location  < -15, 10, -30 > + vcam
  right <4/3,0,0>
  up <0,1,0>
  look_at <0,0,0>+vlook
}

background { color LightBlue }

//----------------------------------------------
// Cores e Texturas

#declare prateado = 
  texture {
    //pigment { color White }
    T_Silver_5B
    //Chrome_Metal
  }

#declare metal_escuro = 
  texture {
    //pigment { color Pink }
    //Bronze_Metal
    T_Brass_2A
  }

#declare cor_bloco = 
  texture {
    pigment { color LightSteelBlue /*Wheat Blue*/ }
  }

#declare cor_borracha = 
  texture {
    pigment { color White*0.3 }
  }

#declare cor_rebimboca = 
  texture {
    pigment {  GreenCopper }
    finish { Metallic_Finish }
    finish { reflection .15 }
  }

#declare cor_avisos =
  texture {
    pigment { color Red }
    
  }


//----------------------------------------------
// Partes do Repermeador Oscilatório Multifuncional

// Combustor Piloto
// A
#declare combustor = 
  union {
    superellipsoid {
      <0.25, 0.25>
      scale <2.5,1.3,2.5>
      texture { metal_escuro }
    }
    cylinder {
      <-1,0,0>, <-1,4,0>, .7
      open
      texture { prateado }
    }
    sphere {
      <-1, 4, 0>, .7
      texture { prateado }
    }
    cylinder {
      <-1, 4, 0>, <-3.5, 5, 0>, .7
      open
      texture { prateado }
    }
    text {
      ttf "arial.ttf"
      "A"
      0.5, 0
      scale 3
      translate <0.5,2,0>
      texture { cor_avisos }
    }
  }


// Rebimboca da parafuseta
#declare rebimboca = 
  cylinder {
    <-6,0,0>, <6,0,0>, .5
    texture { cor_rebimboca }
  }

// -> (seta)
#declare seta =
  prism {                                
    linear_sweep
    linear_spline
    -0.5,         // height 1
    0.5,         // height 2
    8,           // number of points
    <0,0>,
    <-2,-1.8>,
    <-2,-0.8>,
    <-4,-0.8>,
    <-4, 0.8>,
    <-2, 0.8>,
    <-2, 1.8>,
    <0,0>
    rotate <90,0,0>
    texture { cor_avisos }
  }


// Bloco Rotativo
// B
#declare bloco = 
  union {
    box { 
      <-.5, -.5, -.5>, <.5, .5, .5>
      scale <9,7,9>
      texture { cor_bloco }
    }
    object {
      rebimboca
      scale sreb
      translate <0, -2.5, 0>
    }
    #if (usaseta)
      object {
	seta
	translate <-7.5, -2.5, 0>
	translate vseta
      }
      object {
	seta
	translate <-7.5, -2.5, 0>
	translate vseta
	scale <-1,1,1>
      }
    #end
    text {
      ttf "arial.ttf"
      "B"
      0.5, 0
      scale 3
      translate <-.6,-.8,-5>
      texture { cor_avisos }
    }
    finish { phong 1 }
  }


// Mancal Basculante Inferior
// C
#declare mancal =
  difference {
    union {
      box { 
	<-.5, -.5, -.5>, <.5, .5, .5>
	scale <12,2,12>
      }
      cylinder {
	<-8,0,0>, <8,0,0>, .5
	texture { prateado }
      }
      torus {
	.8+.5, .8
	rotate <0,0,90>
	translate <-8,0,0>
	texture { cor_borracha }
      }
      torus {
	.8+.5, .8
	rotate <0,0,90>
	translate <-8,0,0>
	texture { cor_borracha }
      }
      torus {
	.8+.5, .8
	rotate <0,0,90>
	translate <-8,0,0>
	texture { cor_borracha }
	scale <-1,1,1>
      }
      text {
	ttf "arial.ttf"
	"C"
	0.3, 0
	scale 3
	translate <-.6,-.8,-7.5>
	texture { cor_avisos }
      }
    }
    box { 
      <-.5, -.5, -.5>, <.5, .5, .5>
      scale <9,7,9>
    }
    texture { metal_escuro }
  }


//----------------------------------------------
// Cena

union {
  union {
    object { // A
      combustor
      translate <-1,4.45-5,0>
      translate va
    }
    object { // B
      bloco
      translate <0,-5,0>
      translate vb
    }
    rotate rab
  }
  object { // C
    mancal
    translate <0,-7.5,0>
    translate vc
  }
}


plane {
  y, -10
  texture { T_Grnt1 }
}