# Ray tracing

Ray tracing is a general technique from geometrical optics of studying the path taken by light by following rays of light as they interact with optical surfaces. Its primary applications are in 3D computer graphics, where scenes are rendered by following rays from the eyepoint to light sources, and in the design of optical systems, such as lenses and mirrors.

## Computer graphics algorithm

Ray tracing is a realistic method for rendering images (or frames) constructed in 3D computer graphics environments. It works by tracing the path taken by a ray of light through the scene, and calculating reflection, refraction, or absorption of the ray whenever it intersects an object in the world — hence the name.

Ray tracing's popularity stems from its realism over other rendering methods (such as scanline algorithms); effects such as reflections and shadows, which are difficult to simulate in other algorithms, follow naturally from the ray tracing algorithm. The popularity of the ray tracing method also benefits from the fact that it is relatively simple to implement and yet yields impressive graphical results; it thus often represents a first entry into graphics programming for many individuals.

In the real world, starting at a light source, we may trace a ray of light to a surface, which we can think of as a stream of photons travelling along the same path. At this point, one or more of three things might happen with this light ray: absorption, reflection, and refraction. If the surface has any transparent or translucent properties, it refracts a portion of the light beam into itself in a different direction while absorbing some (or all) of the spectrum (and possibly altering the color). It may also reflect all or part of the light ray, in one or more various ways and directions. It can also absorb part of the light ray, resulting in a difference in a loss of intensity of the reflected and/or refracted light (Note: between absorption, reflection, and refraction, all of the incoming light must be accounted for, and no more. A surface cannot, for instance, reflect 66% of an incoming light ray, and refract 50%, since the two would add up to be 116%.) From here, the reflected and/or refracted rays may strike other surfaces, where their absorptive, refractive, and reflective properties are again calculated based on the incoming rays. Some of these rays travel in such a way that they hit our eye, causing us to see the scene and so contribute to the final rendered image.

Tracing rays in such a way on a computer to simulate this real-world process would be extremely wasteful, as only a minuscule fraction of the rays in a scene would reach the eye. The first ray casting (versus full ray tracing) algorithm used for rendering was presented by A. Appel in 1968. Think of an image as a screen-door, with each square in the screen being a pixel. The idea is to shoot rays from the eye, one per pixel, and find the closest object intersected by that ray. This is then the object the eye normally sees through that pixel. Shade the surface seen by using its material properties and the effect of lights in the scene. The simplifying assumption is made that if a surface faces towards a light, the light will reach that surface and not be blocked by anything between, i.e., not be in shadow. The shade of the surface is computed using traditional 3D computer graphics shading models.

Ray casting for producing computer graphics was first used by scientists at Mathematical Applications Group, Inc., (MAGI) of Elmsford, New York, New York. In 1966 the company was created to perform radiation exposure calculations for the Department of Defense. MAGI's software calculated not only how the gamma rays bounced off of surfaces (ray casting for radiation had been done since the 1940s), but also how they penetrated and refracted within. These studies helped the government to determine certain military applications, such as how to construct military vehicles that would protect troops from radiation, but also how to design re-entry vehicles for space exploration. Under the direction of Dr. Philip Mittelman, the scientists developed a method of generating images using the same basic software, and started a commercial animation studio in 1972. This studio used ray casting to generate 3-D computer animation for television commercials, educational films, and eventually feature films -- they created much of the animation in the film Tron -- using ray casting exclusively. MAGI went out of business in 1985.

One important advantage ray casting offered over scanline algorithms is its ability to easily deal with non-planar surfaces and solids, such as cones and spheres. If a mathematical surface can be intersected by a ray, it can be rendered using ray casting. Elaborate objects can be created by using solid modelling techniques and easily rendered.

