Computer Graphics

Lighting

Prof. Dr. David Bommes
Computer Graphics Group

Ray Tracing Operators

images/ray-tracing-pipeline.svg images/ray-tracing.svg

Ray Tracing Operators

images/ray-tracing-pipeline-1.svg images/ray-tracing.svg

Ray Equation

  • Explicit equation for ray \(\vec{r}(t)\), starting at origin \(\vec{o}\)
    and going in (normalized) direction \(\vec{d}\):

\[ \vec{r}(t) = \vec{o} + t \, \vec{d} \]

images/ray.svg

Ray Tracing Operators

images/ray-tracing-pipeline-2.svg images/ray-tracing.svg

Ray-Sphere Intersection

  • Implicit representation of a sphere with center \(\vec{c}\) and radius \(r\): \[ \norm{ \vec{x} - \vec{c} } - r = 0 \]

images/ray-sphere-intersection.svg

  • Insert explicit ray equation into implicit sphere equation: \[ \norm{ \vec{o} + t\vec{d} - \vec{c} } - r = 0 \] and solve for \(t\).

Ray-Plane Equation

  • Implicit equation for a plane with normal \(\vec{n}\) and distance \(d\) from origin: \[ \transpose{\vec{n}} \vec{x} - d = 0 \]

images/ray-plane-intersection.svg

  • Insert explicit ray equation into implicit plane equation \[ \transpose{\vec{n}} \left(\vec{o} + t\,\vec{d} \right) - d = 0 \] and solve for \(t\).

Ray-Triangle Intersection (explicit)

  • Ray and triangle have to coincide \[ \vec{o} + t\,\vec{d} \;=\; \alpha\vec{A} + \beta\vec{B} + \gamma\vec{C}\]
    Four unknowns, but only three equations!
images/ray-triangle-intersection.png

  • Exploit condition \(\alpha+\beta+\gamma=1\) to eleminate \(\alpha\) \[ \vec{o} + t\,\vec{d} \;=\; (1-\beta-\gamma)\vec{A} + \beta\vec{B} + \gamma\vec{C}\]
    Solve \(3 \times 3\) linear system (Cramer’s rule)

  • Check inside conditions: \(0 \leq \alpha, \beta, \gamma \leq 1\)

Ray Tracing Operators

images/ray-tracing-pipeline-3.svg images/ray-tracing.svg

Quiz: Pinhole Camera

What happens when we replace a pin-hole by a finite aperture?

  • The image is blurred everywhere.
  • The image is the same.
  • The image is in perfect focus at some depth, but blurred elsewhere.
  • The image is brighter.

Quiz: Pinhole Camera

Which of the following are true for a camera obscura?

  • Two objects of the same size at the same distance from the pinhole will have the same size on the image plane.
  • Moving the image plane further away from the pinhole reduces the view angle.
  • The image on the image plane is up-side down.
  • A camera obscura was first built by the ancient greeks.

Quiz: Vector Stuff

When are two vectors \(\vec{a}\) and \(\vec{b}\) perpendicular?

  • If \(\vec{a} \times \vec{b} = 0\).
    • No, cross product yields another vector.
  • If \(\vec{a} \cdot \vec{b} \neq 0\).
    • No-no-no, that is the opposite of correct.
  • If \(\vec{a} \times \vec{b} \neq 0\).
    • No, this is true whenever \(\vec{a}\) and \(\vec{b}\) are non-parallel.
  • If \(\vec{a} \cdot \vec{b} = 0\).
    • Yes! The dot-product depends on the cosine, and the cosine is zero for 90 degrees.

Quiz: Plane

How many degrees of freedom (DoFs) does a plane in 3D have?

  • Three
    • Yes, that is correct. The normal vector can be represented by two numbers (it’s a 3D vector, but always has unit length!), and the distance to the origin is the 3rd Dof.
  • Four
    • Nope, too many.
  • Five
    • Nope, too many.
  • Six
    • Nope, way too many.

Quiz: Distance to Plane

How to compute the distance of point \(\vec{x}\) from a plane with normal vector \(\vec{n}\) and centered at point \(\vec{c}\)?

  • \(\iprod{\vec{x}-\vec{c}, \vec{n} }\)
    • Yes, that is correct.
  • \(\norm{\vec{x}-\vec{c} } - \vec{n}\)
    • No, this is nonsense.
  • \(\transpose{\vec{n} }\vec{x} - \vec{c}\)
    • No, this is nonsense.
  • \(\vec{x} \cdot \vec{n} - \vec{c} \cdot \vec{n}\)
    • Yes, this one is (also) correct.

