prev index next 
Subroutines and Include Files 
 POV-Ray Tips, 
 Tricks, and Techniques
Are There Subroutines / Macros? 
 
Felix Morley Finch writes: I thought I'd learn POVray by modeling a house.  Got some 
basics down, then ran into real trouble.  In short, I tried to make "subroutines" and can't.
 
Michael D Johnson replies:
Well... that's because 'subroutines' work a bit differently in POVRay...
 
Felix Morley Finch continues: I declared a basic door.  Then I added transforms to line up the hinge point for opening inwards or outwards, and left or right hinges. Finally, I wanted transforms to position the door for south, north, west, and east walls.  
  
The problem that has me stumped is that the basic Door  

  #declare Door = object {   
    box <0, 0, 0>,   
    <Door_Width, Door_Height, Door_Thickness>   
    pigment { Tan }   
  }   
 
assumes that the real door definition  
 
  #declare Door3068 = object {  
    #declare Door_Width = 3 * 12 + 0  
    #declare Door_Height = 6 * 12 + 8  
    object { Door }  
  }  
...
 

Michael D Johnson explains: Ok.... almost right.  But in the wrong order.  When your Door object is declared, it is declared with the CURRENT values of Door_Width, Door_Height, and Door_Thickness.  Once set, they CANNOT change, as you surmised later in your message.  So, how do 'subroutines' work?  Not quite the way you'd expect :)  The way I do subroutines (and the way it pretty much has to be done, so far) is to create an include file (say, door.inc) that has the basic door, defined as above.  Set your heights and widths then include the file, thusly: 

  #declare Door3068 =  
  object  
  {  
     #declare Door_Width = 36  
     #declare Door_Height = 80  
     #include "door.inc"  
     object { Door }  
  }  

Your Door3068 is now correct. 

So, yes, as you stated, you do need to include the file each time.  Is it messy?  Yes.  Does it work?  Yes.  Could it be better?  Yes, but not until 4.0:) 

One thing you might consider, which I have done in some of mine, is make an include file multi-purpose.  For instance, you could have one file able to create both doors and archways with some variable: 

  #declare PortalType = 1 // Do an archway  
  #include "portals.inc"  

where portals.inc looks something like: 

  #switch (PortalType)  
    #case (0)   // Do a door  
      ...  
    #break  

    #case (1)   // Do an archway  
      ...  
    #break  
  #else  
    #error concat("Bad PortalType: ", str(PortalType, 0, 0),  
                  " in portals.inc\n")  
  #end  

To see some more examples (ones that actually work :) look at my utilities web page at http://quark.gmi.edu/~redbeard/raytrace/POVRay-Util.html.

  1