fullscreen
timer
qrcode
plickers
selector
edit
reset

whitted Raytracing

COS350 - Computer Graphics

Image Formation via raytrace rendering

Raytracing
a specific rendering algorithm that simulates light interacting with scene (geometry+materials) and enterring camera

Raytracing algorithm

Use Whitted-style ray tracing algorithm to generate image


for each pixel {
    determine viewing direction
    intersect ray with scene
    compute illumination
    store result in pixel
}

Viewing

for each pixel {
    determine viewing direction <==
    intersect ray with scene
    compute illumination
    store result in pixel
}

Viewer Model

a painter tracing objects on a canvas in front

[ Marschner 2004–original unknown ]

Viewer Model

simplify to pinhole photography, move photo film / sensor in front of focal point

[ Marschner 2004–original unknown ]

Viewer Model – Parameters

View frustum

Note: all visible points within a truncated pyramid

Generating view rays

Generating view rays

convert pixel coordinates \((x,y)\) to image plane coordinates \((u,v)\)

\[ u = \frac{x + 0.5}{w} \] \[ v = 1 - \frac{y + 0.5}{h} \]

Generating view rays

compute point \(\point{q}\) at center of current pixel \((x,y)\)

Generating view rays

computer ray that starts at camera focal point \(\point{o}\) and passes through pixel center \(\point{q}\)

Generating view rays

\[ \tilde\o = 0,\quad \hat\x = (1,0,0),\quad \hat\y = (0,1,0),\quad \hat\z = (0,0,1) \]

\[\begin{array}{rcl} \tilde\q(u,v) & = & \tilde\o + (u-0.5) w \hat\x + (v-0.5) h \hat\y - d \hat\z \\ & = & 0 + w (u-0.5) (1,0,0)\, + \\ & & +\, h (v-0.5) (0,1,0) - d (0,0,1) \\ & = & (w * (u-0.5), h * (v-0.5), -d) \end{array}\]

Generating view rays

pixel center:

\[ \begin{array}{rcl} \tilde\q'(u,v) & = & O + (u-0.5) w \hat\x' + (v-0.5) h \hat\y' - d\hat\z' \\ & = & \mat{ (u-0.5)w & (v-0.5)h & -d }^T \end{array} \]

notes:

Generating view rays

view ray:

