The magazine of the Melbourne PC User Group
An Introduction to Raytracing
Myles Strous |
|
One of the aspects of personal computers that continues to give me great
pleasure is the ability to create three-dimensional graphics, from photo-realistic images and animations to
clay animation or cartoon figures to realtime immersive environments.
Rendering is the term given for the technique the computer uses to create images from the set of
instructions that you, the artist, have given it.
As the artist you define the position and shape of the objects you see through the window, the colours and
physical properties of their surfaces, and the placement and types of lights that illuminate them. You can do
this using either a graphical interface, where you might see the objects in a wireframe or simplified view,
or through a script interface somewhat like a programming language. Fortunately you don't have to know any of
the mathematics behind all this - the software does it all for you. However, you probably will need a basic
grasp of the X, Y, and Z axes common in the three-dimensional world.
While there are other techniques for rendering, raytracing is in some ways the easiest process to
understand.
Imagine that you have one eye, a single tiny dot, and that you are looking at the world through a small
rectangular window a short distance in front of you, filled with a wire mesh. The small rectangular window is
your computer screen, or a file representing it, and the holes in the screen mesh represents the pixels on
your screen, while your eye is the virtual pinhole camera in the raytracing program. Instead of really seeing
through the camera, the software mathematically projects a line from the imaginary camera (your eye) through
each pixel on the screen (each little hole in the screen mesh) and determines if the line hits any objects in
the scene you have described (see Figure 1). If the line hits, the pixel is filled with the colour of the
object at that position, allowing for lights, reflections and other influences.
|

Figure 1. Basic camera model used by raytracing software
|

Figure 2. Calculating reflections and shadows
|
This method of rendering allows for relatively easy addition of
extra features such as simple shadows and reflections. For shadows, when a ray from the camera hits an
object, the software creates lines from that point on the object to each light you have defined in the scene.
If a line doesn't hit anything else the the point is illuminated by that light, if the line contacts other
objects beforehand then the point will be shadowed from that light. This makes for easy sharp-edged shadows
(a point is either in the shadow or not).
For reflections, the software calculates at what angle the line from the camera would bounce off the surface
it has contacted, and whether the bounced line contacts any other objects in the scene (see Figure
2).
There are a number of other techniques used to calculate or render images, which can also produce very good
images - scanline rendering, z-buffers, a-buffers, etc. A very simple realtime version used by some games is
known as raycasting, which divides the floor of a scene up into squares and only casts a single ray through
each square.
Many people tend to call all non-raytracing techniques by the term scanline rendering, but this is not
always correct, nor is scanline rendering a single technique, but rather a group of techniques. Some of these
alternative techniques are faster than raytracing and are used when speed is more important, such as
animation, when many hundreds of images may need to be rendered. Often there are ways of avoiding
calculations that require excessive amounts of time, such as using an image as a fake reflection.
Large images, or images containing large numbers of lights, complicated objects and complex textures, can
still take a lot of time to render, whatever the technique used. Some home rendering enthusiasts create large
complicated images that may take days to render on slower computers, although there are also home users
creating spectacular images that render in minutes. Some of the frames in Toy Story took 4 hours or more to
render on one computer, even using rendering techniques that were faster rather than raytracing. To get the
large number of frames needed for the movie rendered in a reasonable amount of time, Pixar used a large
number of computers networked together and used specifically for rendering, a setup known as a render
farm.
Some software uses a hybrid renderer, which uses a faster technique for non-reflective non-transparent
objects, but uses slower raytracing calculations for reflective objects or transparent refractive
objects.
Carefully watch computer-generated animations (including feature films) and you'll sometimes see where they
have taken shortcuts e.g. a large glowing object that throws sharp-edged shadows instead of fuzzy ones. In
some cases this may be a cinematic decision for artistic purposes, but in some cases it is used to decrease
rendering times by faking the light cast by the large glowing object with an invisible simpler type of
"lightbulb" or "spotlight" that renders much faster.
My first significant venture into the area of 3D rendering was with a freeware program that I still
recommend and that is still in active development: the Persistence Of Vision Raytracer, better known as
POV-Ray. It now has it's own website, http://www.povray.org/, where you
will also find many links to tutorials, helper utilities, modelling software, ready-made objects, and include
files, the POV-Ray equivalent of plug-in extensions.
The large POV-Ray user community is extremely helpful and generous to new users, and included in the
software distribution package are extensive help files and sample scenes.
POV-Ray uses the programming style of interface where you write scene descriptions to define your image in a
similar manner to writing computer programs. There are utilities and modellers for POV-Ray that will allow
you to do this in a graphical way without writing anything, using instead an interface somewhat similar to a
drawing or illustration program.
Let's look at a very basic scene in POV-Ray. We specify a camera, with some very basic details:
camera
{
// camera position X, Y, Z
location <0.0, 0.5, -4.0>
// camera focus point X, Y, Z
look_at <0.0, 0.0, 0.0>
}
We describe a light by its type, position, and colour. The most common light for beginners is the point
light, which acts like a light globe shining in all directions.
// standard point light source
light_source
{
// light's location X, Y, Z
<-30, 30, -30>
// light's colour defined in terms
// of red, green, and blue
color red 1.0 green 1.0 blue 1.0
}
Thirdly, we describe something to look at : a sphere (chosen
because it is simple to describe and quick to render).
sphere {
// centre X, Y, Z and the radius or size
// of the sphere
<1, 0, 0>, 1
// The basic surface colour of the sphere
pigment {
// Another way of defining red, green,
// and blue.
color rgb <1, 0, 0>
}
}
Here is the whole scene description, with formatting squeezed up :
camera { location <0.0, 0.5, -4.0>
look_at <0.0, 0.0, 0.0> }
light_source { <-30, 30, -30> color
red 1.0 green 1.0 blue 1.0 }
sphere { <1, 0, 0>, 1 pigment { color
rgb <1, 0, 0> } }
We start the computer rendering a scene based on this information, and some time later the software delivers
an image created to our specifications (see Figure 3). As a comparison to the possibly frightening render
times mentioned before, this simple scene used only 5 seconds rendering time on an Athlon running at 750 MHz,
when rendered out at a resolution of 640 by 480 with anti-aliasing (edge smoothing) set to 0.3.
|

Figure 3. Simple raytraced scene example
|
It is possible to write quite complicated scenes using the
script interface, but for those more visually inclined, it is also possible to model an entire POV-Ray scene
using a graphical interface (see Figure 4). One such interface is HamaPatch, a freeware modeler for POV-Ray
and other programs, available from
http://www.crosswinds.net/hamapatch/. You can even start the renderer from within some modellers so that
for some scenes you may never see a line of POV-Ray code (see Figure 5).
|

Figure 4. HannaPatch interface, quad view
|

Figure 5. Image rendered with POV-Ray, modelled in HannaPatch
|
This doesn't even begin to scratch the surface of what POV-Ray
(or any other 3D rendering software) can be used to create. Download POV-Ray yourself from http://www.povray.org/ and enter the wonderful world of three-dimensional
graphics.
After learning the basics of 3D creation and rendering, you might find POV-Ray sufficient for your needs, or
you might wish to investigate some of the commercial rendering software available, for the interface and
productivity features offered.
If you are interested in seeing or talking more about raytracing and rendering,
come along to one of the meetings of the Graphics Special Interest Group, Melb PC SIG room, third Monday of
the month, 7.30 pm. Beginners welcome. Ask for Myles.
Reprinted from the November 2000 issue of PC Update, the
magazine of Melbourne PC User Group, Australia
|