Photon Mapping

Using backwards ray tracing, tracing from light sources as opposed to through the viewing plane by shooting special photon rays from light sources, this program combines with a simple ray tracer to provide a model for global illumination, particularly on the standard Cornell box.

Photon Mapping:

Backwards Ray Tracing

Program Usage:

Compile project file in visual studio 2005 (preferably)

Go to the debug folder in the ray racer directory, not raytracer/raytracer/debug
at command prompt: …\raytracer.exe model.dat output.ppm
the models I used to test are the \raytracer\debug folder namely: model11.dat and model7.dat
The number of photons are defined at the top of raytracer.h

Program Modules:

The program consists of several modules: the file parser, the raytracer class, and the photon map class and photon data structure

The file parser is the one used from the cs480 ray tracing assignment. Model’s adhere to the guidelines setup in

The raytracer that I am using works with quadrilateral models , but not csg models and it has trouble with spheres of certain sized radii.

The raytracer class was adapted to handle the photon mapping involved by adding 2 functions: emitPhotons and TracePhotons which both referenced a photon map which was added to the private data members of the ray tracer class. The ray tracer supports diffuse and reflective surfaces, specular results are not supported.

The photon and photon map data structures follow the format as those as laid down by

Jensen, Realistic Image Synthesis Using Photon Mapping ,.

The photons are defined as:


typedef struct Photon {


float pos[3]; // photon position

short plane; // splitting plane for kd-tree

unsigned char theta, phi; // incoming direction

float power[3]; // photon power (uncompressed)

} Photon;

The photon map holds all of the photons that are absorbed and places them into a kd-tree.

Also the light source in the scene is considered as a diffuse point light at the top of the cornel box. One could try to define a box light using a series of diffuse point lights but being how long it took to render a scene with one light , not to mention that the way the program is currently setup it would emit the number of photons per light source, so if you’re emitting 500,000 photons from each light it would be very costly in terms of computation, not to mention all the vector reflections random number generation that would need to occur. Hence the program right now presumes there is only one light source, which seemed to provide adequate lighting.


  1. From each light source cast photons into the scene following the suggested Monte carlo sampling for emitted Photons, and see whether they intersect with a part of the scene.
  2. If the emitted photon does intersect with some object, store the intersection point, and the material properties(stuff like coefficients of specular, diffuse, etc) at that point and proceed with .the photon scattering process.
  3. Photon scattering works using a Russian roulette system by which you generate a random number [0.1] = probability.
    1. If the probability < the probability of diffuse reflection,
    2. Reflect the photon and scale the power by the diffuse coefficient and call the photonTrace function on the reflected photon
    3. Or if (probability >= prob_diff) && (probability <prob_diff+prob_spec)
    4. Repeat step b but with the power scaled by the specular coefficient
    5. Else store the photon in the photon map.
  4. Balance the kd-tree in the photon map.
  5. Perform ray tracing as per standard procedures
  6. When casting a ray through a pixel into the scene, find the point where it intersects
    1. At that point calculate its’ irradiance estimate based upon the balanced kd-tree. Calling the function irradiance estimate with inputs for the surface position, the normal at the surface, the maximum distance to look in the kd tree to look for photons, and the number of photons to use.
    2. Add in the irradiance values to the ray tracing pixel values.
  7. Done

Troubleshooting / Conclusions:

For the model data I considered the cornel box scene in two configurations. The default configuration in which the boxes have diffuse surfaces with material properties defined as

mat ID 3 1 1 1 0.5 0.6 0 10 0 0 0 0

And a second scene in which the boxes had surfaces with a reflective and diffuse coefficient.

mat ID 4 0.5 0.5 1 0.3 0.5 0 50 0 0.3 0

I rendered the scenes using in my irradiance estimate a search radius of 15, a density estimate of 500, and varying the number of photons as emitted using levels of 50,000 , 250,000, and 500, 000 respectively. I found that 50,000 photons was not enough to properly light the scene. Also, the image quality did not increase when the emitted photons were increased from 250,000 to 500, 000 by any noticeable degree. I did try one render at 1,000,000 photons but ended up getting lots of noise.

Updated (4/11/06): The noise problem has been corrected, I had forgotten to set a cap on the RGB values at one point in my code. No more oversaturation now.

Also, though the procedure appears to be working over all I suspect that the reason for the lack of accuracy in my renders is either my choices involving my search radius and density estimate or that perhaps I forgot to normalize a vector somewhere.

Click Pictures for Full Resolution Versions

Standard Cornel Box with just Ray Tracing Ray Tracing and no shadows
Standard Cornel Box with just Ray Tracing  Ray Tracing and no shadows
50,000 250,000 Photons with no shadows
50,000 250,000 Photons with no shadows
250,000 Photons 500,000 Photons
250,000 Photons 500,000 Photons
1 million photons!!! Same scene but boxes that have ambient and reflective coeffcients 50,000 Photons
1 million photons!!! Same scene but boxes that have ambient and reflective coeffcients 50,000 Photons
250,000 Photons 500,000 Photons
250,000 Photons 500,000 Photons

Here are some new Renders Updated as of 4/11/2006
corrections of the room’s ambient and diffuse properties as well as varying the number of photons and light positions with code that corrects for RGB oversaturation as shown in the previous 1 million photon renders

1 million photons!!!
1 million photons!!!

Source Code:



Major thanks to Jonathan Fung for letting me use his ray tracer code from his cs480 project. Also for other references to the nature of photons / coding ideas see the following sites:

Realistic Image Synthesis Using Photon Mapping

Photon Mapping – Zack Waters

Robust Monte Carlo Methods for Light Transport Simulation

The Cornell Box

Photon Maps


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: