Welcome to Lesson 2 of the POV-Ray tutorial. I'm going to cover a few more advanced techniques here that make POV-Ray much more powerful than your average rendering program. As before, POV-Ray code is represented in blue, and keywords are in red.
You might have noticed that maille uses a lot of rings. It's about as tedious to write out all of those torus definitions as it is to weave all those rings. There has to be a simpler way, right? And there is. You can set up what is known as a for loop that allows you to easily replicate some objects over and over again. Check this out (camera at < 3, 4, 5> ):
#declare I = 0; #while (I < 5) sphere { < -2+I, 0, 0> .5 pigment {color rgb < .2*I, 1-.2*I, 0>} } #declare I = I+1; #end |
With just one sphere definition and a little extra window dressing, we've made
five spheres going from red to green. That's a lot better than actually going
and defining those five spheres, isn't it? Now, to figure out what exactly is
going on here.
#declare I = 0;
This line creates a variable "I" (the letter I is often used in for loops; it
stands for "index"). We can refer to the variable once it has been declared,
like in the position vector and the color vector.
#while (I < 5)
This line sets a certain type of conditional statement
. It says "As long as the variable I is less than the value 5, do
whatever follows between here and the line #end."
Without this line, you won't accomplish much; it causes the program to loop
over your code, thereby generating the extra spheres.
Now we take a look at the sphere. The I variable shows up in three places:
once in the position vector, and twice in the pigment definition. Remember
that I goes from zero to 4 (not 5!) during the course of the loop. So when
the sphere's position vector says
< -2+I, 0, 0>
it says that the sphere should be located at (-2+I) X, 0 Y, and 0 Z. Since I
has varying values, this creates a series of spheres 1 unit apart. Now, the
pigment statement causes the spheres to range from red to green. We do that by
causing the red value of the color vector to start at 0 and reach .8 at the
end, while the green value starts at 1 and reaches .2 at the end. A fairly
simple way to get a nice gradient effect. You can use similar effects anywhere
that POV-Ray needs a number. You could make the spheres grow consecutively
shinier, for example, or create a ring of spheres (or whatever strikes your
fancy) with a rotate statement.
The next item here is incredibly powerful. You can use a #declare to create a special definition for just about anything, give that definition a name, and then refer to that definition later by name. #declares will save you countless amounts of typing; learn to use them effectively and your hands will thank you. I think we'd better get to an example now (standard camera again).
#declare BlueCube = box { < -1, -1, -1> < 1, 1, 1> pigment {color rgb < 0, 0, 1>} } object {BlueCube translate < -2.5, 0, 0>} object {BlueCube} object {BlueCube translate < 2.5, 0, 0>} |
Having created the definition for the blue cube, we can refer to it easily using the object keyword. However, objects are not the only things you can #declare, as this example shows:
#declare ShinyFinish = finish { diffuse .4 ambient .2 phong .5 reflection .4 } #declare Silvery = pigment { color rgb < .6, .6, .6 > } sphere { < -2, 0, 2> .7 pigment {Silvery} finish {ShinyFinish} } box { < -1, -1, -1> < 1, 1, 1> pigment {Silvery} finish{ShinyFinish} } torus { 1.2, .2 rotate < 90, 0, 0> translate < 2.5, 0, 0> pigment {Silvery} finish {ShinyFinish} } |
Come up with a good metallic finish? Never retype it again! Just #declare it and you can include it in any object you make with ease. Same goes for pigments and just about anything else you want to make. #declares will make your life a lot easier, so learn to use them.
Before I go any further, I want to tell you about a bit of shorthand that will
make both of our lives easier. Whenever you type the single character
x, POV-Ray will turn it into
< 1, 0, 0> (note that the 'x" must be
lowercase). Similar things happen with y and
z. Also, if you use a single number where POV-Ray expects a vector, then that
number will get "stretched" into a vector. For example:
box { -1, 1 pigment {color rgb z} } |
That's a lot simpler than writing out the three vectors. It's our standard blue cube, just like the one we #declared earlier, but much more compact. I'll be using this shorthand from here on out, to save myself a little typing.
Now we're going to cover three special techniques that are part of what is known as Constructive Solid Geometry, or CSG for short. CSG is a fancy way of saying that we can add and delete objects from each other. We have three techniques available to use: the union, difference, and intersection techniques. We'll start with the union; it's most commonly used to group several objects in a #declare.
#declare LinkedRings = union { torus { 1.2, .2 rotate 45*x translate -1*x pigment {color rgb y} } torus { 1.2, .2 rotate -45*x translate 1*x pigment {color rgb y} } } object {LinkedRings} object {LinkedRings rotate 45*x} |
Using the union technique, we can group a bunch of objects together and manipulate them simultaneously. You could also use it, say, to apply a single pigment and finish to a group of objects - just stick them all in a union and put the pigment and finish statements at the end.
The next CSG technique, the difference, is used to "subtract" one object from another. Difference requires at least two objects - the object that will be visible and the objects that will be subtracted from it. Here's an example:
difference { box { -1, 1 pigment {color rgb 1} } sphere { 0, 1.2 pigment {color rgb z} } } |
The cube has had a sphere cut out of it. Notice that the inside surface of the new shape is blue - subtracted regions obtain the color and finish of the object that is subtracted, not the original object. If you want a consistent coloring and finish, you'll have to use the same pigment and finish on all of the objects involved.
The final CSG we're going to deal with now is the intersection. Intersection takes in any number of objects, and it creates a shape that consists of all of the space contained by all of the objects. A simple example consists of two spheres (camera at < 0, 2, 3>):
intersection { sphere { -1*x, 1.2 pigment {color rgb x} } sphere { x, 1.2 pigment {color rgb y} } } |
This region is the area contained by both of the spheres in the intersection statement. Simple enough, right? Just realize that if any two of the objects in your intersection don't overlap at all, you'll get precisely nothing for your efforts.
That does it for the more advanced "basic" features of POV-Ray. Good luck!
All items on this site are copyright 2002 Chris Weisiger (a.k.a. Derakon). That's right - I made everything on this site. Reproduction of any of my work i\ n whole or in part requires my express consent.