Computer Graphics

2D Transformations

Prof. Dr. David Bommes
Computer Graphics Group

Rasterization Pipeline

images/graphics-rasterization.svg

Ray Tracing vs. Rasterization

  • Ray Tracing
    • Shoot rays from 2D pixels into 3D scene
    • “backward rendering”
    • needs ray intersections
images/raytracing-vs-rasterization-1.svg

  • Rasterization
    • Project 3D objects onto 2D image plane
    • “forward rendering”
    • needs transformations & projections
images/raytracing-vs-rasterization-2.svg

Which transformations do we need?

images/solar-system.png

2D Transformations

2D Transformations

images/trafo-translation.svgtranslation images/trafo-scaling.svgscaling

images/trafo-rotation.svgrotation

2D Translation

  • Translate object by \(t_x\) in \(x\) and \(t_y\) in \(y\)
    \[ \vector{x \\ y} \mapsto \vector{t_x + x \\ t_y + y} \]
images/trafo-translation.svg
translation by \((2,1)\)

2D Scaling

  • Scale object by \(s_x\) in \(x\) and \(s_y\) in \(y\) (around origin!)
    \[ \vector{x \\ y} \mapsto \vector{s_x \cdot x \\ s_y \cdot y} \]
images/trafo-scaling.svg
scaling by \((2,2)\)

2D Rotation

  • Rotate object by \(\theta\) degrees
    (around origin!)
images/trafo-rotation.svg
rotation by 45 degrees

2D Rotation

  • Rotate point \((x,y) = (r\cos\phi, r\sin\phi)\)
    by \(\theta\) degrees around origin

\[ \begin{eqnarray*} \vector{x\\y} &\mapsto& \vector{r \cos\of{\phi+\theta} \\ r \sin\of{\phi+\theta}} \\[2mm] &=& \vector{r \cos\phi \cos\theta - r \sin\phi \sin\theta \\ r \cos\phi \sin\theta + r \sin\phi \cos\theta} \\[2mm] &=& \vector{\cos\theta \cdot x - \sin\theta \cdot y \\ \cos\theta \cdot y + \sin\theta \cdot x} \\[2mm] &=& \matrix{\cos\theta & -\sin\theta \\ \sin\theta & \cos\theta} \cdot \vector{x \\ y} \end{eqnarray*} \]

images/trafo-rotation-2.svg
rotation by \(\theta\) degrees

2D Rotation

  • Rotate object by \(\theta\) degrees
    (around origin!)

\[ \vector{x\\y} \mapsto \matrix{\cos\theta & -\sin\theta \\ \sin\theta & \cos\theta} \cdot \vector{x \\ y} \]

images/trafo-rotation.svg
rotation by 45 degrees

How to rotate/scale around object center?

  1. Translate center to origin
  2. Scale object
  3. Rotate object
  4. Translate center back
images/trafo-combination.svg
  • This can get quite messy!

Important Questions

  • How to efficiently combine several transformations?
  • Why do straight edges stay straight?
  • Why do rotations preserve angles and lengths?

Represent transformations as matrices!

Matrix Representation

images/trafo-scaling.svg
scaling

\[ \mat{S}\of{s_x, s_y} = \matrix{ s_x & 0 \\ 0 & s_y } \]

images/trafo-rotation.svg
rotation

\[ \mat{R}\of{\theta} = \matrix{\cos\theta & -\sin\theta \\ \sin\theta & \cos\theta} \]

images/trafo-translation.svg
translation

\[ \mat{T}\of{t_x, t_y} = \matrix{ ? & ? \\ ? & ?} \]

Which transformations can be written as matrices?

Linear Maps & Matrices

  • Assume a linear transformation \(L \colon \R^n \to \R^n\)
    • \(L(\vec{a}+\vec{b}) = L(\vec{a}) + L(\vec{b})\)
    • \(L(\alpha \, \vec{a}) = \alpha \, L(\vec{a})\)
  • Point \(\vec{x}=(x_1, \ldots, x_n)\) can be written as \[\vec{x} = x_1 \vec{e}_1 + x_2 \vec{e}_2 + \ldots + x_n \vec{e}_n\]

Linear Maps & Matrices

  • Exploit linearity of \(L\) \[ \begin{eqnarray*} L\of{\vec{x}} &=& L\of{x_1 \vec{e}_1 + x_2 \vec{e}_2 + \ldots + x_n \vec{e}_n} \\[2mm] &=& x_1 \, L\of{\vec{e}_1} + x_2 \, L\of{\vec{e}_2} + \ldots + x_n \, L\of{ \vec{e}_n} \\[2mm] &=& \underbrace{\matrix{ L\of{\vec{e}_1} ,\; L\of{\vec{e}_2} ,\; \ldots ,\; L\of{\vec{e}_n}}}_{=:\mat{L}} \cdot \vector{x_1 \\ \vdots \\ x_n} \;=\; \mat{L}\,\vec{x} \end{eqnarray*} \]
  • Every linear transformation \(L \colon \R^n \to \R^n\) can be written as a unique \((n \times n)\) matrix \(\mat{L}\) whose columns are the images of the basis vectors \(\left\{ \vec{e}_1, \ldots, \vec{e}_n \right\}\).

