#include "retalho.inc"
// ======================================================================
// 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_branco =
 texture{
    pigment{ color rgb < 1, 1, 1 > }
  }

#declare tx_fosca_marrom =
 texture{
    pigment{ color rgb < 0.361, 0.2, 0.09 > }
  }

#declare tx_olhos =
  texture{
    pigment{ color rgb < 0.10, 0.62, 0.10 >}
    finish{ diffuse 0.9 ambient 0.5 }
  }
#declare retalho1 = array[12]
#declare retalho1[0] = <0,0,0>;
#declare retalho1[1] = <1,0,0>;
#declare retalho1[2] = <2,0,0>;
#declare retalho1[3] = <3,0,0>;

#declare retalho1[4] = <0,1,0>;
#declare retalho1[5] = <1,1,0>;
#declare retalho1[6] = <2,1,0>;
#declare retalho1[7] = <3,1,0>;

#declare retalho1[8] = <0,2,1>;
#declare retalho1[9] = <1,2,1>;
#declare retalho1[10] = <2,2,3>;
#declare retalho1[11] = <3,2,1>;

#declare retalho2 = array[12]
#declare retalho2[0] = <0,4,1>;
#declare retalho2[1] = <1,4,4>;
#declare retalho2[2] = <2,4,3>;
#declare retalho2[3] = <3,4,0>;

#declare retalho2[4] = <0,5,0>;
#declare retalho2[5] = <1,5,0>;
#declare retalho2[6] = <2,5,0>;
#declare retalho2[7] = <3,5,0>;

#declare retalho2[8] = <0,6,0>;
#declare retalho2[9] = <1,6,0>;
#declare retalho2[10] = <2,6,0>;
#declare retalho2[11] = <3,6,0>;

#declare emenda= array[4];
#declare emenda[0]= (retalho2[0]+retalho1[8])/2;
#declare emenda[1]= (retalho2[1]+retalho1[9])/2;
#declare emenda[2]= (retalho2[2]+retalho1[10])/2;
#declare emenda[3]= (retalho2[3]+retalho1[11])/2;


//Constantes
#declare N_Esferas = 200;

//roleta para numeros aleatorios
#declare roleta = seed(500);

// ======================================================================
// DESCRIÇÃO DA CENA 

// Funcoes:

#macro interpola (f0, v0, f1, v1, f)
  #local ss = abs(f-f0)/abs(f1-f0);
  #local rr = 1-ss;
  (rr*v0 + ss*v1)
#end

#macro interpola4 (t0, v0, v1, v2, v3, t1, tt)
  #local v01 = interpola(t0, v0, t1, v1, tt);
  #local v12 = interpola(t0, v1, t1, v2, tt);
  #local v23 = interpola(t0, v2, t1, v3, tt);

  #local v012 = interpola(t0, v01, t1, v12, tt);
  #local v123 = interpola(t0, v12, t1, v23, tt);

  #local v0123 = interpola(t0, v012, t1, v123, tt);

  v0123
#end

//Grafico

#macro grafico (x0, y0, y1, y2, y3, x3, N)
  #if (x3>x0) 
    #local controler = 1;
  #else
    #local controler = -1;
  #end
  #local intervalo = abs(x3-x0)/N;
  #local var = x0;

  union {
    #while (var < x3*controler)
      #local yt = interpola4 (x0, y0, y1, y2, y3, x3, var);
      object { sphere{ <0, 0, var>, 0.1 } translate yt }
      #local var = var + intervalo;
    #end
  }
#end

#include "eixos.inc"

// Aqui está a cena, finalmente:
 union {
    object { eixos (10)}
    #local y1a = <0,5,10>;
    #local y2a = <1,6,9>;

    #local y1b = <0,0,4>;
    #local y2b = <3,4,4>;

    #local y1c = <6,10,10>;
    #local y2c = <10,10,1>;

    #local y1d = <1,10,1>;
    #local y2d = <3,3,3>;
    object{union{
    object {grafico(0, (y2d+y1a)/2,	y1a, y2a, (y2a+y1b)/2, 	1, N_Esferas) texture {tx_fosca_marrom}}
    object {grafico(1, (y2a+y1b)/2, 	y1b, y2b, (y2b+y1c)/2, 	2, N_Esferas) texture {tx_olhos}}
    object {grafico(2, (y2b+y1c)/2, 	y1c, y2c, (y2c+y1d)/2, 	3, N_Esferas) texture {tx_fosca_marrom}}
    object {grafico(0, (y1a+y2d)/2, 	y2d, y1d, (y2c+y1d)/2 , 3, N_Esferas) texture {tx_olhos}}
    } translate <5,0,0>}

    object {retalho (retalho1[0],retalho1[1],retalho1[2],retalho1[3],
			retalho1[4],retalho1[5],retalho1[6],retalho1[7],
			retalho1[8],retalho1[9],retalho1[10],retalho1[11],
			emenda[0],emenda[1],emenda[2],emenda[3],
			0.05,tx_fosca_marrom,tx_olhos) }
     object {retalho (emenda[0],emenda[1],emenda[2],emenda[3],
			retalho2[0],retalho2[1],retalho2[2],retalho2[3],
			retalho2[4],retalho2[5],retalho2[6],retalho2[7],
			retalho2[8],retalho2[9],retalho2[10],retalho2[11],
			0.05,tx_fosca_marrom,tx_fosca) }
}
  
#include "camlight.inc"
#declare centro_cena = < 5.00, 5.00, 5.00 >;
#declare raio_cena = 15.0;
#declare dir_camera = < 1, 1, 1 >;
#declare dist_camera = 30.0;
#declare intens_luz = 1.00;
camlight(centro_cena, raio_cena, dir_camera, dist_camera , y, intens_luz)