Benjammin's Povray Pages HOME Benjammin's Povray Pages

GETTING TO KNOW THE CLOCK

One of the things that got me interested in POV-Ray was the ability to create animations. Of course if you're like me and read the manual you've found there's the usual good technical explanations but it doesn't go into great depth (pretty hard to do considering the size of the thing). So hopefully this tutorial will shed a little light on the subject. Once you understand the concept of the clock you'll unleash some of the most powerful aspects of POV-Ray.

But before we jump into the clock variable you'll need to know how to create an INI file to run everything. Besides passing various rendering options through this file you also get to specify your animation options. If you're using Linux or another command line version of POV-Ray you've probably already dealt with the INI file or at the very least you're familiar with passing the options via the command line. Windows, Mac, BeOS and Amiga users might be unfamiliar with this concept but it doesn't matter 'cause you're about to learn.

First lets assume your POV file is called image.pov. Now create a new file called image.ini. For Linux users just use your favourite text editor. For Windows select New_File from the File drop-down menu and Save_As image.ini. Sorry but I don't have a Mac, BeOS or Amiga system so I can't give specifics but I'll assume its a variant on what I've already said for the preceeding examples. Unless you're familiar with the concept of PATHs I suggest creating/saving this INI file in the same folder/directory as your POV file - it'll make your life easier for now.

Here's a very basic INI file you can use as your starting point. I'll explain below

; image.ini

Input_File_Name=image.pov

Initial_Clock=0.000
Final_CLock=1.000

Initial_Frame=1
Final_Frame=10

+KC            ; cyclic animation ON

Jitter=off

Height=120
Width=160

The first line starts with a ; (semi-colon) and is merely a comment that is not seen by POV-Ray. You can use a semi-colon anywhere you'd like within the INI file to write comments and notes.

The line with Input_File_Name=image.pov tells POV-Ray which file is being acted on. This will be the file you want to animate.

The two lines with Initial_Clock=0.000 & Final_CLock=1.000 specify when the clock starts and when it ends. In this case it runs from 0 to 1 but it can be any number as long as the start is less than the end. I've written it as a float instead of an integer because you can use fractions of a whole number when describing the clock. Although I won't detail it here you can use a clock that's less than 0 and greater than 1.

The next two lines Initial_Frame=1 & Final_Frame=10 tells POV-Ray how many frames are being rendered and how to number them.

The line with +KC turns on cyclic animation. This is useful for making looped animations. I've used the shorthand format for this option just to show that you can use either form. If you read the POV-Ray manual you'll see there are two ways to express your options. It doesn't matter which you choose or if you mix and match the two formats. Use whatever works best for you. You'll also notice I've included a comment at the end of this line. Remember everything on the same line after a ; is invisible to POV-Ray.

The option Jitter=off turns off one of the antialiasing features. This is a good idea to include when doing an animation (even with antialiasing turned on) or you may find small pixel sized details within your animation dancing around.

The final two options Height=120 & Width=160 simply tell POV-Ray the size of the frame. Its a good idea to use a ratio of 4:3 (width:height) for your frame size or you may find objects get distorted.

