//Caixa de Surpresas

// ======================================================================
// CORES E TEXTURAS

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


#declare tx_fosca = 
  texture{
    pigment{ color rgb < 1.00, 0.80, 0.10 > }
    finish{ diffuse 0.9 ambient 0.1 }
  }

#declare tx_color = 
  texture{
    pigment{ color rgb < 2.00, 0.40, 0.20 > }
    finish{ diffuse 0.9 ambient 0.1 }
  }


// ======================================================================
// DESCRICAO DA CENA 

#include "eixos.inc"
/*

#macro arm_M (varHand)
   union{
      cylinder { < 0, 0, 0 >, < 0, 0, -2.5 >, 0.50 texture{ tx_fosca }}
      object { Hand () rotate z*varHand translate < 0, 0, -3.1 > }
   }
#end
      
#macro Hand ()
      box{< -0.6, -0.6, -0.6 >, < 0.6, 0.6, 0.6 > texture{ tx_fosca }}
#end


#macro Leg_B (varlegMed, varlegFoot)
   union{
      cylinder{ < 0, 0, 0 >, < 0, 0, -2 >, 0.9 texture{ tx_fosca }}
      object {Leg_M (varlegFoot) rotate y*varlegMed translate < 0, 0, -2 >}
   }
#end

#macro Leg_M (varlegFoot)
   union{
      cylinder { < 0, 0, 0 >, < 0, 0, -1.6 >, 0.70 texture{ tx_fosca }}
      object { Foot () rotate z*varlegFoot translate < 0, 0, -2.4 > }
   }
#end
      
#macro Foot ()
      box{< -0.8, -0.8, -0.8 >, < 0.8, 0.8, 0.8 > texture{ tx_fosca }}
#end

#macro Animal (arm_r, hand_r, arm_l, hand_l, legBig_r, legMed_r, legFoot_r, legBig_l,
legMed_l, legFoot_l)
   union{
      sphere{ < 0, 0, 0 >, 2 texture{ tx_fosca }}
      box{< -0.8, -0.01, -0.08 >, < 0.8, 0.08, 0.08 >
         translate < -2, 1.5, 0 > texture{tx_fosca }
      }
      box{< -0.8, -0.01, -0.08 >, < 0.8, 0.08, 0.08 >
         translate < -2, 1.5, 0.3 > texture{tx_fosca }
      }
      box{< -0.8, -0.01, -0.08 >, < 0.8, 0.08, 0.08 >
         translate < -2, 1.5, -0.3 > texture{tx_fosca }
      }
      box{< -0.8, -0.01, -0.08 >, < 0.8, 0.08, 0.08 >
         translate < 2, 1.5, 0 > texture{tx_fosca }
      }
      box{< -0.8, -0.01, -0.08 >, < 0.8, 0.08, 0.08 >
         translate < 2, 1.5, 0.3 > texture{tx_fosca }
      }
      box{< -0.8, -0.01, -0.08 >, < 0.8, 0.08, 0.08 >
         translate < 2, 1.5, -0.3 > texture{tx_fosca }
      }
      sphere{ < 0.8, 1.4, 0.9 >, .5 texture{ tx_fosca }}
      sphere{ < -0.8, 1.4, 0.9 >, .5 texture{ tx_fosca }}
      cone{< 0, 0, 0 >, .7, < 0, 0, 2 >, 0 translate< -0.5, .30, 1.5 > texture{ tx_fosca }}
      cone{< 0, 0, 0 >, .7, < 0, 0, 2 >, 0 translate< 0.5, .30, 1.5 > texture{ tx_fosca }}
      sphere{ < 0, 0, 0 >, 2.8 translate< 0, 0, -3.5 > texture{ tx_fosca }}
      object{arm_M (hand_r) rotate y*arm_r translate< 2.3, 0, -2.5 >}
      object{arm_M (hand_l) rotate y*arm_l translate< -2.3, 0, -2.5 >}
      object{Leg_B (legMed_r, legFoot_r) rotate y*legBig_r translate< 1, 0, -5.5 >}
      object{Leg_B (legMed_l, legFoot_l) rotate y*legBig_l translate< -1, 0, -5.5 >}
   }
#end


//Encontra quadro anterior
#macro Quadro_anterior(fase)
   #local i = 0;
   #while(i<NQ-1)
      #if((Fase[i]<=fase) & (Fase[i+1]>=fase))
         #local resp = i;
      #end
      #local i = i+1;
   #end
   resp
#end

//Interpola o ponto desejado
#macro Interpola (f0, v0, f1, v1, f)
   #local ss = ((f-f0)/(f1-f0));
   #local rr = (1-ss);
   (rr*v0+ss*v1)
#end


//Macro para fazer gato dançar
#macro Dance(fase)
   #local i = Quadro_anterior(fase);
   #local arm_right = Interpola(Fase[i], Arm_r[i], Fase[i+1], Arm_r[i+1], fase);
   #local arm_left = Interpola(Fase[i], Arm_l[i], Fase[i+1], Arm_l[i+1], fase);
   #local leg_right = Interpola(Fase[i], Leg_r[i], Fase[i+1], Leg_r[i+1], fase);
   #local leg_left = Interpola(Fase[i], Leg_l[i], Fase[i+1], Leg_l[i+1], fase);
   object{Animal(arm_right, -45, arm_left, 45, leg_right, 0, 0, leg_left, 0, 0)}
#end


Dance(clock)
*/

#declare NL=4;
#declare valores= array[NL];
#declare valores[0]= 3;
#declare valores[1]= 5;
#declare valores[2]= 2;
#declare valores[3]= 7;


#macro bit(valor)
   union{
      cylinder {
         < 0, -1, 0 >, < 0, 1, 0 >, 1 texture{ tx_fosca }
      }
      box{
         <-0.2, -0.5, -0.2>, <0.1, 0.5, 0.2> texture{tx_color}
         translate <0, 1, -.78>
      }
      rotate y*(valor*180)
   }
#end

#macro fileira(qtde,num)
   #local resp=num;
   #local i = qtde;
   union{
   #while (i > 0)
      #local resto = mod(resp,2);
      #local resp = int(resp/2);
      object{bit(resto) translate <-(3*i), 0, 0>}
      #local i = i -1;
   #end
   }
#end

#macro abaco(col, lin, valores)
   #local j = lin-1;
   
   #while(j > -1)
      object{fileira (col, valores[j]) translate<0, 0, -(3*j) >}
      #local j = j - 1;
   #end
//    difference{
//       box{
//          <-10, -1, -10>, <10, 1, 10>
//          texture{ tx_fosca }
//       }
//       box{
//          <-9, -1.1, -9>, <9, 1.1, 9>
//          texture{ tx_fosca }
//       }
//       translate <5, 0, 0>
//    }
//    
#end

object {eixos(3.00)}

abaco(6, NL, valores)

abaco(4, NL, valores)

#include "camlight.inc"
#declare centro_cena = < -5.00, 0.00, -3.00 >;
#declare raio_cena = 16.0;
#declare dir_camera = < 20.00, 20.00, 10.00 >;
#declare dist_camera = 300.0;
#declare intens_luz = 1.00;
camlight(centro_cena, raio_cena, dir_camera, dist_camera , z, intens_luz)