The next important research breakthrough came from Turner Whitted in 1979. Previous algorithms cast rays from the eye into the scene, but the rays were traced no further. Whitted continued the process. When a ray hits a surface, it could generate up to three new types of rays: reflection, refraction, and shadow. A reflected ray continues on in the mirror-reflection direction from a shiny surface. It is then intersected with objects in the scene; the closest object it intersects is what will be seen in the reflection. Refraction rays traveling through transparent material work similarly, with the addition that a refractive ray could be entering or exiting a material. To further avoid tracing all rays in a scene, a shadow ray is used to test if a surface is visible to a light. A ray hits a surface at some point. If the surface at this point faces a light, a ray (in truth, a line segment) is traced between this intersection point and the light. If any opaque object is found in between the surface and the light, the surface is in shadow and so the light does not contribute to its shade.

A serious disadvantage of ray tracing is performance. Scanline algorithms and other algorithms use data coherence to share computations between pixels, while ray tracing normally starts the process anew, treating each eye ray separately. However, this separation offers other advantages, such as the ability to shoot more rays as needed to perform anti-aliasing and improve image quality where needed.

This recursive process of shooting rays from the eye is sometimes referred to as backwards ray tracing, since the direction traveled is opposite to how photons actually travel. However, there is confusion with this terminology, and it is normally avoided; early ray tracing was always done from the eye, and early researchers such as James Arvo used the term backwards ray tracing to mean considering shooting rays from the lights and gathering the results. As such, it is clearer to refer to eye-based versus light-based ray tracing. Research over the past decades has explored combinations of computations done using both of these directions, as well as schemes to generate more or fewer rays in different directions from an intersected surface. For example, radiosity algorithms often work by computing how photons from lights affect surfaces and storing these results. This data can then be used by a standard recursive ray tracer to create a more realistic and physically correct image of a scene. In the context of global illumination algorithms, such as photon mapping and Metropolis light transport, ray tracing is simply one of the tools used to compute light transfer between surfaces.

### Algorithm: classical recursive ray tracing

```For each pixel in image {
Create ray from eyepoint passing through this pixel
Initialize NearestT to INFINITY and NearestObject to NULL

For every object in scene {
If ray intersects this object {
If t of intersection is less than NearestT {
Set NearestT to t of the intersection
Set NearestObject to this object
}
}
}

If NearestObject is NULL {
Fill this pixel with background color
} Else {
Shoot a ray to each light source to check if in shadow
If surface is reflective, generate reflection ray: recurse
If transparent, generate refraction ray: recurse
Use NearestObject and NearestT to compute shading function
Fill this pixel with color result of shading function
}
}
```

## In optical design

Ray tracing in computer graphics derives its name and principles from a much older technique used for lens design since the 1900s. Geometric ray tracing is used to describe the propagation of light rays through a lens system or optical instrument, allowing the properties of the system to be modelled. This is used to optimise the design of the instrument (e.g. to minimise effects such as chromatic aberration) before it is built. Ray tracing can be used in any application to which geometrical optics can be applied: that is, systems in which diffraction is not important. It is not only used for designing lenses, as for photography, but can also be used for longer wavelength applications such as designing microwave or even radio systems, and for shorter wavelengths, such as ultraviolet and X-ray optics.

The principles of ray tracing for computer graphics and optical design are similar, but the technique in optical design usually uses much more rigorous and physically correct models of how light behaves. In particular, optical effects such as dispersion, diffraction and the behaviour of optical coatings are important in lens design, but are less so in computer graphics.

Before the advent of the computer, ray tracing calculations were performed by hand. The optical formulas of many classic photographic lenses were optimized by rooms full of people, each of whom handled a small part of the large calculation. Now they are worked out in optical design software such as Zemax. A simple version of ray tracing known as ray transfer matrix analysis is often used in the design of optical resonators used in lasers.

## Books

• Glassner, Andrew (Ed.) (1989). An Introduction to Ray Tracing. Academic Press. ISBN 0-12-286160-4.
• Shirley,Peter and RMorley Keith ,R. (2001) Realistic Ray Tracing,2nd edition. A.K. Peters. ISBN 1-56881-198-5.
• Henrick Wann Jensen. (2001) Realistic image synthesis using photon mapping. A.K. Peters. ISBN 1-56881-147-0.
• Pharr, Matt and Humphreys, Greg (2004). Physically Based Rendering : From Theory to Implementation. Morgan Kaufmann. ISBN 0-12-553180-X.