The Fundamentals of RayTracing SzirmayKalos Lszl Dept of

  • Slides: 41
Download presentation
The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest

The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit. bme. hu Web: http: //www. iit. bme. hu/~szirmay

Rendering Tone mapping pixel Virtual world color Real world

Rendering Tone mapping pixel Virtual world color Real world

Light-object interaction kr Lin Lin ’ ’ Lin fr( ’, ) cos ’ kt

Light-object interaction kr Lin Lin ’ ’ Lin fr( ’, ) cos ’ kt Lin Coherent terms geometric optics prob. density Incoherent terms probability theory BRDF: fr( ’, ) = Lout/Lin cos ’ Bi-directional reflection distribution function

BRDF modelling l Depends on – the wavelength – light direction – viewing direction

BRDF modelling l Depends on – the wavelength – light direction – viewing direction – surface normal Physical measurements l Tuning simple formulae l

Diffuse reflection l Light intensity is independent of the viewing direction ’ ’ Lref

Diffuse reflection l Light intensity is independent of the viewing direction ’ ’ Lref = Lir kd ( ) cos ’ Lambert’s law

Diffuse surfaces

Diffuse surfaces

Phong’s specular reflection ’r ’r ’ = diffuse + Function: large at y=0 and

Phong’s specular reflection ’r ’r ’ = diffuse + Function: large at y=0 and goes to zero ks cosn y y ’

Diffuse+Phong materials own color diffuse Plastics: Phong ks is white diffuse + Phong n=

Diffuse+Phong materials own color diffuse Plastics: Phong ks is white diffuse + Phong n= 5 10 20 50

Diffuse + Phong materials

Diffuse + Phong materials

General rendering problem L r Le t

General rendering problem L r Le t

