280 likes | 303 Views
Learn how matrices position and scale 3D geometry, define models, and work with world and model spaces in computer graphics. Discover matrix arithmetic and multiplication for transforming objects.
E N D
Computer GraphicsMatrices CO2409 Computer Graphics Weeks 6-7
Lecture Contents • Meshes/Models & World/Model Space • Matrices for Positioning • Matrix Definitions • Matrix Arithmetic
Working with 3D Geometry • In 2D we looked at: • Conversion from geometry axes to viewport axes • 2D geometry: positioning & drawing • We didn’t look at rotation or scaling of geometry / sprites • 3D needs similar processes: • Converting between different 3D axes (e.g. world to camera view) • And finally into 2D viewport axes • Positioning of 3D geometry, drawing of 2D geometry • We will also look at rotation and scaling in 3D
Meshes and Models • A mesh defines some 3D geometry • We define a model as a single instance of a mesh in a 3D scene • Like a sprite is an instance of a bitmap in a 2D scene • Several models may use the same mesh in the scene • Like several sprites using the same bitmap • Each model can be positioned differently • Position, rotation and scaling • Example of two models of a square mesh: 1 mesh, 2 models
World / Model Space • We specify our 3D scene using fixed axes XW YW & ZW • A global view of the world and everything in it • This is called world space or the world coordinate system • The axes are the world axes (in red) • An arbitrarily positioned model has: • A position P and local axes, X, Y & Z • The local axes are its own rightward, upward & forward directions • This is the model’s local space or local coordinate system • Also called model space • Compare to the two overlaid sets of axes we saw in 2D
Model Positioning • So the position and rotation of a 3D model can be specified by defining its local space: • Position: a 3D vertex P • Rotation: three 3D vectors – Right, Up and Forward (X,Y & Z) • What about scaling? • The length of the vectors X, Y & Z can define the scaling in that axis • 1.0 = unscaled, 2.0 = double size etc. • Effectively scaling local space
Matrices for Positioning • We can put these 4 elements into a 4x3 matrix: • (RX, RY, RZ) is the model’s rightward direction, which is its local X axis • (UX, UY, UZ) is the up direction (local Y axis) • (FX, FY, FZ) the forward direction (local Z axis) • These are 3D vectors • (PX, PY, PZ) is the model’s position • All defined in world space units • Example on later slide • Or we can use a 3x4 matrix • Put everything in columns rather than rows • DirectX docs use rows, math books and OpenGL docs use columns. We will use rows. or
Alternative Written Form • Here the local X, Y & Z axes are written as R, U and F for right, up and forward: • Easier to understand • Can also use letters X, Y and Z to give this equivalent form: • I frequently use this form • Maths texts may present this matrix with different letters again • This is just presentation, the matrix always has the same basic form
Example Position Matrix • Consider this model (30° rotation): • Looking down Y axis - so it is not shown. Allows me to draw 2D diagram • Position, right, up and forward are given on diagram • All axes length 1 – no scaling • This gives us a model matrix: • Uniquely defines model’s position • This matrix can be used to manipulate the model • So we need to look at the mathematics of matrices…
Matrix Definition • A matrix (plural matrices) is a rectangular table of numbers: • Matrices can have any number of rows and columns. • A matrix with m rows and ncolumns is called an m by n matrix • The above matrices are 3x3 and 2x4 • The value at the i-th row and j-th column is denoted Aij • In the example above: M23 = 5 andN14 = 2
Simple Matrices • A 1xn or nx1 matrix can represent a set of coordinates • E.g. a vector or vertex/point: • A square matrix is one with a equal number of rows and columns, e.g. M from last slide • A 1x1 matrix is simply a number
Simple Matrix Arithmetic • Addition - equal size matrices can be added: • Subtraction – similar to addition: • Multiplication by a scalar:
Matrix Multiplication • We can multiply two matrices R = AB if: • Number of columns in A = number of rows in B • Result has same number of rows as A and columns as B • So if multiplying 2x3 & 3x4 matrices, we get 2x4 matrix result • Values in the result matrix are given by this formula: Rij = Ai1B1j + Ai2B2j+ ... + AinBnj • Stated differently, the value in row i and column j of R is equal to the dot product of i-th row of A & j-th column of B • Need to practice this to understand it…
Matrix Multiplication Properties • Matrix multiplication is associative: (AB)C = A(BC) • And distributive: (A + B)C = AC + BC, C(A + B) = CA + CB • This is the same as scalars • But matrix multiplication is notcommutative: AB ≠ BA (in general) • Very important for transformations later • The order in which you multiply matrices has an effect on the result matrix
Square Matrices • Square matrices can always be multiplied • The result is a square matrix of the same size • A square matrix with only 1’s down the diagonal has special properties: • Such a matrix does not change other matrices under multiplication • This is called the identitymatrix • Or the unit matrix (similarity with the number 1)
Transformations (3x3) • A vector or vertex can be represented as a 1x3 matrix: • Multiply by a 3x3 matrix we get another 1x3 matrix: • The vertex/vector changes (transforms) into a new one • This square matrix is a transformation matrix or a transform • Almost all the vertex processing needed for 3D graphics can be performed with transformation matrices • However, we will see that 3x3 matrices cannot perform some of the transformations we need (movement)
Transformations (4x4) • So we add another element (p) to the vector / vertex to get a 1x4 matrix: • Then multiply by a 4x4 matrix to perform a transformation • Take the right column from the unit matrix (see earlier slide) • Note the transformation leaves the extra element p unchanged • This kind of 4x4 transformation matrix can perform all of the transformations we need for 3D (including movement) • This form is called an Affine Transformation
The 4th Element / Mesh Transforms • The 4th element p specifies if the x, y and z values represent a vertex (point) or a vector 1 = vertex, 0 = vector • Previously we couldn’t distinguish between them (both just x,y,z) • For example: • If we transform all the vertices & normals in a mesh then the entire mesh will be transformed • Recall that normals are just vectors • So we can use these transformations to manipulate entire pieces of geometry
Advanced Note: Homogenous Coordinates • A coordinatein 3D is just an x,y,z point. E.g. (10,15,-5) • The 4 element coordinates introduced on the last slide are called homogenous 3D coordinates • They have x,y,z values plus an extra 4th value • To convert a homogenous coordinate to a point, you divide the x, y and z by the 4th element. • The 4th element is only used like this in creating perspective (see later), and at all other times it will only be 1 or 0 • If the 4th element is 1, dividing by 1 leaves the x,y,z the same • If the 4th element is 0, you can’t divide by 0 indicating that this is not a point, but a vector • This is only used for advanced work, but is mentioned here since some texts refer to homogenous coordinates
Translation Matrices • Translation describes movement of a vertex or mesh • The diagram shows movement along the vector V(p,q,r) • This transformation can be performed with the matrix on the right • The unit matrix with V written into the bottom row • We multiply each vertex in the mesh by this matrix • So the whole mesh is translated
Using a Translation Matrix • This is the full matrix multiplication on a point: • Note that a translation matrix has no effect on a vector • Vectors have no position – moving them makes no sense • E.g. the 3 blue vectors in the diagram are all the same even thought drawn in different places
Rotation Matrices • A rotation matrix rotates a vertex or mesh around an axis • Most often around the X, Y or Z axis • Could be the world axes or the local axes of a mesh (will see this later) • The diagram shows a rotation around the Z-axis • This Z rotation can be performed with the matrix on the right: • cos and sin appear since we are working with a circle
Using a Rotation Matrix • The rotate-Z matrix is used like this: • (not showing full formula for result x & y for simplicity) • Several important notes: • Z values don’t change when rotating around the Z axis • The rotation is around the origin • Not around the centre of the triangle • Vectors are affected by rotation • For example, the edges of the triangle are vectors - notice they are rotated too
Other Rotation Matrices • Can rotate around the X & Y axes using similar matrices • The three basic rotation matrices are: • Note how the row (& column) of the axis of rotation is unchanged in each version
Scaling Matrices • A simple scaling matrix scales the geometry around the origin • The matrix is like the unit matrix with the scaling value on the diagonal: • Note: Scaling is centred on the origin • Note: Vectors are affected by scaling • We can form a non-regular scaling matrix (different scaling in X, Y & Z):
Combining Transformations • We can combine transformations by multiplying their 4x4 matrices together • E.g. A scale followed by a translation: • The resulting matrix can then be used as a combined transformation for vertices / vectors:
Order of Transformations • The order of transformations is important • Use same example reversed, translation followed by a scale: • The result matrix is different • The translation has been scaled, as it occurred first • Recall that matrices are not commutative • I.e. AB ≠ BA in general • So it is very important to consider the order of your transformations before combining • [And to do any multiplications in the exam carefully!]
Creating Combined Transformations • Say we want a scaling matrix that will scale around a given point rather than the origin • We can do this with three basic transforms: • Translate the point to the origin • Scale (around the origin) • Translate the origin back to the point • The result matrix is potentially useful • Would have been difficult to guess