630 likes | 1.09k Views
Transformations. Objectives. Understand how transformations work in 2D and 3D Understand the concept of homogenous coordinate system Understand scene graphs and hierarchical composition of transformations Get familiar with transformations in OpenGL and DirectX. Transformations.
E N D
Objectives • Understand how transformations work in 2D and 3D • Understand the concept of homogenous coordinate system • Understand scene graphs and hierarchical composition of transformations • Get familiar with transformations in OpenGL and DirectX
Transformations • Transformations change 2D or 3D points and vectors, or change coordinate systems. • An object transformation alters the coordinates of each point on the object according to the same rule, leaving the underlying coordinate system fixed. • A coordinate transformation defines a new coordinate system in terms of the old one, then represents all of the object’s points in this new system. • Object transformations are easier to understand, so we will do them first.
Transformations (2) • A (2D or 3D) transformation T( ) alters each point P into a new point Q using a specific formula or algorithm: Q= T(P).
Transformations (3) • An arbitrary point P in the plane is mapped to Q. • Q is the image of P under the mapping T. • We transform an object by transforming each of its points, using the same function T() for each point. • The image of line L under T, for instance, consists of the images of all the individual points of L.
Transformations (4) • Most mappings of interest are continuous, so the image of a straight line is still a connected curve of some shape, although it’s not necessarily a straight line. • Affine transformations, however, do preserve lines: the image under T of a straight line is also a straight line.
Geometric Effects of Affine Transformations • Combinations of four elementary transformations: (a) a translation, (b) a scaling, (c) a rotation, and (d) a shear (all shown below).
P’ P 2D Translations
P’ P 2D Scaling from the origin
y P’(x’,y’) P(x,y) r r x 2D Rotation about the origin Recall the right-hand rule!
2D Rotation about the origin y P’(x’,y’) P(x,y) r y r x x
2D Rotation about the origin. Substituting for r : After all, these are just High-schools formulas! Given us:
2D Rotation about the origin. Rewriting in matrix form gives us :
Example 1 T(S)
T(S) Example 2
Summary Shear in x: Shear in y:
Transformations • Translation. • P=T + P • Scale • P=S P • Rotation • P=R P • Shear • P=H P • We would like all transformations to be multiplications so we can concatenate them express points in homogenous coordinates.
Homogeneous coordinates • Add an extra coordinate, W, to a point. • P(x,y,W). • Two sets of homogeneous coordinates represent the same point if they are a multiple of each other. • (2,5,3) and (4,10,6) represent the same point. • At least one component must be non-zero (0,0,0) is not allowed. • If W 0 , divide by it to get Cartesian coordinates of point (x/W,y/W,1). • If W=0, point is said to be at infinity. • P(x,y,0) can be interpreted as the vector v(x, y) • Both points and vectors are represented by the same set of underlying objects
W P W=1 plane X Y Homogeneous coordinates • If we represent (x,y,W) in 3-space, all triples representing the same point describe a line passing through the origin. • If we homogenize the point, we get a point of form (x,y,1) • homogenised points form a plane at W=1.
Translations in homogenised coordinates • Transformation matrices for 2D translation are now 3x3.
Concatenation • We perform 2 translations on the same point:
Concatenation. Matrix product is variously referred to as compounding, concatenation, or composition. This single matrix is called the Coordinate Transformation Matrix or CTM.
Homogeneous form of scale Recall the (x,y) form of Scale : In homogeneous coordinates :
Composition of Transformations • Suppose we want to rotate around an arbitrary point P • Idea: Compose simple transformations 1. Translate P to origin 2. Rotate around origin 3. Translate origin back to P The order is very important!
y Note: Convenient to think of display as Being left-handed !! ( z into the screen ) x z (out of page) 3D Transformations • Use homogeneous coordinates, just as in 2D case. • Transformations are now 4x4 matrices. • We will use a right-handed (world) coordinate system - ( z out of page ).
More on Homogeneous Coordinates • To: if the object is a vector, add a 0 as the 4th coordinate; if it is a point, add a 1. • From: • If the 4th coordinate is zero, then simply remove it. • If it is not, then divide the other coordinate by it and remove it. • OpenGL uses 4D homogeneous coordinates for all its vertices. • If you send it a 3-tuple in the form (x, y, z), it converts it immediately to (x, y, z, 1). • If you send it a 2D point (x, y), it first appends a 0 for the z-component and then a 1, to form (x, y, 0, 1). • All computations are done within OpenGL in 4D homogeneous coordinates.
Combinations • Linear combinations of vectors and points: • The difference of 2 points is a vector: the fourth component is 1 – 1 = 0 • The sum of a point and a vector is a point: the fourth component is 1 + 0 = 1 • The sum of 2 vectors is a vector: 0 + 0 = 0 • A vector multiplied by a scalar is still a vector • Linear combinations of vectors are vectors
Translation in 3D Simple extension to the 3D case: • In OpenGL: • glMatrixMode(GL_ModelView); • glTranslatef(dx, dy, dz); • In DirectX: • device.Transform.World = Matrix.Translation(dx, dy, dz)
Scale in 3D Simple extension to the 3D case: • In OpenGL: • glMatrixMode(GL_ModelView); • glScalef(Sx, Sy, Sz); • In DirectX: • device.Transform.World = Matrix.Scaling(Sx, Sy, Sz)
Rotation in 3D • Need to specify which axis the rotation is about. • z-axis rotation is the same as the 2D case. • In OpenGL: • glMatrixMode(GL_ModelView); • glRotatef(theta, 0.0f, 0.0f, 1.0f); • In DirectX: • device.Transform.World = Matrix.RotationZ(theta)
Rotation in 3D • For rotation about the x and y axes: • In OpenGL: • glMatrixMode(GL_ModelView); • glRotatef(theta, 1.0f, 0.0f, 0.0f); • //gRotatef(theta,0.0f,1.0f,0.0f) (about y) • In DirectX: • device.Transform.World = Matrix.RotationZ(theta); • //Matrix.RotationY(theta) (about y)
Transformations of coordinate systems • Alternate view: when we apply a transformation, we’re not changing the coordinates of a point within a coordinate system. Instead, we’re changing the coordinate system itself • All the transformations are performed relative to the current coordinate frame origin and axes; OpenGL post-multiplies each new transformation matrix
M1 M2 C1 C2 C3 (a3,b3) C3 a3 b3 C1 C2 M2 M1 Successive Transformations Given P (a3,b3) in C3 What is P’s coordinates in C1? • Get P’s coordinates in C2 • P_c2 = M2 x P • Get P_c2’s coordinates in C1 • P_c1 = M1 x P_c2 • P_c1 = M1 x M2 x P the answer!!
Successive Transformations (2) • The sequence translate 1.5 0 0cube translate 8.5 0 0cubewill draw two cubes with x centres 1.5 and 10.0 respectively. • We could create the same image with the sequence save state translate 1.5 0 0cube restore state save state translate 10.0 0 0cube restore state • Here both cubes have an absolute translation and the order in which the two cubes are drawn does not matter.
Hierarchical composition using Push and Pop • glPushMatrix(); • Save the state. • Push a copy of the CTM onto the stack. • The CTM itself is unchanged. • glPopMatrix(); • Restore the state, as it was at the last Push. • Overwrite the CTM with the matrix at the top of the stack. • glLoadIdentity(); • Overwrite the CTM with the identity matrix.