Local Illumination Light sources pixel ’l L ( ) Le( )+Sl rl Ll (

Local Illumination Light sources pixel ’l L ( ) Le( )+Sl rl Ll ( ’l) fr ( ’l , ) cos ’l 0/1: Light source visibility Shadows Point, directional light sources

Role of the ambient term Local illumination + ambient term L ( ) Le(

Role of the ambient term Local illumination + ambient term L ( ) Le( )+Sl rl Ll ( ’l) fr ( ’l , ) cos ’l + ka La

Ray-tracing Refracted ray Reflection ray shadow ray Ll L( ) = Le( )+Sl rl

Ray-tracing Refracted ray Reflection ray shadow ray Ll L( ) = Le( )+Sl rl Ll ( ’l) fr ( ’l , ) cos ’l + ka La + + kr Lin ( r) + kt Lin( t) from the reflection direction from the refraction direction

Local illumination + shadows Ray-tracing Global illumination Photorealistic image synthesis

Local illumination + shadows Ray-tracing Global illumination Photorealistic image synthesis

Refracted ray Steps of ray-tracing Reflection ray shadow ray l Light intensity coming through

Refracted ray Steps of ray-tracing Reflection ray shadow ray l Light intensity coming through a pixel – Visible point and the normal vector here – Visible light sources from here – mirror and refraction directions – Light intensity coming from the reflection direction – Light intensity coming form the refraction direction – Adding up the light components and Recursion color the pixel

Tasks of ray-tracing l Visibility computation – Determination of that point which is visible

Tasks of ray-tracing l Visibility computation – Determination of that point which is visible from a point in a given direction + normal vector here – point+direction = ray l l Computation of the mirroring and refraction directions Evaluation of the illumination formula L( ) = Le( )+Sl rl Ll ( ’l) fr ( ’l , ) cos ’l + ka La + + kr Lin ( r) + kt Lin( t)

Visibility calculation x ray(t) = eye + v ·t, t > 0 float First.

Visibility calculation x ray(t) = eye + v ·t, t > 0 float First. Intersect(ray iobject, x) // <=0 if no intersection t = -1 FOR each object tnew = Intersect( ray, object ); // <= 0 if no intersection IF (tnew > 0 && tnew < t) t = tnew, iobject = object ENDFOR IF (t > 0) x = eye + v ·t; RETURN t; END

Intersection with a Sphere |r - center |2 = R 2 r R center

Intersection with a Sphere |r - center |2 = R 2 r R center no root 1 root 2 roots |ray(t) - center |2 = R 2 (v ·v) t 2 + 2 ((eye-center) ·v) t +((eye-center) ·(eye-center))- R 2 = 0 Wanted: minimum from the positive solutions Surface normal: (ray(t) - center)/R

Quadric surfaces Quadric surface: Ellipsoid 2 2 z 2 x y + + -1=0

Quadric surfaces Quadric surface: Ellipsoid 2 2 z 2 x y + + -1=0 a 2 b 2 c 2 x [x, y, z, 1] A y = 0 z 1 Infinite cone x 2 y 2 2 2 + - z =0 2 a b Second order equation Infinite cylinder x 2 y 2 + 2 - 1 =0 2 a b

Triangle Phase 1 Phase 2 r 3 n p r 1 1. Intersection with

Triangle Phase 1 Phase 2 r 3 n p r 1 1. Intersection with the plane: (ray(t) - r 1) ·n = 0, t > 0 Normal vector: n = (r 2 - r 1) x (r 3 - r 1) 2. Is the intersection point p inside the triangle? ((r 2 - r 1) x (p - r 1)) · ((r 2 - r 1) x (r 3 - r 1)) > 0 ((r 3 - r 2) x (p - r 2)) · ((r 3 - r 2) x (r 1 - r 2)) > 0 ((r 1 - r 3) x (p - r 3)) · ((r 1 - r 3) x (r 2 - r 3)) > 0 r 2

Shading normals N 2 N N 3 N 1 (X 1 , Y 1

Shading normals N 2 N N 3 N 1 (X 1 , Y 1 , Z 1 ) N = A X + B Y+C N 1 = A X 1 + B Y 1 + C N 2 = A X 2 + B Y 2 + C N 3 = A X 3 + B Y 3 + C A, B, C 3 systems of equations of 3 unknowns

Parametric surfaces control points r(u, v) = S S Bi, j (u, v) ri,

Parametric surfaces control points r(u, v) = S S Bi, j (u, v) ri, j blending functions

Intersecting parametric sufaces r(u, v), u, v in [0, 1] ray(t) = eye +

Intersecting parametric sufaces r(u, v), u, v in [0, 1] ray(t) = eye + v ·t, t > 0 r(u, v) = ray(t) Solution of the equation: u, v, t Test: 0< u, v < 1, t>0 r(0, 0) r(0, 0. 5) r(0. 5, 0) r(1, 0) r(0, 1) r(0. 5, 0. 5) r(1, 1) Recursive tessellation

Animated objects T 1 -1 T 2 -1

Animated objects T 1 -1 T 2 -1

Tasks of ray-tracing l Visibility computation – Determination of that point which is visible

Tasks of ray-tracing l Visibility computation – Determination of that point which is visible from a point in a given direction + normal vector here – point+direction = ray l l Computation of the mirroring and refraction directions Evaluation of the illumination formula

Reflection Direction horizontal vector N new ray = x + vr ·t v +

Reflection Direction horizontal vector N new ray = x + vr ·t v + N cosa v r a a x cos a = - (v·N) vr = v + 2 cos a N L = vr, V= v Reflect. Dir( L, N, V ) { L = V - N * (N * V) * 2; }

Refraction Direction horizontal vector v + Ncosa v a N sin a n =

Refraction Direction horizontal vector v + Ncosa v a N sin a n = sin b N sinb b vt -Ncosb N Snellius law v+Ncosa N = sina vt = N sinb - Ncosb vt = v/n + N(cosa/n- cosb) cosb = 1 - sin 2 b = 1 - sin 2 a/n 2 vt = v/n + N (cosa/n - (1 - cos 2 a)/n 2) )

Camera definition fov, aspect lookat eye XRES, YRES

Camera definition fov, aspect lookat eye XRES, YRES

Ray-tracing algorithm ray p Render( ) for each pixel p Ray r = ray(

Ray-tracing algorithm ray p Render( ) for each pixel p Ray r = ray( eye pixel p ) color = Trace(ray) Write. Pixel(p, color) endfor end p color

Trace function Color Trace( ray, ray )d) Color IF d > dmax THEN RETURN

Trace function Color Trace( ray, ray )d) Color IF d > dmax THEN RETURN La IF ((First. Intersect(ray IF First. Intersect(ray object, x) x) <= <= 0) 0) RETURN L Laa RETURN ENDIF color == L Lee (x, -ray. dir) color += += Direct Lightsource(x, -ray. dir) color IF (( kkrr >> 00 )) THEN IF Reflect. Dir( ray, reflected ray) Reflect. Dir( color += += kkrr ··Trace(reflectedray, ray d+1 ) color ) ENDIF IF (( kktt >> 00 && && Refract. Dir( ray, refracted ray )) )) IF color += += kktt ··Trace(refractedray, ray d+1 ) color ) ENDIF RETURN color RETURN ray x

