Computer Graphics 2 Lecture 13: Ray-Tracing Techniques Dr. Benjamin Mora Benjamin Mora 1 University of Wales Swansea Content • What is Ray-Tracing? • Mathematics for ray-tracing intersections. • Common Ray-Tracing effects. • Other Ray-Tracing Algorithms. • Current Ray-Tracing Technology. Benjamin Mora 2 University of Wales Swansea What Is Ray-Tracing ? Benjamin Mora 3 University of Wales Swansea What is Ray Tracing ? • The alternative to rasterization-based methods (like OpenGL or DirectX). – Image-Order algorithm. – Every pixel is processed separately. • Slower than rasterization methods in the case of few primitives to render. • Rarely used in games, widely used for realistic computer graphics. • www.povray.org. Benjamin Mora 4 University of Wales Swansea What is Ray Tracing ? Jaime Vives Piqueres (2004), http://www.povray.org/i/hof/office-13.jpg Benjamin Mora 5 University of Wales Swansea What is Ray Tracing ? Jaime Vives Piqueres (2001), http://www.povray.org/i/hof/office-13.jpg Benjamin Mora 6 University of Wales Swansea What is Ray Tracing ? image plane primary ray view location secondary rays Benjamin Mora 7 University of Wales Swansea What is Ray Tracing ? • Basic (or Naïve )Algorithm (Whitted 1980): FOR every pixel DO FOR every primitive in the scene DO IF there is a ray/primitive intersection test if this is the closest intersection computed so far. If this is the case, keep this primitive as the closest primitive for the next tests. • Basic Complexity: – O(number of rays*number of primitives). – Optimized algorithms can do much better (See the next lectures)! Benjamin Mora 8 University of Wales Swansea Mathematics for Ray-Tracing intersections Benjamin Mora 9 University of Wales Swansea Parametric Ray/Plane Formulation • A ray can be described with a parametric formulation from a (starting) point and a direction Pt P0 t Vdirection • A parametric plane can be described from one point and two vectors: Px, y P0 x Vx y Vy • A plane can also be defined from a point and a normal vector (here P0 and Vz). Vz Vy Vx P0 Benjamin Mora 10 University of Wales Swansea Ray/Plane Intersection • We are looking for the point p(t) such that p(t) belong to the plane h. • h is defined with a point o and a normal n. p0 vdir n p(t) h o • If vdir perpendicular to n, then either 0 solution, or an infinite number of solutions if p0 belongs to h (the vector p0o perpendicular to n). Benjamin Mora 11 University of Wales Swansea Ray/Plane Intersection • Otherwise, there is only one point that is solution. We have to find out t. • The solution comes from the fact that the vector p(t)o is perpendicular to n: p(t )o n 0 (1) o pt . n 0 (2) p0 vdir pt n o n n p(t) p0 t vdir n o n h o (4) t vdir n o n p0 n p0 o n t Benjamin Mora (3) 12 p0 o n vdir n (5) (6) University of Wales Swansea Ray/Cube Intersection txmin txmax p(t) = p0 +v•t p0 tymax tymin v tymin tymax t txmin txmax The ray intersects the cell only if max(txmin, tymin)<min(txmax, tymax). Finding out the exit face consists of finding out minimum of (txmax, tymax). Benjamin Mora 13 the University of Wales Swansea Ray/Cube Intersection • A cube can be defined with 6 planes (faces). • 3 pairs of opposite faces (let’s reference them as x,y,z) . • A parametric ray p(t) intersects 2 opposite faces at 2 locations, with 2 respective parameters tmin and tmax. (let’s suppose tmin<=tmax). • The 6 faces are intersected with 6 different parameters (txmin,txmax, tymin,tymax, tzmin,tzmax). • The ray intersects the cube only if: Max (txmin,tymin,tzmin)< Min(txmax,tymax,tzmax). Benjamin Mora 14 University of Wales Swansea Ray/Sphere Intersection • A sphere can be defined by a center o and a radius r. • Looking for the points p(t) such as: norm(p(t)o)=r. p0 vdir p(t) r o (1) p(t )o 2 r 2 (2) o p0 t vdir p0 o t vdir r 2 2 2 (3) t 2 vdir t 2 p0 o vdir p0 o 2 r 2 0 2 Benjamin Mora 15 University of Wales Swansea Ray/Triangle Intersections • Several ways to achieve this goal. – Badouel’s Algorithm. – Moller’s Algorithm (not shown). – Other Algorithm (close to Segura et al. 98). p0 vdir p(t) Benjamin Mora 16 University of Wales Swansea Badouel’s Algorithm • First, find the intersection p(t) between the ray and the plane containing the triangle. • Then use the barycentric coordinates of the point to sort the intersection out (2D algorithm). p2 p(t) p0 pt . p0 p1 . p0 p2 0, 0, 1 p1 xt x0 x1 x0 ( x2 x0 ) yt y0 y1 y0 ( y2 y0 ) zt z0 z1 z0 ( z2 z0 ) p0 Benjamin Mora 17 University of Wales Swansea Other Ray/Triangle Algorithms • The edges of the triangles can be considered as oriented. • The Ray should always be “on the same side” of the edges • => Three triple products with the same sign. p vdir p0 p1 p(t) p2 Segura, R.J., Feito, F.R. Segura, R.J., Feito, F.R.: An algorithm for determining intersection segment-polygon in 3D. Computer & Graphics, Vol.22, No.5, pp.587-592, 1998. Benjamin Mora 18 University of Wales Swansea Other Ray/Triangle Algorithms • There is an intersection Ray/Triangle only if: Sign pp0 pp1 v Sign pp1 pp2 v Sign pp2 pp0 v p vdir p0 • Fast algorithm. p2 p1 p(t) – Can take advantage of SIMD instructions on processor. • But: – Only in order to detect an intersection. – Extra work must be done in order to get the tri-linear interpolating weights (including a div) and the real intersection location if needed (but not always required). Benjamin Mora 19 University of Wales Swansea Triangle/Box Intersection • Why? – Bounding boxes are used much to speedup intersections. • Standalone boxes. • Spatial subdivision techniques. – Therefore needed to construct such acceleration structures. 20 Benjamin Mora University of Wales Swansea Triangle/Box Intersection • Solution 1: Working in the triangle plane. • For every face of the cube: – Find the face/triangle plane intersection. • A line! – Clip the triangle/polygon with this line. (Sutherland ). • Intersection occurs if result not NULL! • Complex and Costly. Benjamin Mora 21 University of Wales Swansea Triangle/Box Intersection • Solution 2: Use the Separating Axis Theorem. (Akenine-Moller) • No intersection occurs between 2 convex objects if there exists a separating plane. Benjamin Mora 22 University of Wales Swansea Triangle/Box Intersection (2D) Benjamin Mora 23 University of Wales Swansea Triangle/Box Intersection (2D) Benjamin Mora 24 University of Wales Swansea Triangle/Box Intersection (3D) • 13 planes to be tested in 3D. • For each plane: – Compute distances to the triangle vertices • Using the plane equation. – Compute distances to the box vertices. – No intersection if MIN of one distance set greater than MAX of the other distance set. • If condition is false for all 13 cases, an intersection exists. Benjamin Mora 25 University of Wales Swansea Triangle/Box Intersection (3D) • Plane directions are determined from: – Ei X Tj Where Ei are the directions of the edges of the box, and Tj the directions of the edges of the triangle. (9 cases) – The faces of the box. (3 cases) – The triangle plane normal. (1 case) Benjamin Mora 26 University of Wales Swansea Simple Ray-Tracing Effects Benjamin Mora 27 University of Wales Swansea Simple Ray-Tracing Effects • The main operation is to find an intersection from a given ray. • This operation can be used (by casting secondary rays) to compute simple effects that are more difficult to implement with OpenGL like (but fog): – Reflection. – Refraction. – Shadows. Source: Min Chen – Fog. Benjamin Mora 28 University of Wales Swansea Simple Ray-Tracing Effects • Basic Lighting model: object colour I I object + result of tracing reflective ray result of tracing + refractive ray Rin km I reflection kt I refraction Rfl Benjamin Mora km 29 kt Rfr University of Wales Swansea Simple Ray-Tracing Effects • Shading can be done in the same way as to OpenGL. – But better methods exist. N V (-Rin) R L Source: Min Chen I I a ka f d I p kd L N k s V R n Benjamin Mora 30 University of Wales Swansea Simple Ray-Tracing Effects • Reflection: Benjamin Mora 31 University of Wales Swansea Simple Ray-Tracing Effects • Reflection: N Vincident Vreflected i i L Vreflected Vincident 2 N N Vincident , N 1 Benjamin Mora 32 University of Wales Swansea Simple Ray-Tracing Effects • Refraction: R fr (ri cos i cos r ) N riV N V (-Rin) R Snell’s Law sin r i ir sin i r Benjamin Mora Rfl i i r 33 L Rfr University of Wales Swansea Simple Ray-Tracing Effects • Total Internal Reflection: N V (-Rin) critical angle critical Rfr Benjamin Mora 34 University of Wales Swansea Simple Ray-Tracing Effects • Taking into account reflection and refraction several times (recursively) requires an exponential number of rays! • The algorithm should stop after reaching a maximum number of consecutive reflections/refractions. • Only reduced to some specific lighting effects. Benjamin Mora 35 University of Wales Swansea Simple Ray-Tracing Effects Benjamin Mora 36 University of Wales Swansea Simple Ray-Tracing Effects • Fog effect (also possible with OpenGL). – Depth Cue: – Fog: I out I in 1 f I fog f 0 d Dmin f Dmax Dmin 1 d Dmin Dmin d Dmax d Dmax or f 1 Benjamin Mora 1 dD e 37 University of Wales Swansea Simple Ray-Tracing Effects • Super Sampling can be used in order to improve Anti-Aliasing. non-adaptive supersampling Benjamin Mora adaptive supersampling 38 stochastic sampling University of Wales Swansea Simple Ray-Tracing Effects Benjamin Mora 39 University of Wales Swansea Simple Ray-Tracing Effects • The final color is the average of all the sub-sampled rays. • Uniform Super-Sampling: – Nice but not always needed. • Adaptive Super-Sampling: – Useful for the regions with large color transitions (high frequencies), like object boundaries. – Trade-off between quality and speed. • Stochastic Super-Sampling: – Take advantage of the visual system limitations. Benjamin Mora 40 University of Wales Swansea Simple Ray-Tracing Effects • Stochastic Super-Sampling: – Proposed by Robert L. Cook. • Stochastic Sampling in Computer Graphics, ACM transaction on Graphics No. 5(1), January 1986, pp. 51-72. – The ray location(s) are randomly chosen inside a pixel. – Our visual system is less sensitive to noise than to contours. Benjamin Mora 41 University of Wales Swansea Other Ray-Tracing Algorithms. Benjamin Mora 42 University of Wales Swansea Other Ray-Tracing Algorithms. • Pyramidal Ray-Tracing. • Cone Tracing. • Beam Tracing. Benjamin Mora 43 University of Wales Swansea Pyramidal Ray-Tracing. • Proposed by: Whitted, Turner. An Improved Illumination Model for Shaded Display. C.A.C.M. vol. 23, no. 6, June 1980, pp. 343-3~19. • And improved by: J. Genetti, D. Gordon and G. Williams. Adaptive supersampling in object space using pyramidal rays. Computer Graphics Forum 17 (1998) 29-54. • Main Idea: Replacing Rays by Pyramids to improve aliasing. image plane primary ray view location Benjamin Mora 44 University of Wales Swansea Pyramidal Ray-Tracing. J. Genetti, D. Gordon and G. Williams. Adaptive supersampling in object space using pyramidal rays. Computer Graphics Forum 17 (1998) 29-54. The triangle will contribute here to 7/64 of the final pixel colour. Benjamin Mora 45 University of Wales Swansea Pyramidal Ray-Tracing. • If a pyramid only partially intersects a graphics primitive (e.g., triangle), then the pyramid is subdivided until a given depth is reached. – The final pixel value is the (weighted) average of all the samples inside the triangle. – Non-intersecting pyramids keep on computing intersections with the rest of the scene. Benjamin Mora 46 University of Wales Swansea Cone Tracing. • Proposed by: John Amanatides. Ray Tracing with Cones. Siggraph 1984, pp. 129-135. image plane primary ray view location Partial intersection Benjamin Mora 47 University of Wales Swansea Cone Tracing. • Similar to pyramidal ray-tracing, but the pyramid is now replaced by a cone. – Allows easier intersection computations. – 1 cone per pixel, no subdivision. • For a ray-triangle intersection: – The cone is projected onto the triangle plane. • Circular projection. – If there is a partial intersection detected, the colour of the triangle is weighted by the corresponding covering percentage, and computing intersections will carry on. Benjamin Mora 48 University of Wales Swansea Cone Tracing: Result Benjamin Mora 49 University of Wales Swansea Beam Tracing. • Proposed by: Paul S. Heckbert and Pat Hanrahan. BEAM TRACING POLYGONAL OBJECTS. Siggraph 1984, pp. 119-127. • Takes advantages of spatial coherency between rays to group them and save computations. • Rays are distributed after Reflections and Refractions according to the reflection/refraction coefficients. • Constructs a beam tree. • Outdated. Benjamin Mora 50 University of Wales Swansea Current Ray-Tracing Technology Benjamin Mora 51 University of Wales Swansea Current Ray-Tracing Technology • Ray-Tracing is now used a lot for realistic rendering of scenes. – Not just Reflections/Refractions. • Has a better complexity than OpenGL based renderings (when optimized). – Scalable. • A simple ray representation (no cone or pyramid) is often used. • Therefore, researchers have recently tried to hardware accelerate it. Benjamin Mora 52 University of Wales Swansea Current Ray-Tracing Technology • 3 different approaches: – Cluster based CPU approach. • Use SIMD instructions with packed rays. • A lot of threads in parallel. Ingo Wald, Timothy J. Purcell, Jörg Schmittler, Carsten Benthin and Philipp Slusallek. Realtime Ray Tracing and its use for Interactive Global Illumination. Eurographics 2003 State of the Art Report. – GPU Based. • Not very efficient because not really designed for RT. Tim Foley, Jeremy Sugerman. KD-Tree Acceleration Structures for a GPU Raytracer. In Proceedings of Graphics Hardware 2005. – Hardware (FPGA) accelerated ray-tracing. • Seems to be efficient. • Need to compute a spatial subdivision tree first, which is not really appropriate to dynamic scenes. University of Wales Benjamin Mora 53 Swansea Current Ray-Tracing Technology • The RPU unit. See Video !!! Sven Woop, Jorg Schmittler, and Philipp Slusallek. RPU: A Programmable Ray Processing Unit for Realtime Ray Tracing. Siggraph 2005. Benjamin Mora 54 University of Wales Swansea