// LAB 05
// PATRICIA FARAH CARRIAO

// ======================================================================
// CÂMERA 

camera {
  location  <  110.00, 0.00, 80.00 >  // Posição do observador.
//location  <  0.00, 0.00, 8.00 >  // Posição do observador.
  right     -1.00*x                // Largura RELATIVA da imagem.
  up        0.75*y                 // Altura RELATIVA da imagem.      
  sky       z                      // Qual direção é "para cima"?
  look_at   <  0.00, 0.00, 6.00 >  // Para onde a câmera está apontando.
} 
// Nota: os parâmetros "right" e "up" devem ter a mesma proporção
// que os parâmetros ${WIDTH} e ${HEIGHT} no Makefile.

// ======================================================================
// FONTES DE LUZ

light_source {
  10 * < +50.0, +30.0, +50.0 >              // Posição da lâmpada.
  color rgb 1.2 * < 1.00, 1.00, 1.00 >   // Intensidade e corda luz.
} 

light_source {
  10 * < +50.0, -10.0, +10.0 >             // Posição da lâmpada.
  color rgb 0.8 * < 1.00, 1.00, 1.00 >   // Intensidade e corda luz.
} 

light_source {
  10 * < -50.0, -10.0, +10.0 >             // Posição da lâmpada.
  color rgb 0.3 * < 1.00, 1.00, 1.00 >   // Intensidade e corda luz.
} 


// ======================================================================
// DESCRIÇÃO DA CENA 

background{ color rgb < 0.75, 0.80, 0.85 > }

plane{ z,1
  texture {
    pigment { rgb <0.75,0.8,0.85> }
    finish { ambient 0.1 diffuse 0.9 }
    scale 6
  } 
}
 
#declare tinta_A = 
  texture {
    pigment { color rgb < 1.00, 0.40, 1.00 > }
    finish { diffuse 0.5 specular 0.5 roughness 0.005 ambient 0.1 }
  }

#declare preto = 
  texture {
    pigment { color rgb < 0, 0, 0 > }
    finish { diffuse 0.5 specular 0.5 roughness 0.005 ambient 0.1 }
  }

#declare verde = 
  texture {
    pigment { color rgb < 0, 1, 0 > }
    finish { diffuse 0.5 specular 0.5 roughness 0.005 ambient 0.1 }
  }

#declare azul = 
 texture {
    pigment { color rgb < 0.30, 0.40, 1.00 > }
    finish { diffuse 0.5 specular 0.5 roughness 0.005 ambient 0.1 }
  }


#declare gerador = seed(1234);

#declare vagaof = box { <4,4,2>, <0,0,5.5> texture {tinta_A} }        
#declare frente = cylinder { <2,4,2>, <2,7,2> 2 texture{tinta_A}}
#declare cham = cylinder { <2,6,3>, <2,6,5> 0.5 texture{tinta_A}}
#declare base = box { <4,4,2>, <0,7,2.2> texture {tinta_A} }  
#declare base2 = box { <5,3,2>, <0,8,0> texture {tinta_A} }
#declare vagao = box { <4,-7,2>, <0,-1,5.5> texture {tinta_A} }  
#declare roda = cylinder { <4,5,2>, <4.2,5,2> 0.8 texture{azul} }
#declare janela_quadf = box { < 4, 3.5, 3.5 >, < 4.1, 2.5, 5 > texture{azul} } 
#declare janela_redondaf = cylinder { <4,3,4.3>, <4.2,3,4.3> 0.6 texture{azul} }

#declare janela_quad = box { < 4, -1.5, 3.5 >, < 4.1, -2.5, 5 > texture{azul} } 
#declare janela_redonda = cylinder { <4,-2,4.3>, <4.2,-2,4.3> 0.6 texture{verde} }

#declare trilho = union { torus { 30, 0.2 texture{preto} rotate 90*x } torus { 34, 0.2 texture{preto} rotate 90*x} }
			 
#declare liga = box { < 4.1, -100, 1 >, < 0, -99.6, 1.2 > texture{preto} } 

#declare janela_frente = box { < 3.5, 4, 4.3 >, < 0.5, 4.1, 5.3 > texture{verde} } 

#declare estacao = box { < 32, 5, 3 >, < 35, -5, 0 > texture{verde} }

#declare trem =
 union {
	#declare a = 1;
	#declare locomotiva =
	union {
		object {vagaof} 
		object {base}
		object {cham}
		object {roda}
		object {roda translate<0,-2.3,0>}
		object {roda translate<0,-4.3,0>}
		object {roda translate<-4.2,0,0>}
		object {roda translate<-4.2,-2.3,0>}
		object {roda translate<-4.2, -4.3,0>}
		object {janela_redondaf}
		object {janela_quadf translate<0,-1.5,0> }
		object {janela_redondaf translate<-4.2,0,0> }
		object {janela_quadf translate<-4.2,-1.5,0> }
		object {janela_frente}	
		difference {
	  	   object {frente}
	  	   object {base2}
 		}
	}
	
	object {locomotiva translate<30,-2,0>}

	    #declare vagaotudo = 
	   	union {
            	   object {vagao translate<0,0,0>}	
	 
	    	   object {roda translate<0,(-7*a),0>}
            	   object {roda translate<0,-2.2+(-7*a),0>}
	    	   object {roda translate<0,-4.2+(-7*a),0>}
	    	   object {roda translate<(-4.2),-7*a,0>}
	    	   object {roda translate<(-4.2),-2.2+(-7*a),0>}
	    	   object {roda translate<(-4.2),-4.2+(-7*a),0>}
		   #declare j = 0;
	    	   #while (j<4)
 	       	     #if ((10*rand(gerador)) < 5)
	       	   	object {janela_redonda translate<0,(-7*(a-1))+(-1.5*j),0> }
		  	object {janela_redonda translate<-4.2,(-7*(a-1))+(-1.5*j),0> }
    	             #else 
      		  	object {janela_quad translate<0,(-7*(a-1))+(-1.5*j),0> }
		  	object {janela_quad translate<-4.2,(-7*(a-1))+(-1.5*j),0> }
	       	     #end
                     #declare j = j+1;	
 	    	   #end
		}

		#while (a<7)
 	    	  object {vagaotudo translate<30,4.5,0> rotate -15*z*a }
	    	  #declare a = a+1;
		#end
}

#declare tempo=clock;
#declare h=0.2;
#declare D=360;
#declare A=D/(2*h*(1-h));
#declare B=-A*h*h;
#declare C=2*A*h;


#if (tempo<0.2)
     #declare pos =  A*clock*clock;
#else 
    #if (tempo<0.8)
	#declare pos =  B+C*clock;
    #else 
	#declare pos =  (D-A*(1-clock)*(1-clock));
    #end
 #end

object {trem rotate z*pos}
object {trilho translate<0,0,1>}
object {estacao}