Welcome! Todays Agenda: Rendering Overview Matrices - - PowerPoint PPT Presentation

welcome today s agenda
SMART_READER_LITE
LIVE PREVIEW

Welcome! Todays Agenda: Rendering Overview Matrices - - PowerPoint PPT Presentation

INFOGR Computer Graphics J. Bikker - April-July 2015 - Lecture 5: 3D Engine Fundamentals Welcome! Todays Agenda: Rendering Overview Matrices Transforms INFOGR Lecture 5 3D Engine Fundamentals


slide-1
SLIDE 1

INFOGR – Computer Graphics

  • J. Bikker - April-July 2015 - Lecture 5: “3D Engine Fundamentals”

Welcome!

slide-2
SLIDE 2

Today’s Agenda:

  • Rendering Overview
  • Matrices
  • Transforms
slide-3
SLIDE 3

Rendering

INFOGR – Lecture 5 – “3D Engine Fundamentals” Topics covered so far: Lecture 1:

  • Field study

Lecture 2:

  • Rasters
  • Vectors
  • Color representation

Lecture 3:

  • 2D primitives
  • 3D primitives
  • Textures
slide-4
SLIDE 4

Rendering

INFOGR – Lecture 5 – “3D Engine Fundamentals” Rendering – Functional overview

  • 1. Transform:

translating / rotating / scaling meshes

  • 2. Project:

calculating 2D screen positions

  • 3. Rasterize:

determining affected pixels

  • 4. Shade:

calculate color per affected pixel Transform Project Rasterize Shade meshes vertices vertices fragment positions pixels

Animation, culling, tessellation, ... Postprocessing

slide-5
SLIDE 5

Rendering

INFOGR – Lecture 5 – “3D Engine Fundamentals” Rendering – Data overview

slide-6
SLIDE 6

Rendering

INFOGR – Lecture 5 – “3D Engine Fundamentals” Rendering – Data overview world car wheel wheel wheel wheel turret plane plane car wheel wheel wheel wheel turret buggy wheel wheel wheel wheel dude dude dude camera 𝑈𝑑𝑏𝑛𝑓𝑠𝑏 𝑈𝑑𝑏𝑠1 𝑈𝑞𝑚𝑏𝑜𝑓1 𝑈𝑑𝑏𝑠2 𝑈𝑞𝑚𝑏𝑜𝑓2 𝑈𝑐𝑣𝑕𝑕𝑧

slide-7
SLIDE 7

Rendering

INFOGR – Lecture 5 – “3D Engine Fundamentals” Rendering – Data overview Objects are organized in a hierarchy: the scenegraph. In this hierarchy, objects have translations and

  • rientations relative to their parent node.

Relative translations and orientations are specified using matrices. Mesh vertices are defined in a coordinate system known as object space.

slide-8
SLIDE 8

Rendering

INFOGR – Lecture 5 – “3D Engine Fundamentals” Transform Project Rasterize Shade vertices, transforms pixels Rendering – Data overview Transform takes our meshes from

  • bject space (3D) to camera space

(3D). Project takes the vertex data from camera space (3D) to screen space (2D). textures, shaders, lights camera transform screen buffers vertices vertices fragment positions connectivity data

slide-9
SLIDE 9

Rendering

INFOGR – Lecture 5 – “3D Engine Fundamentals” Rendering – Data overview The screen is represented by (at least) two buffers:

slide-10
SLIDE 10

Rendering

INFOGR – Lecture 5 – “3D Engine Fundamentals” Rendering – Components Scenegraph Culling Vertex transform pipeline Matrices to convert from one space to another Perspective Rasterization Interpolation Clipping Depth sorting: z-buffer Shading Light / material interaction Shadows / reflections / etc. Lecture 7 Lecture 5 Lecture 6 Lecture 7 Lecture 7 P2 P3

slide-11
SLIDE 11

Today’s Agenda:

  • Rendering Overview
  • Matrices
  • Transforms
slide-12
SLIDE 12

Matrices

INFOGR – Lecture 5 – “3D Engine Fundamentals” Bases in ℝ2 and ℝ3 Recall:

  • Two linearly independent vectors form a base.
  • We can reach any point in using:

𝑏 = λ1𝑣 + λ2 𝑤

  • If 𝑣 and

