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

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

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

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

#declare tx_azul  = 
  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_rosa  = 
  texture{
    pigment{ color rgb < 0.40, 0.10, 0.30 > }
    finish{ diffuse 0.8 ambient 0.1 specular 0.5 roughness 0.005 }
  }

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

// ======================================================================
// DESCRICAO DA CENA 



#macro tanque1(xx,yy,zz)
  union{
    object{
      sphere {
        <xx,yy,zz> + <0,0,0>,
        5
      }
      texture{ tx_estranha }
    }
    object{
      cylinder{
        <xx,yy,zz> + <0,0,5>,
        <xx,yy,zz> + <0,0,6>,
        0.5
        texture{ tx_cano }
      }
    }
  }
#end

#macro tanque2(xx,yy,zz)
  union{
    object{
      cylinder{
        <xx,yy,zz> + <0,0,0>,
        <xx,yy,zz> + <0,0,7>,
        4
        texture{ tx_xadrez }
      }
    }
    object{
      cone{
        <xx,yy,zz> + <0,0,7>,
        4
        <xx,yy,zz> + <0,0,10>,
        2
        texture{ tx_xadrez }
      } 
    }
    object{
      cylinder{
        <xx,yy,zz> + <0,4,4>,
        <xx,yy,zz> + <0,5,4>,
        0.5
        texture{ tx_cano }
      }
    }
    object{
      cylinder{
        <xx,yy,zz> + <0,0,10>,
        <xx,yy,zz> + <0,0,11>,
        0.5
        texture{ tx_cano }
      }
    }  
  }
#end

#macro tanque3(xx,yy,zz)
  union{
    object{
      cylinder{
        <xx,yy,zz> + <0,0,0>,
        <xx,yy,zz> + <0,0,10>,
        4
        texture{ tx_rosa }
      }
    }
    object{
      sphere {
        <xx,yy,zz> + <0,0,10>,
        4
      }
      texture{ tx_rosa }
    }
    object{
      cylinder{
        <xx,yy,zz> + <0,4,4>,
        <xx,yy,zz> + <0,5,4>,
        0.5
        texture{ tx_cano }
      }
    }
    object{
      cylinder{
        <xx,yy,zz> + <4,0,4>,
        <xx,yy,zz> + <5,0,4>,
        0.5
        texture{ tx_cano }
      }
    }
    object{
      cylinder{
        <xx,yy,zz> + <-4,0,4>,
        <xx,yy,zz> + <-5,0,4>,
        0.5
        texture{ tx_cano }
      }
    }
  }
#end

#macro tanque4(xx,yy,zz)
  union{
    object{
      cylinder{
        <xx,yy,zz> + <0,0,0>,
        <xx,yy,zz> + <0,0,7>,
        8
        texture{ tx_azul }
      }
    }
    object{
      cylinder{
        <xx,yy,zz> + <0,0,7>,
        <xx,yy,zz> + <0,0,12>,
        6
        texture{ tx_azul }
      }
    }
    object{
      cylinder{
        <xx,yy,zz> + <0,8,4>,
        <xx,yy,zz> + <0,9,4>,
        0.5
        texture{ tx_cano }
      }
    }
    object{
      cylinder{
        <xx,yy,zz> + <0,-8,4>,
        <xx,yy,zz> + <0,-9,4>,
        0.5
        texture{ tx_cano }
      }
    }
    object{
      cylinder{
        <xx,yy,zz> + <8,0,4>,
        <xx,yy,zz> + <9,0,4>,
        0.5
        texture{ tx_cano }
      }
    }
    object{
      cylinder{
        <xx,yy,zz> + <1,2,12>,
        <xx,yy,zz> + <1,2,13>,
        0.5
        texture{ tx_cano }
      }
    }
  }
#end

#macro cano(posicaoInicial, posicaoFinal, altura)
  union{
    object{
      cylinder{
          posicaoInicial,
          posicaoFinal,
          0.5
          texture{ tx_cano }
        }
    }
  }
#end


#macro interpola1(tt,tt0,v0,tt1,v1)
  #local ss = (tt - tt0) / (tt1 - tt0);
  #local vv = (1 - ss) * v0 + ss * v1;
  vv
#end

#macro interpola3(tt, tta, ttb, v0, v1, v2, v3)

  #local v01 = interpola1(tt,tta,v0,ttb,v1);
  #local v12 = interpola1(tt,tta,v1,ttb,v2);
  #local v23 = interpola1(tt,tta,v2,ttb,v3);

  #local v012 = interpola1(tt,tta,v01,ttb,v12);
  #local v123 = interpola1(tt,tta,v12,ttb,v23);
  #local v0123 = interpola1(tt,tta,v012,ttb,v123);

  v0123
#end

