INFOGR – Computer Graphics
Jacco Bikker - April-July 2016 - Lecture 3: “Ray Tracing (Introduction)”
Welcome! Todays Agenda: Primitives (contd.) Ray Tracing - - PowerPoint PPT Presentation
INFOGR Computer Graphics Jacco Bikker - April-July 2016 - Lecture 3: Ray Tracing (Introduction) Welcome! Todays Agenda: Primitives (contd.) Ray Tracing Intersections Assignment 2 Textures INFOGR
Jacco Bikker - April-July 2016 - Lecture 3: “Ray Tracing (Introduction)”
INFOGR – Lecture 2 – “Graphics Fundamentals” 3
Implicit curves: 𝑔 𝑦, 𝑧 = 0 Circle: 𝑦2 + 𝑧2 − 𝑠2 = 0 Line: 𝐵𝑦 + 𝐶𝑧 + 𝐷 = 0 Slope-intersect form of a line: 𝑧 = 𝑏𝑦 + 𝑑 Normal of line 𝐵𝑦 + 𝐶𝑧 + 𝐷 = 0: 𝑂 = 𝐵 𝐶 Distance of line 𝐵𝑦 + 𝐶𝑧 + 𝐷 = 0 to the origin: |𝐷| (if 𝑂 = 1).
INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 4
Parametric representation
Parametric curve: 𝑦 𝑧 = (𝑢) ℎ(𝑢) Example: line 𝑞0 = 𝑦𝑞0, 𝑧𝑞0 , 𝑞1 = (𝑦𝑞1, 𝑧𝑞1) 𝑦 𝑧 = 𝑦𝑞0 𝑧𝑞0 + 𝑢 𝑦𝑞1 − 𝑦𝑞0 𝑧𝑞1 − 𝑧𝑞0 Or 𝑞 𝑢 = 𝑞0 + 𝑢 𝑞1 − 𝑞0 , 𝑢 ∈ ℝ. p0 p1 In this example: 𝑞0 is the support vector; 𝑞1 − 𝑞0 is the direction vector. INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 5
Slope-intercept:
𝑧 = 𝑏𝑦 + 𝑑 Implicit representation: −𝑏𝑦 + 𝑧 − 𝑑 = 0 𝐵𝑦 + 𝐶𝑧 + 𝐷 = 0 Parametric representation: 𝑞 𝑢 = 𝑞0 + 𝑢 𝑞1 − 𝑞0 p0 p1 ∆𝑦 ∆𝑧
INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 6
Circle - parametric
𝑦 𝑧 = 𝑦𝑑 + 𝑠 cos 𝜚 𝑧𝑑 + 𝑠 sin 𝜚
𝜚 = “phi”
𝜚 c 𝜚 𝑠 𝑦 𝑧 cos 𝜚 = 𝑦 𝑠 𝑡𝑗𝑜 𝜚 = 𝑧 𝑠 𝑢𝑏𝑜 𝜚 = 𝑧 𝑦
adjacent
INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 7
Circle – sphere (implicit)
Recall: the implicit representation for a circle with radius 𝑠 and center 𝑑 is: 𝑦 − 𝑑𝑦 2 + 𝑧 − 𝑑𝑧 2 − 𝑠2 = 0
In ℝ3, we get: 𝑦 − 𝑑𝑦
2 + 𝑧 − 𝑑𝑧 2 + 𝑨 − 𝑑𝑨 2 − 𝑠2 = 0
c 𝑞
INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 8
Line – plane (implicit)
Recall: the implicit representation for a line is: 𝐵𝑦 + 𝐶𝑧 + 𝐷 = 0 In ℝ3, we get a plane: 𝐵𝑦 + 𝐶𝑧 + 𝐷𝑨 + 𝐸 = 0 p1 p2
INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 9
Parametric surfaces
A parametric surface in ℝ3 needs two parameters: 𝑦 = 𝑔(𝑣, 𝑤), 𝑧 = (𝑣, 𝑤), 𝑨 = ℎ(𝑣, 𝑤). For example, a sphere: 𝑦 = 𝑠 cos 𝜚 sin 𝜄, 𝑧 = 𝑠 sin 𝜚 sin 𝜄, 𝑨 = 𝑠 cos 𝜄. Doesn’t look very convenient (compared to the implicit form), but it will prove useful for texture mapping. 𝜚 𝜄
INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 10
Parametric planes
Recall the parametric line definition: 𝑞 𝑢 = 𝑞0 + 𝑢 𝑞1 − 𝑞0 For a plane, we need to parameters: 𝑞 𝑡, 𝑢 = 𝑞0 + 𝑡 𝑞1 − 𝑞0 + 𝑢(𝑞2 − 𝑞0)
𝑞 𝑡, 𝑢 = 𝑞0 + 𝑡 𝑤 + 𝑢𝑥 where:
vectors on the plane;
y
z
x
𝑤 𝑥 𝑞0
INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 11
PART 1: Introduction (today) PART 2: Shading (May 10) PART 3: Reflections, refraction, absorption (May 17) PART 4: Path Tracing (June 21)
INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 14
Ray Tracing: World space
Light transport
Light transport INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 15
Ray Tracing: World space
Light transport
Light transport INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 16
INFOGR – Lecture 8 – “Ray Tracing”
Ray Tracing: World space
Light transport
Light transport Note: We are calculating light transport backwards. INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 18
INFOGR – Lecture 8 – “Ray Tracing”
INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 20
INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 21
Physical basis
Ray tracing uses ray optics to simulate the behavior of light in a virtual environment. It does so by finding light transport paths:
At each surface, the light is modulated. The final value is deposited at the pixel (simulating reception by a sensor). INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 22
Ray definition
A ray is an infinite line with a start point: 𝑞(𝑢) = 𝑃 + 𝑢𝐸, where 𝑢 > 0.
struct Ray { float3 O; // ray origin float3 D; // ray direction float t; // distance };
The ray direction is generally normalized. INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 24
Ray setup
A ray is initially shot through a pixel on the screen plane. The screen plane is defined in world space: Camera position: E = (0,0,0) View direction: 𝑊 Screen center: C = 𝐹 + 𝑒𝑊 Screen corners: p0 = 𝐷 + −1, −1,0 , 𝑞1 = 𝐷 + 1, −1,0 , 𝑞2 = 𝐷 + (−1,1,0) From here:
INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 25
Ray setup
Point on the screen: 𝑞 𝑣, 𝑤 = 𝑞0 + 𝑣 𝑞1 − 𝑞0 + 𝑤(𝑞2 − 𝑞0) Ray direction (before normalization): 𝐸 = 𝑞 𝑣, 𝑤 − 𝐹 Ray origin: 𝑃 = 𝐹 𝑞0 𝑞1 𝑞2 𝐹 INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 26
𝑞2 − 𝑞0
Ray intersection
Given a ray 𝑞(𝑢) = 𝑃 + 𝑢𝐸, we determine the smallest intersection distance 𝑢 by intersecting the ray with each of the primitives in the scene. Ray / plane intersection: Plane: p ∙ 𝑂 + 𝑒 = 0 Ray: 𝑞(𝑢) = 𝑃 + 𝑢𝐸 Substituting for 𝑞(𝑢), we get 𝑃 + 𝑢𝐸 ∙ 𝑂 + 𝑒 = 0 𝑢 = −(𝑃 ∙ 𝑂 + 𝑒)/(𝐸 ∙ 𝑂) 𝑄 = 𝑃 + 𝑢𝐸 𝑞0 𝑞1 𝑞2 𝐹 INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 27
Ray intersection
Ray / sphere intersection: Sphere: 𝑞 − 𝐷 ∙ 𝑞 − 𝐷 − 𝑠2 = 0 Ray: 𝑞(𝑢) = 𝑃 + 𝑢𝐸 Substituting for 𝑞(𝑢), we get 𝑃 + 𝑢𝐸 − 𝐷 ∙ 𝑃 + 𝑢𝐸 − 𝐷 − 𝑠2 = 0 𝐸 ∙ 𝐸 𝑢2 + 2𝐸 ∙ 𝑃 − 𝐷 𝑢 + (𝑃 − 𝐷)2−𝑠2 = 0 𝑏𝑦2 + 𝑐𝑦 + 𝑑 = 0 → 𝑦 = −𝑐 ± 𝑐2 − 4𝑏𝑑 2𝑏 𝑏 = 𝐸 ∙ 𝐸 𝑐 = 2𝐸 ∙ (𝑃 − 𝐷) 𝑑 = 𝑃 − 𝐷 ∙ 𝑃 − 𝐷 − 𝑠2 𝑞0 𝑞1 𝑞2 𝐹 Negative: no intersections INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 28
Ray Intersection
Efficient ray / sphere intersection:
void Sphere::IntersectSphere( Ray ray ) { vec3 c = this.pos - ray.O; float t = dot( c, ray.D ); vec3 q = c - t * ray.D; float p2 = dot( q, q ); if (p2 > sphere.r2) return; t -= sqrt( sphere.r2 – p2 ); if ((t < ray.t) && (t > 0)) ray.t = t; // or: ray.t = min( ray.t, max( 0, t ) ); }
Note: This only works for rays that start outside the sphere. O 𝐸 𝑑 t 𝑟 𝑞2
INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 29
Deadline assignment 1:
Wednesday May 11, 23.59 Assignment 2: ”Write a basic ray tracer.”
INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 31
Assignment 2: ”Write a basic ray tracer.” Steps:
looking at (0,0,-1).
Sphere and a Plane class
ray for each pixel
pixel
INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 32
For y = 0, visualize every 10th ray Visualize the intersection points
Extra points:
meeting the minimum requirements. INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 33
Official:
available today from the website.
Thursday June 2nd and on the website. INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 34
INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 36
Texturing a Plane
Given a plane: 𝑧 = 0 (i.e., with a normal vector (0,1,0) ). Two vectors on the plane define a basis: 𝑣 = (1,0,0) and 𝑤 = (0,0,1). Using these vectors, any point on the plane can be reached: 𝑄 = λ1𝑣 + λ2 𝑤. We can now use λ1, λ2to define a color at P: 𝐺(λ1, λ2) = ⋯ . 𝑣 𝑤 P
Example: 𝐺(λ1, λ2) = sin(λ1) Another example: 𝐺(λ1, λ2) = ( int (2 ∗ λ1) + (int)λ2) & 1 INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 37
Other examples (not explained here): Perlin noise Details: http://www.noisemachine.com/talk1 Voronoi / Worley noise Details: “A cellular texture basis function”, S. Worley, 1996. INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 38
INFOGR – Lecture 3 – “Geometry”
Obviously, not all textures can be generated procedurally. For the generic case, we lookup the color value in a pixel buffer. 𝑦 𝑧 = 𝑄 ∙ 𝑣 𝑄 ∙ 𝑤 Note that we find the pixel to read based on 𝑄; we don’t find a ‘𝑄’ for every pixel of the texture. 𝑣 𝑤 P ∗ 𝑈𝑥𝑗𝑒𝑢ℎ 𝑈ℎ𝑓𝑗ℎ𝑢
255 255
INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 40
Retrieving a pixel from a texture: 𝑦 𝑧 = 𝑄 ∙ 𝑣 𝑄 ∙ 𝑤 ∗ 𝑈𝑥𝑗𝑒𝑢ℎ 𝑈ℎ𝑓𝑗ℎ𝑢 We don’t want to read outside the texture. To prevent this, we have two options:
Tiling is efficiently achieved using a bitmask. This requires texture dimensions that are a power of 2.
255 255
𝑦 𝑧 = 𝑑𝑚𝑏𝑛𝑞(𝑄 ∙ 𝑣, 0, 1) 𝑑𝑚𝑏𝑛𝑞(𝑄 ∙ 𝑤, 0,1) ∗ 𝑈𝑥𝑗𝑒𝑢ℎ 𝑈ℎ𝑓𝑗ℎ𝑢 𝑦 𝑧 = 𝑔𝑠𝑏𝑑(𝑄 ∙ 𝑣) 𝑔𝑠𝑏𝑑(𝑄 ∙ 𝑤) ∗ 𝑈𝑥𝑗𝑒𝑢ℎ 𝑈ℎ𝑓𝑗ℎ𝑢 INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 41
Texture mapping: oversampling INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 42
Texture mapping: undersampling INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 43
Fixing oversampling
Oversampling: reading the same pixel from a texture multiple times. Symptoms: blocky textures. Remedy: bilinear interpolation: Instead of clamping the pixel location to the nearest pixel, we read from four pixels. 𝑥𝑞1 : (1 − 𝑔𝑠𝑏𝑑(𝑦)) ∗ (1 − 𝑔𝑠𝑏𝑑(𝑧)) 𝑥𝑞2 : 𝑔𝑠𝑏𝑑 𝑦 ∗ (1 − 𝑔𝑠𝑏𝑑 𝑧 ) 𝑥𝑞3 : 1 − 𝑔𝑠𝑏𝑑 𝑦 ∗ 𝑔𝑠𝑏𝑑(𝑧) 𝑥𝑞4 : 1 − (𝑥𝑄1 + 𝑥𝑄2 + 𝑥𝑄3) INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 44
Fixing oversampling
INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 45
Fixing undersampling
Undersampling: skipping pixels while reading from a texture. Symptoms: Moiré, flickering, noise. Remedy: MIP-mapping. The texture is reduced to 25% by averaging 2x2 pixels. This is repeated until a 1x1 image remains. When undersampling occurs, we switch to the next MIP level. INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 46
INFOGR – Lecture 3 – “Geometry”
INFOGR – Lecture 3 – “Geometry”
Trilinear interpolation: blending between MIP levels. INFOGR – Lecture 3 – “Ray Tracing (Introduction)” 49
Jacco Bikker - April-July 2016 - Lecture 3: “Ray Tracing (Introduction)”
next lecture: “Ray Tracing (Part 2)”