Quiz: Barycentric Coordinates

What are the barycentric coordinates \((\alpha, \beta, \gamma)\) of point \(P = \alpha A + \beta B + \gamma C\)?

images/quiz_baryP.svg
  • (0, 0.5, 0.5)
  • (1, 1, 0)
  • (0.5, 0.5, 0)

Quiz: Barycentric Coordinates

What are the barycentric coordinates \((\alpha, \beta, \gamma)\) of point \(Q = \alpha A + \beta B + \gamma C\)?

images/quiz_baryQ.svg
  • (-1, 1, 1)
  • (-1, 0, 2)
  • (0, 1.5, -0.5)

Quiz: Barycentric Coordinates

What are the barycentric coordinates \((\alpha, \beta, \gamma)\) of point \(R = \alpha A + \beta B + \gamma C\)?

images/quiz_baryR.svg
  • (-0.5, 2, -0.5)
  • (1, -1, 1)
  • (0, -1, 2)

Ray Tracing Operators

images/ray-tracing-pipeline-3.svg images/ray-tracing.svg

Lighting is important!

images/spheres-0.pngno illumination images/spheres-2.pnglocal illumination images/spheres-4.pngglobal illumination

Surface Reflectance

  • How much light is leaving point \(\vec{x}\) in direction \(\vec{\omega}_{\mathrm{out}}\)?
images/lighting-brdf-2.svg

Surface Reflectance

  • How much light is leaving point \(\vec{x}\) in direction \(\vec{\omega}_{\mathrm{out}}\)?
images/lighting-brdf-1.svg

  • Collect incoming light \(L_{\mathrm{in}}\) from all directions \(\vec{\omega}_{\mathrm{in}} \in \Omega\) \[ L_{\mathrm{out}}\of{\vec{\omega}_\mathrm{out}} \;=\; \int_{\Omega} \, f(\vec{\omega}_{\mathrm{in}}, \vec{\omega}_{\mathrm{out}}) \, L_{\mathrm{in}}\of{\vec{\omega}_{\mathrm{in}}} \, \cos\of{\theta_{\mathrm{in}}} \, \mathrm{d}\vec{\omega}_{\mathrm{in}} \]

Surface Reflectance

  • How much light coming in from direction \(\vec{\omega}_{\mathrm{in}}\) is reflected out in direction \(\vec{\omega}_{\mathrm{out}}\)?
images/lighting-brdf-0.svg

  • Determined by the object’s BRDF \(f(\vec{\omega}_{\mathrm{in}}, \vec{\omega}_{\mathrm{out}})\)
    • Bidirectional Reflectance Distribution Function
    • General description of an object’s material

Ray Tracing Approximations

  • Most general reflection equation \[ L_{\mathrm{out}}\of{\vec{\omega}_\mathrm{out}} \;=\; \int_{\Omega} \, f(\vec{\omega}_{\mathrm{in}}, \vec{\omega}_{\mathrm{out}}) \, L_{\mathrm{in}}\of{\vec{\omega}_{\mathrm{in}}} \, \cos\of{\theta_{\mathrm{in}}} \, \mathrm{d}\vec{\omega}_{\mathrm{in}} \]
  • Ray Tracing approximates integral by three directions
    • directions toward light sources
    • directions of perfect reflection
    • directions of perfect refraction
  • Phong Lighting approximates BRDF by three components
    • ambient light
    • diffuse reflection
    • specular reflection

Phong Lighting Model

  • Ambient lighting
    • approximate global light transport / exchange
    • uniform in, uniform out
images/spheres-0.png
  • Diffuse lighting
    • dull / matt surfaces
    • directed in, uniform out
images/spheres-1.png
  • Specular lighting
    • shiny surfaces
    • directed in, directed out
images/spheres-2.png

Ambient Light

images/lighting-ambient-1.svg
uniformly incoming light

Ambient Light

images/lighting-ambient-2.svg
uniformly outgoing light

\[ I \;=\; I_a \, m_a \]

  • \(I_a\): ambient light intensity in the scene
  • \(m_a\): material’s ambient reflection coefficient

Lighting Computations

images/spheres-0.pngambient images/spheres-1.png+diffuse images/spheres-2.png+specular images/spheres-3.png+shadows images/spheres-4.png+reflections

Diffuse Reflection

images/lighting-diffuse-0.svg
directed incoming light

Diffuse Reflection

images/lighting-diffuse-1.svg
uniformly outgoing light

Brightness depends on how much light (density) comes in!

