SlideShare a Scribd company logo
1 of 28
Ray Tracing




              1
What is ray tracing
• Ray tracing is a technique for rendering three-
  dimensional graphics with very complex light
  interactions. This means you can create pictures
  full of mirrors, transparent surfaces, and shadows,
  with stunning results.
• A very simple method to both understand and
  implement.
• It is based on the idea that you can model
  reflection and refraction by recursively following
  the path that light takes as it bounces through an
  environment

                                                        2
Raytraced Images




                   PCKTWTCH by K
                   evin Odhner, POV
                   -Ray
                                3
Kettle, Mike
Miller, POV-
Ray

               4
5
Ray Tracing Model




Source: wikipedia
                                        6
Ray tracing

       Shadow rays




                          Reflection ray




               refracted ray



                                           7
Ray tracing algorithm
• Builds the image pixel by pixel
• Cast additional rays from the hit point to
  determine the pixel color
  – Shoot rays toward each light. If they hit
    something, the object is shadowed from that light,
    otherwise use “standard model” for the light
  – Reflection rays for mirror surfaces, to see what
    should be reflected in the mirror
  – Refraction rays to see what can be seen through
    transparent objects
  – Sum all the contributions to get the pixel color

                                                     8
Recursive ray tracing
• When a reflected or refraction ray hits a
  surface, repeat the whole process from that
  point
  – Send more out shadow rays
  – Send out new reflected rays (if required)
  – Send out a new refracted ray (if required)
  – Generally, reduce the weight of each additional
    ray when computing the contributions to the
    surface color
  – Stop when the contribution from a ray is too small
    to notice or maximum recursion level has been
    reached

                                                     9
Ray tracing implementation
• Ray tracing breakdown into two tasks
  – Constructing the ray to cast
  – Intersection rays with geometry
• The former problem is simple vector
  arithmetic
• Intersection calculation can be done in
  world coordinates or model coordinates


                                            10
Constructing Rays
• Define rays by an initial point and a direction: x(t)=x0+td
• Eye rays: Rays from the eye through a pixel
   – Construct using the eye location and the pixel’s location on the
      image plane. X0 = eye
• Shadow rays: Rays from a point on a surface to the light.
   – X0 = point on surface
• Reflection rays: Rays from a point on a surface in the reflection
  direction
   – Construct using laws of reflection. X0 = surface point
• Transmitted rays: Rays from a point on a transparent surface
  through the surface
   – Construct using laws of refraction. X0 = surface point



                                                                        11
Ray-Object Intersections
• Aim: Find the parameter value, ti , at which the ray
  first meets object i
• Write the surface of the object implicitly: f(x)=0
   – Unit sphere at the origin is x•x-1=0
   – Plane with normal n passing through origin is: n•x=0
• Put the ray equation in for x
   – Result is an equation of the form f(t)=0 where we want t
   – Now it’s just root finding




                                                                12
Ray-Sphere Intersection
             Ray : x (t ) x0 td
          Sphere : x x 1 0
       Substitute: x0 td     x0 td        1 0
                  : d d t2     2 x0 d t     x0 x0 1       0
• Quadratic in t
   – 2 solutions: Ray passes through sphere - take minimum
      value that is > 0
   – 1 solution: Ray is tangent - use it if >0
   – 0 solutions: Ray does not hit sphere
• Numerical stability is very important. For example, can a
  reflection ray hit the same sphere?


                                                              13
Sphere Intersection




A sphere is defined by its center, s, and its radius r. The intersection of a ray
with a sphere can be computed as follows:




                                                                             14
Ray-Plane Intersections
                      Ray :    x (t ) x0 td
                    P lane:    n x 0
                Substit ute:  n x0 td 0
                           :   n d t n x0 0
                                     n x0
                            :t
                                     n d

• This is plane going through the origin
   – What about an arbitrary plane?
• To do polygons, intersect with plane then do point-in-polygon
  test…

                                                                  15
Intersections
• It is necessary to determine the
  intersection between a ray with objects to
  decide which pixel is
• Intersection with a sphere
• Intersection with a plane
• …



                                               16
Ray Tracing Illumination
             Recursive                                                  L                 R
                                                         E
                                                                               ˆ
                                                                               N
                                                                 V                            Vreflected

