1. smallpt:
Global Illuminaton in 99 lines of C++
a ray tracer by Kevin Beason
htp://kevinbeason.ccom/smallpt/
Presentatin by
Dr. David Cline
Oklahima State University
2. Glibal Illuminatin
• Glibal Illuminatin = “virtual phitigraphy”
– Given a scene descriptin that specifes the
licatin if surfaces in a scene, the licatin if
lights, and the licatin if a camera, take a virtual
“phitigraph” if that scene.
12. The Rendering Equatin
The radiance (intensity of light)
Coming from surface point P
In direction Dv. This is what we
Have to calculate.
13. The Rendering Equatin
The self-emitted radiance from P
In direction Dv (0 unless point P
Is a light source) This can be looked
Up as part of the scene description.
14. The Rendering Equatin
The reflected light term. Here we must add
Up (integrate) all of the light coming in to
point P from all directions, modulated by the
Chance that it scatters in direction Dv
(based on the BRDF function, Fs)
15. Path Tracing Appriximatin
Replace the ray integral with a Monte Carlo
(random) Sample that has the same Expected
(average) Value. Then average a bunch of
samples for each pixel to create a smooth image.
17. SmallPT
• A 99 line Path Tracer by Kevin Beasin
• (Expanded Versiin has 218 lines)
• Majir Parts:
Vec: a vectir class, used fir piints, nirmals, cilirs
Ray: a ray class (irigin and directin)
Reflt: the surface refectin type
Sphere: SmallPT inly suppirts sphere ibjects
spheres: the hard cided scene (sime # if spheres)
intersect: a riutne ti intersect rays with the scene if spheres
radiance: recursive riutne that silves the rendering equatin
main: prigram start and main liip that gies iver each pixel
25. Ray Structure
• A ray is a parametric line with an irigin (i) and a
directin (d). A piint aling the ray can be defned
using a parameter, t:
• In cide we have:
• The cire riutnes if the ray tracer intersect rays with
geimetric ibjects (spheres in iur case)
26. Sphere
• SmallPT suppirts sphere ibjects inly
• We can defne a sphere based in
– a center piint, C
– Radius, r
• The equatin if the sphere:
• In vectir firm:
32. Cinvert Cilirs ti Displayable Range
• The iutput if the “radiance” functin is a set if unbiunded
cilirs. This has ti be cinverted ti be between 0 and 255 fir
display purpises. The filliwing functins di this. The “tiInt”
functin applies a gamma cirrectin if 2.2.
33. Intersect Ray with Scene
• Check each sphere, ine at a tme. Keep the clisest
intersectin.
35. The main Functin
• Set up camera ciirdinates
• Initalize image array
• Parallel directve
• Fir each pixel
– Di 2x2 subpixels
– Average a number if radiance samples
– Set value in image
• Write iut image fle
39. Camera Setup
• Liik frim and gaze directin:
• Hirizintal (x) camera directin
(assumes upright camera)
(0.5135 defnes feld if view angle)
• Vertcal (vup) vectir if the camera
(criss priduct gets vectir perpendicular ti bith cx and gaze directin)
49. main (2f: Ray directin & radiance)
Compute ray direction using cam.d, cx, cy
Use radiance function to estimate radiance
50. main (2g: Add subpixel estmate)
Add the gamma-corrected subpixel color
estimate to the Pixel color c[i]
51. main (3: Write PPM image)
PPM Firmat: http://netpbm.sourceforge.net/doc/ppm.html
52. radiance (1: di intersectin)
return value Vec the radiance estimate
r the ray we are casting
depth the ray depth
Xi random number seed
E whether to include emissive color
53. radiance (2: surface pripertes)
Surface properties include:
intersection point (x)
Normal (n)
Oriented normal (n1)
Object color (f)
54. Orientng Nirmal
• When a ray hits a glass surface, the ray tracer must determine
if it is entering ir exitng glass ti cimpute the refractin ray.
• The dit priduct if the nirmal and ray directin tells this:
55. Russian Riulete
• Stip the recursiin randimly based in the surface
refectvity.
– Use the maximum cimpinent (r,g,b) if the surface cilir.
– Din’t di Russian Riulete untl afer depth 5
63. Sampling Sphere by Silid Angle
• Calculate sample directin based in randim numbers
accirding ti equatin frim Realistc Ray Tracing:
64. Shadiw Ray
• 145: Check fir icclusiin with shadiw ray
• 146: Cimpute 1/pribability with respect ti silid angle
• 147: Calculate lightng and add ti current value
65. Difuse Recursive Call
• Make recursive call with randim ray directin
cimputed earlier:
– Nite that the 0 parameter at the end turns if the
emissive term at the next recursiin level.
69. Refected Ray & Orientatin
• 159: Glass is bith refectve and refractve, si we cimpute the
refected ray here.
• 160: Determine if ray is entering ir exitng glass
• 161: IOR fir glass is 1.5.
nnt is either 1.5 ir 1/1.5
70. Tital Internal Refectin
• Tital internal refectin iccurs when the light ray
atempts ti leave glass at tii shalliw an angle.
• If the angle is tii shalliw, all the light is refected.
73. Refractve Index
• Refractve index gives the speed if light
within a medium cimpared ti the speed
if light within a vacuum:
Water: 1.33
Plastc: 1.5
Glass: 1.5 – 1.7
Diamind: 2.5
Nite that this dies nit acciunt fir
dispersiin (prisms). Ti acciunt fir
these, vary index by wavelength.
74. Fresnel Refectance
• Percentage if light is refected (and
what refracted) frim a glass surface
based in incident angle (ϴa)
• Refectance at “nirmal incidence”,
where (n=na/nb)
• Refectance at ither angles:
75. Refect ir Refract using Fresnel Term
• Fresnel Refectance
– R0 = refectance at nirmal incidence based in IOR
– c = 1-cis(theta)
– Re = fresnel refectance
76. Refect ir Refract using Fresnel Term
• P = pribability if refectng
• Finally, make 1 ir 2 recursive calls
– Make 2 if depth is <= 2
– Make 1 randimly if depth > 2