Advanced POV-Ray Effects: Animation
by D.W. Hyatt
Using the Clock Variable
It is possible to send to the POV-Ray program an external value called a clock
variable that can be used animate objects within a POV-Ray scene. The value
is passed via an external script using an additional K parameter in the
form +Kn where
n is a number representing the clock value.
This clock value can be utilized in many ways within the source file in
order to do animations or modify characteristics in the ray traced image.
Consider the following examples:
POV-Ray Command | Explanation |
sphere
{ <0,0,0>, 3.0 + 0.5 * clock}
|
This would cause the radius of a sphere to get bigger by 0.5 times the clock
value.
|
sphere
{ <0,0,0>, 3.0
pigment {
color rgbf < 0.0 + 0.1*clock, 0, 0, 0> } }
|
This would cause the color of the sphere to get redder by 0.1 times
the clock value.
|
sphere
{ <0,0,0>, 3.0
translate
< 0, 3 * (sin(radians(30*clock)), 0> }
|
This will cause the sphere to move up and down in the y direction depending
upon sine of the angle defined as the clock value multiplied by
30 degrees. Note the use of a function to convert the angle into radians.
|
camera {
location <0, 5, -10>
look_at <0, 0, 0>
rotate <0, 30*clock, 0>
}
|
This will cause the camera to rotate 30 degrees times the clock value
|
Using Scripts and the Clock Variable to Help Make an Animation
In order to make an animation, the programmer must make a series of
separate frames with the objects in different locations.
Each of those frames must be saved as a separate image and then put together
to make some kind of movie.
This will be the real advantage of the clock variable because we can define
the animation in terms of that value, and then pass different numbers to the
script.
The simple povray script we have used before can be easily modified
to include a
clock variable that can be added to the outfile name. Look carefully at
the following script to see how it is done:
Example Script:
clockpov
x-povray -I$1.pov +V +D0 +P +X100 +FP16 +O$1$2.ppm +K$2
If the above script is called with the following arguments:
Example Call:
-
clockpov sphere
05
The script will use the filename sphere (
argument $1 ) and the clock value
05 ( argument
$2 ) for the parameters in the script. Povray will look for
the source file sphere.pov, and it will create a PPM image outfile
called sphere05.ppm that is a combination of both arguments.
This will be extremely handy when we use another program to animate the
images and create an animated GIF for a web page.
Animating the Output Files
The output files can be animated using a module from the UNIX program
called ImageMagick. To just animate the various frames, just type:
-
animate
sphere*.ppm
sphere00.ppm
|
sphere01.ppm
|
sphere02.ppm
|
sphere03.ppm
|
sphere04.ppm
|
sphere05.ppm
|
sphere06.ppm
|
sphere07.ppm
|
sphere08.ppm
|
sphere09.ppm
|
sphere10.ppm
|
sphere12.ppm
|
This will take the files that start with sphere and end with .ppm
and will animate them in sequence depending upon their file names. That is
why it is convenient to have them named with a numerical value attached.
With ImageMagick, there are controls to speed up or slow down the animation
as well as make it go forward or reverse.
Making an Animated GIF Image
To create an animated GIF image, ImageMagick has another routine that is
quite helpful called convert. Although it is possible to convert
directly from the PPM images, it is better to use xv to make
some smaller GIF images of each frame, and then use the command:
-
convert
-delay 20
-loop 0
sphere*.gif
animatespheres.gif
This will take all of the source frames and will make them into one
animated GIF image called animatespheres.gif. The -delay 20
argument will cause a 20 hundredths of a second delay between each frame,
and the -loop 0 will cause the gif to loop over and over again.
Click on the image to the right to see it animate. This sequence has used
many of the above concepts including a rotating camera, the moving red
sphere that goes up and down, and the changing color of the translucent
sphere as it goes from blue to clear. Twelve frames were used to make
the animation. The source code is listed below.
-
Source Code
Conditional Clock Values
Sometimes one might want the clock to do one action for a while and
then do something else later. This can be accomplished by a using
conditional statement.
For instance, the following code shows how to use conditional
actions for the povray clock variable
using the #if and #else statements so that the
sphere is drawn one way when the clock is less than 5, and another
way the rest of the time.
The code must also have the #end statement to show the
parser where the statements belonging to the conditional stop and
other normal statements continue. This code also shows
how to define a new variable with #declare. In this case,
we have created something called XShift that we use to
translate or shift along the x-axis that only happens when
the clock exceeds the value of 5 in the example below.
-
#if (clock < 5)
sphere { < 0, clock * 0.1, 0 >, 1 }
#else
sphere { < 0, clock * 0.5, 0 >, 1 }
#declare XShift = (clock - 5) * 0.1
translate < XShift, 0, 0 >
#end
More Documentation on POV-Ray
For the full documentation on POV-Ray 3.1, you may check
out the files that are provided in the current software release which we have
located on our systems in the following directory:
-
/usr/local/lib/povray31
and especially the documentation file: povuser.txt
The full documentation of POV-Ray is quite large though (hundreds of pages!!!),
so if you want a hard copy please
print it at home using your own resources rather than here at school.
In the povray31 directory,
there are a number of other useful files as well as a directory full of
povray scenes with source code in case you want to
see how some other effects are done. Seeing how others have
done something is a great way to learn new skills that you
can then apply in your own creative way.
Using the Cluster to Create an Animation
Animations can be generated more quickly by running
POV-Ray on the Linux Cluster.