CSC4820/6820 Computer Graphics Algorithms Ying Zhu Georgia State University Transformations
Outline 3D transformations Concatenation of transformations Transformations in OpenGL Anatomy of a simple OpenGL program
Coordinate systems Each point and vector is defined in reference to a coordinate system Once we fix the origin of the coordinate system, we can represent all points unambiguously The same point may have different 3D coordinates in different coordinate systems Remember that part of the job of 3D graphics pipeline is to convert vertices from model space to 2D window space Each vertex with go through several coordinate systems along the pipeline
Coordinate systems A 3D coordinate system is defined by its three base vectors (x, y, and z axis) Get familiar with OpenGL’s coordinate system
Geometric transformation In 3D graphics, animation is achieved by transforming geometric objects in the virtual world Transforming geometric objects means transforming points (vertices) and vectors Transformation of points and vectors can be conveniently achieved by matrix multiplications Introduced by Larry Roberts in 1966
Linear Algebra Review Matrix: An array of elements Vectors: A n x 1 matrix is called a column vector In computer graphics, vertex coordinates and vectors (directed line segments) and are represented by column vectors.
Linear Algebra Review Matrix-column vector multiplication
Linear Algebra Review Matrix-matrix multiplication V1 V2 V3
Linear Algebra Review Homogeneous representation A vertex coordinate representation has 1 as the 4th component. OpenGL uses homogeneous coordinates for all vertices. (x, y, z) => (x, y, z, 1) Why use homogeneous coordinates? So that all transformations can be defined by matrix multiplication.
Affine Matrix An affine matrix is a 4x4 matrix with last low [0 0 0 1] Why include the bottom row of (0,0,0,1)? It’ll be useful in perspective transforms. Affine matrix is used to define affine transformation.
Affine Transformation Any transformation preserving collinearity (i.e., all points lying on a line initially still lie on a line after transformation) ratios of distances (e.g., the midpoint of a line segment remains the midpoint after transformation). Affine transformations preserve parallel lines. We can transform vertices with affine transformation. Affine transformations are what we use all the time in computer graphics.
Other Transformations Isometry (preserves distance) Similarity (preserves angles) Collineation (lines remains lines) Perspective projections Non-linear (lines become curves) Twists, bends, etc. Can be used for deformation.
Elementary Affine Transformations Four elementary affine transformations Translation Scaling Rotation (about an axis) Shear Translation, scaling and rotation are the most commonly used transformations.
Translation Translation P’ P d x y
Scaling Scaling
Rotation Rotation about the origin Counter-clock wise rotation by 90 degree.
2D Transformation Summary Translation Scaling Rotation
Composition of Affine Transforms Most of the time we want to apply more than one transformation. Example: Translate by vector t Rotate by r degree about the origin This can be represented by multiple matrix multiplications:
Composition of Affine Transforms Matrix multiplication is associative: We can group the combined transformation as The affine matrices are applied right side first. We can also apply all the transformations to form a single matrix This called composing or concatenation of transforms. The result is another affine transformation.
Composition of Affine Transforms It is important to always remember that matrix multiplication is not commutative The order of transforms DOES matter E.g. scaling then rotating is usually different than rotating then scaling Keep this in mind when you write OpenGL program – you need to know in which order your OpenGL transform calls are executed
Decomposition of Affine Transforms Any affine transform can be decomposed into the product of a rotation, a scale, and a rotation.
Coordinate Transformations We have discussed how to use transformation matrices to move points around We can also think of them as simply changing the coordinate system in which the point is presented The origin of the coordinate system is transformed but the relative position of the point is unchanged In different contexts, either interpretation may be more suitable.
Why use matrix formalisms? Can’t I just hardcode transforms rather than use the matrix multiplication? Yes. But it’s harder to derive, harder to debug, and not any more efficient All current graphics APIs use this matrix formalism Must understand this to use OpenGL or other graphics libraries
3D Affine Transformations 3D affine transformations are an extension of the 2D transformations. Use 4x4 matrices instead of 3x3 matrices in 2D transformations. For example 3D scaling
3D Translation 3D translation matrix OpenGL function: glTranslatef(tx, ty, tz)
3D Scaling 3D scaling matrix OpenGL Function: glScalef(Sx, Sy, Sz)
3D Rotation About the Origin 3D rotation is more complicated than 2D rotation. Rotation about X axis Rotation about Y axis Rotation about Z axis
Arbitrary 3D Rotations Normal practice is to decompose an arbitrary 3D rotation into rotations about x, y, z axes. Create a single transformation matrix by composing.
Rotation in OpenGL (GLfloat angle, GLfloat x, GLfloat y, GLfloat z) Equivalent to multiplying the following matrix
Rotation in OpenGL Rotations are specified by a rotation axis and an angle Normal practice is to decompose an arbitrary 3D rotation into rotations about x, y, z axes Achieved by 3 glRotatef() calls
Rotation About a Fixed Point The order is important: Translate to the origin Rotate about the origin Translate back Example: To rotate by r about z around
Quaternions A quaternion is a 4-tuple of real numbers [s,x,y,z] or [s,v], s is a scalar and v is a three-dimensional vector. Quaternions provide an alternative for describing and manipulating rotations. Quaternions are more efficient than rotation matrices Can be interpolated to obtain smooth sequence of rotations for animation You can convert between a transformation matrix and a quaternion
Multiple 3D Transformations By combining a scaling, rotation, and a translation matrix we get a single transformation matrix. Notice that in the single matrix, translation is separated from scaling and rotation part. We can think of an affine matrix as having a scaling-rotation part and a translation part. If a transformation consists of only translations and rotations, then it’s called a rigid-body transformation.
Transformations in OpenGL Internally each vertex in OpenGL is represented as homogeneous points i.e. a column vector with 1 as the last item. However you don’t need to specify the 1 in your program Each vertex is transformed by the modelview matrix. There is always a current modelview matrix that is applied to all vertices.
Model-view Matrix
Transformation in OpenGL It is important to understand the relationship among Current modelview matrix, Vertices of 3D Object So you can understand why OpenGL code has to be arranged in certain way
What happens inside OpenGL during transformation? Final modelview matrix Matrices are often created dynamically by the OpenGL program. Matrix multiplications are performed in vertex shaders. Vertex coordinates before transformation Vertex coordinates after transformation
Summary Affine transformations are used for transforming 3D objects. Three elementary affine transformations: Translation Scaling Rotation 3D rotation is the most complicated among the three. If we use homogeneous coordinates, then all affine transformations can be represented by affine matrix multiplication. Notice the pattern of transformation in OpenGL programming.
OpenGL Tutorial Programs Download OpenGL transformation tutorial program from http://www.xmission.com/~nate/tutors.html