//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"


#declare NL=6;
#declare valores= array[NL];
#declare valores[0]= (1-clock)*0+clock*(3);
#declare valores[1]= (1-clock)*0+clock*(5);
#declare valores[2]= (1-clock)*3+clock*(6);
#declare valores[3]= (1-clock)*10+clock*(18);
#declare valores[4]= (1-clock)*4+clock*(7);
#declare valores[5]= (1-clock)*9+clock*(11);

#declare roleta = seed(417);

#macro bit(valor, def)
   #if (def=0)
      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)
      }
   #else
      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*90)
      }
   #end
#end

#macro fileira(qtde,num, prob)
   #local resp = num;
   #local i = qtde;
   #local r = int(num);
   #local f = num - r;
   union{
   #while (i > 0)
      #local def = (rand(roleta) < prob);
      #local resto = mod(resp,2);
      #if(resto=0)
         #local bitr = f;
         #local f = 0;
      #else
         #local bitr = 1-f;
      #end
      #local resp = int(resp/2);
      object{bit(bitr, def) translate <-(3*i), 0, 0>}
      #local i = i -1;
   #end
   }
#end

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

object {eixos(3.00)}

abaco(5, NL, valores, 0)


#include "camlight.inc"
#declare centro_cena = < -10.00, -5.00, -9.00 >;
#declare raio_cena = 25.0*(1-clock)+clock*20;
#declare dir_camera = < 2.00*clock, 2.10, 2.00*clock >;
#declare dist_camera = 300.0;
#declare intens_luz = 1.00;
camlight(centro_cena, raio_cena, dir_camera, dist_camera , z, intens_luz)