// CORES E TEXTURAS

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

#declare tx_tanque = 
  texture{
    pigment{ color rgb 1.4*< 0.7, 0.50, 0.40 > }
    //finish{ diffuse 0.8 ambient 0.1 specular 0.5 roughness 0.005 }
  }

#declare tx_pecas = 
  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 }
    finish{ diffuse 0.2 reflection 1.0*< 1.00, 1.00, 1.00 > 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_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
  }

#declare vertice2 =
  union{
    cylinder{
      < +1.00, +1.00, +0.00 >,
      < +1.00, +1.00, +2.00 >,
      1.0
      texture{ tx_tanque }
    }
    cone{
      < +1.00, +1.00, +2.00 >,
      1.0,
      < +1.00, +1.00, +4.00 >,
      0.5
      texture{ tx_tanque }
    }
    cylinder{
      < +1.00, +1.90, +1.00 >,
      < +1.00, +2.25, +1.00 >,
      0.1
      texture{ tx_tanque }
    }
    cylinder{
      < +1.00, +0.10, +1.00 >,
      < +1.00, -0.25, +1.00 >,
      0.1
      texture{ tx_tanque }
    }
  }

#declare vertice3 =
  union{
    cylinder{
      < +1.00, +1.00, +0.00 >,
      < +1.00, +1.00, +2.00 >,
      1.0
      texture{ tx_tanque }
    }
    cone{
      < +1.00, +1.00, +2.00 >,
      1.0,
      < +1.00, +1.00, +4.00 >,
      0.5
      texture{ tx_tanque }
    }
    cylinder{
      < +1.00, +1.90, +1.00 >,
      < +1.00, +2.25, +1.00 >,
      0.1
      texture{ tx_tanque }
    }
    cylinder{
      < +1.00, +0.10, +1.00 >,
      < +1.00, -0.25, +1.00 >,
      0.1
      texture{ tx_tanque }
    }
    cylinder{
      < +1.90, +1.00, +1.00 >,
      < +2.25, +1.00, +1.00 >,
      0.1
      texture{ tx_tanque }
    }
  }

#declare vertice4 =
  union{
    cylinder{
      < +1.00, +1.00, +0.00 >,
      < +1.00, +1.00, +2.00 >,
      1.0
      texture{ tx_tanque }
    }
    cone{
      < +1.00, +1.00, +2.00 >,
      1.0,
      < +1.00, +1.00, +4.00 >,
      0.5
      texture{ tx_tanque }
    }
    cylinder{
      < +1.00, +1.90, +1.00 >,
      < +1.00, +2.25, +1.00 >,
      0.1
      texture{ tx_tanque }
    }
    cylinder{
      < +1.00, +0.10, +1.00 >,
      < +1.00, -0.25, +1.00 >,
      0.1
      texture{ tx_tanque }
    }
    cylinder{
      < +1.90, +1.00, +1.00 >,
      < +2.25, +1.00, +1.00 >,
      0.1
      texture{ tx_tanque }
    }
    cylinder{
      < +0.10, +1.00, +1.00 >,
      < -0.25, +1.00, +1.00 >,
      0.1
      texture{ tx_tanque }
    }
  }

#macro interpola1(tt, t0, t1, p0, p1)
  #local ss = (tt - t0) / (t1 - t0);
  #local xx = (1 - ss) * p0.x + (ss * p1.x);
  #local yy = (1 - ss) * p0.y + (ss * p1.y);
  #local zz = (1 - ss) * p0.z + (ss * p1.z);
  #local ponto = < xx, yy, zz >;
  ponto
#end


#macro testa_interpola1(p0, p1, n, raio)
  #local tt = 0;
  #while (tt < n)
    #local ponto = interpola1(tt, 0, n-1, p0, p1);
    sphere{
      ponto,
      raio
      texture { tx_tanque }
    }
    #local tt = tt + 1;
  #end
#end

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

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

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

#macro testa_interpola3()
  #local tt = 0;
  #while (tt < 1)
    #local ponto = interpola3(tt, 0, 1, < 0, 0, 0 >, < 0, 5, 0 >, < -5, 5, 0 >, < -5, 5, 5 >);
    sphere{
      ponto,
      1
      texture { tx_tanque }
    }
    #local tt = tt + 0.001;
  #end
#end

