Vous êtes sur la page 1sur 52

Ray Tracing III Review

Pseudo codes


RayCast-1 Plain ray caster (direct illumination) RayCast-2 RayCast-1 + shadow rays RayTrace-1 Recursive ray tracer (+ reflection) RayTrace-2 Recursive ray tracer (+ refraction)

Pseudo codes


RayCast-1 Plain ray caster (direct illumination) RayCast-2 RayCast-1 + shadow rays RayTrace-1 Recursive ray tracer (+ reflection) RayTrace-2 Recursive ray tracer (+ refraction)

Friday, Dec 2

Pseudo Code for RayCast-1


// Global Variables rgb lsou; rgb back; rgb ambi; // intensity of light source // background intensity // ambient light intensity

Point light // position of light source Object objects [n] // list of n objects in scene Camera cam; // camera settings Ray r;

My primitives in the scene


1 0 1 2 2 3 4 5 Objects Object type material

Plane A,B,C,D // eq Sphere O // center R // radius

n kd // diffuse reflectivity factor ks // specular reflectivity factor n // shininess factor kr // refractivity index for each object

Image RayCast-1 (int width, int height) RayCast{ Image image = new Image(width, height); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { r = ConstructRayThroughPixel (i, j); Intersection hit = FindIntersection (r); image[i][j] = GetColor (r, hit); } } return image; }

Image RayCast-1 (int width, int height) RayCast{ Image image = new Image(width, height); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { r = ConstructRayThroughPixel (i, j); Intersection hit = FindIntersection (r); image[i][j] = GetColor (r, hit); } } return image; }

Perspective Ray Generation

Perspective Ray Generation


& look v up u! look v up & & look v u v! & look v u

