Magdalena(Schwarzl 1
Agenda Ray(tracing(algorithm( Code(structure( Ray(intersec;ons Sphere Triangle Hints Transforma;ons Debugging How(to(start Magdalena(Schwarzl 2
Basic algortihm For each pixel: 1. Construct ray 2. Test intersection with objects Nearest intersection point visible on image 3. Perform Shading 2
Components Scene: Objects, lights, viewpoint, materials,... xml scene format, see course website Camera: For ray construction Image: 2D Array of Vec3's (for r,g,b), represents pixels Rays: For intersection tests Shading: Illumination models 3
Ray Consists of origin + direction (both Vec3) dir Normalize direction! o Every point p on ray = o+ λ*dir Object Needs intersection routine, returning λ, normal, etc. Additional: Parameters for material properties,... For this lab: Spheres and triangle meshes Scene Arrange objects for easy intersection (for example in a list) 4
Implementation List<SceneObjects> objectlist for (y=0; y<width; y++) iterate over all pixels for (x=0; x<height; x++) ray = camera.constructrayforpixel(x,y) color = trace(ray,objectlist) image.setcolor(x,y,color) 5
Implementation List<SceneObjects> objectlist List<SceneObjects> objectlist for (y=0; y<width; y++) for (y=0; y<width; y++) for (x=0; x<height; x++) for (x=0; x<height; x++) ray = camera.constructrayforpixel(x,y) ray = camera.constructrayforpixel(x,y) color = trace(ray,objectlist) color = trace(ray,objectlist) image.setcolor(x,y,color) image.setcolor(x,y,color) trace(ray,objectlist) for(s=0; s < objectlist.size(); s++) object = objectlist.at(s) result = object.intersect(ray) //determine smallest result.t > 0! if (no intersection) return backgroundcolor else return result.color 6
So far: 7
Shading Extend trace method to use local illumination models: trace(ray,objectlist) for(s=0; s < objectlist.size(); s++) object = objectlist.at(s) result = object.intersect(ray) //determine smallest result.t > 0! if (no intersection) return backgroundcolor else //determine intersection point Vec3 ip = ray.origin + result.t*ray.direction Vec3 normal = result.normal Vec3 light Vec3 color = shade(ray, object, ip, normal, light) return color 8
Shading More than one light source: trace(ray,objectlist) for(s=0; s < objectlist.size(); s++) object = objectlist.at(s) result = object.intersect(ray) //determine smallest result.t > 0! if (no intersection) return backgroundcolor else //determine intersection point Vec3 ip = ray.origin + result.t*ray.direction Vec3 normal = result.normal Vec3 overallcolor; for (l = 0; l < lightsinscene; l++) Vec3 light overallcolor += shade(ray, object, ip, normal, light) return overallcolor 9
Shadows Point is in shadow if not directly illuminated by light Test by creating shadow ray from intersection point to light source Intersect with all objects again if (intersection in front of light) return darkcolor else //perform shading as before 10
So far, only direct illumination: 11
So far, only direct illumination: VS Recursion needed! 12
Recursion For every intersection point: Create new rays for reflection/refraction Trace those rays! Stop recursion after reaching max. bounces 13
Recursion trace(ray,objectlist, depth) //perform intersection and shading as before... if depth >= maxbounces return overallcolor stop after max bounces //depending on object's material properties, cast new rays if (object.transmittance > 0) ray.origin = ip ray.direction = calculatetransmittancedirection() colortrans = trace(ray,objectlist, depth+1) if (object.reflectance > 0) ray.origin = ip ray.direction = calculatereflectancedirection() colorrefl = trace(ray,objectlist, depth+1) return finalcolor = //combine colors 14
Combine colors Coefficients for reflectance, transmittance are defined in scene file Reflectance + Transmittance + own Color = 1 finalcolor = r*reflectedcolor + t*transmittedcolor + (1-r-t)*ownColor 15
Agenda Ray(tracing(algorithm( Code(structure( Ray(intersec;ons Sphere Triangle Hints Transforma;ons Debugging How(to(start Magdalena(Schwarzl 3
Ray(intersec;on A(ray(is(defined(as X = P + λ! d! d P Where(X(is(any(point(on(the(line( Magdalena(Schwarzl 4
Sphere A(sphere(is(defined(as r 2 = ( X C) 2 C r Where X is any point on the sphere Magdalena(Schwarzl 5
Intersect Ray-Sphere We(want(to(find(a(point(on(both! d r 2 = ( X C) 2 X = P + λ! d S 1 Compute lambda C r 2 = ((P + λ d) C! ) 2 P S 2 r r 2 = ((P + λ d) C! )((P + λ d) C! ) Magdalena(Schwarzl 6
Intersect Ray-Sphere r 2 = ((P + λ d) C! )((P + λ d) C! ) 0 = d 2 λ 2 + λ ( 2P d! 2C d! ) + ( P C) 2 r 2 A = d 2 B = ( 2P d! 2C d! ) C = ( P C) 2 r 2 λ 1,2 = B ± B2 4AC 2A Magdalena(Schwarzl 7
Intersect Ray-Sphere λ 1,2 = B ± B2 4AC 2A Magdalena(Schwarzl 8
Intersect Ray-Sphere λ 1,2 = B ± B2 4AC 2A <(0( no(intersec;on( =(0( tangent( >(0( two(intersec;on(points(! d S 1! d P r C P! d S 1 r C P S 2 r C Magdalena(Schwarzl 9
Barycentric coordinates V 2 A(triangle(is(defined(as e 12 V 1 e 23 e 13 V 3 Where X is any point inside the triangle Magdalena(Schwarzl 10
Barycentric coordinates V 2 A(triangle(is(defined(as 1 u v = A 1 e 12 u = A 2 A 1 + A 2 + A 3 A 1 + A 2 + A 3 V 1 A 3 A 2 X A 1 e 23 v = A 3 A 1 + A 2 + A 3 e 13 V 3 Where X is any point inside the triangle Magdalena(Schwarzl 11
Barycentric coordinates V 2 A(triangle(is(defined(as 1 u v = A 1 e 12 u = A 2 A 1 + A 2 + A 3 A 1 + A 2 + A 3 V 1 A 3 A 2 X A 1 e 23 v = A 3 A 1 + A 2 + A 3 e 13 X ( u, v) = ( 1 u v)v 1 + uv 2 + vv 3 u, v 0 V 3 Where X is any point inside the triangle u + v 1 Magdalena(Schwarzl 12
Triangle A triangle is defined by: X ( u, v) = ( 1 u v)v 1 + uv 2 + vv 3 u, v 0 u + v 1 V 1 e 12 V 2 e 13 e 23 V 3 3 points define a plane: Magdalena(Schwarzl 13
Triangle A triangle is defined by: X ( u, v) = ( 1 u v)v 1 + uv 2 + vv 3 u, v 0 u + v 1 V 1 e 12 V 2 e 13 e 23 V 3 3 points define a plane: n = e 13 e 12 e 13 e 12 nx = nv 1 Magdalena(Schwarzl 14
Intersect Ray-Triangle We(want(the(intersec;on ( ) = 1 u v X u, v ( )V 1 + uv 2 + vv 3 V 1 e 12 S 1 V 2 e 23 nx = nv 1 X = P + λ! d! d e 13 Compute lambda P V 3 n( P + λ d! ) = nv 1 Magdalena(Schwarzl 15
Intersect Triangle-Sphere 1) ray-plane λ = nv 1 np n! d Magdalena(Schwarzl 16
Intersect Triangle-Sphere 1) ray-plane λ = nv 1 np n! d!= 0 One intersection = 0 No intersection with plane S 1 S 1 = P + λ! d parallel Inside plane Magdalena(Schwarzl 17
Intersect Ray-Triangle 2) Inside triangle? P + λ d! = ( 1 u v)v 1 + uv 2 + vv 3 Use x,y,z to get linear system of 3 equations Solve for v & u Magdalena(Schwarzl 18
Intersect Ray-Triangle 2) Inside triangle? P + λ d! = ( 1 u v)v 1 + uv 2 + vv 3 Use x,y,z to get linear system of 3 equations Solve for v & u u,v <=1 AND u,v >=0 intersection u,v > 1 OR u,v < 0 no intersection Magdalena(Schwarzl 19
Agenda Ray(tracing(algorithm( Code(structure( Ray(intersec;ons Sphere Triangle Hints Transforma;ons Debugging How(to(start Magdalena(Schwarzl 20
Transformations Think about transformations early! Magdalena(Schwarzl 21
Transformations Think about transformations early! Two approaches: transform object inverse transform ray Precompute inv.-transfomations No translation on vectors Inverse-transpose for normals Magdalena(Schwarzl 22
Precision Problems? Magdalena(Schwarzl 23
Precision Problems? Shadow ray hit its source (collision at distance ==0) Offset the ray cast source a small amount in the direction of the ray. Do never test for == 0 Test for between 0 ± ε Magdalena(Schwarzl 24
Normals, Texures Visual debugging Output textures, normals, reflection vectors as color to see problems 0 ±ε Magdalena(Schwarzl 25
Agenda Ray(tracing(algorithm( Code(structure( Ray(intersec;ons Sphere Triangle Hints Transforma;ons Debugging How(to(start Magdalena(Schwarzl 26
How to start Plan code structure Classes, methods, Decide on libraries xml-parsing, images Error messages!! Create a black image Read in xml scene Compute intersections Use only object color Compute shading ambient, diffuse, specular Test on PC labs Include readme!! compile command If it might take longer ;) Magdalena(Schwarzl 27
Questions Any questions? Magdalena(Schwarzl 28