// ************************************************************************************************************************
// Make_Plant (Joined HFs) Creates Unit-Cube Plant_Object Base-Centered at <0,0,0>
// ************************************************************************************************************************
// Input:  pnPlantNo  
//         Plants_On (0/1)  
//         Images_On (0/1)
// Output: Unit-Cube Plant Object with Base Centered at <0,0,0>
// ************************************************************************************************************************
// Plant List
//    Plant 1: Brown_Tree_1
//    Plant 2: Yucca_Plant
//    Plant 3: Brown_Tree_2
//    Plant 4: Red_Bush
// ************************************************************************************************************************
// Image Definitions (Example image is "_PLANT_5x")
//    _PLANT_5a.jpg  Height_Field with outline of Plant  (black=0=absent, white=1=present)
//    _PLANT_5b.jpg  Height_Field with outline of Leaves (black=0=absent, white=1=present)
//    _PLANT_5c.jpg  Image to use for body of plant      (used for coloration only)
//    _PLANT_5d.jpg  Image to use for leaves             (used for coloration only)
// ************************************************************************************************************************
// Usage: 
//    #declare Brown_Tree = Make_Plant (1) ;
//    Plant {Brown_Tree translate x*10}
//
// ************************************************************************************************************************
// Written /08/26/2004 by James Coons
//    Note: Each individual Plant can have different methods of using the Images for 
//          coloration. An images can be resized and turbulated so it only represents 
//          a color, or it can be scaled meaningfully to produce textures results in 
//          the final image.    
//
//    This macro was designed to simplify the creation of Plants based on Height_Fields. 
//    Each Plant created is referenced by number. This Macro should either be customized 
//    for each Project or it should refer to Plants that are in the PovRay Path. 
//          
//    Object Interaction
//       Plants were designed to create proper interaction with other objects, produce 
//       shadows and reflections, etc. However, since each plant is basically composed 
//       of 3 planar objects, there may be situations where a slight rotation would 
//       prevent problems caused by "on-one" viewing of the plant. Since the Foliage is 
//       translucent, light will permeate the leaves and  produce a realistic effect.
// ************************************************************************************************************************
#macro Make_Plant (pnPlantNo)
   #declare Make_Plant_Body =
      #if (Shapes_On=1)
         height_field {
            #switch (pnPlantNo)
               // Define Height_Fields used for Plant Body 
               #case (1) jpeg "_Tree_1a.jpg"  #break  // Brown_Tree_1
               #case (2) jpeg "_Plant2a.jpg" #break  // Yucca_Plant
               #case (3) jpeg "_Plant3a.jpg" #break  // Brown_Tree_2
               #case (4) jpeg "_Plant4a.jpg" #break  // Red_Bush
            #end
            smooth water_level 35/255}
      #else
         box { <0,0,0> <1,1,1> }
      #end
#declare Make_Plant_Foliage =
   #if (Shapes_On=1)
      height_field {
         #switch (pnPlantNo)
            // Define Height_Fields used for Plant Foliage
            #case (1) jpeg "_Tree_1b.jpg"  #break  // Brown_Tree_1
            #case (2) jpeg "_Plant2b.jpg" #break  // Yucca_Plant
            #case (3) jpeg "_Plant3b.jpg" #break  // Brown_Tree_2
            #case (4) jpeg "_Plant4b.jpg" #break  // Red_Bush
         #end
         smooth water_level 35/255 scale 1.01} 
   #else
      box { <0,0,0> <1,1,1> }
   #end
#declare Plant_Scaling =
   transform {translate -0.5 rotate x*-90 scale <1.0,1,0.01> translate y*0.5 }
      
