Download 11 Cross Product & the Model Matrix

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Computer simulation wikipedia , lookup

Lorentz transformation wikipedia , lookup

Laplace–Runge–Lenz vector wikipedia , lookup

History of numerical weather prediction wikipedia , lookup

Eigenvalues and eigenvectors wikipedia , lookup

Rotation matrix wikipedia , lookup

Non-negative matrix factorization wikipedia , lookup

Linear algebra wikipedia , lookup

Bra–ket notation wikipedia , lookup

Four-vector wikipedia , lookup

Transcript
CO1301: Games Concepts
Lecture 11
Cross Product & the Model Matrix
Dr Nick Mitchell (Room CM 226)
email: [email protected]
Material originally prepared by Gareth Bellaby
Reading
Rabin, Introduction to Game Development:
 4.1:
"Mathematical Concepts"
Van Verthe, Essential Mathematics for Games:
 Chapter 2: "Linear Transformations and Matrices"
 Chapter 3: "Affine Transformations".
Frank Luna, Introduction to 3D Game Programming with
DirectX 9.0c: A Shader Approach
 Chapter 1: "Vector Algebra"
Vectors & Dot Product (revision)
Cross Product
Vectors
 A vector is a directed edge. In 3 dimensions a
vector has three components: v(x, y, z)
v  x, y, z 
For example the
vector a = (2, 2, 0)
Length of a vector
 The length of a vector can be calculated from its
components.
length of v
 v

x y z
2
2
2
The normalised vector
 A normalised vector is a vector whose length is 1.
 Also known as the unit vector.
 A vector can be normalised by dividing each of its
components by its length:
The normalised form of v
 vˆ
 x y z 
 
, , 
 v v v 
Dot product
The dot product of two vectors
v and w is:


vw  v w  v w  v w 
y y
z z
 x x
The relationship between two vectors can be
calculated using the dot product.
The dot product of two vectors expresses the
angle between the vectors:
v  w  v w cos 
Cross Product
The cross product (like the dot product) takes its
name from the symbol used: a single cross between
two vectors.
vw
Pronounced "The cross product of
v and w", or just
the shorthand of "the cross of v and w ".
The cross product takes two vectors and produces a
single vector as its result.
Cross Product Illustration
Cross Product
v×w
Vector v
Vector w
Cross Product
vw 
(v y w z  w y v z , v z w x  w z v x , v x w y  w x v y )
The cross product of two vectors is the vector which
are orthogonal (perpendicular or at right-angles) to
both of the vectors.
There will always be two vectors which are orthogonal
to our original two vectors: one sticking upwards and
the other downwards (relative to the original vectors).
The cross product is not commutative. The order of
the vectors produces the direction.
Why is it important to you?
What is interesting about the cross product?
The
cross product of two vectors v and w
would produce the local axis of rotation
between the two vectors.
For example the cross product is used to
construct a LookAt() function and to implement
shader techniques such as normal mapping and
parallax mapping.
Introducing Transformations
Model Positioning Reminder
A 3D model has its own
local space:
 Three 3D vectors
X,Y & Z
These are the local axes
of the model.
So when you move a model according to its local z
axis you are calling up the local axes of the model in
order to do the movement.
Model Positioning
These local axes define the local rotation of the model,
e.g. rotating around its local y-axis, etc.
The following terms are less commonly used
nowadays but just in case you come across them:
 rotation around the local X is pitch (up and
down)
 rotation around the local Y is yaw (side to side)
 rotation around the local Z is roll
3D Models in graphics
The axes of the
model are local to the
model. The axes are
relative to the origin
of the model, i.e. ( 0,
0, 0 ).
The position of the
model are its
coordinates in world
space. The position is
the location of its
local origin.
3D Models in graphics
Every model has 4 items of data:
Vector representing the x-axis (relative to
model origin)
 Vector representing the y-axis (relative to
model origin)
 Vector representing the z-axis (relative to
model origin)
 Vector containing the coordinates of the
model (relative to the world origin)
Why is it done in this way?
Is it possible to do this any other way?