#macro interpola3_multi(tt, n, p_ini, p_fin, p1, p2)
  #local k = int(tt);
  #local v1 = p1[k];
  #local v2 = p2[k];
  #if (k = 0)
    #local v0 = p_ini;
    #local v3 = (p2[0] + p1[1]) / 2;
  #end
  #if (k = n - 1)
    #local v0 = (p2[k-1] + p1[k]) / 2;
    #local v3 = p_fin;
  #end
  #if (k > 0 & k < n-1)
    #local v0 = (p2[k-1] + p1[k]) / 2;
    #local v3 = (p2[k] + p1[k+1]) / 2;
  #end
  #local ponto = interpola3(tt, k, k+1, v0, v1, v2, v3);
  ponto
#end
  
#include "bezpoly.inc"
#macro testa_interpola3_multi()
  #local raio = 0.15;
  #local p_ini = < 0, 0, 0 >;
  #local p_fin = < 10, 5, 10 >;
  #local n = 3;
  #local p1 = array[n];
  #local p2 = array[n];
  #local p1[0] = < 3, 3, 5 >;
  #local p2[0] = < 4, 8, 5 >;
  
  #local p1[1] = < 7, 4, 1 >;
  #local p2[1] = < 7, 0, 5 >;
  
  #local p1[2] = < 10, 0, 10 >;
  #local p2[2] = < 9, 3, 8 >;
  union{
    object{ bezpoly_multi(n, p_ini, p1, p2, p_fin, 0.9*raio) }

    #local tt = 0;
    #local delta_tt = 0.001;
    #while (tt < n)
      #local ponto = interpola3_multi(tt, n, p_ini, p_fin, p1, p2);
      sphere{
        ponto,
        raio
        texture { tx_tanque }
      }
      #local tt = tt + delta_tt;
    #end
  }
#end

testa_interpola3_multi()

#local m = 6;
#local n = 6;

#declare roleta = seed( 31159 );

#declare ponta_cano = array[3000]
#declare N = 0;

#macro gera_tanques(m, n)
  #local i = 0;
  #local j = 0;
  #local n_tanque = 1;

  #while (i < m)
    #local j = 0;
    #while (j < n)
      #local n_tanque = 1 + int(3.0*rand(roleta));
      #if (n_tanque = 1)
        object{ tanque1
                translate < 5.0*i, 5.0*j, 0.0 >
              }
        #declare ponta_cano[N]   = < (5.0*i + 1.00), (5.0*j + 1.75), +3.00 >;
        #declare ponta_cano[N+1] = < (5.0*i + 1.00), (5.0*j + 0.25), +3.00 >;
        #declare ponta_cano[N+2] = < (5.0*i + 1.00), (5.0*j + 1.00), +4.25 >;
        #declare N = N + 3;
      #end
      #if (n_tanque = 2)
        object{ tanque2
                translate < 5.0*i, 5.0*j, 0.0 >
              }
        #declare ponta_cano[N]   = < (5.0*i + 1.00), (5.0*j + 2.00), +3.00 >;
        #declare ponta_cano[N+1] = < (5.0*i + 1.00), (5.0*j + 1.50), +4.25 >;
        #declare ponta_cano[N+2] = < (5.0*i + 1.00), (5.0*j + 0.50), +4.25 >;
        #declare N = N + 3;
      #end
      #if (n_tanque = 3)
        object{ tanque3
                translate < 5.0*i, 5.0*j, 0.0 >
              }
        #declare ponta_cano[N]   = < (5.0*i + 1.00), (5.0*j + 2.25), +1.50 >;
        #declare ponta_cano[N+1] = < (5.0*i + 1.00), (5.0*j - 0.25), +1.50 >;
        #declare ponta_cano[N+2] = < (5.0*i + 1.00), (5.0*j + 1.00), +4.25 >; 
        #declare N = N + 3;
      #end
      #local j = j + 1;
    #end
    #local i = i + 1;
  #end
#end

#include "eixos.inc"
// object{ eixos(10) }

#declare cmin = <  -1, -1, -1 >;
#declare cmax = < +12, +8,+11 >;

box{ cmin-0.1*z, <cmax.x,cmax.y,cmin.z>  texture{ pigment{ color rgb <0.80,0.70,0.60> } finish {diffuse 0.7 ambient 0.3 } } }

#include "camlight.inc"
#declare centro_cena = (cmin + cmax)/2;
#declare raio_cena = 0.5*vlength(cmin - cmax);
#declare dir_camera = < 7.00, 2.00, 1.00 >;
#declare dist_camera = 7*raio_cena;
#declare intens_luz = 1.20;
camlight(centro_cena, raio_cena, dir_camera, dist_camera , z, intens_luz)