Ray Tracing and Polygon Rasterization
The goal of computer graphics is to transform raw data stored in a
computer into something visually recognizable by a human being. On a
lower level, that means that for every pixel on the screen, the
computer has to decide what color to give that pixel. For three
dimensional scenes, there are two accepted methods for making this
decision: ray tracing and polygon rasterization.
Ray Tracing
Ray tracing is based upon how light works in the real world: beams of
light bounce off of objects and into your eyeball, allowing you to see.
With ray tracing, rays are shot out from the eye into the 3D world.
When a ray hits something, a new ray bouncing off the object can be
calculated to take into account reflection or refraction. Multiple rays
bouncing off the same object can take lights into account for cool
shading effects. Continually bouncing a ray (the ray hits an object,
then bounces and hits another object, then bounces to another object,
etc.) can create better looking images by taking more of the scene into
account.
Now, I am by no means an expert at ray tracing. I have never
implemented a ray tracing project. I have, however, completed a ray casting project.
Ray casting is a simplified version of ray tracing, where rays are shot
from the eye into the scene once, and no bouncing is done. Here is a
description of ray casting in action:
For each pixel on the screen, the computer finds a ray going from the
eye point through that pixel. The eye point tells the computer exactly
where in 3D space the scene is to be viewed from. When a ray has been
computed, the computer then needs to check and see if the ray hits any
objects in the scene. If the ray does hit an object, then the pixel is
assigned the color of that object.
This is an expensive process, taking a long time even on modern
computers. Consider a 640x480 display (considered small by current
standards). With 640 pixels per line, and 480 lines, that makes 307,200
pixels that need to be drawn. Each pixel needs to be put through the
process of calculating a ray and detecting what objects in the scene
the ray touches. The more objects that are in the scene, the more
computing power this calculation requires. When taking the leap from
ray casting into full blown ray tracing, the calculations involved
increase immensely.
Polygon Rasterization
In polygon Rasterization, every object in the 3D scene is composed of
polygons. In almost all instances these polygons are triangles, since
any 3D shape can be approximated using triangles (in the same way that
any 2D shape can be approximated using line segments). To rasterize a
triangle, the computer projects its three points from the 3D scene onto
the 2D computer screen, and fills in the resulting area with the
triangle's color.
This method is significantly faster than ray tracing, due to simpler
calculations. The projection from 3D into 2D can be computed as a
single matrix multiplication which is the same for every object in the
scene. Also, the act of filling in the area of a 2D triangle goes much
quicker than calculating intersections between rays and numerous 3D
objects.
The nature of this method brings some shortcomings to the table.
Because it does not follow how light actually behaves, it is impossible
to get realistic lighting effects in some cases. Also, the requirement
for everything to be made of polygons eliminates truly round edges, and
it has to make do with an approximation.
Comparison
Both ray tracing and polygon
rasterization have their place in modern computer graphics. To recap,
here are the advantages and disadvantages of each:
Ray tracing allows for high quality images, with impressive lighting,
reflection, and refraction. Also, ray tracing is not restricted to
(although it can use) objects made of polygons; more "pure" objects
such as perfectly round spheres can be used. But the quality comes at a
cost: ray traced images take a long time to render, ranging from
seconds to hours depending on the complexity of the scene.
Polygon rasterization is quick, but produces lower quality images. Some
of the effects that ray tracing can handle are simply not possible with
polygon rasterization.
Ray tracing is the method of choice computer generated movies and
images. Time is not a concern with these projects, as it only needs to
be rendered once and from then on it will be as desired. These projects
often take hours for each frame of a film to render.
For real time applications, and in particular games, polygon
rasterization is the only method available. In order to create a
successful illusion of motion for the human eye, new frames need to be
rendered 15 to 20 times per second. Thirty to sixty frames per second
is most desirable as it allows for smoother game play. At these speeds,
draw times need to be measured in milliseconds, not minutes or hours.
For viewing pleasure, here are some images showcasing both methods:
Ray
tracing example - note the reflections and shadows
Polygon
rasterized sphere - see how the edges are not truly round