Download Transformations - Studentportalen

Document related concepts

2.5D wikipedia , lookup

Tensor operator wikipedia , lookup

Quaternions and spatial rotation wikipedia , lookup

Transcript
Lecture 3:
Transformations
Todays lecture
• Object representations in CG
• Review of linear algebra
• Linear transformations (rotation, scaling)
with matrices.
• Translation and homogeneous
coordinates.
• Main point: A lot of interesting transforms
can be expressed with matrix multiplication
Todays lecture in context
Classical graphical rendering pipeline
3D Object representation for CG
• Surface representations
• Discrete explicit surfaces (points, polygons)
• Explicit functional surface definitions (x,y,z) <- f(a,b,…)
• Implicit surface definitions f(x,y,z) = C
• Solid (volumetric) models
• Discrete volumetric models (voxels)
• Implicit solid models f(x,y,z) in range [ ]
3D Object representation for CG
• An object is described by its surface (i.e. objects are hollow).
• An object is specified by a set of 3D points (vertices).
• An object is composed of (or can be approximated by) flat
convex polygons.
• Transform an object by transforming its vertices
Smooth objects?
• May be approximated by polygons
• If the (screen)size of each polygon is smaller than the size of a
pixel, there is no visual difference.
• Modern graphics cards can render LOTS of triangles very fast.
• We will talk more about smooth surfaces later in the course.
CG-dinosaur in Jurrasic Park
Other object representations
Solid/Volumetric modelling
Objects are composed of a set of Voxels = 3D pixels
Volumetric Modelling
Volumetric rendering of a foot. (Project at the end of the course)
Other object representations
Implicit surfaces, F(x,y,z)=C
y
x
r=1
Sphere defined implicitly by 𝑥 2 + 𝑦 2 + 𝑧 2 = 12
Scalars points and vectors
• Scalars
– Real (or complex) numbers
• Points
– Locations in space (no size or shape)‫‏‬
• Vectors
– Directions in space (magnitude but no
position)‫‏‬
1.2
-2.3
7.3i
Vector operations
Review of vector operations:
• Adding/Subtracting two vectors
• Multiplication by scalars
• Magnitude of a vector
• Normalizing a vector
Vector Addition
a = (2, 4)
b = (2, 2)
y
x
Vector Addition
a = (2, 4)
b = (2, 2)
a + b = c = (4, 6)
y
x
Vector Addition
a = (2, 4)
b = (2, 2)
b + a = c = (4, 6)
y
x
Vector Subtraction
a = (2, 4)
b = (2, 2)
a - b = (2, 4) – (2, 2) = (0, 2) = d
y
x
Vector Subtraction
y
a = (2, 4)
b = (2, 2)
b - a = (2, 2) - (2, 4) = (0, -2) = e ≠ d
x
Order is important when subtracting vectors!
Vector Multiplied by Scalar
a = (4, 2)
y
x
Vector Multiplied by Scalar
b = 2 * a = 2 * (4, 2) = (8, 4)
y
x
Vector Multiplied by Scalar
b = -0.5 * a = -0.5 * (4, 2) = (-2, -1)
y
x
Length of Vector
a = (ax, ay)
||a|| =
𝑎𝑥 2 + 𝑎𝑦 2
Length of vector a (Euclidian norm)
(Remember: Pythagoras' Theorem)
Length of Vector
a = (ax, ay)
||a|| =
𝑎𝑥 2 + 𝑎𝑦 2
Length of vector a (Euclidian norm)
b = (bx, by, bz)
||b|| =
𝑏𝑥 2 + 𝑏𝑦 2 + 𝑏𝑧 2
Length of Vector
a = (ax, ay)
||a|| =
𝑎𝑥 2 + 𝑎𝑦 2
Length of vector a (Euclidian norm)
b = (bx, by, bz)
||b|| =
𝑏𝑥 2 + 𝑏𝑦 2 + 𝑏𝑧 2
||a|| = 𝒂•𝒂
Dot product
Normalizing a Vector
Normalize a vector = set its length to 1.
Divide the vector by its Euclidian norm (length)
Skriv en ekvation här.
𝒂
𝒂=
|𝒂|
Matrix multiplication
𝑎
𝑑
𝑔
𝑏
𝑒
ℎ
𝑐
𝑓
𝑖
𝑥
𝑦
𝑧
=
𝑎
𝑑
𝑔
𝑏
𝑒
ℎ
𝑐
𝑓
𝑖
𝑗
𝑚
𝑝
𝑘
𝑛
𝑞
𝑎𝑗 + 𝑏𝑚 + 𝑐𝑝
𝑑𝑗 + 𝑒𝑚 + 𝑓𝑝
𝑔𝑗 + ℎ𝑚 + 𝑖𝑝
𝑎𝑥 + 𝑏𝑦 + 𝑐𝑧
𝑑𝑥 + 𝑒𝑦 + 𝑓𝑧
𝑔𝑥 + ℎ𝑦 + 𝑖𝑧
𝑙
𝑜 =
𝑟
𝑎𝑘 + 𝑏𝑛 + 𝑐𝑞
𝑑𝑘 + 𝑒𝑛 + 𝑓𝑞
𝑔𝑘 + ℎ𝑛 + 𝑖𝑞
𝑎𝑙 + 𝑏𝑜 + 𝑐𝑟
𝑑𝑙 + 𝑒𝑜 + 𝑓𝑟
𝑔𝑙 + ℎ𝑜 + 𝑖𝑟
Linear combinations of vectors
• Convex combinations
(coefficients are all positive and add up to 1)‫‏‬
• E.g., linear interpolation:
p(t)=(t)a +(1-t)b
where a and b are points and 0≤t≤1.
Linear combinations of vectors
a = (2, 6)
b = (10, 4)
c = t*b + (1-t)*a
y
t = 0.0
t = 0.25
t = 0.50
t = 0.75
t = 1.0
x
Linear combinations of vectors
What are convex combinations used for in CG?
Can you imagine some examples?
Linear combinations of vectors
Morphing‫‏‬the‫“‏‬Ballephant”
Source object
100% Elephant
Target object
50% Elephant
50% Ball
25% Elephant
75% Ball
100% Ball
Intermediate objects are linear combinations of corresponding vertices
in the source and target geometries
module2s.rfx AGP2010
Dot products
• Vectors are orthogonal if dot product is 0.
• Vectors are less than 90o apart if dot product is
positive.
a•𝐛 = 0
a•𝐛 > 0
a•𝐛 < 0
Dot products
•Perpendicular projection of a onto b is (a•𝐛)𝐛
Dot products
Geometric interpretation in CG:
Perpendicular projection of a onto b is (a•𝐛)𝐛
a
b
𝐛
c
c = projection of a onto b
Dot products
What can the dot product be used for in CG?
Can you imagine some applications?
Dot products
What can the dot product be used for in CG?
Illumination/shading
(angle between surface and light direction)
Backface culling (angle between viewing direction and surface)
Collision detection (distance point / plane)
Reflection of light rays (mirror) or object motion (bouncing)
…
Dot products
• How do we get the reflection b of a vector a?
a
b
Dot products
• Reflection b of a is –a+2(a•𝒏)𝒏 where 𝒏 is the unit
normal vector to the reflection surface.
a
𝒏
-a
b
(a•𝒏)𝒏
(a•𝒏)𝒏
Cross products
Cross products
Determinant
Right hand rule
Cross products
Geometric interpretation and usage of the cross product
in CG:
◦ a ₓ b is a vector perpendicular to a and b
◦ Length of this vector is equivalent with the area of the
parallelogram defined by a and b
◦ Estimating surface normal vectors for L&S
◦ Determining normal basis / reference frames
◦ (Test for collinearity of vectors), but dot product is faster
◦ (Angle between vectors) , but dot product is faster
Transformations
• To create and move objects we need to
be able to transform objects in different
ways.
• There are many classes of
transformations.
– Translation (move around)‫‏‬
– Rotation
– Scaling
• Can you think of more?
Transformations
• To create and move objects we need to
be able to transform objects in different
ways.
• There are many classes of
transformations.
– Translation (move around)‫‏‬
– Rotation
– Scaling
• Can you think of more?
- Shear
- Mirroring/Flipping
Translation
Simply add a translation vector
Scaling (about the origin)‫‏‬
Multiply by a scale factor
Rotation (about the origin)‫‏‬
P(x,y) in polar coordinates
P'(x',y') in polar coordinates
Rotation (about the origin)‫‏‬
P(x,y) in polar coordinates
P'(x',y') in polar coordinates
Substitute x and y
Vectors and matrices (2D)
Rotation on matrix form:
(Rotation around the origin.)
Arbitrary rotation center
(0,0)
Arbitrary rotation center
•Translate the rotation center to the origin
•Rotate
•Translate back
Vectors and matrices (2D)
Translation
Rotation
Scaling
Vectors and matrices (2D)
Translation
x’‫‏‬and‫‏‬y’‫‏‬result‫‏‬from‫‏‬an‫‏‬
additional independent
factor
Rotation
Scaling
x’‫‏‬and‫‏‬y’‫‏‬are‫‏‬linear‫‏‬
combinations of x,
and y
Homogenous coordinates
“Stepping‫‏‬up‫‏‬one‫‏‬dimension”:
If W=1, this is called a Homogenous coordinate
Note: These are 2D homogenous coordinates and not 3D (spatial)
coordinates.
Homogenous‫‏‬coordinates‫‏‬are‫‏‬a‫“‏‬mathematical‫‏‬vehicle”‫‏‬to‫‏‬model‫‏‬
various spatial transformations of coordinates in a uniform way.
Example
1 0
0 1
0 0
−2
3
1
4
6
1
=
2
9
1
This way, matrix multiplication
can be used also for translation!
Why bother?
• Compact notation for concatenated
transforms.
• Translation followed by Rotation ≠
Rotation followed by Translation
Using homogenous coordinates
Translation
Rotation
Scaling
What about the last element W?
• If W=0, then P is not affected by
translations
• Be aware of this during the assignments!
Concatenation of transforms
The order of matrices (right to left) is
important!
Example of concatenated transforms:
1
P′ = 0
0
0
1
0
−4
1 0
−2 ∙ 0 2
1
0 0
Translation
0
0 −1
0 ∙ 1 0
1
0 0
Scaling
0
1 0
0 ∙ 0 1
1
0 0
Rotation
𝑥
4
2 ∙ 𝑦
1
1
Translation P
Moving to 3D
• Translation and scaling are the same
• Rotation is a bit more complicated, and
becomes Rx, Ry and Rz.
• Observe that RxRy ≠‫‏‬RyRx
3D transformations
Translation
Scaling
3D transformation 3
X Rotation
Y Rotation
Z Rotation
Arbitrary rotation
Rotation about an arbitrary axis a:
• Find a matrix M that aligns e.g. (1,0,0) with a.
• Apply M-1.
• Rotate around (1,0,0)‫‏‬
• Apply M.
How do we find M?
• The matrix M represents a change of
coordinate system.
• This (among other things) is the topic of
the next lecture.
Lab 2
• Transformations
•
•
•
Translation
Rotation
Scaling
• Positioning of a camera in a graphical
scene