// Last edited on DATE TIME by USER
// Processed by remove-cam-lights

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

#declare tx_tronco =
  texture{
    pigment{ color rgb < 0.65, 0.16, 0.16 > }
    finish{ diffuse 0.9 ambient 0.1 }
  }

#declare tx_chao =
  texture{
    pigment{ color rgb < 0.15, 0.16, 0.16 > }
    finish{ diffuse 0.9 ambient 0.1 }
  }

#declare tx_branco =
  texture{
    pigment{ color rgb < 0.8, 0.8, 0.8 > }
    finish{ diffuse 0.9 ambient 0.8 }
  }

#declare tx_vermelha =
  texture{
    pigment{ color rgb < 0.89, 0.15, 0.13 > }
    finish{ diffuse 0.9 ambient 0.8 }
  }

#declare tx_xadrez =
  texture{
    pigment{ checker color rgb < 0, 0, 0 >, color rgb < 1, 1, 1 >}
    finish{ diffuse 0.9 ambient 0.1 }
    scale 2.0
  }

#declare chao =
  box{
    <-250,-250,-1>,
    <+250,+250,0>
    texture{ tx_chao }
  }

#macro esfera(raio)
  sphere {
    <0, 0, 0>,
    raio
  }
#end

#macro cilindro(tamanho, raio)
  cylinder {
    <0, 0, 0>,
    <0, 0, tamanho>,
    raio
  }
#end

#macro bit(b, d)
union {
  object { cilindro(20, 1) texture { tx_tronco } }
  #if(d = 0)
    object { esfera(4) texture { tx_vermelha } translate <0, 0, b * 12 + 4> }
  #else
    object { esfera(4) texture { tx_xadrez } translate <0, 0, b * 12 + 4> }
  #end
  translate <0, 0, -10>
}
#end

#macro fileira(bits, numero, prob)
union {
  object {
    box {
      <0, 0, 0>
      <2, 10 * bits, 2>
      texture { tx_tronco }
    }
    translate <0, 0, -12>
  }

  #local i = 0;

  #local r = int(numero);
  #local f = numero - r;

  #if(f > 0)
    #local numero2 = r + 1;
  #else
    #local numero2 = r;
   #end

  #while(i < bits)
    #local b = int(numero / pow(2,(bits - i - 1)));
    #local c = int(numero2 / pow(2,(bits - i - 1)));

    #if(b = 1)
      #local numero = numero - pow(2,(bits - i - 1));
    #end

    #if(c = 1)
      #local numero2 = numero2 - pow(2,(bits - i - 1));
    #end

    #if(b = c)
      #local obit = b;
    #else
      #if(b < c)
        #local obit = f;
      #else
        #local obit = 1 - f;
      #end
    #end

    #local ran = rand(SEED);

    #if(ran < prob)
      #local d = 1;
    #else
      #local d = 0;
    #end

    object { bit(obit, d) translate <0, (i * 10) + 4, 0> }
    #local i = i + 1;
  #end
}
#end

#macro abaco(linhas, bits, valores, prob)
union {
  difference {
    object {
      box {
        <2, -2, -15>
        <-2, bits * 10 + 2, ((linhas - 1) * 22) + 13>
      }
    }
    object {
      box {
        <2.1, 0, -13>
        <-2.1, bits * 10, ((linhas - 1) * 22) + 11>
      }
    }
    texture { tx_tronco }
  }

  #local i = 0;

  #while (i < linhas)
    object { fileira(bits, valores[i], prob) translate <0, 0, i * 22> }
    #local i = i + 1;
  #end

}
#end

#include "eixos.inc"

#declare bits1 = 7;
#declare linhas1 = 6;
#declare valores1 = array[linhas1];
#declare valores1[0] = (1-clock) * 3 + clock * 42;
#declare valores1[1] = (1-clock) * 8 + clock * 38;
#declare valores1[2] = (1-clock) * 16 + clock * 32;
#declare valores1[3] = (1-clock) * 32 + clock * 16;
#declare valores1[4] = (1-clock) * 38 + clock * 8;
#declare valores1[5] = (1-clock) * 42 + clock * 3;

#declare bits2 = 8;
#declare linhas2 = 4;
#declare valores2 = array[linhas2];
#declare valores2[0] = (1-clock) * 18 + clock * 128;
#declare valores2[1] = (1-clock) * 27 + clock * 80;
#declare valores2[2] = (1-clock) * 80 + clock * 27;
#declare valores2[3] = (1-clock) * 0 + clock * 16;

#declare SEED = seed(4815162342);

union{
  object { chao texture{ tx_xadrez } }
  object { eixos(10.0) }
  object { abaco(linhas1, bits1, valores1, 0.15) }
  object { abaco(linhas2, bits2, valores2, 0.35) translate <0, 100, 0> }
}

#include "camlight.inc"
#declare centro_cena = < 0.00, (10 * max(bits1, bits2)) + 15, (22 * (max(linhas1, linhas2) - 1)) / 2 >;
#declare raio_cena = 16 * (linhas1 + linhas2);
#declare dir_camera = < 16 * (linhas1 + linhas2) * sin((30 * (1-clock) + 150 * clock) * pi / 180), 16 * (linhas1 + linhas2) * cos((30 * (1-clock) + 150 * clock) * pi / 180), (22 * (max(linhas1, linhas2) - 1)) / 2 >;
#declare dist_camera = 16 * (linhas1 + linhas2);
#declare intens_luz = 1.00;
camlight(centro_cena, raio_cena, dir_camera, dist_camera , z, intens_luz)