Sierpinski Pyramid POVRay Part 2

Published on May 29, 2022 at 5:03 pm by LEW

Introduction

In this second post of the Sierpinski Pyramid series (the first post can be found here), I will be discussing some POVRay mechanics. I find these tend to make scripting a scene easier for me. But your mileage may vary.

Before continuing, please review the previous post. It provides some context into what we are doing. In this post we will discuss several specific scripting language features and some caveats that you should be aware of when writing scene scripts in POVRay.

Creating Scene Objects

The POVRay scripting language has the ability to make objects. Objects are useful for things you plan on reusing in a scene, and for making your code more readable. I have read the on line manual and many tutorials, and I find the explanation of objects has always confused me. Not that the tutorials are wrong, but that they can be a little confusing about how to define and use objects in your script.

Objects in POVRay are similar to variables, in that they can be declared. We are going to use the Mesh pyramid we discussed in the last post and make a declared object out of it. It is probably just me, but I have always found the existing documentation and tutorials a bit vague on how to do this.

It is a two step process, declaration, then usage. We must first declare a variable for our mesh and make it equal to the mesh. Below we declare the variable pyramid, and set it equal to our mesh at a global level (for this project I am not going to use any local variables, so everything will be global).

#declare pyramid =           // Mesh pyramid structure

mesh {
     triangle {<1, 0, 1>, <1, 0, -1>, <0, 1, 0>}
     triangle {<-1, 0, 1>, <-1, 0, -1>, <0, 1, 0>}
     triangle {<1, 0, -1>, <-1, 0, -1>, <0, 1, 0>}
     triangle {<1, 0, 1>, <-1, 0, 1>, <0, 1, 0>}
     triangle {<1,0,1>, <1,0,-1>, <-1,0,-1>} 
     triangle {<1,0,1>,<-1,0,-1>, <-1, 0,1>}
}

Once we have done this, we can use our keyword with the object container anywhere in our program, and also apply modifiers to it (pigment, texture, transformation, etc)

object {                // Using the mesh object pyramid
     pyramid
     texture {
          pigment {rgb <1, 0, 0>}
     }
     scale 2
     translate <0, 4, 0>
} 

Order of Modifiers

The order in which an object is modified in POVRay is important. Otherwise we will get some unexpected results. Basically when an object is rotated or scaled, said operation is done around the axis of origin. If you translate an object, this moves it off the axis of origin, and causes some interesting results from both scaling and rotation.

The takeaway from this; always rotate and scale first, then translate the position. This will help insure no unexpected results.

Loops and Recursion

To make the render, at higher recursion levels we need to create many scaled and translated pyramids. To do this from our single object will require a  loop structure of some sort. POVRay has a few options available in the scene scripting language. They are (in no particular order) the While Loop, the For Loop, and the Macro Loop.

While Loop

This loop needs a condition to compare against. The loop continues until the condition is meet, then exits. See the syntax below, this loop will continue until the counter equals one.

#declare counter = 10;

#while(Index >= 1)
     (script statements to execute while in the loop)
     #declare counter = counter - 1;
#end

For Next Loop

While called a for next loop in other programing languages, POVRay refers to as just a For loop. This loop differs from the While loop, in that there is a counter that automatically decrements by a set amount after each iteration of the loop. In the example below the loop has an identifier (Name), A start Count (0), and end count (5), and a step (1). Note if unspecified, step is assumed to be one.

#for (Name, 0, 5, 1)
     (script statements to execute while in the loop)
#end

Macro Loop

The macro loop is a little harder to explain. It involves a macro calling itself. It is the more complicated structure. In the below example we have a macro called test. When called it runs through its code, then checks the value of counter. If it is less than ten, the macro increments the counter and calls itself again. If it is greater than ten the macro exits.

#declare counter = 0;

#macro tester ()
     #declare counter = counter + 1;
     (script statements to execute while in the loop)
     #if(counter<10) tester() #end
     #declare counter = counter – 1;
#end

tester()

Recursion

Recursion is basically a function calling itself to solve smaller and smaller instances of a specific problem. In our case the problem is placing smaller copies of the pyramid correctly.

It might seem the macro calling itself would be the best fit here, While it is, there are some limits in POVRay around how many times a macro can call itself. Depending on how much recursion will be done, it might be better to using one of the other looping structures.

Conclusion

In this post we discussed at some of the options available for writing scripts in POVRay.

We continue to build towards our goal. In our next post, we will start looking into the math involved in creating Sierpinski Pyramid.

Sierpinski Pyramid POVRay Part 1

Sierpinski Pyramid POVRay Part 2

Sierpinski Pyramid POVRay Part 3

Sierpinski Pyramid POVRay Part 4

 

Add New Comment

Your email address will not be published. Required fields are marked *