Transcript 7

Transformations
CS 445/645
Introduction to Computer Graphics
David Luebke, Spring 2003
Admin
● Call roll
● Assn 1
■ News flash: Assn 1 not necessarily a gimme
■ Turn-in instructions by email soon
■ Problems with lab?
David Luebke
2
7/27/2016
Recap:
OpenGL: Modeling Transforms
● OpenGL provides several commands for performing
modeling transforms:
■ glTranslate{fd}(x, y, z)
○ Creates a matrix T that transforms an object by translating
(moving) it by the specified x, y, and z values
■ glRotate{fd}(angle, x, y, z)
○ Creates a matrix R that transforms an object by rotating it
counterclockwise angle degrees about the vector {x, y, z}
■ glScale{fd}(x, y, z)
○ Creates a matrix S that scales an object by the specified factors in
the x, y, and z directions
David Luebke
3
7/27/2016
Recap:
OpenGL: Matrix Manipulation
● Each of these postmultiplies the current matrix
■ E.g., if current matrix is C, then C=CS
■ The current matrix is either the modelview matrix or the
projection matrix (also a texture matrix, won’t discuss)
■ Set these with glMatrixMode(), e.g.:
glMatrixMode(GL_MODELVIEW);
glMatrixMode(GL_PROJECTION);
■ WARNING: common mistake ahead!
○ Be sure that you are in GL_MODELVIEW mode before making
modeling or viewing calls!
○ Ugly mistake because it can appear to work, at least for a while…
David Luebke
4
7/27/2016
Recap:
OpenGL: Matrix Manipulation
● More matrix manipulation calls
■ To replace the current matrix with an identity matrix:
glLoadIdentity()
■ Postmultiply the current matrix with an arbitrary matrix:
glMultMatrix{fd}(float/double m[16])
■ Copy the current matrix and push it onto a stack:
glPushMatrix()
■ Discard the current matrix and replace it with whatever’s
on top of the stack:
glPopMatrix()
■ Note that there are matrix stacks for both modelview and
projection modes
David Luebke
5
7/27/2016
OpenGL: Specifying Color
● Can specify other properties such as color
■ To produce a single aqua-colored triangle:
glColor3f(0.1, 0.5, 1.0);
glVertex3fv(v0);
glVertex3fv(v1);
glVertex3fv(v2);
■ To produce a Gouraud-shaded triangle:
glColor3f(1, 0, 0); glVertex3fv(v0);
glColor3f(0, 1, 0); glVertex3fv(v1);
glColor3f(0, 0, 1); glVertex3fv(v2);
■ In OpenGL, colors can also have a fourth component 
(opacity)
○ Generally want  = 1.0 (opaque);
David Luebke
6
7/27/2016
OpenGL: Specifying Normals
● Calling glColor() sets the color for all vertices
following, until the next call to glColor()
● Calling glNormal() sets the normal vector for the
following vertices, till next glNormal()
● So flat-shaded lighting requires:
glNormal3f(Nx, Ny, Nz);
glVertex3fv(v0);glVertex3fv(v1);glVertex3fv(v2);
■ While smooth shading requires:
glNormal3f(N0x, N0y, N0z); glVertex3fv(v0);
glNormal3f(N1x, N1y, N1z); glVertex3fv(v1);
glNormal3f(N2x, N2y, N2z); glVertex3fv(v2);
■ (Of course, lighting requires additional setup…)
David Luebke
7
7/27/2016
More OpenGL
● Other things you’ll need to know:
■ To clear the screen:
glClearColor(r, g, b, a);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
● Nate Robins has an excellent set of OpenGL tutorials
that help illustrate many of these concepts & functions:
http://www.cs.utah.edu/~narobins/opengl.html
Also http://xmission.com/~nate/tutors.html
● Next: the math and concepts underlying the
transformation calls
David Luebke
8
7/27/2016
Translations
● For convenience we usually describe objects in
relation to their own coordinate system
● We can translate or move points to a new position
by adding offsets to their coordinates:
 x'  x  tx 
 y '   y   ty 
     
 z '   z  tz 
■ Note that this translates all points uniformly
David Luebke
9
7/27/2016
Scaling
● Scaling a coordinate means multiplying each of its
components by a scalar
● Uniform scaling means this scalar is the same for all
components:
2
David Luebke
10
7/27/2016
Scaling
● Non-uniform scaling: different scalars per
component:
X  2,
Y  0.5
● How can we represent this in matrix form?
David Luebke
11
7/27/2016
Scaling
● Scaling operation:
 x' ax 
 y '  by 
   
 z '   cz 
● Or, in matrix form:
 x '   a 0 0  x 
 y'  0 b 0  y 
  
 
 z '  0 0 c   z 
scaling matrix
David Luebke
12
7/27/2016
2-D Rotation
(x’, y’)
(x, y)

David Luebke
x’ = x cos() - y sin()
y’ = x sin() + y cos()
(Draw it)
13
7/27/2016
2-D Rotation
● This is easy to capture in matrix form:
 x' cos  sin   x 
 y '   sin  cos   y 
  
 
● 3-D is more complicated
■ Need to specify an axis of rotation
■ Simple cases: rotation about X, Y, Z axes
David Luebke
14
7/27/2016
3-D Rotation
● What does the 3-D rotation matrix look like for a
rotation about the Z-axis?
■ Build it coordinate-by-coordinate
 x' cos()  sin( ) 0  x 
 y '   sin( ) cos() 0  y 
  
 
 z '   0
0
1  z 
David Luebke
15
7/27/2016
3-D Rotation
● What does the 3-D rotation matrix look like for a
rotation about the Y-axis?
■ Build it coordinate-by-coordinate
 x'  cos() 0 sin( )   x 
 y '   0



1
0
y
  
 
 z '   sin( ) 0 cos()  z 
David Luebke
16
7/27/2016
3-D Rotation
● What does the 3-D rotation matrix look like for a
rotation about the X-axis?
■ Build it coordinate-by-coordinate
0
0  x
 x' 1
 y '  0 cos()  sin( )  y 
  
 
 z '  0 sin( ) cos()   z 
David Luebke
17
7/27/2016
3-D Rotation
● General rotations in 3-D require rotating about an
arbitrary axis of rotation
● Deriving the rotation matrix for such a rotation
directly is a good exercise in linear algebra
● Standard approach: express general rotation as
composition of canonical rotations
■ Rotations about X, Y, Z
David Luebke
18
7/27/2016
Composing Canonical
Rotations
● Goal: rotate about arbitrary vector A by 
■ Idea: we know how to rotate about X,Y,Z
So, rotate about Y by  until A lies in the YZ plane
Then rotate about X by  until A coincides with +Z
Then rotate about Z by 
Then reverse the rotation about X (by -)
Then reverse the rotation about Y (by -)
David Luebke
19
7/27/2016