CS452/552; EE465/55 Transformations 1-29-15
Outline! Transformations Read: Angel, Chapter 4 (study cube.html/cube.js example) Helpful links: Linear Algebra: Khan Academy Lab1 is posted on github, due: Friday, 1/3, midnight Lab2: use ASDW to move a 2D shape around
Recap: Translation Matrix We can express translation using a 4 x 4 matrix T in homogeneous coordinates p =Tp where T = T(d x, d y, d z ) = & 1 $ $ $ $ % 1 1 This form is better for implementation because all affine transformations can be expressed this way and multiple transformations can be concatenated together d d d x y z 1 #!!!! " Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
Translation! Move the origin to a new location!!!!!! " # $ $ $ $ $ $ % & = 1 1 1 1 ),, ( z y x z y x t t t t t t T
Rotation (2D) Consider rotation about the origin by θ degrees radius stays the same, angle increases by θ x = r cos (φ + θ) y = r sin (φ + θ) x = x cos θ y sin θ y = x sin θ + y cos θ x = r cos φ y = r sin φ Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
Rotation about the z axis! Rotation about z axis in three dimensions leaves all points with the same z Equivalent to rotation in two dimensions in planes of constant z x = x cos θ y sin θ y = x sin θ + y cos θ z = z or in homogeneous coordinates p =R z (θ)p Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
R = R z (θ) = Rotation Matrix!!!! " # $ $ $ $ % & θ θ θ θ 1 1 cos sin sin cos Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
Rotation about x and y axes! Same argument as for rotation about z axis For rotation about x axis, x is unchanged For rotation about y axis, y is unchanged & 1 # $ cos θ - sin θ $ R = R x (θ) = $ sin θ cos θ $!!!! % 1" & cos θ sin θ # $ R = R y (θ) = 1 $ $ - sin θ cos θ $!!!! % 1" Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
Scaling Expand or contract along each axis (fixed point of origin) x =s x x y =s y y z =s z z p =Sp S = S(s x, s y, s z ) = & sx $ $ $ $ % s y s z #!!!! 1" Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
Reflection corresponds to negative scale factors s x = -1 s y = 1 original s x = -1 s y = -1 s x = 1 s y = -1 Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
Inverses! Although we could compute inverse matrices by general formulas, we can use simple geometric observations Translation: T -1 (d x, d y, d z ) = T(-d x, -d y, -d z ) Rotation: R -1 (θ) = R(-θ) Holds for any rotation matrix Note that since cos(-θ) = cos(θ) and sin(-θ)=-sin(θ) R -1 (θ) = R T (θ) Scaling: S -1 (s x, s y, s z ) = S(1/s x, 1/s y, 1/s z ) Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
Concatenation! We can form arbitrary affine transformation matrices by multiplying together rotation, translation, and scaling matrices! Because the same transformation is applied to many vertices, the cost of forming a matrix M=ABCD is not significant compared to the cost of computing Mp for many vertices p! The difficult part is how to form a desired transformation from the specifications in the application Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
Order of Transformations! Note that matrix on the right is the first applied! Mathematically, the following are equivalent p = ABCp = A(B(Cp))! Note many references use column matrices to represent points. In terms of column matrices p T = p T C T B T A T Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
MATRIX ORDER Multiply left to right Results are drastically different (an angry vertex) JEFF CHASTINE
MATRIX ORDER Multiply left to right Results are drastically different Order of operations Rotate 45 JEFF CHASTINE
MATRIX ORDER Multiply left to right Results are drastically different Order of operations Rotate 45 Translate 1 units JEFF CHASTINE
MATRIX ORDER Multiply left to right Results are drastically different Order of operations Rotate 45 Translate 1 units before after JEFF CHASTINE
MATRIX ORDER Multiply left to right Results are drastically different Order of operations JEFF CHASTINE
MATRIX ORDER Multiply left to right Results are drastically different Order of operations Translate 1 units JEFF CHASTINE
MATRIX ORDER Multiply left to right Results are drastically different Order of operations Translate 1 units Rotate 45 JEFF CHASTINE
MATRIX ORDER Multiply left to right Results are drastically different Order of operations Translate 1 units Rotate 45 after before JEFF CHASTINE
General Rotation About the Origin! A rotation by θ about an arbitrary axis can be decomposed into the concatenation of rotations about the x, y, and z axes R(θ) = R z (θ z ) R y (θ y ) R x (θ x ) y θ v θ x θ y θ z are called the Euler angles x Note that rotations do not commute We can use rotations in another order but with different angles z Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
Rotation About a Fixed Point other than the Origin Move fixed point to origin Rotate Move fixed point back M = T(p f ) R(θ) T(-p f ) Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
WebGL Transformations! Learn how to carry out transformations in WebGL Rotation Translation Scaling! Introduce MV.js transformations Model-view Projection Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215 3 24
The Big Picture! How to we get from space to space?! For every model Have a (M)odel matrix! Transforms from object to world space M? Jeff Chastine
The Big Picture! How to we get from space to space?! To put in camera space Have a (V)iew matrix Usually need only one of these M V Jeff Chastine
The Big Picture! How to we get from space to space?! The ModelView matrix Sometimes these are combined into one matrix Usually keep them separate for convenience M V MV Jeff Chastine
Back to The Big Picture! How might we make the model matrix? M Jeff Chastine
Back to The Big Picture! How might we make the model matrix? M Translation matrix T Rotation matrix R 1 Rotation matrix R 2 Scale matrix S Jeff Chastine
Back to The Big Picture! How might we make the model matrix? M Translation matrix T Rotation matrix R 1 Rotation matrix R 2 Scale matrix S T * R 1 * R 2 * S = M Jeff Chastine
Pre 3.1 OpenGL Matrices! In Pre 3.1 OpenGL matrices were part of the state! Multiple types Model-View (GL_MODELVIEW) Projection (GL_PROJECTION) Texture (GL_TEXTURE) Color(GL_COLOR)! Single set of functions for manipulation! Select which to manipulated by glmatrixmode(gl_modelview); glmatrixmode(gl_projection); Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
Why Deprecation! Functions were based on carrying out the operations on the CPU as part of the fixed function pipeline! Current model-view and projection matrices were automatically applied to all vertices using CPU! We will use the notion of a current transformation matrix with the understanding that it may be applied in the shaders Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
Current Transformation Matrix (CTM)! Conceptually there is a 4 x 4 homogeneous coordinate matrix, the current transformation matrix (CTM) that is part of the state and is applied to all vertices that pass down the pipeline! The CTM is defined in the user program and loaded into a transformation unit p C p =Cp vertices CTM vertices Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
CTM operations! The CTM can be altered either by loading a new CTM or by postmutiplication Load an identity matrix: C I Load an arbitrary matrix: C M Load a translation matrix: C T Load a rotation matrix: C R Load a scaling matrix: C S Postmultiply by an arbitrary matrix: C CM Postmultiply by a translation matrix: C CT Postmultiply by a rotation matrix: C C R Postmultiply by a scaling matrix: C C S Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
Rotation about a Fixed Point Start with identity matrix: C I Move fixed point to origin: C CT Rotate: C CR Move fixed point back: C CT -1 Result: C = TR T 1 which is backwards. This result is a consequence of doing postmultiplications. Let s try again. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
Reversing the Order We want C = T 1 R T so we must do the operations in the following order C I C CT -1 C CR C CT Each operation corresponds to one function call in the program. Note that the last operation specified is the first executed in the program Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
CTM in WebGL! OpenGL had a model-view and a projection matrix in the pipeline which were concatenated together to form the CTM! We will emulate this process Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
Using the ModelView Matrix! In WebGL, the model-view matrix is used to Position the camera Can be done by rotations and translations but is often easier to use the lookat function in MV.js Build models of objects! The projection matrix is used to define the view volume and to select a camera lens! Although these matrices are no longer part of the OpenGL state, it is usually a good strategy to create them in our own applications q = P*MV*p Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
Rotation, Translation, Scaling Create an identity matrix: var m = mat4(); Multiply on right by rotation matrix of theta in degrees where (vx, vy, vz) define axis of rotation var r = rotate(theta, vx, vy, vz) m = mult(m, r); Also have rotatex, rotatey, rotatez Do same with translation and scaling: var s = scale( sx, sy, sz) var t = translate(dx, dy, dz); m = mult(s, t); Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
Example using MV.js! Rotation about z axis by 3 degrees with a fixed point of (1., 2., 3.) var m = mult(translate(1., 2., 3.), rotate(3.,.,., 1.)); m = mult(m, translate(-1., -2., -3.));! Remember that last matrix specified in the program is the first applied Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
Arbitrary Matrices! Can load and multiply by matrices defined in the application program! Matrices are stored as one dimensional array of 16 elements by MV.js but can be treated as 4 x 4 matrices in row major order! OpenGL wants column major data! gl.unifrommatrix4f has a parameter for automatic transpose but it must be set to false.! flatten function converts to column major order which is required by WebGL functions Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
Matrix Stacks! In many situations we want to save transformation matrices for use later Traversing hierarchical data structures (Chapter 9)! Pre 3.1 OpenGL maintained stacks for each type of matrix! Easy to create the same functionality in JS push and pop are part of Array object var stack = [ ] stack.push(modelviewmatrix); modelviewmatrix = stack.pop(); Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
Examples! TranslatedTriangle! ScaledTriangle_Matrix! RotatedTriangle_Matrix! TranslatedRotatedTriangle! RotatingTranslatedTriangle available on website https://sites.google.com/site/webglbook/ 43
TranslatedTriangle! Vertex Shader attribute vec4 a_position; uniform vec4 u_translation; void main() { } gl_position = a_position + u_translation; 44
TranslatedTriangle, cont. var vertices = new Float32Array([,.5, -.5, -.5,.5, -.5]); var vertexbuffer = gl.createbuffer(); // Create a buffer object // Bind the buffer object to target & write data to the buffer gl.bindbuffer(gl.array_buffer, vertexbuffer); gl.bufferdata(gl.array_buffer, vertices, gl.static_draw); // Assign the buffer object to the attribute variable var a_position = gl.getattriblocation(gl.program, 'a_position'); gl.vertexattribpointer(a_position, 2, gl.float, false,, ); // Enable the assignment to a_position variable gl.enablevertexattribarray(a_position);
TranslatedTriangle, cont. // The translation distance for x, y, and z direction var Tx =.5, Ty =.5, Tz =.; // Pass the translation distance to the vertex shader var u_translation = gl.getuniformlocation(gl.program, 'u_translation'); gl.uniform4f(u_translation, Tx, Ty, Tz,.);
ScaledTriangle_Matrix! Vertex Shader attribute vec4 a_position; uniform mat4 u_xformmatrix; void main() { } gl_position = u_xformmatrix * a_position; 47
ScaledTriangle_Matrix, cont. var Sx = 1., Sy = 1.5, Sz = 1.; // Note: WebGL is column major order var xformmatrix = new Float32Array([ Sx,.,.,.,., Sy,.,.,.,., Sz,.,.,.,., 1. ]); // Pass the rotation matrix to the vertex shader var u_xformmatrix = gl.getuniformlocation(gl.program, 'u_xformmatrix'); gl.uniformmatrix4fv(u_xformmatrix, false, xformmatrix);
RotatedTriangle_Matrix! Vertex Shader attribute vec4 a_position; uniform mat4 u_xformmatrix; void main() { } gl_position = u_xformmatrix * a_position; 49
RotatedTriangle_Matrix, cont. // Create Matrix4 object for the rotation matrix var xformmatrix = new Matrix4(); // Set the rotation matrix var ANGLE = 9.; // The rotation angle xformmatrix.setrotate(angle,,, 1); // Pass the rotation matrix to the vertex shader var u_xformmatrix = gl.getuniformlocation(gl.program, 'u_xformmatrix'); gl.uniformmatrix4fv(u_xformmatrix, false, xformmatrix.elements);
RotatedTriangle_Matrix, cont. // xformmatrix.setrotate(angle,,, 1); Matrix4.setRotate = function(angle, x, y, z) { s = Math.sin(angle); c = Math.cos(angle); see code in cuon-matrix.js Alternative: Angel MV.js
Example: Rotating Cube! Example: Begin with a cube rotating! Use mouse or button listener to change direction of rotation! Start with a program that draws a cube in a standard way Centered at origin Sides aligned with axes Will discuss modeling in next lecture! Two methods for display: vertices vs. indices Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
Where do we apply transformation?! Same issue as with rotating square in application to vertices in vertex shader: send MV matrix in vertex shader: send angles! Choice between second and third unclear! Do we do trigonometry once in CPU or for every vertex in shader GPUs have trig functions hardwired in silicon Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
Rotation Event Listeners document.getelementbyid( "xbutton" ).onclick = function () { axis = xaxis; }; document.getelementbyid( "ybutton" ).onclick = function () { axis = yaxis; }; document.getelementbyid( "zbutton" ).onclick = function () { axis = zaxis; }; function render(){ gl.clear( gl.color_buffer_bit gl.depth_buffer_bit); theta[axis] += 2.; gl.uniform3fv(thetaloc, theta); gl.drawarrays( gl.triangles,, NumVertices ); requestanimframe( render ); } Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
Rotation Shader attribute vec4 vposition; attribute vec4 vcolor; varying vec4 fcolor; uniform vec3 theta; void main() { vec3 angles = radians( theta ); vec3 c = cos( angles ); vec3 s = sin( angles ); // Remember: these matrices are column-major mat4 rx = mat4( 1.,.,.,.,., c.x, s.x,.,., -s.x, c.x,.,.,.,., 1. ) Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
Rotation Shader (cont) mat4 ry = mat4( c.y,., -s.y,.,., 1.,.,., s.y,., c.y,.,.,.,., 1. ); mat4 rz = mat4( c.z, -s.z,.,., s.z, c.z,.,.,.,., 1.,.,.,.,., 1. ); fcolor = vcolor; gl_position = rz * ry * rx * vposition; } Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
Modeling a Cube Define global array for vertices var vertices = [ vec3( -.5, -.5,.5 ), vec3( -.5,.5,.5 ), vec3(.5,.5,.5 ), vec3(.5, -.5,.5 ), vec3( -.5, -.5, -.5 ), vec3( -.5,.5, -.5 ), vec3(.5,.5, -.5 ), vec3(.5, -.5, -.5 ) ]; Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
Colors Define global array for colors var vertexcolors = [ [.,.,., 1. ], // black [ 1.,.,., 1. ], // red [ 1., 1.,., 1. ], // yellow [., 1.,., 1. ], // green [.,., 1., 1. ], // blue [ 1.,., 1., 1. ], // magenta [., 1., 1., 1. ], // cyan [ 1., 1., 1., 1. ] // white ]; Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215
Draw cube from faces function colorcube( ) { quad(,3,2,1); quad(2,3,7,6); quad(,4,7,3); quad(1,2,6,5); quad(4,5,6,7); quad(,1,5,4); } 1 5 6 2 7 4 3 Note that vertices are ordered so that we obtain correct outward facing normals. Each quad generates two triangles Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 215