Lecture 11 Ray tracing
Introduction Projection vs. ray tracing Projection Ray tracing Rendering
Projection vs. ray tracing Projection Ray tracing Basic methods for image generation Major areas of computer graphics: Modelling ( creating 3d virtual worlds ) Rendering ( creating 2d images from 3d models ) Animation ( creating motion from sequences of 2d images ) Two basic methods for rendering: Projective methods Ray tracing
Projection vs. ray tracing Projection Ray tracing Projective methods A popular method for generating images from a 3D-model is projection, e.g.: 3D triangles project to 2D triangles Project vertices Fill/shade 2D triangle Q: what if two or more triangles project onto the same pixels?
Projection vs. ray tracing Projection Ray tracing Projective methods Used by OpenGL and DirectX Very fast: hardware supported Real-time applications (games) You will do this in the practicals this year
Projection vs. ray tracing Projection Ray tracing Ray tracing / ray casting For photo-realistic rendering, usually ray tracing algorithms are used: for every pixel Compute ray from viewpoint through pixel center Determine intersection point with first object hit by ray Calculate shading for the pixel (possibly with recursion)
Projection vs. ray tracing Projection Ray tracing Ray tracing / ray casting Global Illumination Traditionally (very) slow Recent developments: real-time ray tracing This was done in last year s practicals
Projection vs. ray tracing Projection Ray tracing Examples: landscapes (www.terragen.org)
Projection vs. ray tracing Projection Ray tracing Examples: office (www.povray.org)
Projection vs. ray tracing Projection Ray tracing Examples: last year s practicals This year we will do shader programming
Projective methods vs. ray tracing Projective methods: Object-order rendering, i.e. For each object...... find and update all pixels that it influences Ray tracing: Image-order rendering, i.e. For each pixel...... find all objects that influence it and update it accordingly
A basic ray tracing algorithm FOR each pixel DO find 1st object hit by ray and surface normal n set pixel color to value computed from hit point, light, and n
Lines and rays Introduction We need to shoot a ray from the view point e through a pixel s on the screen towards the scene/objects Hmm, that should be easy with...
Lines and rays Introduction... a parametric line equation: where p(t) = e + t( s e) e is a point on the line (aka its support vector) s e is a vector on the line (aka its direction vector)
Lines and rays Introduction With this, our ray... starts at e (t = 0), goes throught s (t = 1), and shoots towards the scene/objects (t > 1) Hmm, calculation would become much easier if we would have...
Coordinate system Introduction... a camera coordinate system: That s easy! Using our camera position e our viewing direction w and a view up vector t we get u = w t v = w u
Coordinate system Introduction... a camera coordinate system: That s easy! Using our camera position e our viewing direction w and a view up vector t we get u = w t v = w u
Coordinate system Introduction... a camera coordinate system: That s easy! Using our camera position e our viewing direction w and a view up vector t we get u = w t v = w u
Coordinate system Introduction... a camera coordinate system: That s easy! Using our camera position e our viewing direction w and a view up vector t we get u = w t v = w u
Coordinate system Introduction Normalizing, i.e. w/ w u/ u v/ v gives us our coordinate system. Q: why did we chose w as viewing direction and not w?
Viewing window Introduction With this new coordinate system we can easily define our viewing window: left side: u = l right side: u = r top: v = t bottom: v = b
Viewing window Introduction Assuming our window has n x n y pixels, this becomes a simple window transformation from (r l) (t b) to n x n y. Hence, we can express a pixel at position (i, j) in our new coordinate system as (u, v) with u = l+(r l)(i+0.5) n x v = b+(t b)(j+0.5) n y
Viewing rays Introduction For orthographic views, viewing rays have the same direction w but different origin We get the origin with the previously introduced mapping from (i, j) to (u, v): u = l+(r l)(i+0.5) n x v = b+(t b)(j+0.5) n y and can write it as e + u u + v v
Viewing rays Introduction For perspective views, viewing rays have the same origin e but different direction If d denotes the origin s distance to the plane, and u, v are calculated as before, we can write the direction as d w + u u + v v.
A basic ray tracing algorithm FOR each pixel DO find 1st object hit by ray and surface normal n set pixel color to value computed from hit point, light, and n
Ray-object intersection (implicit surface) In general, the intersection points of a ray p(t) = e + td and an implicit surface f( p) = 0 can be calculated by f( p(t)) = 0 or f( e + td) = 0
Spheres Introduction The implicit equation for a sphere with center c = (x c, y c, z c ) and radius R is (x x c ) 2 +(y y c ) 2 +(z z c ) 2 R 2 = 0 or in vector form ( p c) ( p c) R 2 = 0
Intersections between rays and spheres Intersection points have to fullfil the ray equation p(t) = e + t d the sphere equation (x x c ) 2 + (y y c ) 2 +(z z c ) 2 R 2 = 0 Hence, we get ( e + t d c) ( e + t d c) R 2 = 0 which is the same as ( d d)t 2 +2 d ( e c)t+( e c) ( e c) R 2 = 0
Intersections between rays and spheres ( d d)t 2 +2 d ( e c)t+( e c) ( e c) R 2 = 0 is a quadratic equation in t, i.e. At 2 + Bt + C = 0 that can be solved by t 1,2 = B + B 2 4AC 2A and can have 0, 1, or 2 solutions.
Ray-object intersection (parametric surface) The intersection points of a ray p(t) = e + t d and a parametric surface f(u, v) can be calculated by x e + tx d = f(u, v) y e + ty d = f(u, v) z e + tz d = f(u, v) or e + t d = f(u, v) Notice that these are 3 equations with 3 unknowns (t, u, v).
For ray-triangle intersections: We first calculate the intersection point of the ray with the plane defined by the triangle. Then we check if this point is within the triangle or not.
Plane specification Introduction Recall that the plane V through the points a, b, and c can be written as p(β, γ) = a + β( b a) + γ( c a)
Plane specification Introduction Recall that the plane V through the points a, b, and c can be written as p(β, γ) = a + β( b a) + γ( c a)
Plane specification Introduction Recall that the plane V through the points a, b, and c can be written as p(β, γ) = a + β( b a) + γ( c a)
Ray-plane intersection Again, intersection points must fullfil the plane and the ray equation. Hence, we get e + t d = a + β( b a) + γ( c a) That give us...
Ray-plane intersection... the following three equations x e + tx d = x a + β(x b x a ) + γ(x c x a ) y e + ty d = y a + β(y b y a ) + γ(y c y a ) z e + tz d = z a + β(z b z a ) + γ(z c z a ) which can be rewritten as or as (x a x b )β + (x a x c )γ + x d t = x a x e (y a y b )β + (y a y c )γ + y d t = y a y e (z a z b )β + (z a z c )γ + z d t = z a z e x a x b x a x c x d β x a x e y a y b y a y c y d γ = y a y e z a z b z a z c z d t z a z e
Ray-plane intersection If we write x a x b x a x c x d β x a x e y a y b y a y c y d γ = y a y e z a z b z a z c z d t z a z e as then we see that β x a x e A γ = y a y e t z a z e β x a x e γ = A 1 y a y e t z a z e
Rays: parametric representation We can use t to calculate the intersection point p(t) (or β, γ to calculate p(β, γ)). And we can use β and γ to verify if it is inside of the triangle or not: β > 0 γ > 0 β + γ < 1
Ray-object intersection (implicit plane) Of course, we can also get the ray-plane intersection if the latter is given as implicit equation: ( p p 1 ) n = 0 Putting our ray p(t) = e + t d in this equation and solving for t gives us: t = ( p 1 e) n d n
If the plane was defined by a planar polygone, we can easily check if the resulting intersection point p(t) is within the polygone or not. Do you remember how?
A basic ray tracing algorithm FOR each pixel DO find 1st object hit by ray and surface normal n set pixel color to value computed from hit point, light, and n
model Introduction Remember our shading model: c = c r (c a + c l max(0, n l)) + c l ( h n) p with Ambient shading Lambertian shading Phong shading and Gouraud interpolation.
A basic ray tracing algorithm FOR each pixel DO compute viewing ray IF (ray hits an object with t [0, )) THEN Compute n Evaluate shading model and set pixel to that color ELSE set pixel color to background color
Shadows Ideal specular reflection Refraction Shadow feelers Shadows can be implemented fairly easy by so called shadow feelers / rays. Shoot a shadow ray p + t l from a point p towards a light source l. If ray hits object, p is in the shadow. Otherwise it s not. Because of potential precision issues, we often look at point p + ɛ l instead of p.
Shadows Ideal specular reflection Refraction Ideal specular or mirror reflection Key characteristic of a mirror: If looking from direction d to a spot on the reflecting surface, the viewer sees the same image as if looking from the surface point in direction r. Hence, we need to calculate the reflection vector: r = d 2( d n) n Then we shoot a ray in that direction.
Shadows Ideal specular reflection Refraction Ideal specular or mirror reflection
Shadows Ideal specular reflection Refraction Refraction Light traveling from one transparent medium into another one is refracted.
Shadows Ideal specular reflection Refraction Snell s law The angles before and after refraction are related as follows: n sin θ = n t sin φ. where n and n t are the refractive indices of the source and target media, respectively, and θ and φ the angles indicated in the image.
Shadows Ideal specular reflection Refraction Getting rid of sines An equation that relates sines of the angles θ and φ is not as convenient as an equation that relates the cosines of the angles. With the identity sin 2 φ + cos 2 φ = 1 we derive the following equation from Snell s law: cos 2 φ = 1 n2 (1 cos 2 θ) n 2 t
Shadows Ideal specular reflection Refraction Getting rid of sines Snell s law: n sin θ = n t sin φ sin φ = n n t sin θ Trigonometric identity: sin 2 φ + cos 2 φ = 1 And hence cos 2 φ = 1 sin 2 φ sin 2 φ = 1 cos 2 φ
Shadows Ideal specular reflection Refraction Constructing an orthonormal basis How do we find the refracted vector t? Assume the incoming vector d and the normal n are normalized. First, t lies in the plane spanned by d and n. Next, we can set up an orthonormal basis in this plane by picking an appropriate vector b.
Shadows Ideal specular reflection Refraction Finding the refraction vector We have t = b sin φ n cos φ d = b sin θ n cos θ
Shadows Ideal specular reflection Refraction Finding the refraction vector We have t = b sin φ n cos φ d = b sin θ n cos θ So we can solve for b: b = d+ n cos θ sin θ and for t: t = sin φ( d+ n cos θ) sin θ = n( d+ n cos θ) n t = n( d n( d n)) n t n cos φ n cos φ n 1 n2 (1 ( d n) 2 ) n 2 t
Instancing Constructive Solid Geometry Copying and transforming objects Instancing is an elegant technique to place various transformed copies of an object in a scene. Expl.: circle elipse O M 1 O M 3 O M 2 O M 4 O
Instancing Constructive Solid Geometry Copying and transforming objects Instead of making actual copies, we simply store a reference to a base object, together with a transformation matrix. That can save us lots of storage. Hmm, but how do we compute the intersection of a ray with a randomly rotated elipse? O M 1 O M 3 O M 4 O M 2 O
Instancing Constructive Solid Geometry Ray-instance intersection O p M 1 r MO Mp r
Instancing Constructive Solid Geometry Ray-instance intersection To determine the intersection q of a ray r with an instance MO, we first compute the intersection p of the inverse transformed ray M 1 r and the original object O. The point q is then simply M p. O MO p M 1 r Mp This way, complicated intersection tests (e.g. ray/ellipsoid) can often be replaced by simpler tests (ray/sphere). r
Instancing Constructive Solid Geometry Ray-instance intersection Two pitfalls: Surface normals transform differently! use (M 1 ) T instead of M 1 for normals The direction vector of the ray should not be normalized O MO p M 1 r Mp r
Instancing Constructive Solid Geometry Constructive Solid Geometry C S For ray tracing, we can basically use any object that allows us to calculate its intersection with a 3D line. Using Constructive Solid Geometry (CSG), we can build complex objects from simple ones with set operations. C S S C C S C S
Instancing Constructive Solid Geometry Intersections and CSG Big advantage: instead of actually constructing the objects, we can calculate ray-object intersections with the original objects and perform set operations on the resulting intervals. S C C S C S
Instancing Constructive Solid Geometry Intersections and CSG For every base object, we maintain an interval (or set of intervals) representing the part of the ray inside the object. The intervals for combined objects are computed with the same set operations that are applied to the base objects. S C C S C S