// CORES E TEXTURAS
#include "eixos.inc"
background{ color rgb < 0.75, 0.80, 0.85 > }


#declare random = seed (1153);

#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 < 0, 0.80, 0> }
    finish{ diffuse 0.9 ambient 0.1 }
  }

#declare tx_verde = 
  texture{
    pigment{ color rgb < 0, 0.80, 0> }
    finish{ diffuse 0.9 ambient 0.1 }
  }

#declare tx_marrom = 
  texture{
    pigment{ color rgb < 0.6, 0.3, 0> }
    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_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 roleta = seed(1);

#declare chao = 
  box{ <0,0,-2>, <+40,+40,-3> texture{tx_xadrez}}

#declare cilindro_base = 
  cylinder{
    < 0.00, -1.00, 3.00>,
    < 0.00, +1.00, 3.00>,
    0.75
    texture{ tx_marrom }
  }

#declare cone_asa_direita =
  cone{
    <0.75, 0.00, 3.00>, 0.4,
    <2, 0.00, 3.00>, 0
    texture{ tx_marrom }
  }

#declare cone_asa_esquerda =
  cone{
    <-0.75, 0.00, 3.00>, 0.4,
    <-2.00, 0.00, 3.00>, 0
    texture{ tx_marrom }
  }

#declare furo_cilindro = 
  cylinder{
    < 0.00, -2.00, 3.00>,
    < 0.00, +2.00, 3.00>,
    0.5
    texture{ tx_marrom }
  }

#declare cilindro_tronco = 
  cylinder{
    < 0.00, 0.00, -2.00>,
    < 0.00, 0.00,  2.00>,
    1
    texture{ tx_marrom }
  }

#declare esfera_tronco = 
  sphere{
    <0, 0, 3>, 1
    texture{ tx_marrom }
  }

#declare esfera_folha = 
  sphere{
    <0, 0, 3>, 1
    texture{ tx_verde }
  }

#declare cone_folha = 
  cone{
    <0, 0, 4>, 1
    <0, 0, 5.5>, 0
    texture{ tx_verde }
  }

#macro galho ()
  union{
    difference{
        object {cilindro_base}
        object {furo_cilindro}
    }
    object {cone_asa_direita translate < 0,-0.5,0 >}
    object {cone_asa_direita translate < 0,0.5,0 >}
    object {cone_asa_esquerda translate < 0,-0.5,0 >}
    object {cone_asa_esquerda translate < 0,0.5,0 >}
  }
#end

#macro esfera_galhos()
  union{
      object{esfera_tronco translate<0, 0, 0>}
      object{galho() translate<0,+3.5,-0.25> rotate<30,0,0>}
      object{galho() translate<0,-3.5,-0.25> rotate<330,0,0>}
  }
#end

#macro tronco_arvore()
  union{
    object {cilindro_tronco translate<0, 0, 0>}
    object {esfera_tronco translate<0,0,-6>}
  }
#end

#macro corpo_arvore(P)
  union{
      object{tronco_arvore()}
      object{esfera_galhos()}
      translate <P.x,P.y,P.z>
  }
#end

#macro folha(P)
  union{
    object{esfera_folha}
    object{cone_folha}
    translate <P.x, P.y,P.z>
  }
#end

#macro arvore (P, A, K)
  union{
    object{corpo_arvore(P)}
    #local valor = rand(random);
    #if(valor < 0.3 & K != 0 & A != 0)
      object{arvore(<P.x, P.y - 3.25, P.z + 8>, A-8, K-1)}
      object{arvore(<P.x, P.y + 3.25, P.z + 8>, A-1, K-1)}
    #else
      object{folha (<P.x, P.y - 3.25, P.z + 2>)}
      object{folha (<P.x, P.y + 3.25, P.z + 2>)}
    #end

  }
#end

#macro plantacao (M,N,A,K)
  union{
    #local l = 0;
    #local xcanteiro = 1;
    #while(l < M)
      #local c = 0;
      #local ycanteiro = 1;
      #while(c < N)
        #local Xarvore = xcanteiro + xcanteiro * rand(roleta);
        #local Yarvore = ycanteiro + ycanteiro * rand(roleta);
        object{arvore(<Xarvore,Yarvore,2>, A, K) scale <0.75, 0.75, 0.75>}
        #local c = c + 1;
        #local ycanteiro = ycanteiro + 10;
      #end
      #local l = l + 1;
      #local xcanteiro = xcanteiro + 10;
    #end
  }
#end

object{ eixos(3.00) }
object{ chao }
object{plantacao(4,3,50,4)}

#include "camlight.inc"
#declare centro_cena = < 20, 20, 5.00 >;
#declare raio_cena = 50.0;
#declare dir_camera = <5, 4, 4 >;
#declare dist_camera = 5*raio_cena;
#declare intens_luz = 1.20;
camlight(centro_cena, raio_cena, dir_camera, dist_camera , z, intens_luz)