// HF and Regular Texture
#declare Make_Plant_Object =
   union {
      // Central Plant Body
      object {Make_Plant_Body
         transform Plant_Scaling
         #if (Images_On)
            pigment {image_map {
               // Define Images used for Color of Central Plant Body
               #switch (pnPlantNo)
                  #case (1) jpeg "_Tree_1c.jpg" #break      // Brown_Tree_1
                  #case (2) jpeg "_Plant2c.jpg" #break     // Yucca_Plant
                  #case (3) jpeg "_Plant3c.jpg" #break     // Brown_Tree_2
                  #case (4) jpeg "_Plant4c.jpg" #break     // Red_Bush
               #end
               interpolate 2.0 map_type 0 }
               rotate x*90 transform Plant_Scaling scale 0.001 turbulence 5.0
               }
            #break
         #else
            pigment {
               // Colors of Central Plant Body
               #switch (pnPlantNo)
                  #case (1) color Brown     #break          // Brown_Tree_1
                  #case (2) color Yellow    #break          // Yucca_Plant
                  #case (3) color Brown*0.8 #break          // Brown_Tree_2
                  #case (4) color Red*0.3   #break          // Red_Bush
               #end
               }
            #break
         #end
         finish {ambient 0.3 reflection 0.1 }
         #if (Normals_On=1) normal {agate 1.0 scale 0.01} #end
         }
      #if (Shapes_On=1)
         // Foliage In Front of Central Plant Body 
         object {Make_Plant_Foliage
            transform Plant_Scaling
            #if (Images_On)
               pigment {image_map {
                  // Define Images used for Plant Foliage
                  #switch (pnPlantNo)
                     #case (1) jpeg "_Tree_1d.jpg"  #break  // Brown_Tree_1
                     #case (2) jpeg "_Plant2d.jpg" #break  // Yucca_Plant
                     #case (3) jpeg "_Plant3d.jpg" #break  // Brown_Tree_2
                     #case (4) jpeg "_Plant4d.jpg" #break  // Brown_Tree_2
                  #end
                  interpolate 2.0 map_type 0 }
                  rotate x*90 transform Plant_Scaling
                     scale 0.25
                  }
            #else
               pigment {color Green*0.6 filter 0.975}
            #end
            finish {ambient 0.55 reflection 0.2 }
            translate z*-0.02
            #if (Normals_On=1) normal {agate 1.0 scale 0.01} #end
            double_illuminate
            }
         // Foliage Behind Central Plant Body 
         object {Make_Plant_Foliage
            transform Plant_Scaling
            #if (Images_On)
               pigment {image_map {
                  // Define Images used for Plant Foliage
                  #switch (pnPlantNo)
                     #case (1) jpeg "_Tree_1d.jpg"  #break  // Brown_Tree_1
                     #case (2) jpeg "_Plant2d.jpg" #break  // Yucca_Plant
                     #case (3) jpeg "_Plant3d.jpg" #break  // Brown_Tree_2
                     #case (4) jpeg "_Plant4d.jpg" #break  // Red_Bush
                  #end
                  interpolate 2.0 map_type 0 }
                  rotate x*90 transform Plant_Scaling
                  }
            #else
               pigment {color Green*0.6 filter 0.975}
            #end
            finish {ambient 0.45 reflection 0.0}
            translate z*0.02
            #if (Normals_On=1) normal {agate 1.0 scale 0.01} #end
            double_illuminate
            }
      #end
      }
   
// Return Plant Object to Caller
   union { 
      // Equal rotation and scaling produces "symmetrical" plants which look unnatural
      //object {Make_Plant_Object rotate y*37} // Main Plant Object
      //object {Make_Plant_Object rotate y*(37+120) } // Plant Object, rotated 47 degrees
      //object {Make_Plant_Object rotate y*(37+240) } // Plant Object, rotated 240 degrees
      //  Unequal rotation and scaling seems to produce better results
      object {Make_Plant_Object rotate y*37}                     // Main Plant Object
      object {Make_Plant_Object scale 1.05 rotate y*(37+119) }   // Plant Object, first rotation
      object {Make_Plant_Object scale 0.95 rotate y*(37+251) }   // Plant Object, second rotation
   }      

#end
