// Exemplo de arquivo de descricao de cena para POV-ray
// Last edited on 2010-03-04 15:44:01 by stolfi

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

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

#declare tx_plastico = 
  texture{
    pigment{ color rgb < 0.10, 0.80, 1.00 > }
    finish{ diffuse 0.8 ambient 0.1 specular 0.5 roughness 0.005 }
  }

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

#declare tx_espelho = 
  texture{
    pigment{ color rgb < 1.00, 0.85, 0.30 > }
    finish{ diffuse 0.2 reflection 0.7*< 1.00, 0.85, 0.30 > ambient 0.1 }
  }

#declare tx_vidro = 
  texture{
    pigment{ color rgb < 0.85, 0.95, 1.00 > filter 0.70 }
    finish{ diffuse 0.03 reflection 0.25 ambient 0.02 specular 0.25 roughness 0.005 }
  }

#declare tx_head = 
  texture{
    pigment{ color rgb < 0.90, 0.15, 0.10 > }
    finish{ diffuse 0.8 ambient 0.1 specular 0.5 roughness 0.005 }
  }

#declare tx_red = 
  texture{
    pigment{ color rgb < 1, 0, 0 > filter 0.70 }
    finish{ diffuse 0.8 reflection 0.25 ambient 0.02 specular 0.25 roughness 0.005 }
  }
#declare tx_blue =
  texture{
    pigment{ color rgb < 0.30, 0.5, 0.90 > }
    finish{ diffuse 0.8 ambient 0.1 specular 0.5 roughness 0.005 }
  }

#declare tx_brown =
  texture{
    pigment{ color rgb < 0.55, 0.15, 0.45 > }
    finish{ diffuse 0.8 ambient 0.1 specular 0.5 roughness 0.005 }
  }

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

#declare tx_green =
  texture{
    pigment{ color rgb < 0.30, 0.9, 0.20 > }
    finish{ diffuse 0.8 ambient 0.1 specular 0.5 roughness 0.005 }
  }

#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 bit (valor,defeito)  
       #if (valor = 0)
       	   #local pos = 1.2;
       #else
	   #local pos = 3.8;
       #end
       union {
              cylinder {
	 	  <0,0,0>,
		  <0,0,5>
		0.2
		texture {tx_head}
	      }
	      difference {	      		 
	      		 sphere {<0,0,pos>, 1 texture {tx_yellow}}
                         cylinder {  <0,0,0>,  <0,0,5> 0.25}
			 #if (defeito)
	      		 sphere {<0,0,pos>, 1 texture {tx_yellow} translate <0,0.7,0.3>}
			 #end
			 
	      }
	}
#end
#macro quadro(n)
difference {
	   box {<-1,-2,0>,<1,n*2,5> texture {tx_blue}}
   	   box {<-1.1,-1.8,0.1>,<1.1,n*2-0.2,4.9>}

}

#end
#declare roleta = seed (417);
#macro defeito(prob)



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

#macro fileira(n,valor,prob)
       union {
       object {quadro(n)}
       #local i = n;

       #local divisor= valor;

       #while (divisor >= 1)
              #local resto = mod(divisor,2);
       	      object { bit(resto,defeito(prob)) translate<0,(i-1)*2,0> }
	      #local divisor = int(divisor/2);
	      #local i = i-1;
       #end
       #while (i>0)
      	      object { bit(0,defeito(prob)) translate<0,(i-1)*2,0>}
	      #local i = i-1;
       #end
       }
#end

//m fileiras
//n tamanho
#macro abaco(m,n,valores,prob)
       union {
       difference {
              box {<-1,-3,-1>,<1,n*2+1,5*m+1> texture {tx_blue}}
   	   box {<-1.1,-1.8,0.1>,<1.1,n*2-0.2,5*m-0.1>}
	   }
       #local i = 0;
       #while (i < m)
      	     object{fileira(n,valores[i],prob) translate<0,0,5*i>}
	     #local i = i +1;
       #end
}
#end

// Aqui está a cena, finalmente:
#include "eixos.inc"
union{
  object{ eixos(3.00) }
  //object{ bit(1)}
  //object{ bit(0) translate <0,2,0>}
  //object {fileira(7,12)}
  #declare m = 7;
  #declare valores = array[m];
  #declare valores[0] = 12; 
  #declare valores[1] = 63; 
  #declare valores[2] = 0; 
  #declare valores[3] = 7; 
  #declare valores[4] = 37; 
  #declare valores[5] = 40; 
  #declare valores[6] = 26; 
  object {abaco (m,6,valores,0.5) translate <-10,5,-5*m/2>}

  #declare m2 = 10;
  #declare valores2 = array[m2];
  #declare valores2[0] = 150; 
  #declare valores2[1] = 225; 
  #declare valores2[2] = 511; 
  #declare valores2[3] = 0; 
  #declare valores2[4] = 327; 
  #declare valores2[5] = 418; 
  #declare valores2[6] = 92; 
  #declare valores2[7] = 13; 
  #declare valores2[8] = 450; 
  #declare valores2[9] = 199; 

  object {abaco (m2,9,valores2,0.5) translate <-10,-30,-5*m2/2> }





}

#include "camlight.inc"
#declare centro_cena = < 0.00, 0.00, 1.00 >;
#declare raio_cena = 40.0;
#declare dir_camera = < 10.00, 2.00, 0.00 >;
#declare dist_camera = 30.0;
#declare intens_luz = 1.00;
camlight(centro_cena, raio_cena, dir_camera, dist_camera , z, intens_luz)