Ray tracing


Python implementation of ray tracing


Introduction

Ray tracing is a rendering technique for generating a 2D image of a 3D scene by tracing the path of light as pixels in an image plane and simulating the effects of its encounters with virtual objects in the scene. The technique is capable of producing a very high degree of visual realism. Ray tracing is capable of simulating a wide variety of optical effects, such as reflection and refraction, scattering, and dispersion phenomena such as chromatic aberration.


Code

The python script is capable of rendering different objects, such as spheres, triangles, planes and checkerboards. A scene is simply a collection of objects with some properties like position, color and reflection. There are two possible lighting objects, directional and point light. The input to the script is a text file with specified lights and objects.
For each pixel in the virtual camera, the program trace the ray to the scene and if it intersect an object, its reflection is computed. The output of the algorithm is a color value for each pixel in the image. This is an example of scene produced by the script.

Spheres on checkerboard
Spheres on checkerboard

Python code