Okay so that's a basic POV-Ray INI file and the one you'll run when you render your animation. For Linux do x-povray image.ini (assuming you're using x-povray), in Windows focus on image.ini and hit the RUN button, everybody else I'll assume knows how to make things go on their system. There's a lot more options you could (and should) include but I won't cover that right now 'cause I'm sure you'd like me to get on with explaining the clock. (Read the manual for all the INI file options)


The Clock

The clock goes into your POV file and can be used to animate any number of things, objects, camera, textures etc. Here's a simple POV file that animates a box which you can use for your image.pov file. I'll explain how it works below.

camera { location <0.0, 2.0, -8.0> 
		look_at <0.0, 0.0, 0.0> }

light_source { 0*x color rgb <1,1,1> 
		translate <-30, 30, -30> }

plane { y, -1.5 pigment {color rgb <1,0,1>} }

box { <-1, -1, -1> < 1,  1,  1> 
	pigment {color rgb <0,1,0>} 
	rotate y*360*clock }
an1_1.gif

When rendered as a 10 frame animation you get what you see in the GIF. Okay it looks a little jerky and isn't antialiased but you could fix that up by using more frames and some antialiasing options. The basic scene file doesn't need an explanation as I'll assume you are familiar with POV's language. What you want to look at is the end of the box statement where it says rotate y*360*clock.

The rotate y*360 is pretty straight forward as all that does is tell POV-Ray to spin the object 360 degrees on the y axis, one complete turn. The magic happens when you add *clock. By multiplying the total amount of rotation by the clock you are telling POV-Ray to start at the first frame using NO rotation and then calculate the needed amount for each following frame until the total of 360 degrees has been reached. Because +KC (cyclic animation) was used you don't actually get the full rotation until the loop starts again. In other words each frame is advanced 360/10=36 degrees of rotation. If you weren't using cyclic animation you'd get 360/9=40 degrees of rotation for each frame but you'd end up with the first and last frames being exactly the same. Make sense? If not, think about it, it'll come to you:-)

Here's a little table that may show this idea a bit better in the format of frame_number=degree_of_rotation.

Using cyclic animation
1=0 2=36 3=72 4=108 5=144 6=180 7=216 8=252 9=288 10=324

Without cyclic animation
1=0 2=40 3=80 4=120 5=160 6=200 7=240 8=280 9=320 10=360

Both animations start at the same place with no rotation being applied so frame 1 is at 0 degrees. But as you can see by the time the last frame is reached there is a big difference. So just remember that if you want a looping animation use the +KC option in your INI file, otherwise leave it out.

Okay that covers the basics of using the clock as applied to one thing. What if you want the spinning box to move across the frame as well as rotate? Take a look at the next example and I'll explain below.

camera { location <0.0, 2.0, -8.0>
		look_at <0.0, 0.0, 0.0> }

light_source { 0*x color rgb <1,1,1>
		translate <-30, 30, -30> }

plane { y, -1.5 pigment {color rgb <1,0,1>} }

box { <-1, -1, -1> < 1,  1,  1> 
		pigment {color rgb <0,1,0>} 
		rotate y*360*clock 
		translate x*-8 
		translate x*16*clock }
an1_2.gif

This time the rotating box is moving from left to right across the frame. Once again we look at the end of the box statement to where is says rotate y*360*clock translate x*-8 translate x*16*clock. Here we've got two clocks, one applied to rotate and the other to translate, as well as an extra translate.

The rotate y*360*clock part is the same as in the first example. Then there is a translate x*-8 directive which moves the box to the left and just out of frame which is where I want the box to start its movement along the x axis. It doesn't have a clock applied to it. The final translate x*16*clock will apply animation and should move the box a total of 16 units to the right which should take it just out of the frame on the other side. Well not exactly. Because the cyclic animation option was used rotate y*360*clock does what we want but translate x*16*clock only moves the box 14.4 units to the right. Howzat?!! We've got 16 units of translate and 10 frames which should give 16/10=1.6 units of movement per frame. Because we started at x*-8 on frame 1 and added 1.6 units of movement per frame along the x axis we end the animation with the box at x*6.4 before the loop starts again.

So what if we really wanted the box to end up at x*8? One answer is this formula:

(desired_movement/(total_frames-1))*total_frames = needed_movement

Filling in the blanks from the animation gives:

(16/(10-1))*10 = 17.7778

So the animated translate directive using looping animation should actually read translate x*17.7778*clock if you wanted the box to finish at x*8 before the loop starts again. Hope that makes sense, and of course if you're not using the +KC option you won't need to do the extra math.

The last thing I'll mention about this animation is the order of the directives. You'll notice that I used rotate first before I moved the box anywhere. That's because as far as POV-Ray is concerned the center of the universe is at xyz <0,0,0>. The box was created using that as its center so when rotated it will do exactly what you'd expect, spin around its axis. After that was done I moved the box along its x axis with translate x*-8 to where I wanted it to start. I then applied the other translate clock so it would move properly in a lateral direction during the animation. If rotate y*360*clock were after translate x*16*clock the movement would be something all together different. Try it and you'll see what I mean.

So that brings me to the next animation which will show what happens when rotate is applied after translate and how it can be useful for making a simple hydogen atom. Here's the basic POV file with the explanation below.

camera { location <0.0, 2.0, -8.0>
		look_at <0.0, 0.0, 0.0> }

light_source { 0*x color rgb <1,1,1>
		translate <-30, 30, -30> }

plane { y, -1.5 pigment {color rgb <1,0,1>} }

sphere { <0, 1, 0> 0.5 
		pigment {color rgb <0,1,0>} }

sphere { <0, 1, 0> 0.25 
		pigment {color rgb <1,1,0>} 
		translate x*-4 
		rotate y*360*clock }
an1_3.gif

What we have here is one sphere orbiting another. The first (green) sphere is only there to show you where the center of the universe is. The second (yellow) sphere is the one with the clock applied to it. What's happened here is the yellow sphere has had translate x*-4 applied to move it 4 units off center. Then rotate y*360*clock was applied and voila its orbiting the center of the universe! You probably don't need an explanation at this point because its pretty straight forward what's going on. Just remember that POV-Ray applies things in the order it finds them. If you're creating a complex animation and it doesn't work right take a look at where and in which order you've put your clocks.

If you don't want all your animations to be centered at xyz <0,0,0> just apply the clock first and then move the object. In the preceeding example you could #declare a new object using a union and then move that object where ever you'd like. Something like this.

#declare hydrogen_atom = union {
			sphere { <0, 1, 0> 0.5 
			pigment {color rgb <0,1,0>} }

			sphere { <0, 1, 0> 0.25 
			pigment {color rgb <1,1,0>} 
			translate x*-4 
			rotate y*360*clock }
}

... bunch of pov stuff ...

object { hydrogen_atom translate <-3, 4.25, 5> }

The above snippet will keep the animation intact while moving it to where you want. You could even animate it further from within the object statement ... and on and on.

If you've made it this far and you're anything like me you're probably wishing you could save yourself the trouble of writing these files yourself. That's why I've provided this link so you can download a zip of the files for the 3 animations.

Final Thoughts

Although I've only applied the clock to objects there's no reason you couldn't do the same for the camera or even animate a texture. Try experimenting. If you are comfortable using POV conditionals like #if you'll be able to have different parts of your scene animated depending on what the value of the clock is.

#if (clock < 0.5)
  ... whatever you want to animate goes here ...
#end

Or try applying sin to the clock and see what happens. Actually try this out on the first (green) sphere in the last animation.

sphere { <0, 1, 0> 0.5 
	pigment {color rgb <0,1,0>} 
	translate sin(clock*(22/7))*y }

The possibilites are only limited by your imagination.

Happy Animating! - Benjammin

BACK

Copyright Ben Teschendorff 2002 2003 2004 2005

 00707 

Povray Links
Povray Now! www.povray.org
Moray Now! www.stmuc.com/moray
My Links
Benjammin's MainVision Pages - scripts and info
Benjammin's Music Pages
The Gasworks - Always a Babe Fest
Atomic Linux - automate your home
Benjammin's Software Pages
Video Links
Main Concept - excellent video software
Virtual Dub - excellent free video software
EyeDropVideo - royalty free animations, images, audio
More Links
Very cool sculptures, statues and jewelery