𝑤 are perpendicular unit vectors, the base is

  • rthonormal.
  • The Cartesian coordinate system is an example of

this, with 𝑣 = (1,0) and 𝑤 = (0,1). By manipulating 𝑣 and 𝑤, we can create a ‘coordinate system’ within a coordinate system. 𝑣 𝑤

slide-13
SLIDE 13

INFOGR – Lecture 5 – “3D Engine Fundamentals” Bases in ℝ2 and ℝ3 This extends naturally to ℝ3: Three vectors, 𝑣, 𝑤 and 𝑥 allow us to reach any point in 3D space; 𝑏 =λ1𝑣 + λ2 𝑤 + λ3 𝑥 Again, manipulating 𝑣, 𝑤 and 𝑥 changes where coordinates specified as (λ1, λ2 , λ3) end up.

y

z

x

𝑣 𝑤 𝑥

Matrices

slide-14
SLIDE 14

INFOGR – Lecture 5 – “3D Engine Fundamentals” Matrices A vector is an ordered set of d scalar values (i.e., a d-tuple): 𝑤 = 𝑤1 𝑤2 𝑤3

  • r (𝑤1, 𝑤2, 𝑤3) or …

A 𝑛 × 𝑜 matrix is an array of 𝑛 ∙ 𝑜 scalar values, sorted in 𝑛 rows and 𝑜 columns: 𝐵 = 𝑏11 𝑏12 𝑏21 𝑏22 The elements 𝑏𝑗𝑘 are referred to as the coefficients of the matrix (or elements, entries). Note that here 𝑗 is the row; 𝑘 is the column.

Matrices

slide-15
SLIDE 15

INFOGR – Lecture 5 – “3D Engine Fundamentals” Terminology – special matrices

  • A diagonal matrix is a matrix for which all elements aij are zero if 𝑗 ≠ 𝑘.
  • An identity matrix is a diagonal matrix where each element 𝑏𝑗𝑗 = 1.
  • The zero matrix contains only zeroes.

𝐵 = 1.5 0.99 3.14 𝐵 = 1 1 1 𝐵 = Before we continue, what is a matrix?

  • Just a group of numbers;
  • In graphics: often a representation of a coordinate system.

x y z

Matrices

slide-16
SLIDE 16

INFOGR – Lecture 5 – “3D Engine Fundamentals” Matrices - operations Matrix addition is defined as: 𝐵 = 𝐶 + 𝐷, with: c𝑗𝑘 = 𝑏𝑗𝑘 + 𝑐𝑗𝑘 Note that addition is only defined for matrices with the same dimensions. Example: 1 1 + 2 2 4 4 = 3 2 4 5 Subtraction works the same.

Matrices

slide-17
SLIDE 17

INFOGR – Lecture 5 – “3D Engine Fundamentals” Matrices - operations Multiplying a matrix with a scalar is defined as follows: 𝐵 = λ𝐶, with: a𝑗𝑘 = λ𝑐𝑗𝑘 Example: 2 1 1 = 2 2

Matrices

slide-18
SLIDE 18

INFOGR – Lecture 5 – “3D Engine Fundamentals” Matrices - operations Multiplying a matrix (dimensions 𝑛𝐵 × 𝑜𝐵) with another matrix (dimensions 𝑛𝐶 × 𝑜𝐶): 𝐷 = 𝐵𝐶, with: Example: 2 6 1 5 2 4 1 4 2 5 3 6 = 17 44 21 54 Note the dimensions of the resulting matrix: 𝑛𝐵 × 𝑜𝐶. Matrix multiplication is only defined if 𝑜𝐵 = 𝑛𝐶. 𝑑11 =

𝑙=1 2

𝑏1𝑙 𝑐𝑙1 = 2 ∗ 1 + 6 ∗ 2 + 1 ∗ 3 = 17 𝑑21 =

𝑙=1 2

𝑏2𝑙 𝑐𝑙1 = 5 ∗ 1 + 2 ∗ 2 + 4 ∗ 3 = 21 𝑑12 =

𝑙=1 2

𝑏1𝑙 𝑐𝑙2 = 2 ∗ 4 + 6 ∗ 5 + 1 ∗ 6 = 44 𝑑22 =

𝑙=1 2

