520 likes | 677 Views
Chapter 2 – Making them Move. Dr. Paige H. Meeker CSC 458. Geometric Transformations. We can now create points, lines, and polygons. Let’s learn how to manipulate them. Geometric Transformations allow us to: Move (Translate) Orient (Rotate) Resize (Scale). Geometric Transformations.
E N D
Chapter 2 – Making them Move Dr. Paige H. Meeker CSC 458
Geometric Transformations • We can now create points, lines, and polygons. Let’s learn how to manipulate them. • Geometric Transformations allow us to: • Move (Translate) • Orient (Rotate) • Resize (Scale)
Geometric Transformations • Why do we care about transformations? • Usually we define our shapes in a coordinate system convenient to us. Using transformation equations enables us to easily modify object locations in the world coordinate system. In addition, if we transform our objects and play back the CG images fast enough, our eyes will be tricked to believe we are seeing motion. This principle is used extensively in animation, and we shall look into it in detail in Chapter 7. When we study 3D models, we will also see how we can use transformations to define hierarchical objects and to "transform" the camera position. • This chapter introduces the basic 2D transformations.
Concepts: • Vectors and matrices • 2D Transformations: translation, scaling, and rotation • How to use OpenGL to transform objects • Composition of transforms
Math We Need to Know • Vectors • Matrices
Vectors • One of the most important mathematical devices in computer graphics, physics, and engineering is the vector. • Vectors are used to express displacement. • Length and direction are the most important vector properties.
Vectors • Line with both length and direction. • A 2D vector has a length of x units along the x axis and y units along the y-axis and is denoted like this - • The vector between points P and Q below is denoted as (the displacement from one point to the other.
Vectors • Interestingly, any point P1 with coordinates (x,y) corresponds to the vector vPI, with its head at (x,y) and tail at (0,O) • So, there is a one-to-one correspondence between a vector, with its tail at the origin, and a point on the plane. This means that we can also represent the point P(x,y) by the vector • Why is this important? Because often, the mathematical representation of points for transformation equations will use vectors to represent points.
Vector Operations • Fundamental: addition, subtraction, and multiplication with a real number.
Vector Addition • Arguably the simplest of all vector operations. • Add each component of the vectors with each other • p = (5,3) q = (-2, -5) • p + q = [5 + (-2), 3 + (-5)] = (3,-2)
Vector Multiplication • Multiplying a vector by a numbers results in a vector whose length has been scaled by s. For this reason, the number s is also referred to as a scalar. If s is negative, then this results in a vector whose direction is flipped as well.
Vector Multiplication • Multiplying s by a vector – • s > 1 Increases length • s = 1 No change • 0 < s < 1 Decreases length • s = 0 Null vector (0 length, direction undefined) • -1 < s < 0 Decreases length and reverses direction • s = -1 Reverses direction only • s < -1 Reverses direction and increases length
Properties of Vector Addition and Multiplication • Given vectors a, b, and c and scalars k and l: • a + b = b + a • a + (b + c) = (a + b) + c • k(la) = kla • (k + l)a = ka + la • k(a + b) = ka + kb
Vector Subtraction • Addition of a vector that has been flipped.
Vector Magnitude • The length of the vector • The magnitude of a vector is the distance from the tail to the head of the vector. It is represented as IVI and is equal to • It is often useful to scale the vector to a length of 1 while keeping it’s direction. This is called normalizing the vector. To do this, we scale it by 1/|V| • This is also known as a unit vector and is denoted in mathematical circles as
Dot Product • The dot product of two vectors is a scalar quantity. The dot product is used to solve a number of important geometric problems in graphics. The dot product is written as V1 .V2 and is calculated as:
Practice Exercises • Given a = [-2, 0] and b = [4,1], compute • a’s unit vector • b’s unit vector • c = a – 2b • c = 3a • c = a + b • c = a – b • Length of a, |a| • Length of b, |b|
Vectors • The Internet is a wonderful thing… be sure to take advantage of it. A nice site to look at can be found at: http://zonalandeducation.com/mstm/physics/mechanics/vectors/vectors.html • Or their more general site: http://zonalandeducation.com/index.html
Matrices • A matrix is a set of numbers or other mathematical elements arranged in a rectangular array of rows and columns. The structure of a matrix makes it easy to assemble and work with certain kinds of mathematical data. • One matrix can operate on another matrix to change the data in some way. • Matrices can be used to represent geometric objects in computer data bases, to perform geometric transformations like translation, rotation, and scale, etc.
Matrices • A matrix is an array of numbers. The number of rows (m) and columns (n) in the array defines the cardinality (or order) of the matrix (m x n). • A vector is simply a matrix with one column (or a one-dimensional matrix). • Displays are just a matrix of pixels. • A frame buffer is a matrix of pixel values for each pixel on the display.
Special Matrices • Square Matrix: number of rows = number of columns (m=n) • Row or Column Matrix: a matrix with a single row (or column) – also called vectors • Diagonal Matrix: has zero elements except along the diagonal (If all the elements along the diagonal are also equal, then the diagonal matrix is called a scalar matrix.) • Null Matrix: One whose elements are all zero. • Symmetric Matrix: One whose elements are symmetric about the main diagonal • Antisymmetric Matrix: aij = -aji
Identity Matrix • A square matrix (an equal number of rows and columns) with all zeroes, except for 1’s in its diagonal. • Multiplication of a vector/matrix by an identity matrix has no effect.
Matrix Addition • Matrices can be added component wise, provided they have the same cardinality. • Similarly, matrices can be subtracted component wise as well. • We can add or subtract a sequence of more than two matrices as long as they are the same order.
Matrix Multiplication • Multiplication of a matrix by a scalar is simply the multiplication of its components by this scalar. It produces a new matrix of the same cardinality.
Matrix Multiplication • Two matrices can be multiplied if and only if the number of columns of the first matrix (m x n) is equal to the number of rows of the second (n x p). The result is calculated by applying the dot product of each row of the first matrix with each column of the second. The resultant matrix has a cardinality of (m x p).
Matrix Transpose • Interchanging the rows and columns of a matrix A and we obtain its transpose. (AT) • aijT = aji A = |a c e| |b d f|
Matrix Properties • Addition and Scalar Multiplication (A, B, C are matrices; b, d are scalars) • A + B = B + A • A + (B + C) = (A + B) + C • b(A + B) = bA + bB • (b+d)A = bA + dA • b(dA) = (bd)A = d(bA)
Matrix Properties • Matrix Multiplication • (AB) C = A(BC) • A(B+C) = AB + AC • (A+B)C = AC + BC • A(kB) = k(AB) = (kA)B
Matrix Properties • Matrix Transpose • (A+B)T = AT + BT • (kA)T = kAT • (AB)T = BT AT • If AAT = I, then A is an orthogonal matrix
Why am I telling you all this? • How do you think we make things move in computer graphics? • We use matrices to perform geometric transformations – to move, orient, and resize objects.
Geometric Transformations • The functions used for modifying the size, location, and orientation of objects or of the camera in a CG world.
Object Transformations • Transformations are applied within a particular space domain, be it an object space or the camera space or a texture space. • We shall mainly be discussing transformations applied in the object space, also called object transformations. • Internally, this is represented as a matrix. Operations on the matrix then transform each object.
Common Transformations • Translation • Rotation • Scale
Translation – we want to MOVE! • In a 2D world, we can move the object from left to right (translate along the x-axis) or up and down (translate along the y-axis). • This is denoted as Tx and Ty. • Consider a circular shape, P, as shown on the previous slide. To move P by a distance of (tx,ty) then all points P(x,y) on this shape will move to a new location P'(x,y) = P(x+Tx, y+Ty)
Translation • All transformations are represented as vectors. So, the point P is represented as: • The translation transformation is also represented as a vector:
Translation • Mathematically, then, the transformation becomes just the result of adding together two vectors. P’ = P + T
Translating Objects • Mathematically, we can translate an object by applying this equation to every point on the object. Usually, we can get away with just applying the translation to the vertices of the shape and then recalculating the translated shape.
Scaling • An object can be scaled (stretched or shrunk) along the x- and y- axis by multiplying all its points by the scale factors Sx and Sy. • P' = (x',y') such that x' = Sx.x, y' = Sy.y.
Rotation • A shape can be rotated about any of the three axes. • A rotation about the z-axis produces a reorientation about the xy-plane, which is what we desire in our 2D world. • The points are rotated through an angle about the world origin • Mathematically, a rotation about the z-axis by an angle q would result in point P (x,y) transforming to P' (x',y') as defined here: x’ = xcos(theta) – ysin(theta) y’ = xsin(theta + ycos(theta)
Rotation • In matrix form: P’ = R.P
Homogeneous Coordinates • The matrix representations for translation, rotation, and scale all use different matrix representations; wouldn’t it be nice if they could all agree on one representation so that many transformations could take place at once? • To get this representation, we borrow an idea from geometry called “homogeneous coordinates” – the location of the point in projected space.
Homogeneous Coordinates • With homogenous coordinates, we add a third coordinate to a (2D) point. • Instead of being represented by a pair of numbers (x,y), each point is now represented by a triplet (x,y, W), or in vector notation as • To go from homogenous coordinates back to our original non-homogenous world, we just divide the coordinates by W - (x/W, y/W). • (Typically, W=1)
Homogenous Coordinates • Two points of homogenous coordinates (x,y, W) and (x ',y ', W') are the same point if one is a multiple of the other. • (1,2,1) and (2,4,2) are the same points represented by different coordinate triples. • The points with W=O are points at infinity.
Homogenous Coordinates • What does this buy us? • Because 2D points are now three element column vectors, transformation matrices used to transform a point to a new location must be of cardinality 3x3.