VERY useful fact! VERY-VERY useful!

Linear vs. Affine Transformations

  • Every linear transformation has to preserve the origin
    • \(L(\vec{0}) = \mat{L} \cdot \vec{0} = \vec{0}\)
  • Translation is not a linear mapping
    • \(T(0,0) = (t_x, t_y)\)
  • Translation is affine transformation
    • affine mapping = linear mapping + translation
    • \(\vector{x \\ y} \mapsto \matrix{a & b \\ c & d} \cdot \vector{x \\ y} + \vector{t_x \\ t_y} = \mat{L}\vec{x} + \vec{t}\)
images/trafo-translation.svg

Homogeneous Coordinates

  • Extend cartesian coordiantes \((x,y)\) to homogeneous coordinates \((x,y,w)\)
    • Points are represented by \(\transpose{(x,y,1)}\)
    • Vectors are represented by \(\transpose{(x,y,0)}\)
  • Only homogeneous coordinates with \(w \in \{0,1\}\) make sense
    • vector + vector = vector
    • point - point = vector
    • point + vector = point
    • point + point = ??
  • Only affine combinations of points \(\vec{x}_i\) make sense
    • \(\sum_i \alpha_i \vec{x}_i\) with \(\sum_i \alpha_i = 1\)

Homogeneous Coordinates

  • Matrix representation of translations
    \[ \vector{x \\ y} + \vector{t_x \\ t_y} \quad\longleftrightarrow\quad \matrix{ 1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1} \cdot \vector{x \\ y \\ 1} \]
  • Matrix representation of arbitrary affine transformation
    \[ \matrix{a & b \\ c & d} \cdot \vector{x \\ y} + \vector{t_x \\ t_y} \quad\longleftrightarrow\quad \matrix{ a & b & t_x \\ c & d & t_y \\ 0 & 0 & 1} \cdot \vector{x \\ y \\ 1} \]

Matrix Representation

images/trafo-scaling.svgscaling

\[ \matrix{ s_x & 0 & 0 \\ 0 & s_y & 0 \\ 0 & 0 & 1 } \]

images/trafo-rotation.svgrotation

\[ \matrix{\cos\theta & -\sin\theta & 0 \\ \sin\theta & \cos\theta & 0 \\ 0 & 0 & 1} \]

images/trafo-translation.svgtranslation

\[ \matrix{ 1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1} \]

Columns of matrix are images of basis vectors!

Concatenation of Transformations

  • Apply sequence of affine transformations \(\mat{A}_1, \ldots, \mat{A}_k\)

  • Concatenate transformations by matrix multiplication

    \[ A_k\of{\ldots A_2\of{A_1\of{\vec{x}}}} \;=\; \underbrace{\mat{A}_k \cdots \mat{A}_2 \cdot \mat{A}_1}_{\mat{M}} \cdot \vec{x} \]

  • Precompute matrix \(\mat{M}\) and apply it to all (many!) object vertices. Very important for performance!

Ordering of Matrix Multiplication

  • First rotation, then translation images/trafo-rot-trans.svg
  • First translation, then rotation images/trafo-trans-rot.svg

How to rotate/scale around object center?

  1. Translate center to origin
  2. Scale object
  3. Rotate object
  4. Translate center back
images/trafo-combination.svg

What is the matrix representation?

Matrix Representation?

images/trafo-quiz.svg

Columns of matrix are images of basis vectors!

Important Questions (again)

  • How to efficiently combine several transformations?
  • Why do straight edges stay straight?
  • Why do rotations preserve angles and lengths?

Transform straight edges

  • Any point \(\vec{C}\) on a line is an affine combination \[(1-\alpha)\vec{A} + \alpha\vec{B}\]

    of its endpoints \(\vec{A}\) and \(\vec{B}\).

  • Affine transformation \(\mat{M}\) preserves affine combinations \[ \mat{M}\of{(1-\alpha)\vec{A} + \alpha\vec{B}} \;=\; (1-\alpha)\mat{M}\of{\vec{A}} + \alpha\mat{M}\of{\vec{B}} \]

images/trafo-lines.png

Orthogonal Transformations

  • A matrix \(\mat{M}\) is orthogonal iff
    • its columns are orthonormal vectors
    • its rows are orthonormal vectors
    • its inverse is its transposed: \(\mat{M}^{-1} = \transpose{\mat{M}}\)
  • Orthogonal matrices / mappings
    • preserve angles, lengths, and areas
    • can only be rotations or reflections
    • have determinant +1 or -1

Next Time: 3D Transformations & Projections

Literature

  • Akenine-Möller, Haines, Hoffman: Real-Time Rendering, Taylor & Francis, 2008.
    • Chapter 4
images/akenine.png
  • Hughes et al.: Computer Graphics: Principles and Practice, 3rd Edition, Addison-Wesley, 2014.
    • Chapter 10,11
images/cgpp.png
  • Shreiner, Seller, Kessenich, Licea-Kane: OpenGL Programming Guide, 2013.
    • Chapter 3
images/shreiner.png