𝑏2𝑙 𝑐𝑙2 = 5 ∗ 4 + 2 ∗ 5 + 4 ∗ 6 = 54 𝑑𝑗𝑘 =

𝑙=1 𝑜𝐵

𝑏𝑗𝑙 𝑐𝑙𝑘

Matrices

slide-19
SLIDE 19

INFOGR – Lecture 5 – “3D Engine Fundamentals” Matrices - operations Doing matrix multiplication manually: 1 4 2 5 3 6 2 6 1 5 2 4 ? ? ? ? Note that each cell in the resulting matrix is essentially the dot product of a row and a column. Some properties: Matrix multiplication is distributive over addition: 𝐵 𝐶 + 𝐷 = 𝐵𝐶 + 𝐵𝐷 𝐵 + 𝐶 𝐷 = 𝐵𝐷 + 𝐶𝐷 …and associative: 𝐵𝐶 𝐷 = 𝐵 𝐶𝐷 However, matrix multiplication is not commutative, i.e., in general: 𝐵𝐶 ≠ 𝐶𝐵

Matrices

slide-20
SLIDE 20

INFOGR – Lecture 5 – “3D Engine Fundamentals” Matrices - operations Doing matrix multiplication manually: 1 4 2 5 3 6 2 6 1 5 2 4 ? ? ? ? 𝑏 𝑑 𝑐 𝑒 1 1 𝑏 𝑑 𝑐 𝑒 Multiplying by the zero matrix yields the zero matrix: 0𝐵 = 𝐵0 = 0 Multiplying by the identity matrix yields the original matrix: 𝐽𝐵 = 𝐵𝐽 = 𝐵

Matrices

slide-21
SLIDE 21

INFOGR – Lecture 5 – “3D Engine Fundamentals” Matrices - operations The transpose 𝐵𝑈 of an 𝑛 × 𝑜 matrix is an 𝑜 × 𝑛 matrix that is obtained by interchanging rows and columns: 𝑏𝑗𝑘 becomes 𝑏𝑘𝑗 for all 𝑗, 𝑘: 𝐵 = 𝑏11 𝑏12 𝑏13 𝑏21 𝑏22 𝑏23 𝑏31 𝑏32 𝑏33 𝐵𝑈 = 𝑏11 𝑏21 𝑏31 𝑏12 𝑏22 𝑏32 𝑏13 𝑏23 𝑏33 The transpose of the product of two matrices is: 𝐵𝐶 𝑈 = 𝐶𝑈𝐵𝑈

Matrices

slide-22
SLIDE 22

INFOGR – Lecture 5 – “3D Engine Fundamentals” Matrices - operations The inverse of a matrix 𝐵 is a matrix 𝐵-1 such that 𝐵𝐵−1 = 𝐵−1A = 𝐽 Note: only square matrix possibly have an inverse.

Matrices

slide-23
SLIDE 23

INFOGR – Lecture 5 – “3D Engine Fundamentals” Matrices - operations We can multiply a d-dimensional vector by an 𝑛 × 𝑒 matrix: 𝑏11 ⋯ 𝑏1𝑒 ⋮ ⋱ ⋮ 𝑏𝑛1 ⋯ 𝑏𝑛𝑒 𝑤1 ⋮ 𝑤𝑒 = 𝑏11𝑤1 + ⋯ + 𝑏1𝑒𝑤𝑒 ⋯ + ⋯ + ⋯ 𝑏𝑛1𝑤1 + ⋯ + 𝑏𝑛𝑒𝑤𝑒 Example: multiply a 3D vector by a 3x3 matrix: 𝑏11 𝑏12 𝑏13 𝑏21 𝑏22 𝑏23 𝑏31 𝑏32 𝑏33 𝑦 𝑧 𝑨 = 𝑏11𝑦 + 𝑏12𝑧 + 𝑏13𝑨 𝑏21𝑦 + 𝑏22𝑧 + 𝑏23𝑨 𝑏31𝑦 + 𝑏32𝑧 + 𝑏33𝑨

Matrices

Note: This is the same as matrix concatenation; the vector is simply an 𝑛 × 1 matrix.

slide-24
SLIDE 24

