// Makes a gnarled leafy tree if parameters are set right
  // Must use Pov 3.1 or later

  #include "colors.inc"

  #declare PdV = <0,10,-25>; 

  camera {
  location  PdV
  direction <0.0,0.0,3>
  up        <0.0,1.0,0>
  right     <4/3,0.0,0>
  look_at   <0.0, 12,0>}

  #declare colTree = rgb<0.52,0.46,0.43>;

  light_source{< -300,1040, -520>color colTree*2}
  light_source{<-1000,1040,-1020>color rgb<1,.5,.3>}
  light_source{PdV color colTree}

  #declare txtTree =
  texture{pigment{granite color_map{
  [0.0 color colTree*2 ]
  [0.5 color colTree*1 ]
  [1.0 color colTree*.5]}}
  normal{wrinkles 2 scale .3}
  finish{ambient .2}scale<1,6,1>*.3}

//  background{White}

// Tree macro
// Based on the tree macro bundled in R. Suzuki's isosurface patch (1997)
// Quick and dirty adaptation by Gilles Tran
// Adaptation by Ken Tyler & Steven Pigeon - added leaves on 1-1-1999
// Please modify & improve !
// L = length of branches
// leaflevel - Set Branch Level where Leaves Start 
// Hint set leaflevel higher than recursion level to turn leaves off
// B = Broadness of Branches
// D = Level of recursion (can be VERY long to parse if D>8)
// rX,rY,rZ : several parameters (!)
// yL = dummy used in macro to transfer the branch length between
//  recursion levels, leave it to 0
// s1 : starts the random process before the macro
// R : sets the random process for the leaf rotation
// MESH_SIZE - # of triangles that make up the leaf
// Leaf_Size - overall scale of the leaf object

//////////////////////////////////////////////////////////////
// Make the leaf object

#declare R         = seed(1999);
#declare MESH_SIZE = 6;
#declare Leaf_Size = .6;

#declare One_Leaf  =
 mesh{
   #declare        A  = 1;
   #while        ( A <= MESH_SIZE )
    // Calculate random location for first point.
    #declare X1 =      ( rand(R) * 2.0 ) - 1.0;
    #declare Y1 =      ( rand(R) * 2.0 ) - 1.0;
    #declare Z1 =      ( rand(R) * 2.0 ) - 1.0;

    // Move a little way from *first* point.
    #declare X2 = X1 + ( rand(R) * 0.6 ) - 0.3;
    #declare Y2 = Y1 + ( rand(R) * 0.6 ) - 0.3;
    #declare Z2 = Z1 + ( rand(R) * 0.6 ) - 0.3;

    // Move a little way from *first* point.
    #declare X3 = X1 + ( rand(R) * 0.6 ) - 0.3;
    #declare Y3 = Y1 + ( rand(R) * 0.6 ) - 0.3;
    #declare Z3 = Z1 + ( rand(R) * 0.6 ) - 0.3;

    // Make the "leaf" triangle.
    triangle {<X1,Y1,Z1>,<X2,Y2,Z2>,<X3,Y3,Z3>}


    // Make the "leaf branch" triangles.
    triangle {<X1,Y1,Z1>,<0,0,0>,<0.0,0,0.1>  }
    triangle {<X1,Y1,Z1>,<0,0,0>,<0.1,0,0.0>  }
    #declare A = A + 1;
   #end // while (A<=MESH_SIZE)
   scale Leaf_Size
  } // mesh.


/////////////////////////////////////////////
// The Tree Macro section

#declare s1        = seed(8991);

#macro mTree(L, leaflevel, B, D,rX,rY,rZ,yL)
#if (D > 0)
 union
  {
   #declare   r1 = 0.5+rand(s1)/3;
   #declare   r2 = 0.4+rand(s1)/3;
   #declare   r3 = 0.4+rand(s1)/3;
   #declare   r4 = 0.5-rand(s1);
   #local  New_D = D - 1;
   #local  New_B = B*r1;
   #local  New_L = L*r1;
   #declare    i = 1;
   #declare   Va = <0,0,0>;
   #declare   Ba = B;
   #declare nSeg = D+3;

   #while (i<nSeg)
    #declare   r5 = 0.5-rand(s1);
    #declare   r6 = 0.5-rand(s1);
    #if        (i = nSeg)
    #declare   Vb = y*L;
    #else
    #declare   Vb = <r5*B*1.5,L*i/nSeg,r6*B*1.5>;
    #end

    #if        (D = 1)
     #declare FinB = 0.01;
    #else
     #declare FinB = New_B;
    #end

    #declare   Bb = B+(FinB-B)*i/nSeg;

    cone  {Va,Ba,Vb,Bb texture{txtTree}}
    sphere{0,Bb translate Vb texture{txtTree}}

    #declare    i = i+1;
    #declare   Va = Vb;
    #declare   Ba = Bb;
   #end // (i<nSeg)

   // These parms set the branch angles
   mTree( New_L, leaflevel, New_B, New_D, 45*r1, 10*r2,15*r4,L)
   mTree( New_L, leaflevel, New_B, New_D,-55*r2,-70*r3,25*r4,L)
   mTree( New_L, leaflevel, New_B, New_D,-65*r3, 70*r2,35*r4,L)
   rotate x*rX rotate y*rY rotate y*rZ translate y*L
  } // union
 #end // if (D!=0)
 #if (D<=leaflevel)
  #declare   Vb = y*L;
  object{One_Leaf rotate<0,90*rand(R)-45,0> translate Vb pigment{rgb<0,.5,0>}}
 #end // if (D>2)
#end // macro


///////////////////////////////////
// example usage
  #declare Tree1 = mTree( 6,0, .1, 4, 19, 19, 19,0)
/*#declare Tree2 = mTree( 6,2, .1, 6,  5, 20,  0,0)
  #declare Tree3 = mTree( 6,2, .2, 4, -5,  0, 20,0)
  */
  object { Tree1 }
  /*object { Tree2 rotate y*30 translate -x*6}
  object { Tree3 rotate -y*70 translate x*6}*/