Endless POVabilities

- By Sonya Roberts

The Fourth Lesson - More About #WHILE...#END and RAND() -- Continued

Part 2 - Getting Rather More Complex

These trees and bubbles have been created using relatively simple combinations of loops and the rand() function. Let's now make a more complex routine, one that creates a variety of related but different objects. One good example for this would be flowers; they all possess leaves, a stem, and a blossom, but the colours, shapes, and placement of these can vary greatly. For this lesson, we'll use an easy example, where each flower is a single unique grouping of objects. Each of these will include some degree of randomness in how the components making them are assembled, so that each flower is not an exact carbon copy of the other flowers in it's same "species".

We'll make the flower creation routine in the form of an include file, and then add some code to our scene file that uses this include file to create a scattering of flowers in our scene. This example is going to be fairly lengthy, so rather than trying to include it all, I'm going to be using a lot of ellipses ("...") to indicate where I've skipped over things in the example file. I'll also be explaining chunks of code as I go along, rather then leaving all the explanation to the end.

parts.pov

#declare DandeLeaf=
object {
...
}

#declare PinksLeaf=
object {
...
}

...
...
...

In this file we're defining a bunch of pieces that POV will use when creating the flowers. We're defining them in a seperate file from the .INC file, as otherwise POV would have to parse and re-parse these bits of code every time the .INC file was called to add in a new flower. Instead, we'll simply #include this file before the section of scene code that uses the "flowers.inc" file, and these bits and pieces will therefore be defined and ready for use when the .INC file is invoked.

flowers.inc

#ifndef (SD1)...#end
#ifndef (WhichShape)...#end

We're starting by setting up the seed value for our random numbers, and a variable which can be used to select which "flower" to use.

#switch (WhichShape)
#case (1)
// -- Dandelion-like Flower --
...
#break

#case (2)
// -- Pink Flower --
...
#break

#case (3)
// -- Forget-me-not Flower --
...
#break
#end


These are all the definitions of the different "flower" objects we're using. If you look at the definitions, you'll see random values being used for many purposes, from varying the number of leaves and petals to perturbing their positions so that they aren't all at the same angle or position. #While loops also play an important part in these definitions; once a rand() number has decided that there will be, for example, 5 leaves, those five leaves need to be correctly positioned in relation to the stem. Since we've split up the definitions using a #case...#switch statement, the file will only create one of these flowers when called, the flower whose #case matches by the "WhichShape" variable.

Finally, we need to add some code to our "items.pov" file that will include the definitions of the pieces used to create the flowers, and then create a scattering of these flowers across our ground. As with the trees and soap bubbles, we'll make this conditional, so that we can turn the flowers on and off by switching a variable in our original scene file.

scene.pov

#declare I_Items=True
#if (I_Items)
#declare I_Bubbles=True
#declare I_Trees=True
#declare I_Flowers=True
#include "items.pov"
#end


items.pov

#declare SD4=seed(66)
#if (I_Flowers)
#include "parts.pov"
object {
union {
#declare X1=-15
#while (X1<=15)
#declare Y1=-15
#while (Y1<=15)
object {
#declare WhichShape=1+int(rand(SD4)*2.999)
#include "flowers.inc"
translate <X1+(-1+(rand(SD4)*2)),0,0+Y1+(-1+(rand(SD4)*2))>
}
#declare Y1=Y1+2.5
#end
#declare X1=X1+2.5
#end
}
}
#end


The Next Lesson

In the next lesson, we'll take a look at things that can be done with images, from the simple image map through to bump_maps, height_fields, and material_maps.

<Crystal Clarity> <Topas Try Me's> <Povabilities> <Soap Box> <Cranky's Corner>
<Top> <Home> <CG Web Board> <Contact>