INFOGR – Lecture 5 – “3D Engine Fundamentals” Matrices - operations We can multiply a d-dimensional vector by an 𝑛 × 𝑒 matrix: 𝑏11 ⋯ 𝑏1𝑒 ⋮ ⋱ ⋮ 𝑏𝑛1 ⋯ 𝑏𝑛𝑒 𝑤1 ⋮ 𝑤𝑒 = 𝑏11𝑤1 + ⋯ + 𝑏1𝑒𝑤𝑒 ⋯ + ⋯ + ⋯ 𝑏𝑛1𝑤1 + ⋯ + 𝑏𝑛𝑒𝑤𝑒 Example: multiply a 3D vector by a 3x3 matrix: 𝑣𝑦 𝑤𝑦 𝑥𝑦 𝑣𝑧 𝑤𝑧 𝑥𝑧 𝑣𝑨 𝑤𝑨 𝑥𝑨 𝑦 𝑧 𝑨 = 𝑣𝑦𝑦 + 𝑤𝑦𝑧 + 𝑥𝑦𝑨 𝑣𝑧𝑦 + 𝑤𝑧𝑧 + 𝑥𝑧𝑨 𝑣𝑨𝑦 + 𝑤𝑨𝑧 + 𝑥𝑨𝑨 = 𝑦𝑣 + 𝑧 𝑤 + 𝑨𝑥

Matrices

Note: This is the same as matrix concatenation; the vector is simply an 𝑛 × 1 matrix. u v w

slide-25
SLIDE 25

INFOGR – Lecture 5 – “3D Engine Fundamentals” Matrices – determinant The determinant 𝐵 of an 𝑜 × 𝑜 matrix A is the signed area or volume spanned by its column vectors. Example (in ℝ2 ): 𝐵 = 𝑏11 𝑏12 𝑏21 𝑏22 det A = |A| = 𝑏11 𝑏12 𝑏21 𝑏22 In this case, the determinant is the oriented area of the parallelogram defined by the two column vectors. The determinant is positive if the vectors are counter- clockwise, or negative if they are clockwise. Therefore: det 𝑏1 𝑏2 = −det |𝑏2 𝑏1|

Matrices

(𝑏12, 𝑏22) (𝑏11, 𝑏21)

slide-26
SLIDE 26

INFOGR – Lecture 5 – “3D Engine Fundamentals” Matrices – determinant The determinant 𝐵 of an 𝑜 × 𝑜 matrix A is the signed volume spanned by its column vectors. In ℝ3, the determinant is the oriented area of the parallelepiped defined by the three column vectors. det 𝐵 = 𝐵 = 𝑏11 𝑏12 𝑏13 𝑏21 𝑏22 𝑏23 𝑏31 𝑏32 𝑏33

Matrices

slide-27
SLIDE 27

INFOGR – Lecture 5 – “3D Engine Fundamentals” Matrices – determinant Calculating determinants: Laplace’s expansion. The determinant of a matrix is the sum of the products

  • f the elements of any row or column of the matrix

with their cofactors. The cofactor of an entry 𝑏𝑗𝑘 in an 𝑜 × 𝑜 matrix A is:

  • The determinant of the (𝑜 − 1) × 𝑜 − 1 matrix A’ ,
  • that is obtained from A by removing the 𝑗-th row

and 𝑘-th column,

  • multiplied by -1i+j .

Matrices

Example: 𝐵 = 𝑏11 𝑏12 𝑏13 𝑏21 𝑏22 𝑏23 𝑏31 𝑏32 𝑏33 𝑏11

𝑑 = 𝑏22

𝑏23 𝑏32 𝑏33 ∗ (−12) 𝑏12

𝑑 = 𝑏21

𝑏23 𝑏31 𝑏33 ∗ (−13) 𝑏13

𝑑 = 𝑏21

𝑏22 𝑏31 𝑏32 ∗ (−14) 𝐵 = 𝑏11𝑏11

𝑑 + 𝑏12 𝑏12 𝑑 +𝑏13𝑏13 𝑑

slide-28
SLIDE 28

INFOGR – Lecture 5 – “3D Engine Fundamentals” Matrices – determinant Full example for 3 × 3 matrix: 1 2 3 4 5 6 7 8 = 0 4 5 7 8 − 1 3 5 6 8 + 2 3 4 6 7 3 5 6 8 = 3 ∗ 8 ∗ −12 + 5 ∗ 6 ∗ −13 = −6 3 4 6 7 = 3 ∗ 7 ∗ −12 + 4 ∗ 6 ∗ −13 = −3 0 – 1 ∗ −6 + 2 ∗ −3 = 0.

