Presentation is loading. Please wait.

Presentation is loading. Please wait.

Presentation by Dr. David Cline Oklahoma State University

Similar presentations


Presentation on theme: "Presentation by Dr. David Cline Oklahoma State University"— Presentation transcript:

1 Presentation by Dr. David Cline Oklahoma State University
smallpt: Global Illumination in 99 lines of C++ a ray tracer by Kevin Beason Presentation by Dr. David Cline Oklahoma State University

2 Global Illumination Global Illumination = “virtual photography”
Given a scene description that specifies the location of surfaces in a scene, the location of lights, and the location of a camera, take a virtual “photograph” of that scene.

3 “Headlight” rendering of a simple scene

4 Adding surface details

5 Direct lighting with hard shadows

6 “Ambient occlusion” = direct lighting of a cloudy day.

7 Ambient Occlusion and depth of field

8 Global illumination showing different surface types, glass surfaces, caustics (light concentrations), and depth of field.

9 Another Example Ad-hoc Lighting vs. Global Illumination

10 How to form a GI image?

11 The Rendering Equation

12 The Rendering Equation
The radiance (intensity of light) Coming from surface point P In direction Dv. This is what we Have to calculate.

13 The Rendering Equation
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 Equation
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 Approximation
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.

16 Path Tracing Algorithm

17 SmallPT A 99 line Path Tracer by Kevin Beason
(Expanded Version has 218 lines) Major Parts: Vec: a vector class, used for points, normals, colors Ray: a ray class (origin and direction) Refl_t: the surface reflection type Sphere: SmallPT only supports sphere objects spheres: the hard coded scene (some # of spheres) intersect: a routine to intersect rays with the scene of spheres radiance: recursive routine that solves the rendering equation main: program start and main loop that goes over each pixel

18 Squashed Code 1:

19 Squashed Code 2:

20 Expanded version (1) Preliminaries

21 Expanded version (2) Vec (Points, Vectors, Colors)

22 Normalize “Normalize” a vector = divide by its length

23 Dot Product

24 Cross Product

25 Ray Structure A ray is a parametric line with an origin (o) and a direction (d). A point along the ray can be defined using a parameter, t: In code we have: The core routines of the ray tracer intersect rays with geometric objects (spheres in our case)

26 Sphere SmallPT supports sphere objects only
We can define a sphere based on a center point, C Radius, r The equation of the sphere: In vector form:

27 Start with vector equation of sphere
Sphere Intersection

28 Intersection Routine

29 Full Sphere Code

30 The Scene

31 The Scene Description

32 Convert Colors to Displayable Range
The output of the “radiance” function is a set of unbounded colors. This has to be converted to be between 0 and 255 for display purposes. The following functions do this. The “toInt” function applies a gamma correction of 2.2.

33 Intersect Ray with Scene
Check each sphere, one at a time. Keep the closest intersection.

34 End Part 1

35 The main Function Set up camera coordinates Initialize image array
Parallel directive For each pixel Do 2x2 subpixels Average a number of radiance samples Set value in image Write out image file

36 main (1)

37 main (1a: set up image)

38 main (1b: set up camera)

39 Camera Setup Look from and gaze direction:
Horizontal (x) camera direction (assumes upright camera) ( defines field of view angle) Vertical (vup) vector of the camera (cross product gets vector perpendicular to both cx and gaze direction)

40 Camera Setup cy gaze direction cx look from

41 main (2: Create Image)

42 main (2a: OpenMP directive)
States that each loop iteration should be run in its own thread.

43 main (2b: Loop over image pixels)
Loop over all pixels in the image.

44 main (2c: Subpixels & samples)
Pixels composed of 2x2 subpixels. The subpixel colors will be averaged.

45 main (2d: Pixel Index) Calculate array index for pixel(x,y)

46 main (2e: Tent Filter) r1 and r2 are random values of a tent filter
(Determine location of sample within pixel)

47 Tent Filter From Realistic Ray Tracing (Shirley and Morley)

48 Tent Filter From Realistic Ray Tracing (Shirley and Morley)

49 main (2f: Ray direction & radiance)
Compute ray direction using cam.d, cx, cy Use radiance function to estimate radiance

50 main (2g: Add subpixel estimate)
Add the gamma-corrected subpixel color estimate to the Pixel color c[i]

51 main (3: Write PPM image)
PPM Format:

52 radiance (1: do intersection)
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 properties)
Surface properties include: intersection point (x) Normal (n) Oriented normal (n1) Object color (f)

54 Orienting Normal When a ray hits a glass surface, the ray tracer must determine if it is entering or exiting glass to compute the refraction ray. The dot product of the normal and ray direction tells this:

55 Russian Roulette Stop the recursion randomly based on the surface reflectivity. Use the maximum component (r,g,b) of the surface color. Don’t do Russian Roulette until after depth 5

56 Diffuse Reflection For diffuse (not shiny) reflection
Sample all lights (non-recursive) Send out additional random sample (recursive)

57 Diffuse Reflection Construct random ray: Get random angle (r1)
Get random distance from center (r2s) Use normal to create orthonormal coordinate frame (w,u,v)

58 Sampling Unit Disk From Realistic Ray Tracing (Shirley and Morley)

59 Sampling Unit Hemisphere
w=z u=x v=y

60 Sampling Lights

61 Sampling Sphere by Solid Angle
Create coordinate system for sampling: sw, su, sv

62 Sampling Sphere by Solid Angle
Determine max angle amax

63 Sampling Sphere by Solid Angle
Calculate sample direction based on random numbers according to equation from Realistic Ray Tracing:

64 Shadow Ray 145: Check for occlusion with shadow ray
146: Compute 1/probability with respect to solid angle 147: Calculate lighting and add to current value

65 Diffuse Recursive Call
Make recursive call with random ray direction computed earlier: Note that the 0 parameter at the end turns off the emissive term at the next recursion level.

66 Ideal Specular (Mirror) Reflection

67 Ideal Specular (Mirror) Reflection
Reflected Ray: Angle of incidence = Angle of reflection

68 Glass (Dielectric)

69 Reflected Ray & Orientation
159: Glass is both reflective and refractive, so we compute the reflected ray here. 160: Determine if ray is entering or exiting glass 161: IOR for glass is 1.5. nnt is either 1.5 or 1/1.5

70 Total Internal Reflection
Total internal reflection occurs when the light ray attempts to leave glass at too shallow an angle. If the angle is too shallow, all the light is reflected.

71 Reflect or Refract using Fresnel Term
Compute the refracted ray

72 Refraction Ray

73 Refractive Index Refractive index gives the speed of light within a medium compared to the speed of light within a vacuum: Water: 1.33 Plastic: 1.5 Glass: 1.5 – 1.7 Diamond: 2.5 Note that this does not account for dispersion (prisms). To account for these, vary index by wavelength.

74 Fresnel Reflectance Percentage of light is reflected (and what refracted) from a glass surface based on incident angle (ϴa) Reflectance at “normal incidence”, where (n=na/nb) Reflectance at other angles:

75 Reflect or Refract using Fresnel Term
Fresnel Reflectance R0 = reflectance at normal incidence based on IOR c = 1-cos(theta) Re = fresnel reflectance

76 Reflect or Refract using Fresnel Term
P = probability of reflecting Finally, make 1 or 2 recursive calls Make 2 if depth is <= 2 Make 1 randomly if depth > 2

77 Convergence From:


Download ppt "Presentation by Dr. David Cline Oklahoma State University"

Similar presentations


Ads by Google