Direct. Lightsource function Direct. Lightsource( x, vdir ) color = ka La shadow FOR

Direct. Lightsource function Direct. Lightsource( x, vdir ) color = ka La shadow FOR each lightsource l DO shadowray = x to lightsource[l] t = First. Intersect( shadowray ); IF (t < 0 || t > |x - lightsource[l]. pos|) color += Brdf(ldir, x, vdir) cos l' lightsource[l]. Intensity ENDIF ENDFOR RETURN color pixel vdir x

Material Le, kd, ks, shine kr, kt, n BRDF Reflect. Dir Refract. Dir Ray-tracer

Material Le, kd, ks, shine kr, kt, n BRDF Reflect. Dir Refract. Dir Ray-tracer class diagram Object transform Primitive( ) Primitive Scene La Define Render First. Intersect Direct. Lightsource Trace(Ray) Light Le, pos Dir(Point) Intens(Point) Intersect(Ray) Normal( ) Camera Sphere Center, radius Intersect(Ray) Normal( ) Mesh vertices[ ] Intersect(Ray) Normal( ) XRES, YRES eye, lookat, vup fov, aspect Ray Get. XRes( ) Get. YRes( ) Get. Ray(int, int) Dir( ) Start( ) dir, start

Ray-tracing: results Pov. Ray Computation time Pixel# · Object# ·(Light source# + 1)

Ray-tracing: results Pov. Ray Computation time Pixel# · Object# ·(Light source# + 1)

Speeding up the intersection calculation Speeding up for each objects: – bounding volumes l

Speeding up the intersection calculation Speeding up for each objects: – bounding volumes l Computing for less number of objects: – space partitioning l

Bounding Volume float Intersect. BV( ray, object ) // < 0 ha nincs IF

Bounding Volume float Intersect. BV( ray, object ) // < 0 ha nincs IF ( Intersect( ray, bounding volume of object) <=0) RETURN -1; RETURN Intersect( ray, object ); END

Space partitioning methods objects preprocessing Space-partitioning data structure raytracing First intersection Data structure: •

Space partitioning methods objects preprocessing Space-partitioning data structure raytracing First intersection Data structure: • Knowing the ray, it reduces the number of objects the ray may intersect. • If we find an intersected object, the search can be terminated.

Uniform space partitioning Preprocessing: FOR each cell identify the objects that are partly here

Uniform space partitioning Preprocessing: FOR each cell identify the objects that are partly here complexity: O(n ·c ) = O(n 2) Ray-tracing: FOR each cell of the line // line drawing Intersect objects registered in this cell IF intersection exists RETURN ENDFOR average case complexity: O(1 )

Tree build IF a cell contains just a few objects register objects in this

Tree build IF a cell contains just a few objects register objects in this cell ELSE subdivide cells: c 1, c 2, …, c 8 Tree build(c 1); … Tree build(c 8); ENDIF Octree 2 1 3 Ray-shooting: FOR each cell intersecting the line Intersect objects registered in this cell IF intersection exists RETURN ENDFOR 1 2 Octree 3 3

Tree build: IF a cell contains just a few objects register objects in this

Tree build: IF a cell contains just a few objects register objects in this cell ELSE find a subdivision plane cell halving with the plane: c 1, c 2 Tree build (c 1); Tree build (c 2); ENDIF Kd (BSP) tree 2 1 Ray-shooting: FOR each cell intersecting the line Intersect with the included objects IF intersection exists RETURN ENDFOR 3 1 kd-tree 2 3

Example 1

Example 1

Example 2

Example 2