Matrices

Generic approach for a for 3 × 3 matrix: 𝑏 𝑐 𝑑 𝑒 𝑓 𝑔 𝑕 ℎ 𝑗 = 𝑏 𝑓 𝑔 ℎ 𝑗 − ⋯ = 𝑏𝑓𝑗 + 𝑐𝑔𝑕 + 𝑑𝑒ℎ − (𝑑𝑓𝑕 + 𝑏𝑔ℎ + 𝑐𝑒𝑗) 𝑏 𝑐 𝑑 𝑒 𝑓 𝑔 𝑕 ℎ 𝑗 𝑏 𝑐 𝑑 𝑒 𝑓 𝑔 𝑕 ℎ 𝑗 Rule of Sarrus for 2 × 2: 𝑏 𝑐 𝑑 𝑒 = 𝑏𝑒 − 𝑐𝑑

slide-29
SLIDE 29

INFOGR – Lecture 5 – “3D Engine Fundamentals” Matrices – adjoint The adjoint (or adjugate) 𝐵 of matrix 𝐵 is the transpose of the cofactor matrix of A. Example: 𝐵 = 2 5 1 3  𝐷 = 3 ∗ (−12) 1 ∗ (−13) 5 ∗ (−13) 2 ∗ (−14) = 3 −1 −5 2 𝑏𝑒𝑘 𝐵 = 𝐷𝑈 = 3 −5 −1 2 .

Matrices

The cofactor of an entry 𝑏𝑗𝑘 in an 𝑜 × 𝑜 matrix A is:

  • The determinant of the

(𝑜 − 1) × 𝑜 − 1 matrix A’ ,

  • that is obtained from A by removing

the 𝑗-th row and 𝑘-th column,

  • multiplied by -1i+j .
slide-30
SLIDE 30

INFOGR – Lecture 5 – “3D Engine Fundamentals” Matrices – inverse The adjoint is used to calculate the inverse 𝐵

_1 of a matrix A:

𝐵

_1 =

𝐵 |𝐵|

Matrices

slide-31
SLIDE 31

INFOGR – Lecture 5 – “3D Engine Fundamentals” Matrices – overview 𝐵 = 𝑏11 𝑏12 𝑏13 𝑏21 𝑏22 𝑏23 𝑏31 𝑏32 𝑏33 = 1 1 1 𝑜 × 𝑛: n rows, m columns det 𝐵 = 𝐵 = 1 = 𝑏𝑓𝑗 + 𝑐𝑔𝑕 + 𝑑𝑒ℎ − (𝑑𝑓𝑕 + 𝑏𝑔ℎ + 𝑐𝑒𝑗) 𝐵 = 1 1 note: −1 1 = −1, and: det 𝑏1 𝑏2 = −det |𝑏2 𝑏1| cofactor 𝑏11

𝑑 = 𝑏22

𝑏23 𝑏32 𝑏33 ∗ (−12) Adjoint 𝐵 of A is 𝐷𝑈; inverse 𝐵

_1 is

𝐵 |𝐵| .

Matrices

y

z

x

𝑏 𝑐 𝑑 𝑒 𝑓 𝑔 𝑕 ℎ 𝑗 𝑏 𝑐 𝑑 𝑒 𝑓 𝑔 𝑕 ℎ 𝑗

slide-32
SLIDE 32

Today’s Agenda:

  • Rendering Overview
  • Matrices
  • Transforms
slide-33
SLIDE 33

Transforms

