Ray Tracing in C++

Written by Luis Cruz on Sunday, 21 August 2011. Posted in Academic Portfolio

Computer Graphics | CS3451

Ray Tracing in C++

This is a simple implementation of a Ray Tracing algorithm in C++. It only renders geometric figues suchas triangles, spheres and cubes with a single white light source point and shadows. The program has as an input a text file with a descriptio of the scene.

Output Renders

Here you can see 6 different ray tracing renderings from this implementation:

cs3451-raytracing-renders

Input File Format

The input file (assume its name is 'input.txt' and it's placed in the current directory) will specify the view,

window size, light source and the primitives (triangles and spheres), in the following order:

1. The desired resolution of the output image (two integers, m and n if the output is to be m × n)
2. The coordinates of the viewpoint e (three floats)
3. The screen data: l (lower left corner), ~h and ~v (vectors running along the horizontal and vertical edges)
4. The light source (b, the coordinates and I, the intensity)
5. The ambient light intensity
6. Number of primitives
7. List of primitives (spheres and triangles)

The list of primitives will consist of a number of primitive records. A primitive record will start with a letter, S for a sphere and T for a triangle. For a sphere, we shall then list the coordinates of the center and the radius (4 floats) in one line and the material description (8 floats): kdr, kdg, kdb, kar, kag, kab, ks and nspec. The first three are the diffuse reflection coefficients for red green and blue components, then come the ambient reflection coefficients and the specular coefficient and exponent. Note that we won't have separate RGB components for the specular coefficient: we'll just use the same value for all of them (i.e. set ksr = ksg = ksb = ks). A triangle record will start with a letter S and contain a list of the coordinates of each of the three vertices (9 floats) and the material properties (in the same form as for spheres).

The input files will be designed so that the intensity of a pixel should never exceed 1 (except possibly for

a tiny numerical error).

Thus, starting portion of the file will look like this:

m n
ex ey ez
lx ly lz
hx hy hz
vx vy vz
bx by bz I
Ia

N

Then, there will follow the description of the primitives. For a triangle, we will have:

T
a1x a1y a1z
a2x a2y a2z
a3x a3y a3z
kdr kdg kdb kar kag kab ks nspec

And for a sphere:
S
cx cy cz r
kdr kdg kdb kar kag kab ks nspec

Social Bookmarks

About the Author

Luis Cruz

I am pursuing my MS in Computer Science at Georgia Institute of Technology (GaTech) with emphasis in Computer Graphics. I received by BS in Computer Science at GaTech in 2009 with specialization in Software Engineering and Computer Graphics.

Leave a comment

You are commenting as guest.