I ( E, V )     I direct     I reflected      I transmitted   I                                     I reflected
                                                                                      P

                                                                      I transmitted        Vtransmitte d
    I reflected      k r I ( P,Vreflected )

    I transmitted      kt I ( P,Vtransmitted )

    I direct      k a I ambient    I light      ˆ ˆ
                                             kd N L              ks        ˆ ˆ
                                                                          V R
                                                                                          nshiny



     Check for shadowing (intersection with object along ray (P,L))
The Ray Tree
                                     N3         T3
      R                                                                  Viewpoint
               2

                   N2                T1
                                                     R
                        R                                3
                            1
                                                                                       L1
          L2
                                N1               L3
                                           L1                        R                 T1
                                                                          1



                                                                              L2             L3
                                     Eye
Ni surface normal
Ri reflected ray                                             R                     R        T3
                                                                 2                      3

Li shadow ray
Ti transmitted (refracted) ray
Reflection Rays
• The laws of reflection




                             19
Reflection
• Reflection angle = view angle




                                  20
Reflection
• The maximum depth of the tree affects the handling of refraction
• If we send another reflected ray from here, when do we stop? 2
  solutions (complementary)
    – Answer 1: Stop at a fixed depth.
    – Answer 2: Accumulate product of reflection coefficients and stop when
      this product is too small.




                                                                              21
Reflection




             22
Refraction
                                                                                          ˆ
                                                                                          N cos        ˆ ˆ
                                                                                                       I N
                                                                                                   i
                           sin
 Snell’s Law                             t        i
                                                                                                               ˆ
                           sin           i        t
                                                          r
                                                                                            ˆ
                                                                                            I                  N cos    i
                                                                                                           i                ˆ
                                                                                                                            M
ˆ
T    sin          ˆ
                  M              ˆ
                           cos t N
              t

                    ˆ
                  ( N cos                    ˆ
                                             I)
     ˆ
     M                           i                                                                              t
                                                                                                                        ˆ
                       sin                                                                             ˆ
                                                                                                       N               T
                                 i

ˆ    sin      t      ˆ                       ˆ         ˆ
T                  ( N cos           i       I ) cos t N                                    Note that I is the negative of
     sin      i                                                                                 the incoming ray
ˆ
T    (       cos                    ˆ
                            cos t ) N                     ˆ
                                                          I
         r            i                               r

     cos              ˆ ˆ
                      N I
              i

     cos                  1 sin 2                     1       2
                                                                  sin 2           1   2        ˆ ˆ
                                                                                          (1 ( N I ) 2 )
              t                               t               r           i           r


Tˆ            ˆ ˆ
             (N I )          1                2        ˆ ˆ       ˆ
                                                  (1 ( N I ) 2 ) N                ˆ
                                                                                  I
         r                                    r                               r



                                                                                                                                23
Refraction




             24
Shadow Rays
• Shadows are important lighting effect that
  can be easily with ray tracing
• If we wish to compute the illinumination with
  shadows for a point, we shoot an additional
  ray from the point to every light source
• A light is only allowed to contribute to the final
  color if the ray doesn’t hit anything between
  the point and a light source


                                                   25
Pseudo Code for Ray Tracing
rgb lsou;       // intensity of light source
rgb back;       // background intensity
rgb ambi;       // ambient light intensity

Vector L        // vector pointing to light source
Vector N        // surface normal
Object objects [n] //list of n objects in scene
float Ks [n]    // specular reflectivity factor for each object
float Kr [n]    // refractivity index for each object
float Kd [n]    // diffuse reflectivity factor for each object
Ray r;

void raytrace() {
   for (each pixel P of projection viewport in raster order) {
        r = ray emanating from viewer through P
        int depth = 1; // depth of ray tree consisting of multiple paths
        the pixel color at P = intensity(r, depth)
   }
}


                                                                           26