Diffuse Reflection

images/lighting-lambert-1.svg images/lighting-lambert-2.svg

  • Intensity inversely proportional to illuminated area
  • Illuminated area inversely proportional to \(\cos\of{\theta}\)
  • Therefore intensity proportional to \(\cos\of{\theta}\)
  • So-called Lambertian reflection

Diffuse Reflection

images/lighting-diffuse-2.svg

\[ I \;=\; I_l \, m_d \, \cos\theta \]

  • \(I_l\): intensity of light source \(l\)
  • \(m_d\): material’s diffuse reflection coefficient
  • How can we compute \(\cos\theta\) efficiently?

Diffuse Reflection

images/lighting-diffuse-2.svg

\[ I \;=\; I_l \, m_d \, \cos\theta \;=\; I_l \, m_d \, \left( \vec{n} \cdot \vec{l} \right) \]

  • \(I_l\): intensity of light source \(l\)
  • \(m_d\): material’s diffuse reflection coefficient
  • directions \(\vec{n}\) and \(\vec{l}\) assumed to be normalized
  • no illumination if \(\vec{n} \cdot \vec{l} < 0\) (why?)

Lighting Computations

images/spheres-0.pngambient images/spheres-1.png+diffuse images/spheres-2.png+specular images/spheres-3.png+shadows images/spheres-4.png+reflections

Specular Reflection

images/lighting-specular-0.svg
directed incoming, directed outgoing

Specular Reflection

images/lighting-specular-1.svg

Specular Reflection

images/lighting-specular-2.svg

How to compute reflected ray \(\vec{r}\)?

  • \(\vec{r} = \vec{l} + 2 \vec{s}\)
  • \(\vec{s} = \vec{n} \left( \vec{n} \cdot \vec{l} \right) - \vec{l}\)
  • \(\vec{r} = 2\, \vec{n} \left( \vec{n} \cdot \vec{l} \right) - \vec{l}\)

Specular Reflection

images/lighting-specular-1.svg

\[ I \;=\; I_l \, m_s \, \cos\of{\alpha} \;=\; I_l \, m_s \, \left( \vec{r} \cdot \vec{v} \right) \]

  • \(I_l\): intensity of light source \(l\)
  • \(m_s\): material’s specular reflection coefficient
  • all directions assumed to be normalized
  • no illumination if \(\vec{n} \cdot \vec{l} < 0\) or \(\vec{r} \cdot \vec{v} < 0\)
  • How can we control the surface’s shininess?

Specular Reflection

images/lighting-specular-1.svg

\[ I \;=\; I_l \, m_s \, \cos^s\of{\alpha} \;=\; I_l \, m_s \, \left( \vec{r} \cdot \vec{v} \right)^s \]

  • \(I_l\): intensity of light source \(l\)
  • \(m_s\): material’s specular reflection coefficient
  • all directions assumed to be normalized
  • no illumination if \(\vec{n} \cdot \vec{l} < 0\) or \(\vec{r} \cdot \vec{v} < 0\)
  • \(s\): cosine exponent controls shininess
images/cosine-powers.svg

Phong Lighting Model

images/torus-a.pngambient: \(I_a m_a\) images/torus-d.pngdiffuse: \(I_l m_d \left( \vec{n} \cdot \vec{l} \right)\) images/torus-s.pngspecular: \(I_l m_s \left( \vec{r} \cdot \vec{v} \right)^s\)

images/torus-ads.png
\[ I \;=\; I_a m_a + I_l \left( m_d \left( \vec{n} \cdot \vec{l} \right) + m_s \left( \vec{r} \cdot \vec{v} \right)^s \right) \]

Colors & Lighting

  • Lighting depends on wavelength \(\lambda\) \[ I_{\lambda} \;=\; I_{a, \lambda} m_{a, \lambda} + I_{l, \lambda} \left( m_{d, \lambda} \left( \vec{n} \cdot \vec{l} \right) + m_{s, \lambda} \left( \vec{r} \cdot \vec{v} \right)^s \right) \]
  • We approximate it by RBG components \[ \begin{eqnarray*} I_{R} &=& I_{a,R} m_{a,R} + I_{l,R} \left( m_{d,R} \left( \vec{n} \cdot \vec{l} \right) + m_{s,R} \left( \vec{r} \cdot \vec{v} \right)^s \right)\\ I_{G} &=& I_{a,G} m_{a,G} + I_{l,G} \left( m_{d,G} \left( \vec{n} \cdot \vec{l} \right) + m_{s,G} \left( \vec{r} \cdot \vec{v} \right)^s \right)\\ I_{B} &=& I_{a,B} m_{a,B} + I_{l,B} \left( m_{d,B} \left( \vec{n} \cdot \vec{l} \right) + m_{s,B} \left( \vec{r} \cdot \vec{v} \right)^s \right)\\ \end{eqnarray*} \]
  • For RGB light colors/intensities \(\vec{I}\) and RGB material colors/coefficients \(\vec{m}\) and with component-wise product “\(*\)\[ \vec{I} \;=\; \vec{I}_a * \vec{m}_a + \vec{I}_l * \left( \vec{m}_d \left( \vec{n} \cdot \vec{l} \right) + \vec{m}_s \left( \vec{r} \cdot \vec{v} \right)^s \right) \]

