Download presentation
Presentation is loading. Please wait.
Published byLouisa Baker Modified over 9 years ago
1
CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby Lecture 11 Cross Product & the Model Matrix
2
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"
3
Vectors & Dot Product (revision) Cross Product
4
Vectors A vector is a directed edge. In 3 dimensions a vector has three components: v(x, y, z) For example the vector a = (2, 2, 0)
5
Length of a vector The length of a vector can be calculated from its components.
6
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:
7
Dot product The dot product of two vectors v and w is: The relationship between two vectors can be calculated using the dot product. The dot product of two vectors expresses the angle between the vectors:
8
Cross Product The cross product (like the dot product) takes its name from the symbol used: a single cross between two vectors. 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.
9
Cross Product Illustration Vector v Vector w Cross Product v×w
10
Cross Product 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.
11
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.
12
Introducing Transformations
13
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.
14
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
15
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.
16
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?
17
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];
18
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. Ignore the final component of the vectors for now.
19
3D Models in graphics As a 4x4 array: Initialisation in C++: float matrix[4][4] = { { 0.2, 0.4, 0.1, 0.0 }, { 1.0, 1.0, 1.0, 0.0 }, { 0.3, 0.3, 0.3, 0.0 }, { 10.0, 3.0, 7.0, 1.0 } };
20
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. matrix[3][0]; // Px matrix[3][1]; // Py matrix[3][2]; // Pz
21
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 ).
22
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.
23
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] );
24
Introduction to Matrices
25
Matrices Singular: matrix Plural: matrices A matrix is a rectangular table of numbers. A matrix is composed of rows and columns.
26
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.
27
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.
28
Matrix Sizes Rows by columns. 2x3: 3x2: 3x1: 1x3: 4x4:
29
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.
30
Matrix Addition
31
Subtraction is identical to addition.
32
Scalar multiplication Scalar multiplication is simply when each component of a matrix is multiplied by a single value, in effect scaling it.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.