INFOGR – Lecture 5 – “3D Engine Fundamentals” Spaces - introduction As we have seen before, we can multiply a matrix with a vector. In 2D: 𝑏11 𝑏12 𝑏21 𝑏22 𝑦 𝑧 = 𝑏11𝑦 + 𝑏12𝑧 𝑏21𝑦 + 𝑏22𝑧 In 3D: 𝑏11 𝑏12 𝑏13 𝑏21 𝑏22 𝑏23 𝑏31 𝑏32 𝑏33 𝑦 𝑧 𝑨 = 𝑏11𝑦 + 𝑏12𝑧 + 𝑏13𝑨 𝑏21𝑦 + 𝑏22𝑧 + 𝑏23𝑨 𝑏31𝑦 + 𝑏32𝑧 + 𝑏33𝑨 Geometric interpretation: scalar multiplication of 𝑏11 𝑏21 by 𝑦, plus scalar multiplication of 𝑏12 𝑏22 by 𝑧 yields transformed point. = 𝑦 𝑏11 𝑏21 + 𝑧 𝑏12 𝑏22 = 𝑦 𝑏11 𝑏21 𝑏31 + 𝑧 𝑏12 𝑏22 𝑏32 + 𝑨 𝑏13 𝑏23 𝑏33 𝑏11 𝑏21 𝑏12 𝑏22

slide-34
SLIDE 34

INFOGR – Lecture 5 – “3D Engine Fundamentals” Spaces – introduction A matrix allows us to transform a coordinate system.

× =

rotation + scale

Transforms

slide-35
SLIDE 35

INFOGR – Lecture 5 – “3D Engine Fundamentals” Spaces – scaling To scale by a factor 2 with respect to the origin, we apply the matrix 2 2 Applied to a vector, we get: 2 2 𝑦 𝑧 = 2𝑦 + 0𝑧 0𝑦 + 2𝑧 = 2𝑦 2𝑧 This is called uniform scaling.

𝑦, 𝑧 = (2,1) 2𝑦, 2𝑧 = (4,2)

Transforms

slide-36
SLIDE 36

INFOGR – Lecture 5 – “3D Engine Fundamentals” Spaces – projection If we set one of the 𝑏𝑗𝑗 to 0, we get an

  • rthographic projection.

1 This is useful for projecting a shadow

  • f the dragon on the x-axis, or to draw

a 3D object on a 2D screen.

Transforms

slide-37
SLIDE 37

INFOGR – Lecture 5 – “3D Engine Fundamentals” Spaces – reflection We can construct a matrix that will swap 𝑦 and 𝑧 coordinates to get a reflection in the line 𝑧 = 𝑦: 1 1 𝑦 𝑧 = 0𝑦 + 1𝑧 1𝑦 + 0𝑧 = 𝑧 𝑦 𝑧 = 𝑦

Transforms

slide-38
SLIDE 38

INFOGR – Lecture 5 – “3D Engine Fundamentals” Spaces – shearing Pushing things sideways: 1 1 1 𝑦 𝑧 = 1𝑦 + 1𝑧 1𝑧 = 𝑦 + 𝑧 𝑦 This is called shearing.

Transforms

slide-39
SLIDE 39

INFOGR – Lecture 5 – “3D Engine Fundamentals” Spaces – rotation To rotate counter-clockwise about the

  • rigin, we use the following matrix:

cos ∅ −𝑡𝑗𝑜∅ sin ∅ 𝑑𝑝𝑡∅ For clockwise rotation, we use cos ∅ 𝑡𝑗𝑜∅ −sin ∅ 𝑑𝑝𝑡∅ Ф

Transforms

slide-40
SLIDE 40

INFOGR – Lecture 5 – “3D Engine Fundamentals” Spaces – linear transformations A function 𝑈: ℝ𝑜 → ℝ𝑛 is called a linear transformation, if it satisfies:

  • 1. 𝑈 𝑣 +

𝑤 = 𝑈 𝑣 + 𝑈 𝑤 for all 𝑣, 𝑤 ϵ ℝ𝑜.

  • 2. 𝑈 𝑑

𝑤 = 𝑑𝑈 𝑤 for all 𝑤 ∈ ℝ𝑜 and all scalars c. Linear transformations can be represented by matrices. We can summarize both conditions into one equation: 𝑈 𝑑1𝑣 + 𝑑2 𝑤 = 𝑑1𝑈 𝑣 + 𝑑2𝑈 𝑤 for all 𝑣, 𝑤 ∈ ℝ𝑜 and all scalars c1, c2.

𝑦, 𝑧 = (2,1) 2𝑦, 2𝑧 = (4,2)

Transforms

slide-41
SLIDE 41

