Directives

In addition to #include, POV-Ray implements a few very powerful directives. We shall briefly cover #declare, #if-else and #while on this page.

Declaring Identifiers

This is done with #declare. With this directive, one can give numbers, vectors, expressions, objects and so on names up to 40 characters. The following are examples:
#declare   Radius = 2.5                      // constant
#declare   Unit_Sphere =
                sphere { 0*x, 1 }            // shape
#declare   MovingDirection = < 1, 2, 3>      // vector
#declare   PI = 3.1415926
#declare   PI2 = 2*PI                        // expression
#declare   ShootingDirection = x + y + z     // equal to < 1, 1, 1 >
#declare   Triple = 3*ShootingDirection      // vector expression
#declare   MyPigment =                       // pigment
               pigment { color SpicyPink }
#declare   MyFinish =                        // finish
               finish { phong 1  ambient 0.2 }
It is interesting to note that an identifier is actually a variable. The following illustrates this capability:
#declare  Count = 0                     // Count is zero now
     .......
#declare  Count = Count + 1             // Count becomes 1
     .......
#declare  Count = 10*Counter            // Counter becomes 100
     .......
#declare  Count = 0                     / Counter is reset to zero

IF ELSE Directive

The #if-else directive has the following syntax:
#if ( floating-expression )
     ..... do this if the expression is non-zero .....
#else
     ..... otherwise, do this part .....
#end
For example, the following code shows that if Which_Pigment is 1, MyPigment is used so that the sphere would have a spicy pink color; otherwise, the sphere is colored in yellow. It should be pointed out that if you declare some POV-Ray entity with an identifier (i.e., pigment, finish, light_source and so on), this identifier must be enclosed in that entity's keyword when it is used. The following shows that even though MyPigment is declared as a pigment, it has to be used within pigment { }.
#declare  Which_Pigment = ..... // your value
#declare   MyPigment = pigment{ color SpicyPink }

#declare   Center        = 0*x
#declare   Radius        = 2.5
     .....
sphere {
     Center, Radius
     #if (Which_Pigment)
          pigment { MyPigment }
     #else
          pigment { color Yellow }
     #end
     finish { phong 1 }
}

WHILE Directive

The #while directive has the following syntax:
#while ( floating-expression )
     ...............
#end
It is usually used with #declare directives to control the value of the condition. The following generates a series of spheres around a circle of radius Distance:
#declare   Distance = 100
#declare   Radius   = 0.05
     ..........
#declare   Angle = 0                    // start with 0 degree
#while (Angle <= 360)                   // if less than 360 degree
     sphere {                           // put a sphere at the origin
          0*x, Radius
          pigment { color  Red }
          finish  { phong 1 }
          translate  Distance*x         // translate in x-direction
          rotate     Angle*y            // rotate it to desired position
     }                                  // sphere def. ends here
     #declare  Angle = Angle + 10       // increase Angle
#end