Difference between revisions of "HowTo:Use macros and loops"

From POV-Wiki
Jump to navigation Jump to search
(New page: == Loops == Loops are usually created to do one set of code thousands of times. The beauty of the loop is that the code can reference an array or a spline to write a rollercoaster track or...)
 
(Add source tag for highlighting POV SDL)
Line 4: Line 4:
 
Loops may be constructed through the use of the #while statement. A typical loop may be constructed as follows.
 
Loops may be constructed through the use of the #while statement. A typical loop may be constructed as follows.
  
  #declare step = 0;
+
<source lang="pov">
  #declare steps = 100;
+
#declare step = 0;
  #while (step <= steps)
+
#declare steps = 100;
    #sphere { step*x, 0.4 pigment {Blue} }
+
#while (step <= steps)
    #declare step = step + 1;
+
  #sphere { step*x, 0.4 pigment {Blue} }
  #end
+
  #declare step = step + 1;
 +
#end
 +
</source>
  
 
Nested loops may be created to generate a two or three dimensional aspect to the scene. Beware that creating extra nested loops will dramatically increase the parse time for the scene.
 
Nested loops may be created to generate a two or three dimensional aspect to the scene. Beware that creating extra nested loops will dramatically increase the parse time for the scene.
Line 15: Line 17:
 
The following nested loop will draw a series of Blue balls on the XZ plain
 
The following nested loop will draw a series of Blue balls on the XZ plain
  
   #declare xpos = 0;
+
<source lang="pov">
 +
#declare xpos = 0;
 +
#declare zpos = 0;
 +
#declare xfinal = 10;
 +
#declare zfinal = 10;
 +
#while (xpos <= xfinal)
 +
  #while (zpos <= zfinal)
 +
    #sphere { <xpos,0,zpos> 0.4 pigment {Blue} }
 +
    #declare zpos = zpos + 1;
 +
  #end
 +
   #declare xpos = xpos + 1;
 
   #declare zpos = 0;
 
   #declare zpos = 0;
  #declare xfinal = 10;
+
#end
  #declare zfinal = 10;
+
</source>
  #while (xpos <= xfinal)
 
    #while (zpos <= zfinal)
 
      #sphere { <xpos,0,zpos> 0.4 pigment {Blue} }
 
      #declare zpos = zpos + 1;
 
    #end
 
    #declare xpos = xpos + 1;
 
    #declare zpos = 0;
 
  #end
 

Revision as of 13:31, 8 December 2007

Loops

Loops are usually created to do one set of code thousands of times. The beauty of the loop is that the code can reference an array or a spline to write a rollercoaster track or a roadway such that very complicated scenes can be created with the use of simple structures.

Loops may be constructed through the use of the #while statement. A typical loop may be constructed as follows.

#declare step = 0;
#declare steps = 100;
#while (step <= steps)
  #sphere { step*x, 0.4 pigment {Blue} }
  #declare step = step + 1;
#end

Nested loops may be created to generate a two or three dimensional aspect to the scene. Beware that creating extra nested loops will dramatically increase the parse time for the scene.

The following nested loop will draw a series of Blue balls on the XZ plain

#declare xpos = 0;
#declare zpos = 0;
#declare xfinal = 10;
#declare zfinal = 10;
#while (xpos <= xfinal)
  #while (zpos <= zfinal)
    #sphere { <xpos,0,zpos> 0.4 pigment {Blue} }
    #declare zpos = zpos + 1;
  #end
  #declare xpos = xpos + 1;
  #declare zpos = 0;
#end