The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology Web:
Rendering Tone mapping Real worldcolor pixelVirtual world
Light-object interaction L in k r L in k t L in L in L in f r ( ’, ) cos ’ Coherent terms geometric optics Incoherent terms probability theory ’’ ’’ BRDF: f r ( ’, ) = L out /L in cos ’ Bi-directional reflection distribution function prob. density
BRDF modelling l Depends on –the wavelength –light direction –viewing direction –surface normal l Physical measurements l Tuning simple formulae
Diffuse reflection l Light intensity is independent of the viewing direction ’’ ’’ L ref = L ir k d ( ) cos ’ Lambert’s law
Diffuse surfaces
Phong’s specular reflection ’’ ’r’r ’’ ’r’r = diffuse + Function: large at =0 and goes to zero k s cos n
Diffuse+Phong materials n= diffuse Phong diffuse + Phong own color Plastics: k s is white
Diffuse + Phong materials
General rendering problem L LeLe rr tt
Local Illumination pixel L ( ) L e ( )+ l r l L l ( ’ l ) f r ( ’ l, ) cos ’ l Point, directional light sources Light sources 0/1: Light source visibility Shadows ’l’l
Role of the ambient term L ( ) L e ( )+ l r l L l ( ’ l ) f r ( ’ l, ) cos ’ l + k a L a Local illumination + ambient term
Ray-tracing LlLl shadow ray Reflection ray Refracted ray L ( ) = L e ( )+ l r l L l ( ’ l ) f r ( ’ l, ) cos ’ l + k a L a + + k r L in ( r ) + k t L in ( t ) from the reflection direction from the refraction direction
Local illumination Local illumination + shadows Ray-tracing Global illumination Photorealistic image synthesis
Steps of ray-tracing 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 color the pixel Recursion shadow ray Reflection ray Refracted ray
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 Computation of the mirroring and refraction directions l Evaluation of the illumination formula L ( ) = L e ( )+ l r l L l ( ’ l ) f r ( ’ l, ) cos ’ l + k a L a + + k r L in ( r ) + k t L in ( t )
Visibility calculation ray(t) = eye + v ·t, t > 0 float FirstIntersect(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 x
Intersection with a Sphere |ray(t) - center | 2 = R 2 Surface normal: (ray(t) - center)/R center R |r - center | 2 = R 2 r (v ·v) t ((eye-center) ·v) t +((eye-center) ·(eye-center))- R 2 = 0 Wanted: minimum from the positive solutions no root 1 root 2 roots
+ + -1=0 Quadric surfaces xyz1xyz1 [x,y,z,1] A = 0Quadric surface: Second order equation Ellipsoid x 2 y 2 z 2 a 2 b 2 c 2 + - z 2 =0 Infinite cone x 2 y 2 a 2 b 2 Infinite cylinder x 2 y 2 a 2 b 2 + - 1 =0
Triangle 1. Intersection with the plane: (ray(t) - r1) ·n = 0, t > 0 Normal vector: n = (r2 - r1) x (r3 - r1) 2. Is the intersection point p inside the triangle? ((r2 - r1) x (p - r1)) · ((r2 - r1) x (r3 - r1)) > 0 ((r3 - r2) x (p - r2)) · ((r3 - r2) x (r1 - r2)) > 0 ((r1 - r3) x (p - r3)) · ((r1 - r3) x (r2 - r3)) > 0 r1 r2 p r3 n Phase 1 Phase 2
Shading normals N1N1 N2N2 N3N3 N N = A X + B Y + C (X 1, Y 1, Z 1 ) 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 3 systems of equations of 3 unknowns A, B, C
Parametric surfaces r(u,v) = B i,j (u,v) r i, j blending functions control points
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 Recursive tessellation r(0.5,0.5) r(0,0) r(0,1) r(1,1) r(1,0) r(0,0.5) r(0.5,0)
Animated objects T1T1 T2T2 T 1 -1 T 2 -1
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 Computation of the mirroring and refraction directions l Evaluation of the illumination formula
Reflection Direction v r = v + 2 cos N vrvr v v + N cos N N cos L = v r, V= v ReflectDir( L, N, V ) { L = V - N * (N * V) * 2; } cos = - ( v ·N) horizontal vector new ray = x + v r ·t x
Refraction Direction vtvt v v + N cos N NN N sin -Ncos v t = v n + N (cos n - (1- (1 - cos 2 )/ n 2 ) ) n =n = sin sin Snellius law v Ncos sin N =N = v t = N sin Ncos horizontal vector v t = v n + N(cos n cos ) cos 1- sin 2 1- sin 2 / n 2
Camera definition eye lookat fov, aspect XRES, YRES
Ray-tracing algorithm Render( ) for each pixel p Ray r = ray( eye pixel p ) color = Trace(ray) WritePixel(p, color) endfor end ray color p p
Trace function Color Trace( ray ) IF (FirstIntersect(ray object, x) <= 0) RETURN L a ENDIF color = L e (x, -ray.dir) color += Direct Lightsource(x, -ray.dir) IF ( k r > 0 ) THEN ReflectDir( ray, reflected ray) color += k r · Trace( reflected ray ) ENDIF IF ( k t > 0 && RefractDir( ray, refracted ray ) ) color += k t · Trace( refracted ray ) ENDIF RETURN color Color Trace( ray, d) IF d > dmax THEN RETURN L a IF ( FirstIntersect(ray object, x) <= 0) RETURN L a ENDIF color = L e (x, -ray.dir) color += Direct Lightsource(x, -ray.dir) IF ( k r > 0 ) THEN ReflectDir( ray, reflected ray) color += k r · Trace( reflected ray, d+1 ) ENDIF IF ( k t > 0 && RefractDir( ray, refracted ray ) ) color += k t · Trace( refracted ray, d+1 ) ENDIF RETURN color ray x
DirectLightsource function DirectLightsource( x, vdir ) color = k a L a FOR each lightsource l DO shadowray = x to lightsource[l] t = FirstIntersect( shadowray ); IF (t |x - lightsource[l].pos|) color += Brdf(ldir, x, vdir) cos l ' lightsource[l].Intensity ENDIF ENDFOR RETURN color shadow pixel x vdir
Ray-tracer class diagram SceneObject SphereMesh Primitive( ) Intersect(Ray) Normal( ) Intersect(Ray) Normal( ) Define Render FirstIntersect DirectLightsource Trace(Ray) Light Dir(Point) Intens(Point) LatransformLe, pos Camera XRES, YRES eye, lookat, vup fov, aspect GetXRes( ) GetYRes( ) GetRay(int,int) Ray Dir( ) Start( ) dir, start Material Le, kd, ks, shine kr, kt, n BRDF ReflectDir RefractDir Center, radiusvertices[ ] Primitive Intersect(Ray) Normal( )
Ray-tracing: results Computation time Pixel# · Object# ·(Light source# + 1) PovRay
Speeding up the intersection calculation l Speeding up for each objects: –bounding volumes l Computing for less number of objects: –space partitioning
Bounding Volume float IntersectBV( ray, object ) // < 0 ha nincs IF ( Intersect( ray, bounding volume of object) <=0) RETURN -1; RETURN Intersect( ray, object ); END
Space partitioning methods 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. pre- processing objects Space-partitioning data structure ray- tracing First intersection
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 ) Uniform space partitioning Preprocessing: FOR each cell identify the objects that are partly here complexity: O(n · c ) = O(n 2 )
Ray-shooting: FOR each cell intersecting the line Intersect objects registered in this cell IF intersection exists RETURN ENDFOR Octree Octree Tree build IF a cell contains just a few objects register objects in this cell ELSE subdivide cells: c1, c2, …, c8 Tree build(c1); … Tree build(c8); ENDIF
Ray-shooting: FOR each cell intersecting the line Intersect with the included objects IF intersection exists RETURN ENDFOR Kd (BSP) tree kd-tree 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: c1, c2 Tree build (c1); Tree build (c2); ENDIF 1 23
Example 1
Example 2