Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
58 views

Linear Algebra in Computer Graphics and Games: DR Damon Daylamani-Zad

The document discusses linear algebra concepts used in computer graphics and games, including vectors, matrices, translation, scaling, rotation, and projection. Vectors are used to represent points, and matrices allow efficient manipulation of multiple points through operations like translation, scaling, and rotation. Projection transforms 3D coordinates to 2D for rendering by applying a perspective or parallel projection matrix. Clipping algorithms are used to remove parts of geometry that fall outside the view volume.

Uploaded by

knight rider
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Linear Algebra in Computer Graphics and Games: DR Damon Daylamani-Zad

The document discusses linear algebra concepts used in computer graphics and games, including vectors, matrices, translation, scaling, rotation, and projection. Vectors are used to represent points, and matrices allow efficient manipulation of multiple points through operations like translation, scaling, and rotation. Projection transforms 3D coordinates to 2D for rendering by applying a perspective or parallel projection matrix. Clipping algorithms are used to remove parts of geometry that fall outside the view volume.

Uploaded by

knight rider
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Linear Algebra in Computer

Graphics and Games


DR DAMON DAYLAMANI-ZAD
D.D.ZAD@GRE.AC.UK
Overview
Vectors
Using matrices in computer graphics
Translation
Scaling
Rotation
Range of projectiles
Character controller
Projection and Clipping
Basic geometry
Why do we care about points in games?
There are many ways of discretising geometries.
Instead of always programming intricate details of graphics, games developers
use maths to make their lives easier (e.g. fractals).
But, essentially every graphic can be reduced to a series of points.
Being able to manipulate the position of points in order to represent objects is
therefore vital in game development.
While much of this is automated these days, engines always need refinement
and more abilities added to them.
Vectors
We will consider all points as column vectors:

𝑥
𝑦
𝑧
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

You will see why these are special shortly!


Translation
A translation means adding a vector to a point – transforming a point to a new
position.
Translation
A simple example:
2
Let’s move our point 3 , one unit in the x direction, two units in the y
5
direction, and three units in the z direction.
What will its new position be?

2+1 3
3+2 = 5
5+3 8
Translation
So why do we need matrices?

Ting Yip (2001)


Translation
If we have a number of points, we can just use matrix multiplication to find the
new position of ALL of the points:

Gives us the new,


translated coordinates

𝑡𝑥 is the translation in the x direction


𝑡𝑦 is the translation in the y direction
𝑡𝑧 is the translation in the z direction 𝑥, 𝑦, 𝑧 are the original co-ordinates
Scaling

Ting Yip (2001)


Scaling
Matrix for scaling:

Gives us the new,


scaled point

𝑠𝑥 is the scale factor in the x direction


𝑠𝑦 is the scale factor in the y direction
𝑠𝑧 is the scale factor in the z direction 𝑥, 𝑦, 𝑧 are the original co-ordinates
Rotation
Rotation
Matrix for rotation around the x Axis:

Gives us the new,


rotated point
𝑥, 𝑦, 𝑧 are the original co-ordinates
θ is the angle of rotation
Rotation: Example

Ting Yip (2001)


Rotation: Example
Range of a projectile (from the ground)
g: the gravitational acceleration—usually taken to be 9.81m/s2(32 f/s2) near the
Earth's surface
θ: the angle at which the projectile is launched
v: the velocity at which the projectile is launched
d: the total horizontal distance travelled by the projectile.
Range of a projectile (from the ground)
Shooting Example
2
𝑣 sin 2𝜃
𝑑=
𝑔
Character controller
Velocity + Rotation = Move my character
Viewing and Projection
Our eyes collapse 3-D world to 2-D retinal image (brain then has to reconstruct 3D)
In CG, this process occurs by projection
Projection has two parts:
◦ Viewing transformations: camera position and direction
◦ Perspective/orthographic transformation: reduces 3-D to 2-D
Use homogeneous transformations
Outline of procedure
1. Transform from world to camera coordinates
2. Project into view volume or screen coordinates
3. Clip geometry that falls outside of the view volume
Homogeneous transformations
1. We need to map local coordinates to a Local to World Matrix
2. Change from Local to World, to a World to Camera matrix
3. Then apply the projection matrix on it.
4. Remember that Coordinates (x, y, z,w) and (cx, cy, cz, cw) represent the same point
Parallel Projection
Project coordinates onto plane at z = 0
x' = x, y’ = y, z’ = 0

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

Space is split into nine regions


The centre corresponds to the area visible on the screen
Each region is encoded by four bits
Cohen-Sutherland algorithm

1st bit Above top of screen (y > 𝑦𝑚𝑎𝑥 )


2nd bit Below bottom of screen (y < 𝑦𝑚𝑖𝑛 )
3rd bit Right of right edge of screen (x > 𝑥𝑚𝑎𝑥 )
4th bit Left of left edge of screen (x < 𝑥𝑚𝑖𝑛 )
Sutherland-Hodgman algorithm
Traverse edges and divide into four types:
For example

You might also like