Linear Algebra in Computer Graphics and Games: DR Damon Daylamani-Zad
Linear Algebra in Computer Graphics and Games: DR Damon Daylamani-Zad
𝑥
𝑦
𝑧
2
3
5
We need to introduce “Homogeneous coordinates”
This is because as well as considering x, y, and z, we need to consider its
projection.
Homogeneous coordinates have an extra dimension called w, which scales the x,
y, and z dimensions. This is also referred to as Quaternion.
We will add w:
𝑥
𝑦
𝑧
𝑤
It’s easy to see this with a 2D example:
2D Example
𝑥 15
𝑦 = 21
𝑤 3
http://www.tomdalling.com/blog/modern-opengl/explaining-homogenous-coordinates-and-projective-geometry/
2D Example
𝑥 5
Dividing by three: 𝑦 = 7
𝑤 1
http://www.tomdalling.com/blog/modern-opengl/explaining-homogenous-coordinates-and-projective-geometry/
Using matrices in computer graphics
Matrices for translation and perspective projection transformations can only be
applied to homogeneous coordinates, which is why they are so common in 3D
computer graphics.
The x, y, and z values are said to be "correct" when w=1.
𝑥 𝑥
𝑦 𝑦
𝑧 = 𝑧
𝑤 1
Diagonal matrices
A diagonal matrix is a matrix which has 0 everywhere apart from possibly on the
main diagonal (formally 𝒂𝒊𝒋 = 0 for i ≠ j). For example:
2 0 0
0 0 0
0 0 −1
Note that the diagonal elements may be anything, including zero – everything
else must be zero
Identity matrix
A special, and very important, type of matrix is called an identity matrix.
The notation 𝑰𝒏 is used for the n x n diagonal matrix with 1s down the main
diagonal.
1 0 0
For example, 𝑰𝟑 = 0 1 0
0 0 1
2+1 3
3+2 = 5
5+3 8
Translation
So why do we need matrices?
1 0 0 0
𝑀𝑜𝑟𝑡ℎ = 0 1 0 0
0 0 0 0
0 0 0 1
1 0 0 0 𝑥 𝑥
0 1 0 0 𝑦 = 𝑦
0 0 0 0 𝑧 0
0 0 0 1 1 1
Parallel Projection
Similarly, project coordinates onto plane at x = 0
x' = 0, y’ = y, z’ = z
0 0 0 0
𝑀𝑜𝑟𝑡ℎ = 0 1 0 0
0 0 1 0
0 0 0 1
0 0 0 0 𝑥 0
0 1 0 0 𝑦 = 𝑦
0 0 1 0 𝑧 𝑧
0 0 0 1 1 1
Perspective Projection
Perspective projection - simple case
Perspective projection
1 0 0 0
0 1 0 0
𝑀𝑃𝑟𝑜𝑗𝑒𝑐𝑡𝑖𝑜𝑛 = 0 0 1
1
0
0 0 −𝑑 0
𝑥
1 0 0 0 𝑥 𝑥 −𝑧Τ𝑑
0 1 0 0 𝑦 𝑦 𝑦
0 0 1 0 𝑧 = 𝑧𝑧 = −𝑧Τ𝑑
1
0 0 − 0 1 − −𝑑
𝑑 𝑑
1
The View Frustum
The Projection Matrix (canonical view)
𝑛
0 0 0
𝑟
𝑛
0 0 0
𝑃𝑃𝑒𝑟𝑠𝑝𝑒𝑐𝑡𝑖𝑣𝑒 = 𝑡
−𝑓 −𝑓𝑛
0 0
𝑓−𝑛 𝑓−𝑛
0 0 −1 0
Clipping
Lines in 3D become lines in 2D
Polygons in 3D become polygons in 2D
Clipping
They may intersect the canonical view volume, then we need to
perform clipping:
◦ Clipping lines (Cohen-Sutherland algorithm)
◦ Clipping polygons (Sutherland-Hodgman algorithm)
Cohen-Sutherland algorithm
Input: Screen and 2D line segment
Output: Clipped 2D line segment
Cohen-Sutherland algorithm