INFOGR – Lecture 5 – “3D Engine Fundamentals” Spaces – linear transformations 𝑈 𝑑1𝑣 + 𝑑2 𝑤 = 𝑑1𝑈 𝑣 + 𝑑2𝑈 𝑤 for all 𝑣, 𝑤 ∈ ℝ𝑜 and all scalars c1, c2. Remember Cartesian coordinates, where each vector 𝑥 can be expressed as a linear combination of base vectors 𝑣 and 𝑤: 𝑥 = 𝑦 𝑧 = 𝑦 1 0 + 𝑧 0 1 If we apply a linear transform T to this vector, we get 𝑈 𝑦 𝑧 = T 𝑦 1 0 + 𝑧 0 1 = 𝑦𝑈( 1 0 ) + 𝑧𝑈( 0 1 )

𝑦, 𝑧 = (2,1) 2𝑦, 2𝑧 = (4,2)

Transforms

slide-42
SLIDE 42

INFOGR – Lecture 5 – “3D Engine Fundamentals” Spaces – linear transformations 𝑈 𝑑1𝑣 + 𝑑2 𝑤 = 𝑑1𝑈 𝑣 + 𝑑2𝑈 𝑤 for all 𝑣, 𝑤 ∈ ℝ𝑜 and all scalars c1, c2. Matrices are constructed conveniently using two base vectors. 𝑣 𝑤 𝑣 𝑤 𝑣 𝑤

Transforms

slide-43
SLIDE 43

INFOGR – Lecture 5 – “3D Engine Fundamentals” Spaces – transforming normals Unfortunately, normals are not always transformed correctly. To transform a normal vector 𝑜 correctly under a given linear transformation 𝐵, we have to apply the matrix 𝐵

_1 𝑈

Why? Note: if the transform is orthonormal, A

_1 = 𝐵 T ; therefore 𝐵 _1 𝑈 = 𝐵 .

𝑜 𝑢

Transforms

slide-44
SLIDE 44

INFOGR – Lecture 5 – “3D Engine Fundamentals” Spaces – transforming normals We know that tangent vectors are transformed correctly: 𝐵 𝑢 = 𝑢𝐵 . But: 𝐵𝑜 ≠ 𝑜𝐵. Goal: find ind a a mat atrix 𝐍 that tr transforms 𝒐 correctly, i.e. 𝑁𝑜 = 𝑜𝑁, where 𝑜𝑁 is the correct normal of the transformed surface. Because the original normal vector 𝑜 is perpendicular to the original tangent vector 𝑢, we know that 𝑜𝑈 𝑢 = 0. This is the same as 𝑜𝑈 𝐽 𝑢 = 0. Since I = 𝐵

_1𝐵, this is the same as 𝑜𝑈 (𝐵 _1𝐵)

𝑢 = 0. Because 𝐵 𝑢 = 𝑢𝐵 is the correctly transformed tangent vector, we have 𝑜𝑈𝐵

_1

𝑢𝐵 = 0. Because their scalar product is 0, 𝑜𝑈𝐵

_1 must be orthogonal to

𝑢𝐵. So, the vector we are looking for must be: 𝑜𝑁

𝑈 = 𝑜𝑈𝐵

_1 (which suggests 𝑁 = 𝐵 _1).

Because of how matrix multiplication is defined, 𝑜𝑁

𝑈 and 𝑜𝑈 are transposed vectors. We can rewrite

this to 𝑜𝑁 = (𝑜𝑈𝐵

_1)T. And finally, remember that 𝐵𝐶 𝑈 = 𝐶𝑈𝐵𝑈, which gets us 𝑜𝑁 = 𝐵 _1 𝑈𝑜.

Transforms

slide-45
SLIDE 45

INFOGR – Lecture 5 – “3D Engine Fundamentals” Spaces – needful things Three things left undiscussed:

  • 1. Reverting a transform
  • 2. Combining transforms
  • 3. Translation

Reverting a transform: Invert the matrix. Note: doesn’t always work; e.g. the matrix for orthographic projection has no inverse. Combining transforms: Use matrix multiplication. Note: matrix multiplication is not commutative, mind the order!

Transforms

slide-46
SLIDE 46

INFOGR – Lecture 5 – “3D Engine Fundamentals” Spaces – translation Translation is not a linear transform. With linear transforms, we get: 𝑏11 𝑏12 𝑏21 𝑏22 𝑦 𝑧 = 𝑏11𝑦 + 𝑏12𝑧 𝑏21𝑦 + 𝑏22𝑧 But we need something like: 𝑦 𝑧 = 𝑦 + 𝑦𝑢 𝑧 + 𝑧𝑢 We can do this with a combination of linear transformations and translations called affine transformations.