rgb intensity (Ray r, int depth) {
   Ray flec, frac;
   rgb   spec, refr, dull, intensity;

   if (depth >= 5) intensity = back;
   else {
       find the closest intersection of r with all objects in scene
       if (no intersection) {
           intensity =back;
       } else {
           Take closest intersection which is object[j]
           compute normal N at the intersection point
           if (Ks[j] >0)    { // non-zero specular reflectivity
                 compute reflection ray flec;
                 refl = Ks[j]*intensity(flec, depth+1);
           } else refl =0;
           if (Kr[j]>0) {      // non-zero refractivity
                compute refraction ray frac;
                refr = Kr[j]*intensity(frac, depth+1);
           } else refr =0;
           check for shadow;
           if (shadow) direct = Kd[j]*ambi
           else direct = Phong illumination computation;
           intensity = direct + refl +refr;
      } }
    return intensity; }

                                                                      27
Thank for attention!




                       28

More Related Content

What's hot

Image segmentation ppt
Image segmentation pptImage segmentation ppt
Image segmentation ppt
Gichelle Amon
 

What's hot (20)

Illumination model
Illumination modelIllumination model
Illumination model
 
Cyrus beck line clipping algorithm
Cyrus beck line clipping algorithmCyrus beck line clipping algorithm
Cyrus beck line clipping algorithm
 
Region based image segmentation
Region based image segmentationRegion based image segmentation
Region based image segmentation
 
Morphological image processing
Morphological image processingMorphological image processing
Morphological image processing
 
Chapter10 image segmentation
Chapter10 image segmentationChapter10 image segmentation
Chapter10 image segmentation
 
Simultaneous Smoothing and Sharpening of Color Images
Simultaneous Smoothing and Sharpening of Color ImagesSimultaneous Smoothing and Sharpening of Color Images
Simultaneous Smoothing and Sharpening of Color Images
 
Image segmentation ppt
Image segmentation pptImage segmentation ppt
Image segmentation ppt
 
Properties of light
Properties of lightProperties of light
Properties of light
 
Lect 02 second portion
Lect 02  second portionLect 02  second portion
Lect 02 second portion
 
Color Image Processing: Basics
Color Image Processing: BasicsColor Image Processing: Basics
Color Image Processing: Basics
 
3 d viewing
3 d viewing3 d viewing
3 d viewing
 
Image segmentation
Image segmentationImage segmentation
Image segmentation
 
Image compression .
Image compression .Image compression .
Image compression .
 
3D Graphics : Computer Graphics Fundamentals
3D Graphics : Computer Graphics Fundamentals3D Graphics : Computer Graphics Fundamentals
3D Graphics : Computer Graphics Fundamentals
 
Color Image Processing
Color Image ProcessingColor Image Processing
Color Image Processing
 
HSV color model
HSV color modelHSV color model
HSV color model
 
Image Enhancement in Spatial Domain
Image Enhancement in Spatial DomainImage Enhancement in Spatial Domain
Image Enhancement in Spatial Domain
 
Back face detection
Back face detectionBack face detection
Back face detection
 
Spatial filtering
Spatial filteringSpatial filtering
Spatial filtering
 
Computer Graphics - clipping
Computer Graphics - clippingComputer Graphics - clipping
Computer Graphics - clipping
 

Similar to Ray tracing

Similar to Ray tracing (20)

Ray Tracing.pdf
Ray Tracing.pdfRay Tracing.pdf
Ray Tracing.pdf
 
Color Img at Prisma Network meeting 2009
Color Img at Prisma Network meeting 2009Color Img at Prisma Network meeting 2009
Color Img at Prisma Network meeting 2009
 
Lecture 33 reflection and refraction
Lecture 33   reflection and refractionLecture 33   reflection and refraction
Lecture 33 reflection and refraction
 
RAY OPTICS.pdf
RAY OPTICS.pdfRAY OPTICS.pdf
RAY OPTICS.pdf
 
1 ray optics_1
1 ray optics_11 ray optics_1
1 ray optics_1
 
3.2 form 4 light
3.2 form 4 light3.2 form 4 light
3.2 form 4 light
 
Introduction to chaos
Introduction to chaosIntroduction to chaos
Introduction to chaos
 
3.2 form 4 light
3.2 form 4 light3.2 form 4 light
3.2 form 4 light
 
optics-review
optics-reviewoptics-review
optics-review
 
CS 354 Global Illumination
CS 354 Global IlluminationCS 354 Global Illumination
CS 354 Global Illumination
 