3D Models in graphics
Each model within the TL-Engine has a floating point
array associated with it. The array is actually a matrix
and we'll talk about matrices in a while.
It can be considered to be a 4 by 4 array:
float matrix[4][4];
3D Models in graphics
The first row of the array is
the model's local x-axis.
The second row of the array
is the model's local y-axis.
The third row of the array is
the model's local z-axis.
The final row of the array are
the model's coordinates.
x
y
x
xy
xz
x
yy
yz
x
zy
zz
x
Py
Pz
z
P
Ignore the final component of the vectors for now.

0
0

1
0
3D Models in graphics
As a 4x4 array:
Initialisation in C++:
 xx
y
 x
 zx

 Px
float matrix[4][4] =
{ { 0.2, 0.4, 0.1,
{ 1.0, 1.0, 1.0,
{ 0.3, 0.3, 0.3,
{ 10.0, 3.0, 7.0,
xy
yy
xz
yz
zy
Py
zz
Pz
0.0
0.0
0.0
1.0
},
},
},
} };
0

0
0

1
Transforming the model
The matrix can be obtained using the GetMatrix()
method.
float matrix[4][4];
box->GetMatrix( &matrix[0][0] );
The fourth row of the array are the coordinates of
the model. The fourth row is accessed by setting the
first subscript of the array to 3.  x
x
x 0
matrix[3][0]; // Px
matrix[3][1]; // Py
matrix[3][2]; // Pz
x
y
 x
 zx

 Px
y
z
yy
yz
zy
Py
zz
Pz

0
0

1
Transforming the model
So doing the following would set the coordinates of
the model to ( 4, 6, 3 )
matrix[3][0] = 4.0f;
matrix[3][1] = 6.0f;
matrix[3][2] = 3.0f;
The matrix can be set using the SetMatrix() method.
box->SetMatrix( &matrix[0][0] );
And
the box (in this case) would move to location (
4, 6, 3 ).
Scaling
The axes also provide a scaling. The length of the
vectors X, Y & Z can define the scaling in that axis
 1.0 = normal, 2.0 = double size etc.
 Effectively scaling local space
So if you obtain the matrix of the model and
multiply the components of the first row by 2, you
will double the size of the model along the x-axis.
Scaling
To scale the model in all of its axes, you would need
to multiply the second and third rows of the array in
the same way as the first, the effect of this would be
to reproduce the Scale() method:
float matrix[4][4];
box->GetMatrix( &matrix[0][0] );
matrix[0][1] *= 2.0f;
matrix[0][2] *= 2.0f;
matrix[0][3] *= 2.0f;
box->SetMatrix( &matrix[0][0] );
Introduction to Matrices
Matrices
Singular:
matrix
Plural: matrices
A matrix is a rectangular table of numbers.
A matrix is composed of rows and columns.
1 5 2
3 2 7


 4 1 2
4 2 1 


7 5 3
 4 2
 3 2


3 5
Why are matrices important to you?
Matrices are an essential part of graphics.
Transformations are carried out using matrices.
Matrices are key element of linear algebra and
hence of computer graphics.
Matrices
You write the numbers of rows first and the
number of columns second.
So a 2x3 matrix is composed of 2 rows and 3
columns.
1 1 2
3 2 1


Matrix Sizes
Rows by columns.
3x1:
3x2:
1 
3
 
0.5
1 1 
 3 2


2 1
1x3:
0.2
1 1
2x3:
1 1 2
3 2 1


4x4:
1
3

6

7
1
2
1
9
2
1
2
5
3

5
2

3
Matrix Addition
Matrices can be summed together.
Addition is done component by component (the
same way as vectors).
This only works if the matrices are the same size.
1 1 4 3 1  4 1  3 
3 2  2 2  3  2 2  2

 
 

Matrix Addition
3 3 4 3 7 6
3 2  2 2  5 4

 
 

2
3 1  1 0.3 1  3 3.3 2
2 1 2 1 4 2
3 2  2 1  5 3

 
 

1 1 2 1 3 2
Matrix Addition
 1  1 2 1  3 0
  3 2    2 2    1 4

 
 

Subtraction is identical to addition.
1 1 2 2   1  1
 2 2   2  2   0 4 

 
 

Scalar multiplication
Scalar multiplication is simply when each
component of a matrix is multiplied by a single
value, in effect scaling it.
1 1 2 2
2



3 2 6 4
32 4 1  6 12  3