Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

smallpt: Global Illumination in 99 lines of C++

458 Aufrufe

Veröffentlicht am

a ray tracer by Kevin Beason
http://kevinbeason.com/smallpt/

Veröffentlicht in: Bildung
  • Als Erste(r) kommentieren

  • Gehören Sie zu den Ersten, denen das gefällt!

smallpt: Global Illumination in 99 lines of C++

  1. 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. 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.
  3. 3. • “Headlight” rendering if a simple scene
  4. 4. • Adding surface details
  5. 5. • Direct lightng with hard shadiws
  6. 6. • “Ambient icclusiin” = direct lightng if a cliudy day.
  7. 7. • Ambient Occlusiin and depth if feld
  8. 8. • Glibal illuminatin shiwing diferent surface types, glass surfaces, caustcs (light cincentratins), and depth if feld.
  9. 9. Anither Example • Ad-hic Lightng vs. Glibal Illuminatin
  10. 10. Hiw ti firm a GI image?
  11. 11. The Rendering Equatin
  12. 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. 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. 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. 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.
  16. 16. Path Tracing Algirithm
  17. 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
  18. 18. Squashed Cide 1:
  19. 19. Squashed Cide 2:
  20. 20. Expanded versiin (1) Preliminaries
  21. 21. Expanded versiin (2) Vec (Piints, Vectirs, Cilirs)
  22. 22. Nirmalize • “Nirmalize” a vectir = divide by its length
  23. 23. Dit Priduct
  24. 24. Criss Priduct
  25. 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. 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:
  27. 27. Sphere Intersectin Start with vector equation of sphere
  28. 28. Intersectin Riutne
  29. 29. Full Sphere Cide
  30. 30. The Scene
  31. 31. The Scene Descriptin
  32. 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. 33. Intersect Ray with Scene • Check each sphere, ine at a tme. Keep the clisest intersectin.
  34. 34. End Part 1
  35. 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
  36. 36. main (1)
  37. 37. main (1a: set up image)
  38. 38. main (1b: set up camera)
  39. 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)
  40. 40. Camera Setup look from cx cy gaze direction
  41. 41. main (2: Create Image)
  42. 42. main (2a: OpenMP directve) States that each loop iteration should be run in its own thread.
  43. 43. main (2b: Liip iver image pixels) Loop over all pixels in the image.
  44. 44. main (2c: Subpixels & samples) Pixels composed of 2x2 subpixels. The subpixel colors will be averaged.
  45. 45. main (2d: Pixel Index) Calculate array index for pixel(x,y)
  46. 46. main (2e: Tent Filter) r1 and r2 are random values of a tent filter (Determine location of sample within pixel)
  47. 47. Tent Filter • Frim Realistc Ray Tracing (Shirley and Mirley)
  48. 48. Tent Filter • Frim Realistc Ray Tracing (Shirley and Mirley)
  49. 49. main (2f: Ray directin & radiance) Compute ray direction using cam.d, cx, cy Use radiance function to estimate radiance
  50. 50. main (2g: Add subpixel estmate) Add the gamma-corrected subpixel color estimate to the Pixel color c[i]
  51. 51. main (3: Write PPM image) PPM Firmat: http://netpbm.sourceforge.net/doc/ppm.html
  52. 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. 53. radiance (2: surface pripertes) Surface properties include: intersection point (x) Normal (n) Oriented normal (n1) Object color (f)
  54. 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. 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
  56. 56. Difuse Refectin • Fir difuse (nit shiny) refectin – Sample all lights (nin-recursive) – Send iut additinal randim sample (recursive)
  57. 57. Difuse Refectin • Cinstruct randim ray: – Get randim angle (r1) – Get randim distance frim center (r2s) – Use nirmal ti create irthinirmal ciirdinate frame (w,u,v)
  58. 58. Sampling Unit Disk • Frim Realistc Ray Tracing (Shirley and Mirley)
  59. 59. Sampling Unit Hemisphere w=z u=x v=y
  60. 60. Sampling Lights
  61. 61. Sampling Sphere by Silid Angle • Create ciirdinate system fir sampling: sw, su, sv
  62. 62. Sampling Sphere by Silid Angle • Determine max angle amax
  63. 63. Sampling Sphere by Silid Angle • Calculate sample directin based in randim numbers accirding ti equatin frim Realistc Ray Tracing:
  64. 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. 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.
  66. 66. Ideal Specular (Mirrir) Refectin
  67. 67. Ideal Specular (Mirrir) Refectin • Refected Ray: – Angle if incidence = Angle if refectin
  68. 68. Glass (Dielectric)
  69. 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. 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.
  71. 71. Refect ir Refract using Fresnel Term • Cimpute the refracted ray
  72. 72. Refractin Ray
  73. 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. 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. 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. 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
  77. 77. Cinvergence From: http://kevinbeason.com/smallpt/

×