Try it yourself!

Multiple Light Sources

We assumed linear superposition of light contributions and therefore can simply sum over all light sources

images/lighting-multiple.svg

\[ I \;=\; I_a m_a + \sum_{l} I_l \left( m_d \left( \vec{n} \cdot \vec{l}_l \right) + m_s \left( \vec{r}_l \cdot \vec{v} \right)^s \right) \]

Lighting Computations

images/spheres-0.pngambient images/spheres-1.png+diffuse images/spheres-2.png+specular images/spheres-3.png+shadows images/spheres-4.png+reflections

Shadows

  • Send shadow ray from intersection point to light source.
  • Discard diffuse and specular contribution if light source is blocked by another object.
images/lighting-shadow-1.svg
Point in light: ambient + diffuse + specular
images/lighting-shadow-2.svg
Point In shadow: ambient lighting only

Shadows

  • Send shadow ray from intersection point to light source.
  • Discard diffuse and specular contribution if light source is blocked by another object.
images/spheres_no_reflections_offset.png
we want this
images/spheres_no_reflections.png
but we get this
  • Why??

Shadows

  • Floating point errors might lead to erroneous self-shadowing (shadow acne).
    • Solution 1: Discard secondary intersection points that are too close.
      • in our implementation: slightly displace ray origin along new ray direction.
    • Solution 2: Offset primary intersection point along surface normal.
images/shadow-acne-1.svg
exact computation
images/shadow-acne-2.svg
precision problems

Lighting Computations

images/spheres-0.pngambient images/spheres-1.png+diffuse images/spheres-2.png+specular images/spheres-3.png+shadows images/spheres-4.png+reflections

Ray Tracing Operators

images/ray-tracing-pipeline-4.svg images/ray-tracing.svg

Recursive Ray Tracing

  • At each intersection point, reflect and/or refract incoming viewing ray at surface normal, and trace child rays recursively.
images/ray-tracing.svg

Recursive Ray Tracing

  • At each intersection point, reflect and/or refract incoming viewing ray at surface normal, and trace child rays recursively.
images/reflection.svg

\[ \vec{\omega}_{\mathrm{out}} = \left( \mat{I} - 2\vec{n}\transpose{\vec{n}} \right) \vec{\omega}_{\mathrm{in}} \]

images/refraction.svg

\[ n_1 \sin\theta_1 = n_2 \sin\theta_2 \]

Snell’s law with refraction
indices \(n_1\), \(n_2\).

  • Remember precision issue: You need to offset ray origin!

Recursive Ray Tracing

  • At each intersection point, reflect and/or refract incoming viewing ray at surface normal, and trace child rays recursively.

  • The final color is interpolated between local illumination, reflection, and refraction based on material properties.

images/ray-tracing.svg

Lighting Computations

images/spheres-0.pngambient images/spheres-1.png+diffuse images/spheres-2.png+specular images/spheres-3.png+shadows images/spheres-4.png+reflections

Ray Tracing Pipeline

  • Now you know about ray generation, ray intersection, lighting computations, and recursive ray tracing.
  • That’s all you need to implement a complete (basic) ray tracer!
  • Next week: Raytracing triangle meshes and acceleration data structures

images/ray-tracing-pipeline.svg images/ray-tracing.svg

Literature

  • Shirley et al.: Fundamentals of Computer Graphics, 3rd Edition, AK Peters, 2009.
    • Chapter 10, 20
images/shirley_combined.jpg
  • Hughes et al.: Computer Graphics: Principles and Practice, 3rd Edition, Addison-Wesley, 2014.
    • Chapter 27.5
images/cgpp.png
  • Akenine-Möller, Haines, Hoffman: Real-Time Rendering, Taylor & Francis, 2008.
    • Chapters 5 and 7
images/akenine.png