Raytracing

If we look closely at the screen capture taken from our animation program, we'll notice that the puppet appears to be floating. This is because there are no shadows on the ground. Shadows help us determine the position of objects relative to each other visually.

There are a few different ways to draw 3D objects on a computer screen. A simple method is to tell the computer what objects are at what location and the computer displays them for us. This method is very fast, and is what is used to display objects in our animation program. However, objects do not know their relation to each other in this method, so visual cues such as shadows are not displayed.

Another method is called raytracing. Raytracing is much slower. It involves calculating the colour of each pixel on the screen.

For each pixel on the screen, we can draw a line (ray) from the camera to the pixel. Next, we ask each object in the screen if it intersects our line.

  • If our line intersects no object. We display nothing at that pixel.
  • If our line hits an object, we find where it intersects the object at.
  • If our line hits multiple objects, We want to keep the closest object.

Once we know what object should be drawn in the pixel, we want to know its colour. Without light, there is no colour, so we draw a ray from our intersection point to the light.

  • If our ray hits the light and does not hit any other objects, then our object at that pixel is lit, and we draw its colour, taking into account the colour of the light.
  • If our ray hits another object before reaching the light, then our object at that pixel is in shadow, and no colour is drawn.
  • The may be multiple lights in a scene. In that case, we need to cast rays to each light point to see if our object is lit by a particular light. An object can be in shadow from some light sources but not others.

Besides shadows, we can also calculate more complex relationships between objects. For example, if a pixel displays a sphere that is reflective, we can calculate what object is reflecting off that sphere, and take the colour of the reflected object into account when calculating our colour.

Because we are processing thousands of pixels one at a time (a 400x300 image has 120,000 pixels), raytracing can be a very slow process. However, images rendered with raytracing tends to be higher quality, as we take into account the relationship between the objects. There are more complex things we can do with raytracing besides just calculating shadows and reflections, but that is beyond the scope of this tutorial.

on to software and applications
back to index