& 2 tan( fovx / 2) & (x ! u W & 2 tan( fovx / 2) & (y ! v W


& look (2i  1  W ) & (2 j  1  H ) & d (i, j ) !  (x  (y look 2 2

Perspective Ray Generation

Image RayCast-1 (int width, int height) RayCast{ Image image = new Image(width, height); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { r = ConstructRayThroughPixel (i, j); Intersection hit = FindIntersection (r); image[i][j] = GetColor (r, hit); } } return image; }

My primitives in the scene


1 0 1 2 2 3 4 5 Objects Object type material

Plane A,B,C,D // eq Sphere O // center R // radius

n Intersection index // closest-hit (-1 if none) P // point N // normal kd // diffuse reflectivity factor ks // specular reflectivity factor n // shininess factor kr // refractivity index for each object

Ray-Scene Intersection
Intersections with geometric primitives: Sphere Plane Triangle (you MUST implement the barycentric approach) Groups of primitives (scene)

Slides sets!! ray-lec-1 ray-intersect-2

Image RayCast-1 (int width, int height) RayCast{ Image image = new Image(width, height); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { r = ConstructRayThroughPixel (i, j); Intersection hit = FindIntersection (r); image[i][j] = GetColor (r, hit); } } return image; }

rgb GetColor (Ray r, Intersection hit) { rgb intensity; if (hit.id = -1) // no intersection intensity = back else Intensity = Phong_illumination (r, hit); return intensity; }
nls

I ! ka I a  I i kd Li N  k s Ri V
i !1

rgb Phong_Illumination (Ray r, Intersection hit) { rgb intensity;


nls

I ! ka I a  I i kd Li N  k s Ri V
i !1

return intensity; }

R P

Pseudo codes


RayCast-1 Plain ray caster (direct illumination) RayCast-2 RayCast-1 + shadow rays RayTrace-1 Recursive ray tracer (+ reflection) RayTrace-2 Recursive ray tracer (+ refraction)

Mon, Dec 5

Pseudo Code for RayCast-2


// Global Variables Exactly as in RayCast-1 rgb lsou; rgb back; rgb ambi; // intensity of light source // background intensity // ambient light intensity

Point light // position of light source Object objects [n] // list of n objects in scene Camera cam; // camera settings Ray r;

// Exactly as in RayCast-1
Image RayCast-2 (int width, int height) RayCast{ Image image = new Image(width, height); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { r = ConstructRayThroughPixel (i, j); Intersection hit = FindIntersection (r); image[i][j] = GetColor (r, hit); } } return image; }

rgb GetColor (Ray r, Intersection hit) { rgb intensity; if (hit.id = -1) // no intersection intensity = back else { shadow = CheckShadow (hit); if (shadow) { Kd = objects[hit.id].material.kd Intensity = ambi * Kd } else Intensity = Phong_illumination (r, hit); } return intensity; }

boolean CheckShadow (Intersection hit) {

P
NOT in shadow!

P
In shadow!

Pseudo codes


RayCast-1 Plain ray caster (direct illumination) RayCast-2 RayCast-1 + shadow rays RayTrace-1 Recursive ray tracer (+ reflection) RayTrace-2 Recursive ray tracer (+ refraction)

Wed, Dec 7

Reflection

Reflection


Reflection angle = view angle

Pseudo Code for RayTrace-1


// Global Variables rgb lsou; rgb back; rgb ambi; // intensity of light source // background intensity // ambient light intensity

Point light // position of light source Object objects [n] // list of n objects in scene Camera cam; // camera settings Int depth; // depth of ray tree consisting of multiple paths Ray r;

Image RayTrace-1 (int width, int height) RayTrace{ Image image = new Image(width, height); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { r = ConstructRayThroughPixel (i, j); image[i][j] = GetColor (r); } } return image; }
Notice that now all ray-objects intersections are called within GetColor()

rgb GetColor (Ray r) { Recursive! Ray flec; rgb spec, local, intensity; depth = depth +1 if (depth >= 5) intensity = back; else { Intersection hit = FindIntersection (r); if (hit.id = -1) intensity = back; else { if (objects.[hit.id].material.Ks >0) { flec = ComputeReflectionRay (hit) ; spec = objects.[hit.id].material.Ks* GetColor(flec); } else spec = 0; Check for shadow // as in RayCast-2() intensity = local + spec; } } depth = depth -1 return intensity; }
shadow = CheckShadow (hit); if (shadow) { Kd = objects[hit.id].material.kd local = ambi * Kd } else local = Phong_illumination (r, hit);

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.

rgb GetColor (Ray r) { Recursive! Ray flec; rgb spec, local, intensity; depth = depth +1 if (depth >= 5) intensity = back; else { Intersection hit = FindIntersection (r); if (hit.id = -1) intensity = back; else { if (objects.[hit.id].material.Ks >0) { flec = ComputeReflectionRay (hit) ; spec = objects.[hit.id].material.Ks * GetColor(flec); } else spec = 0; Check for shadow // as in RayCast-2() intensity = local + spec; } } depth = depth -1 return intensity; }
shadow = CheckShadow (hit); if (shadow) { Kd = objects[hit.id].material.kd local = ambi * Kd } else local = Phong_illumination (r, hit);

Memory stack

__
0

spec = _____ * local = ________ shadow? ___ intensity = _____ + _____

__
3

eye __ __ __ __
2 1

__

spec = _____ * local = ________ shadow? ___ intensity = _____ + _____

spec = _____ * local = ________ shadow? ___ intensity = _____ + _____

0 1 2 3

(kd, ks, Phong) = (0.5, 0.7, 0.5) (kd, ks, Phong) = (0.1, 0.4, 0.3) (kd, ks, Phong) = (0.6, 0.2, 0.7) (kd, ks, Phong) = (0.3, 0.8, 0.8) (back, ambi) = (0.32, 0.7)

spec = _____ * local = ________ shadow? ___ intensity = _____ + _____ depth = __________________

Pseudo codes


RayCast-1 Plain ray caster (direct illumination) RayCast-2 RayCast-1 + shadow rays RayTrace-1 Recursive ray tracer (+ reflection) RayTrace-2 Recursive ray tracer (+ refraction)

Sat, Dec 10

Ray Tracing Issues (RayTrace-2) (RayTrace-

I(P) = Ilocal(P) + ksI(Pr) + kr(Pt)


Local term Reflected Transmitted

1) Cast a ray 2) Determine Intersections 3) For closest Intersection:  Extend light shadow ray + calculate local term  Spawn Reflected Ray (go to step 2)  Spawn Transmitted Ray (go to step 2)

Refraction


From Color and Light in Nature by Lynch and Livingston

Refraction
Snell s Law
sin Ut Lt ! ! Lr sin U i Li N cosU i  I N I
U i N cos U i

T ! sin U t M  cosU t N

( N cosU i  I ) M ! sin U i sin U t ( N cosU  I )  cosU N T! i t sin U i T ! (L r cosU i  cosU t ) N  L r I cosU i ! N I

Ut
N

Note that I is the negative of the incoming ray

cosU t ! 1  sin 2 U t ! 1  L r2 sin 2 U i ! 1  L r2 (1  ( N I ) 2 ) T ! L r ( N I )  1  L r2 (1  ( N I ) 2 ) N  L r I

Refraction
N cosU i  I N

Snell s Law

U i N cos U i

sin Ut Lt ! ! Lr sin Ui Li
Index of Refraction

Ut
N

Note that I is the negative of the incoming ray

! L ( N I )  1  L 2 (1  ( N I ) 2 ) N  L I T r r r

Refraction & Sidedness of Objects




Make sure you know whether youre entering or leaving the transmissive material:
I I
i=1 T= i=

material index 1

material index

T=

T N

Refraction Indices
Index of refraction for various materials:
Material Vacuum Air Water Alcohol Fused quartz Crown glass Flint glass Sapphire Heavy flint glass Diamond Index 1.0 1.0003 1.33 1.36 1.46 1.52 1.65 1.77 1.89 2.42

Ray

indexmedium index ! indexmaterial

! L ( N I )  1  L 2 (1  ( N I ) 2 ) N  L I T r r r

NewRay

indexmedium index ! indexmaterial

entry_position

Attention to the order!

indexmaterial index ! indexmedium


! L ( N I )  1  L 2 (1  ( N I ) 2 ) N  L I T r r r

entry_position

NewRay

Pseudo Code for RayTrace-2


// Global Variables (same as in RayTrace-1) rgb lsou; rgb back; rgb ambi; // intensity of light source // background intensity // ambient light intensity

Point light // position of light source Object objects [n] // list of n objects in scene Camera cam; // camera settings Int depth; // depth of ray tree consisting of multiple paths Ray r;

// Same as in RayTrace-1
Image RayTrace-2 (int width, int height) RayTrace{ Image image = new Image(width, height); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { r = ConstructRayThroughPixel (i, j); image[i][j] = GetColor (r); } } return image; }

rgb GetColor (Ray r) { Recursive! Ray flec, frac; rgb spec, refr, local, intensity; depth = depth +1 if (depth >= 5) intensity = back; else { Intersection hit = FindIntersection (r); if (hit.id = -1) intensity = back; else { Check for reflection // as in RayTrace-1() if (objects.[hit.id].material.Ks[j] >0) { flec = ComputeReflectionRay (hit) ; spec = Ks[j] * GetColor (flec); } else spec = 0; Check for shadow // as in RayCast-2() intensity = local + spec; } } depth = depth -1 return intensity; }

rgb GetColor (Ray r) { Recursive! Ray flec, frac; rgb spec, refr, local, intensity; depth = depth +1 if (depth >= 5) intensity = back; else { Intersection hit = FindIntersection (r); if (hit.id = -1) intensity = back; else { Check for reflection // as in RayTrace-1()
if (objects.[hit.id].material.Ks >0) if (objects.[hit.id].material.Ks[j] >0) { { flec = ComputeReflectionRay (hit) ;; flec = ComputeReflectionRay (hit) spec =spec = objects.[hit.id].material.Ks * GetColor (flec); Ks[j] * GetColor (flec); } else spec = 0;} else spec = 0;

Check for shadow // as in RayCast-2() intensity = local + spec; } } depth = depth -1 return intensity; }
shadow = CheckShadow (hit); if (shadow) { Kd = objects[hit.id].material.kd local = ambi * Kd } else local = Phong_illumination (r, hit);

rgb GetColor (Ray r) { Ray flec, frac; rgb spec, refr, local, intensity; depth = depth +1 if (depth >= 5) intensity = back; else { Intersection hit = FindIntersection (r); if (hit.id = -1) intensity = back; else { Check for reflection // as in RayTrace-1() if (objects.[hit.id].material.Kr >0) { frac = ComputeRefractionRay (hit) ; refr = objects.[hit.id].material.Kr * GetColor (frac); } else refr = 0; Check for shadow // as in RayCast-2() intensity = local + spec + refr; } } depth = depth -1 return intensity; }

rgb GetColor (Ray r) { Recursive! Ray flec, frac; rgb spec, refr, local, intensity; depth = depth +1 if (depth >= 5) intensity = back; else { Intersection hit = FindIntersection (r); if (hit.id = -1) intensity = back; else { Check for reflection // as in RayTrace-1() if (objects.[hit.id].material.Kr[j] >0) { frac = ComputeRefractionRay (hit) ; refr = objects.[hit.id].material.Kr * GetColor (frac); } else refr = 0; Check for shadow // as in RayCast-2() intensity = local + spec + refr; } } depth = depth -1 return intensity; }

rgb GetColor (Ray r) { Ray flec, frac; rgb spec, refr, local, intensity; depth = depth +1 if (depth >= 5) intensity = back; else { Intersection hit = FindIntersection (r); if (hit.id = -1) intensity = back; else { Reflection if (objects.[hit.id].material.Kr[j] >0) { Refraction frac = ComputeReflectionRay (hit) ; refr = Ks[j] * GetColor (frac); Shadowing= 0; } else refr intensity = local + spec + refr; } } depth = depth -1 return intensity; }

My primitives in the scene


1 0 1 2 2 3 4 5 Objects Object type material

Plane A,B,C,D // eq Sphere O // center R // radius Triangle (p1,p2,p3)

n Intersection index // closest-hit (-1 if none) P // point N // normal

Mesh class MD2

Other

kd // diffuse reflectivity factor ks // specular reflectivity factor n // shininess factor kr // refractivity index for each object

Vous aimerez peut-être aussi