CM0304 Graphics I Graphics Hardware I.1 Graphics Systems
CMT107 Visual Computing
Copyright By PowCoder代写 加微信 powcoder
II.1 Transformations
Xianfang Sun
School of Computer Science & Informatics
Cardiff University
➢ Model transformations
• 2D/3D linear transformations
• 2D/3D affine transformations
➢Homogeneous coordinates
• Homogeneous affine transformations
➢Coordinate transformations
• Reference frames
• Object vs. Frame Transformations
• Camera Transformation
➢OpenGL transformations
3:56 PM 2CMT107 Visual Computing
Model Transformations
3:56 PM 3CMT107 Visual Computing
➢ Transforming an object: transforming all its points
➢ Transforming a polygonal model: transforming its vertices
Transformations can be combined
(with simple algebra)
x” = (xsx) cos – (ysy) sin
y” = (ysy) cos + (ysy) sin
Basic 2D Transformations
3:56 PM 4CMT107 Visual Computing
x”’ = x” + hx y”
y”’ = y”+ hy x”
x”” = x” + hx y” + tx
y”” = y” + hy x” + ty
x’ = x sx
y’ = y sy
(mirror: sx and/or sy = -1)
x’ = x cos – y sin
y’ = x sin + y cos
x’ = x + hx y
y’ = y + hy · x
➢ Translate:
x’ = x + tx
y’ = y + ty
Matrix Representations
3:56 PM 5CMT107 Visual Computing
➢ Matrices are convenient to represent linear transformations:
• In general:
➢ Efficient due to hardware matrix multiplication
Linear Transformations
3:56 PM 6CMT107 Visual Computing
➢ Linear transformations are combinations of
– scaling, mirroring, rotation, shearing
➢Properties of linear transformations T:
• Satisfies
• Origin maps to origin
• Lines map to lines
• Parallel lines remain parallel
• Ratios are preserved
• Closed under composition (The composition of two or
more linear transformations is a linear transformation)
➢ Translation is not linear transformation
Rssssss +=+
2122112211
,),()()( vTvTvvT
)())(()))(((
vTvTTTvTTT ==
Affine Transformations
3:56 PM 7CMT107 Visual Computing
➢ Affine transformations are combinations of
• Linear transformations (matrices)
•Translations (vectors)
• General representation
➢ Properties of affine transformations:
• Origin does not necessarily map to origin
• Lines map to lines
• Parallel lines remain parallel
• Ratios are preserved
• Closed under composition
Homogeneous Coordinates
3:56 PM 8CMT107 Visual Computing
➢ Homogeneous coordinates in 2D
• (x, y, w) represents a point at position (x/w, y/w)
•(x, y, 0) represents a point at infinity or direction
•(0, 0, 0) is not allowed
➢ We need a 3rd coordinate for 2D points to represent
translations solely with matrices
➢ 2D translation can be represented by a 3 3 matrix:
Homogeneous 2D Transformations
3:56 PM 9CMT107 Visual Computing
➢ Basic 2D homogeneous transformation matrices
• Translate:
3D Transformations
3:56 PM 10CMT107 Visual Computing
➢ Same idea as 2D transformations
• Linear transformation:
• Affine transformation:
➢ Common 3D transformation matrices:
Scale/mirror Rotate around Z axis
Rotate around Y axis Rotate around X axis
Homogeneous 3D Transformations
3:56 PM 11CMT107 Visual Computing
➢ Homogeneous coordinates in 3D:
• (x, y, z, w) represents 3D position (x/w, y/w, z/w)
• (x, y, z, 0) represents a point at infinity or direction
• (0, 0, 0, 0) is not allowed
➢ Affine transformations represented by matrices
Identity Scale Mirror over x axis
Homogeneous 3D Rotations
3:56 PM 12CMT107 Visual Computing
Rotate around z axis Rotate around y axis
Rotate around x axis Translation
Matrix Composition
3:56 PM 13CMT107 Visual Computing
➢ Transformations can be combined by matrix multiplication
➢ Using homogeneous coordinates all affine transformations
can be represented by matrices
• Matrix multiplication is associative:
• Simple way to combine transformations
• Only one matrix multiplication to transform vertices
➢ Beware: order of transformations matters
• Matrix multiplication is not commutative:
))(())()(())))((((
pTTTpTTTpTTTp ===
pTTpTT
Reference Frames
3:56 PM 14CMT107 Visual Computing
Coordinate Transformations
3:56 PM 15CMT107 Visual Computing
➢ Scenes are defined in a world-coordinate system
➢ Objects in a scene are represented in a local object
coordinate system
• Transform local coordinates into other local coordinates
• Ultimately transform local coordinates into world
coordinates
➢ A camera is represented in a camera coordinate system
• A scene is viewed by a camera from an arbitrary
position and orientation
• Transform world-coordinates into camera coordinates
➢ Transformation from object coordinate system to camera
coordinate system can be represented by a signal matrix
called model-view matrix in OpenGL.
Object vs. Coordinate Transformations
3:56 PM 16CMT107 Visual Computing
➢ Object transformations transfer a object in a fixed
coordinate system
➢ Coordinate transformations transform an object’s
coordinates from one coordinate system to another,
while keep the object at its original position.
➢ The coordinates of a object transformation can be
obtained equivalently by a coordinate transformation
Object vs. Coordinate Transformations
3:56 PM 17CMT107 Visual Computing
➢ Translate an object by (tx, ty, tz) is equivalent to translate
the reference frame by (-tx, -ty, -tz)
➢ Rotate an object around an axis by angle is equivalent
to rotate the reference frame around the same axis by
➢ Scale an object in a direction by value s is equivalent to
scale the reference frame in the same direction by value
Camera Analogy
3:56 PM 18CMT107 Visual Computing
➢ Define a synthetic camera to determine view of a scene
➢ Camera parameters:
• Eye position (x, y, z)
• View direction (towards vector, up vector)
• Field of view (xfov, yfov)
Camera Coordinates
3:56 PM 19CMT107 Visual Computing
➢ Mapping from world to camera coordinates (normalisation)
• Origin moves to eye position
• Up vector maps to Y axis, right vector maps to X axis
• Canonical coordinate system for camera coordinates
• Convention is right-handed
• New versions of OpenGL adopts left-handed Frame
Camera Transformation
3:56 PM 20CMT107 Visual Computing
➢ Transformation matrix maps camera basis vectors to
canonical vectors in camera coordinate system
world coordinates camera coordinates
(xw, yw, zw, ww)
t M→ (xc, yc, zc, wc)
Derivation of Camera Transformation
3:56 PM 21CMT107 Visual Computing
➢ Let the camera transformation matrix be M, then
because R, U, B, and E are transformed to [1 0 0 0]T,
[0 1 0 0]T, [0 0 1 0]T, and [0 0 0 1]T, respectively, we have
, , ,
➢ Combine them together form the matrix equation
➢ And thus matrix M is the inverse of the right matrix
➢ Note that R, U, and B represent direction, so Rw=Uw=Bw=0
➢ E represents a point, so here Ew = 1
Current Transformation Matrix
3:56 PM 22CMT107 Visual Computing
➢ Conceptually two 44 matrices:
• a model-view and a projection matrix in pipeline
• Both matrices form the current transformation matrix
• All vertices are transformed by the CTM
OpenGL Transformations
3:56 PM 23CMT107 Visual Computing
➢ Early versions of OpenGL use some functions to represent
transformations (matrix computations)
➢ Current OpenGL with shaders needs the programmers to
write their own transformation code
➢ Maths libraries for matrix computations are available
• vecmath from java package javax.vecmath
➢ An example simple matrix computation package is
provided in the labs of this module
➢ Vec3.java, Vec4.java, Mat4.java, Transform.java
Matrix Representation in OpenGL
3:56 PM 24CMT107 Visual Computing
➢ OpenGL uses 4×4 matrices to represent transformations
➢ A matrix is stored in a vector in the program
➢ Two orders to store a matrix in a vector
• Row major (in row by row order)
• column major (in column by column order)
➢We use row major order in the package provided
➢ Shaders use column major order to represent matrices
➢ Post-multiplying with column-major matrices produces
the same result as pre-multiplying with row-major
Transform Class
3:56 PM 25CMT107 Visual Computing
➢ In Transfrom.java, a class Transform is defined. T is the
transformation matrix
➢ Constructor Transform(), or function initialize() will assign
T as an identity matrix
➢ Functions scale(), translate(), rotateX(), rotateY(),
rotateZ() perform as their names defined
➢ rotateA() performs rotation around an arbitrary axis
➢ reverseZ() is to convert right-hand frame to left-hand
➢ lookAt() is to locate the camera in the scene
• Transform the model coordinates into camera frame
➢ ortho(), frustum(), and perspective() perform projection
transformation (discuss later)
Function scale()
3:56 PM 26CMT107 Visual Computing
➢ Pre-multiply the current matrix T by a scaling
transformation matrix
➢ For scale(sx, sy, sz), the scaling matrix is:
➢ Current matrix is modified as:
Function scale()
3:56 PM 27CMT107 Visual Computing
➢ Implementation of function scale(sx, sy, sz):
public void scale(float sx, float sy, float sz){
for(int i=0;i<4;i++) { T.M[0][i] = T.M[0][i]*sx; T.M[1][i] = T.M[1][i]*sy; T.M[2][i] = T.M[2][i]*sz; 3:56 PM 28CMT107 Visual Computing Simulate gluLookAt() function in early versions of OpenGL void lookAt(eyex,eyey,eyez, atx,aty,atz, upx,upy,upz) Use Transform class 3:56 PM 29CMT107 Visual Computing // Define a Transformation instance // Transformation matrix is initialised as Identity; Transform T = new Transform(); // In display(), load Identity matrix T.initialize(); //Do transformations T.scale(scale, scale, scale); T.rotateX(rx); T.rotateY(ry); T.translate(tx, ty, 0); //set up the camera T.lookAt(0, 0, 0, 0, 0, -100, 0, 1, 0); //default parameters // Send model_view matrix to shader. Here true for transpose //means converting the row-major matrix to column major one gl.glUniformMatrix4fv( ModelView, 1, true, T.getTransformv(), 0 ); 3:56 PM 30CMT107 Visual Computing ➢ What is a reference frame? How can points in space be represented? ➢ What are linear and affine transformations? ➢ What are homogeneous coordinates? For what are they ➢ List some common/basic linear and affine 2D/3D transformations and their representation for Cartesian and homogeneous coordinates. ➢ What is object transformation and what is frame transformation? What’s their relation? ➢ How can one build more complex affine transformations from the basic transformations? 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com