#macro executa_interpola(n, raio, p0, p1, p2, p3)
  union{

    #local k = 0;

    #while (k < n)
      #local c = interpola3(k, 0, 500, p0, p1, p2, p3);
      object{
        sphere {
          c,
          raio
        }
        texture{ tx_rosa }
      }
      #local k = k + 1;
    #end
  }
#end


#declare NMAX = 1000;
#declare arrayDasPontas = array[NMAX];
#declare aleatorio = seed(45187);
#declare CONSTANTE_POSICAO = 25;
#declare CONSTANTE_ALTURA = 20;
#declare numeroTotalPontas = 0;

#macro geraTanques(m,n)

  union {

    #local indexM = 0;
    #while(indexM < m)
      #local xx = indexM * CONSTANTE_POSICAO;

      #local indexN = 0;
      #while(indexN < n)
        #local yy = indexN * CONSTANTE_POSICAO;
        #local zz = rand(aleatorio) * CONSTANTE_ALTURA;

        #if(rand(aleatorio) < 0.5)
          #local zz = zz * -1;
        #end

        #local tanqueEscolhido = rand(aleatorio) * 4;

        #if(tanqueEscolhido < 1)
          object { tanque1(xx,yy,zz) }

          #declare arrayDasPontas[numeroTotalPontas] = <xx, yy, zz> + <0,0,5.5>;
          #declare numeroTotalPontas = numeroTotalPontas + 1;
        #else 
          #if(tanqueEscolhido < 2)
            object { tanque2(xx,yy,zz) }

            #declare arrayDasPontas[numeroTotalPontas] = <xx, yy, zz> + <0,5,3.5>;
            #declare arrayDasPontas[numeroTotalPontas + 1] = <xx, yy, zz> + <0,0,10.5>;
            #declare numeroTotalPontas = numeroTotalPontas + 2;
          #else
            #if(tanqueEscolhido < 3)
              object { tanque3(xx,yy,zz) }

              #declare arrayDasPontas[numeroTotalPontas] = <xx, yy, zz> + <0,5,3.5>;
              #declare arrayDasPontas[numeroTotalPontas + 1] = <xx, yy, zz> + <5,0,3.5>;
              #declare arrayDasPontas[numeroTotalPontas + 2] = <xx, yy, zz> + <-5,0,3.5>;
              #declare numeroTotalPontas = numeroTotalPontas + 3;
            #else
              object { tanque4(xx,yy,zz) }

              #declare arrayDasPontas[numeroTotalPontas] = <xx, yy, zz> + <0,9,3.5>;
              #declare arrayDasPontas[numeroTotalPontas + 1] = <xx, yy, zz> + <0,-9,3.5>;
              #declare arrayDasPontas[numeroTotalPontas + 2] = <xx, yy, zz> + <9,0,3.5>;
              #declare arrayDasPontas[numeroTotalPontas + 3] = <xx, yy, zz> + <1,2,12.5>;
              #declare numeroTotalPontas = numeroTotalPontas + 4;
            #end
          #end
        #end

        #local indexN = indexN + 1;
      #end

      #local indexM = indexM + 1;
    #end

  }
#end

#macro geraTubulacoes(m,n)

  union{

    #while(numeroTotalPontas > 0)
      #local i = int(numeroTotalPontas * rand(aleatorio));
      #local j = int((numeroTotalPontas - 1) * rand(aleatorio));

      #if(j = i)
        #local j = j + 1;
      #end

      #declare posicaoToco1 = arrayDasPontas[i];
      #declare posicaoToco2 = arrayDasPontas[j];
      #local altura = rand(aleatorio) * 8 + 13;

      object{ cano(posicaoToco1, posicaoToco2, altura) }

      #declare arrayDasPontas[i] = arrayDasPontas[numeroTotalPontas - 1];
      #declare numeroTotalPontas = numeroTotalPontas - 1;

      #if(numeroTotalPontas > 0)
        #declare arrayDasPontas[j] = arrayDasPontas[numeroTotalPontas - 1];
        #declare numeroTotalPontas = numeroTotalPontas - 1;
      #end
    #end

  }

#end

#include "eixos.inc"

// Aqui este a cena, finalmente:

#declare mm = 4;
#declare nn = 4;

union {

  //object { geraTanques(mm,nn) }
  //object { geraTubulacoes(mm,nn) }
  object { tanque1(0,0,0) }
  object { tanque2(-20,20,-15) }
  object { executa_interpola(500, 0.5, <0,0,6>, <0,15,9>, <-15,-15,0>, <-20,20,-4>) }

}

#include "camlight.inc"
#declare centro_cena = < 0.00, 0.00, -1.00 >;
#declare raio_cena = 50.0;
#declare dir_camera = < 3.00, 3.00, 1.00 >;
#declare dist_camera = 5*raio_cena;
#declare intens_luz = 1.20;
camlight(centro_cena, raio_cena, dir_camera, dist_camera , z, intens_luz)