Home Reference VEX VEX contexts 

photon vex context

Write a program to calculate photon maps.

When mantra is generating photon maps, photon shaders are used instead of surface shaders. Thus, the photon context is very similar to the surface context. However, because of the behavioral differences between the contexts, most of the surface context specific functions are not valid in the photon context.

When mantra is rendering a photon map, photons are spawned from light sources. Each photon represents a “bit” of energy that has left the light and will interact with the scene. When a surface is hit by a photon, the photon shader is invoked.

When a photon hits a surface, a fraction of the photon’s energy will be absorbed by the surface, and another portion of the energy will be reflected into the scene. For example, a mirror surface would absorb very little of the energy of the photon, but reflect a large amount of the energy back into the scene (creating a caustic effect), while a diffuse surface would be more likely to absorb energy, but might still distribute a fraction of the energy (creating color bleeding).

However, to create “good” photon maps, all of the photons stored in the map should have roughly the same energy levels (otherwise filtering will be biased). Thus, instead of storing a fraction of the energy and distributing the remainder, the typical method distributing photons is to use a monte-carlo technique called “Russian Roulette”. Rather than dealing with fractions of the energy, it’s better to deal with fractions of photons. For example, instead of storing a photon with 30% of the incoming energy, instead, only store 30% of the photons which hit the surface (and reflect the other 70%).

When a photon strikes a surface, there are four standard light paths for spawning new photons.

  1. The photon will be absorbed by the surface (terminating the light path).

  2. The photon will be reflected as diffuse light (diffuse light will scatter over the hemisphere above the surface)

  3. The photon will be reflected specularly (as in a mirror reflection)

  4. The photon will be refracted

When a photon gets stored, it may be stored in one or more maps. Mantra keeps track of three individual maps:

  1. Direct Photon Map: If a photon is stored and has not undergone any bounces (i.e. getraybounce() == 0), the photon will be stored in the direct illumination map (if defined). Note that light sources may specify that their photons may not contribute to direct photon illumination.

  2. Caustic Photon Map: If a photon is stored and has undergone either specular reflection or transmission, it will be stored in the caustic photon map.

  3. Global Photon Map: Any photon which gets stored is added to the global photon map. Thus, the global photon map will contain all photons in the direct map, all caustic photons as well as any diffuse reflected photons. Note that light sources may specify that their photons may not contribute to direct photon illumination.

In some ways, the structure of the photon shader is dependent on how the photon map will be accessed. Typically, the photon map will store incoming illumination. That is, photons directly from the light sources, or indirectly from other surfaces. Before a photon is reflected from the photon shader, the “color” of the surface would be used to modulate the photon color. That is, if the surface were red, the photon color would be tinted red.

However, it’s also possible to pre-compute the combined illumination.

Globals

Time (read-only)

The time at which the shading occurs, as a number from 0 (shutter open) to 1 (shutter close).

dPdz (read-only)

Floating point step size for volume rendering.

P (read-only)

Position of the point on the surface being shaded. In light or shadow shaders, the P variable contains the point on the light source.

Lz (read-only)

Z-axis in the space of the light. This is a unit vector.

Ng (read-only)

Geometric normal for the surface. This normal represents the “true” normal of the surface being shaded. For example, with Phong shading, the N variable represents the interpolated normal, while the Ng variable represents the true polygon normal.

L (read-only)

Vector from the point on the surface to the light source. The length of this vector represents the distance to the light source.

dPds (read-only)

Change in position with respect to the parametric s.

Pz (read-only)

Z component of the point being shaded.

I (read-only)

Direction from the eye to the point being shaded. This may or may not be a normalized vector.

Eye (read-only)

Position of the eye.

dPdt (read-only)

Change in position with respect to the parametric t.

t (read-only)

Parametric t (sometimes called v) coordinate of the surface being shaded.

N (read-only)

Shading normal for the surface.

Cl (read-only)

Light color.

s (read-only)

Parametric s (sometimes called u) coordinate of the surface being shaded.