
From Wikipedia: "Ray tracing is a technique for generating an
image by tracing the path of
light through
pixels in an
image plane. The technique is capable of producing a very high degree of
photorealism".
Basically, it means that we want to render images by imitating how we perceive the physical world as its image is recreated in our mind using the light rays that hit objects in space and are then returned to our eyes. We do this by modeling the world in a virtual 3d space. We place a virtual "eye" which is looking at a given direction and has a viewing plane, which is the portion of the world which the eye sees. Then, project a "ray of light" from the eye and through each pixel on the viewing plane and follow the ray along its path in space.
We test to see if the ray intersects any objects along that path. If it does, we calculate the color of the object at the point of intersection, taking into account visible light sources, the object's properties such as color, material, etc. This process is actually known as "Ray Casting". To make it "Ray Tracing" we continue tracing the ray after the intersection to see if the reflected ray hits any objects, and so on.
One of the assignments in the Computer Graphics
course I took last semester was to write a simple ray tracer in Java. Written by myself and my more than capable partner, Gur Dotan, the tracer was implemented from scratch, and includes the following features:
- Primitive objects: Sphere, Box, Disc, Cylinder, Rectangle and a Torus.
- Light sources: Directional, Point and Light Area.
- Phong lighting model: Diffuse, Specular, Ambient and Emission parameters for all objects.
- Textures: PNG files, or a Checkers pattern with variable size / colors.
- Recursive ray tracing: Trace rays reflection recursively upon intersection.
- Supersampling: Using multiple rays per pixel for anti-aliasing
You can download the application
here. Simply run the JAR file to start things up. It is completely self contained and has no 3rd party dependencies. The right window is where you enter the model of the world, and you can download some reference scenes
here. Then, simply click Render.
The source code is actually pretty straightforward, can be found
here, in the form of an
Eclipse project. A short document outlining things is contained in the zip file.
Please note that the code is provided mostly for educational purposes and it is released as-is, without warranty of any kind. That being said, feel free to make any use of it. It is free both as in "free beer" and as in "free speech".
If you're looking for a commercial grade ray tracer, I recommend
POV-Ray, an excellent open source ray tracer written in C++.
Click to view in my Flickr
Have fun,
Barak