0511 week10 reflection and refraction
0511 week10  reflection and refraction0511 week10  reflection and refraction
0511 week10 reflection and refraction
 
Interactive Volumetric Lighting Simulating Scattering and Shadowing
Interactive Volumetric Lighting Simulating Scattering and ShadowingInteractive Volumetric Lighting Simulating Scattering and Shadowing
Interactive Volumetric Lighting Simulating Scattering and Shadowing
 
smallpt: Global Illumination in 99 lines of C++
smallpt:  Global Illumination in 99 lines of C++smallpt:  Global Illumination in 99 lines of C++
smallpt: Global Illumination in 99 lines of C++
 
Class XII-OPTICS.pdf
Class XII-OPTICS.pdfClass XII-OPTICS.pdf
Class XII-OPTICS.pdf
 
Lec03 light
Lec03 lightLec03 light
Lec03 light
 
non linear optics
non linear opticsnon linear optics
non linear optics
 
Graphics Lecture 7
Graphics Lecture 7Graphics Lecture 7
Graphics Lecture 7
 
Optics
OpticsOptics
Optics
 
Optics geometry
Optics geometryOptics geometry
Optics geometry
 
14.20 o1 m miller
14.20 o1 m miller14.20 o1 m miller
14.20 o1 m miller
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software Engineering
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

