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

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

#declare tx_xadrez =
  texture{
    pigment{ checker color rgb < 0.10, 0.32, 0.60 >, color rgb < 1.00, 0.97, 0.90 > }
    finish{ diffuse 0.9 ambient 0.1 }
    scale 2.0
  }

#macro registrador (numbits,valor)
  #local r = int(valor);
  #local f = valor - r;
  #local k = 0;
  #while (k < 0)
    #local vb = r mod 2;
    #if (vb = 0)
      bitk = f;
      f = 0;
    #else
      bitk = 1-f;
    #end
    #local r = int(r/2);
  #end
#end

#macro abaco (m,n, valores, prob)
  #local moldura =
  difference{
    object{ box{ <0,0,0>, <0.2,n+0.4,m+0.4> texture{ pigment{ color rgb <1,0.8,0> }}}}
    object{ box{ <-0.1,0.2,0.2>, <0.3,n+0.2,m+0.2> }}};

  union{
    object { moldura }
    #local i = 0;
    #while (i < m)
      object { fileira(n,valores[i],prob) translate <0,0,i+0.15> }
      #local i = i+1;
    #end
  }
#end

#declare roleta = seed(417);

#macro fileira (numbits, valor, prob)
  #local k = 0;
  union{
    object{ divisoria(numbits) }
    object{ divisoria(numbits) translate <0,0,0.6> }
    #local q = int(valor);
    #local f = valor - q;
    #while (k < numbits)
      object{ cilindro translate <0.1,(n-1)-k+0.7,0.2>}

      #if (rand(roleta) < prob)
        #local defeito = 1;
      #else
        #local defeito = 0;
      #end

      #local vb = mod(q,2);
        #if (vb = 0)
          bit(f,k,defeito)
          #local f = 0;
        #else
          bit(1-f,k,defeito)
        #end
      #local q = int(q/2);
      #local k = k+1;
    #end
  }
#end

#macro bit (valor,k,defeito)
  #if (defeito = 0)
    object { bola translate <0.1,(n-1)-k+0.7,valor*0.3+0.35> }
  #end
#end

#macro divisoria (n)
  object{ box { <0,0.2,0.2>, <0.2,n+0.2,0.25> texture{ pigment{ color rgb <1,0.8,0> }}}}
#end

#declare cilindro =
  object { cylinder{ <0,0,0>, <0,0,0.6>, 0.1 } texture{ pigment{ color rgb <1,0.8,0> }}}
#declare bola =
  sphere {<0,0,0> 0.15 texture{ pigment{ color rgb <1,1,1>}}}

#declare chao =
  box{ <-20,-20,-1>, <+20,+20,0> }

#include "eixos.inc"

#declare m = 5;
#declare n = 7;
#declare valores =
array[5] {clock*127,(1-clock)*64+clock*127,clock*63,(1-clock)*7+clock*15,(1-clock)*10+clock*12}

union{

  object{ chao  translate < 0,0,-5 > texture{ tx_xadrez } }

  object{ abaco(m,n,valores,0) translate <0,-3.5,-2> }

}

#include "camlight.inc"
#declare centro_cena = < 0.00, 0.00, 1.00 >;
#declare raio_cena = 6.0+0.5*(1-2*abs(0.5-clock));
#declare dir_camera = < 1, 0, 0.5*(1-2*abs(0.5-clock)) >;
#declare dist_camera = 10.0+0.5*(1-2*abs(0.5-clock));
#declare intens_luz = 1;
camlight(centro_cena, raio_cena, dir_camera, dist_camera , z, intens_luz)