\[ \begin{array}{rcl} \tilde\p'(t) & = & \tilde\e + t \hat\d \\ & = & O + t (\tilde\q - O) / || \tilde\q - O || \\ & = & t \hat\q \end{array} \]

note: last line abuses notation by mixing up points and vectors

Intersection

for each pixel {
    determine viewing direction
    intersect ray with scene    <==
    compute illumination
    store result in pixel
}

Geometry Model

Ray-Shape Intersection

Ray-Sphere Intersection

point on a ray: \(\tilde\p(t) = \tilde\e + t\hat\d\)

point on a sphere: \(|| \tilde\p(t)-\tilde\c || = r\)

by substitution: \( || \tilde\e + t\hat\d - \tilde\c || = r \)

Ray-Sphere Intersection

algebraic equation: \(a t^2 + b t + c = 0\)

with: \(a = ||\hat\d||^2\), \(b=2 \hat\d \* (\tilde\e-\tilde\c)\), \(c = ||\tilde\e-\tilde\c||^2 - r^2\)

determinant: \(d = b^2 - 4ac\)

no solution for \(d < 0\)

Ray-Sphere Intersection

two solutions

\[t_{\pm} = \frac{-b \pm \sqrt{d}}{2a}\,,\quad d=b^2-4ac\]

pick smallest \(t\) such that \(t \in ( t_{min}, t_{max} )\)

Ray-Sphere Intersection

shading frame \(\frame{f} = \{ \point{o}, \direction{x}, \direction{y}, \direction{z} \}\)

where


attributes (ex: color or uv) can be interpolated at \(\point{p}(t)\) using \(\theta\), \(\phi\)

\[ u = 1 - \frac{\phi}{\pi} \qquad v = 0.5 + \frac{\theta}{2 \pi} \]

Ray-Plane Intersection

point on a ray: \(\tilde\p(t) = \tilde\e + t\hat\d\)

point on a plane: \((\tilde\p(t)-\tilde\c) \* \hat\n = 0\)

by substitution: \( (\tilde\e + t\hat\d -\tilde\c) \* \hat\n = 0 \)

Ray-Plane Intersection

one solution for \(\hat\d \* \hat\n \neq 0\), no/infinite solutions otherwise

\[t = \frac{(\tilde\c - \tilde\e) \* \hat\n}{\hat\d \* \hat\n}\]

check that \(t \in ( t_{min}, t_{max} )\)

Ray-Plane Intersection

shading frame \(\frame{f} = \{ \point{o}, \direction{x}, \direction{y}, \direction{z} \}\)

Ray-Disk Intersection

Ray-Disk intersection uses Ray-Plane intersection with additional condition

Ray-Quad Intersection

Ray-Quad intersection uses Ray-Plane intersection with additional condition

to handle rotated quad, compute distance of \(\point{p}(t)\) from origin in the local frame (i.e., transform \(\point{p}(t)\) to local frame, \(\point{p}'(t)\))

attributes (ex: color or uv) can be interpolated at \(\point{p}(t)\) using \(\point{p}'(t)\)

\[ u = 0.5 + \frac{p_x'}{2r} \qquad v = 0.5 + \frac{p_y'}{2 r} \]

Ray-Triangle Intersection

point on ray: \(\tilde\p(t) = \tilde\e + t\hat\d\)

point on triangle: \(\tilde\p(\alpha,\beta) = \alpha(\tilde\a-\tilde\c) + \beta(\tilde\b-\tilde\c) + \tilde\c\)

by substitution: \(\tilde\e + t\hat\d = \alpha(\tilde\a-\tilde\c) + \beta(\tilde\b-\tilde\c) + \tilde\c\)

Ray-Triangle Intersection

\[ \tilde\e + t\hat\d = \alpha(\tilde\a-\tilde\c) + \beta(\tilde\b-\tilde\c) + \tilde\c \rightarrow \]

\[ \alpha(\tilde\a-\tilde\c) + \beta(\tilde\b-\tilde\c) - t\hat\d = \tilde\e - \tilde\c \rightarrow \]

\[ \alpha\a' + \beta\b' - t\hat\d = \e' \rightarrow \]

\[ \mat{{-\hat\d} & \a' & \b'} \mat{ t \\ \alpha \\ \beta } = \e' \]

Ray-Triangle Intersection

use Cramer's rule

\[ t = \dfrac{\matdet{\e' & \a' & \b'}}{\matdet{{-\hat\d} & \a' & \b'}} = \dfrac{(\e' \xx \a') \* \b'}{(\hat\d \xx \b') \* \a'} \]

\[ \alpha = \dfrac{\matdet{{-\hat\d} & \e' & \b'}}{\matdet{{-\hat\d} & \a' & \b'}} = \dfrac{(\hat\d \xx \b') \* \e'}{(\hat\d \xx \b') \* \a'} \]

\[ \beta = \dfrac{\matdet{{-\hat\d} & \a' & \e'}}{\matdet{{-\hat\d} & \a' & \b'}} = \dfrac{(\e' \xx \a') \* \hat\d}{(\hat\d \xx \b') \* \a'} \]

test for

\[ t \in (t_{min},t_{max}), \,\,\,\, \alpha \ge 0, \,\,\,\, \beta \ge 0, \,\,\,\, \alpha + \beta \le 1 \]

Ray-Triangle Intersection

shading frame \(\frame{f} = \{ \point{o}, \direction{x}, \direction{y}, \direction{z} \}\)


vertex attributes (ex: color or uv) can be interpolated at \(\point{p}(t)\) using \(\alpha\), \(\beta\), \(\gamma\) as linear weights

\[ c_{\point{p}(t)} = \alpha c_{\point{a}} + \beta c_{\point{b}} + \gamma c_{\point{c}} \]

Intersection and Coord Systems

Intersection and Coord Systems

Image so far

Intersecting many shapes

Intersecting many shapes – pseudocode

maxDistance = infinity
hit = null
foreach surface s {
  if(s.intersect(ray,intersection)) {
    if(intersection.distance < maxDistance) {
      hit = s
      maxDistance = intersection.distance
    }
  }
}

Image so far

×