Transforms

slide-47
SLIDE 47

INFOGR – Lecture 5 – “3D Engine Fundamentals” Spaces – translation Observe: in 2D, shearing “pushes things sideways” (e.g., in the 𝑦 direction), in a “fixed level” (the 𝑧 value). We are thus performing a translation in a 1D subspace (a line), using matrix multiplication in 2D. In 3D, shearing leads to translation in a 2D subspace, i.e. a plane. (𝑦, 𝑧, 0) (𝑦, 𝑧, 𝑚) (𝑦 + 𝑦𝑢, 𝑧, 𝑚) (𝑦 + 𝑦𝑢, 𝑧, 0)

Transforms

slide-48
SLIDE 48

INFOGR – Lecture 5 – “3D Engine Fundamentals” Spaces – translation By adding a 3rd dimension to 2D space, we can use matrix multiplication to do translation. 𝑁 𝑦 𝑧 𝑨 = 𝑦 + 𝑦𝑢 𝑧 + 𝑧𝑢 𝑨 But: what does matrix 𝑁 look like? What about 𝑦𝑢 and 𝑧𝑢? And how do we deal with the third coordinate 𝑨?

Transforms

slide-49
SLIDE 49

INFOGR – Lecture 5 – “3D Engine Fundamentals” Spaces – translation Shearing in 3D based on the z coordinate is a simple generalization of 2D shearing: 1 𝑦𝑢 1 𝑧𝑢 1 𝑦 𝑧 𝑨 = 1𝑦 + 𝑨𝑦𝑢 + 0𝑨 1𝑧 + 𝑨𝑧𝑢 + 0𝑨 0𝑦 + 0𝑧 + 𝑨 = 𝑦 + 𝑦𝑢𝑨 𝑧 + 𝑧𝑢𝑨 𝑨 The final step is to set z to 1. 1 𝑦𝑢 1 𝑧𝑢 1 𝑦 𝑧 1 = 𝑦 + 𝑦𝑢 𝑧 + 𝑧𝑢 1

Transforms

slide-50
SLIDE 50

INFOGR – Lecture 5 – “3D Engine Fundamentals” Spaces – translation Translations in 2D can be represented as shearing in 3D by looking at the plane 𝑨 = 1. By representing our 2D points (𝑦, 𝑧) by 3D vectors (𝑦, 𝑧, 1), we can translate them about (𝑦𝑢, 𝑧𝑢) by applying the following matrix: 1 𝑦𝑢 1 𝑧𝑢 1 𝑦 𝑧 1 = 𝑦 + 𝑦𝑢 𝑧 + 𝑧𝑢 1 That works for points. What about vectors? We use the following transform: 1 𝑦𝑢 1 𝑧𝑢 1 𝑦 𝑧 = 𝑦 𝑧

Transforms

slide-51
SLIDE 51

INFOGR – Lecture 5 – “3D Engine Fundamentals” Spaces – translation Affine transformations (i.e., linear transformations and translations) can be done with matrix multiplication if we add homogeneous coordinates, i.e.

  • A third coordinate 𝑨 = 1 to each point: 𝑞 =

𝑦 𝑧 1

  • A third coordinate z = 0 to each vector:

𝑤 = 𝑦 𝑧

  • A third row (0 0 1) to each matrix.

Transforms

slide-52
SLIDE 52

INFOGR – Lecture 5 – “3D Engine Fundamentals” Spaces – translation These concepts apply naturally to 3D, in which case we again add a homogeneous coordinate, i.e.

  • A fourth coordinate w = 1 to each point;
  • A fourth coordinate 𝑥 = 0 to each vector;
  • A fourth row (0 0 0 1) to each matrix.

Transforms

slide-53
SLIDE 53

Today’s Agenda:

  • Rendering Overview
  • Matrices
  • Transforms
slide-54
SLIDE 54

INFOGR – Computer Graphics

  • J. Bikker - April-July 2015 - Lecture 5: “3D Engine Fundamentals”

END of “3D Engine Fundamentals”

next lecture: “Transformations”