Ray tracing

  • 2. What is ray tracing • Ray tracing is a technique for rendering three- dimensional graphics with very complex light interactions. This means you can create pictures full of mirrors, transparent surfaces, and shadows, with stunning results. • A very simple method to both understand and implement. • It is based on the idea that you can model reflection and refraction by recursively following the path that light takes as it bounces through an environment 2
  • 3. Raytraced Images PCKTWTCH by K evin Odhner, POV -Ray 3
  • 5. 5
  • 7. Ray tracing Shadow rays Reflection ray refracted ray 7
  • 8. Ray tracing algorithm • Builds the image pixel by pixel • Cast additional rays from the hit point to determine the pixel color – Shoot rays toward each light. If they hit something, the object is shadowed from that light, otherwise use “standard model” for the light – Reflection rays for mirror surfaces, to see what should be reflected in the mirror – Refraction rays to see what can be seen through transparent objects – Sum all the contributions to get the pixel color 8
  • 9. Recursive ray tracing • When a reflected or refraction ray hits a surface, repeat the whole process from that point – Send more out shadow rays – Send out new reflected rays (if required) – Send out a new refracted ray (if required) – Generally, reduce the weight of each additional ray when computing the contributions to the surface color – Stop when the contribution from a ray is too small to notice or maximum recursion level has been reached 9
  • 10. Ray tracing implementation • Ray tracing breakdown into two tasks – Constructing the ray to cast – Intersection rays with geometry • The former problem is simple vector arithmetic • Intersection calculation can be done in world coordinates or model coordinates 10
  • 11. Constructing Rays • Define rays by an initial point and a direction: x(t)=x0+td • Eye rays: Rays from the eye through a pixel – Construct using the eye location and the pixel’s location on the image plane. X0 = eye • Shadow rays: Rays from a point on a surface to the light. – X0 = point on surface • Reflection rays: Rays from a point on a surface in the reflection direction – Construct using laws of reflection. X0 = surface point • Transmitted rays: Rays from a point on a transparent surface through the surface – Construct using laws of refraction. X0 = surface point 11
  • 12. Ray-Object Intersections • Aim: Find the parameter value, ti , at which the ray first meets object i • Write the surface of the object implicitly: f(x)=0 – Unit sphere at the origin is x•x-1=0 – Plane with normal n passing through origin is: n•x=0 • Put the ray equation in for x – Result is an equation of the form f(t)=0 where we want t – Now it’s just root finding 12
  • 13. Ray-Sphere Intersection Ray : x (t ) x0 td Sphere : x x 1 0 Substitute: x0 td x0 td 1 0 : d d t2 2 x0 d t x0 x0 1 0 • Quadratic in t – 2 solutions: Ray passes through sphere - take minimum value that is > 0 – 1 solution: Ray is tangent - use it if >0 – 0 solutions: Ray does not hit sphere • Numerical stability is very important. For example, can a reflection ray hit the same sphere? 13
  • 14. Sphere Intersection A sphere is defined by its center, s, and its radius r. The intersection of a ray with a sphere can be computed as follows: 14
  • 15. Ray-Plane Intersections Ray : x (t ) x0 td P lane: n x 0 Substit ute: n x0 td 0 : n d t n x0 0 n x0 :t n d • This is plane going through the origin – What about an arbitrary plane? • To do polygons, intersect with plane then do point-in-polygon test… 15
  • 16. Intersections • It is necessary to determine the intersection between a ray with objects to decide which pixel is • Intersection with a sphere • Intersection with a plane • … 16
  • 17. Ray Tracing Illumination Recursive L R E ˆ N V Vreflected I ( E, V ) I direct I reflected I transmitted I I reflected P I transmitted Vtransmitte d I reflected k r I ( P,Vreflected ) I transmitted kt I ( P,Vtransmitted ) I direct k a I ambient I light ˆ ˆ kd N L ks ˆ ˆ V R nshiny Check for shadowing (intersection with object along ray (P,L))
  • 18. The Ray Tree N3 T3 R Viewpoint 2 N2 T1 R R 3 1 L1 L2 N1 L3 L1 R T1 1 L2 L3 Eye Ni surface normal Ri reflected ray R R T3 2 3 Li shadow ray Ti transmitted (refracted) ray
  • 19. Reflection Rays • The laws of reflection 19
  • 21. Reflection • The maximum depth of the tree affects the handling of refraction • If we send another reflected ray from here, when do we stop? 2 solutions (complementary) – Answer 1: Stop at a fixed depth. – Answer 2: Accumulate product of reflection coefficients and stop when this product is too small. 21
  • 23. Refraction ˆ N cos ˆ ˆ I N i sin Snell’s Law t i ˆ sin i t r ˆ I N cos i i ˆ M ˆ T sin ˆ M ˆ cos t N t ˆ ( N cos ˆ I) ˆ M i t ˆ sin ˆ N T i ˆ sin t ˆ ˆ ˆ T ( N cos i I ) cos t N Note that I is the negative of sin i the incoming ray ˆ T ( cos ˆ cos t ) N ˆ I r i r cos ˆ ˆ N I i cos 1 sin 2 1 2 sin 2 1 2 ˆ ˆ (1 ( N I ) 2 ) t t r i r Tˆ ˆ ˆ (N I ) 1 2 ˆ ˆ ˆ (1 ( N I ) 2 ) N ˆ I r r r 23
  • 25. Shadow Rays • Shadows are important lighting effect that can be easily with ray tracing • If we wish to compute the illinumination with shadows for a point, we shoot an additional ray from the point to every light source • A light is only allowed to contribute to the final color if the ray doesn’t hit anything between the point and a light source 25
  • 26. Pseudo Code for Ray Tracing rgb lsou; // intensity of light source rgb back; // background intensity rgb ambi; // ambient light intensity Vector L // vector pointing to light source Vector N // surface normal Object objects [n] //list of n objects in scene float Ks [n] // specular reflectivity factor for each object float Kr [n] // refractivity index for each object float Kd [n] // diffuse reflectivity factor for each object Ray r; void raytrace() { for (each pixel P of projection viewport in raster order) { r = ray emanating from viewer through P int depth = 1; // depth of ray tree consisting of multiple paths the pixel color at P = intensity(r, depth) } } 26
  • 27. rgb intensity (Ray r, int depth) { Ray flec, frac; rgb spec, refr, dull, intensity; if (depth >= 5) intensity = back; else { find the closest intersection of r with all objects in scene if (no intersection) { intensity =back; } else { Take closest intersection which is object[j] compute normal N at the intersection point if (Ks[j] >0) { // non-zero specular reflectivity compute reflection ray flec; refl = Ks[j]*intensity(flec, depth+1); } else refl =0; if (Kr[j]>0) { // non-zero refractivity compute refraction ray frac; refr = Kr[j]*intensity(frac, depth+1); } else refr =0; check for shadow; if (shadow) direct = Kd[j]*ambi else direct = Phong illumination computation; intensity = direct + refl +refr; } } return intensity; } 27