transformations · transformations david carr virtual environments, fundamentals spring 2005 based...

18
1 Jan-27-05 SMM009, Transformations 1 L INSTITUTIONEN FÖR SYSTEMTEKNIK LULEÅ TEKNISKA UNIVERSITET Transformations David Carr Virtual Environments, Fundamentals Spring 2005 Based on Slides by E. Angel Jan-27-05 SMM009, Transformations 2 L Overview Standard transformations - Rotations, Translation, Scaling, Shear - Homogeneous coordinate transformation matrices - Constructing arbitrary transformation matrices from simple ones Transformations in OpenGL - Rotation, Translation , Scaling - OpenGL matrix modes + Model-view, Projection Jan-27-05 SMM009, Transformations 3 L INSTITUTIONEN FÖR SYSTEMTEKNIK LULEÅ TEKNISKA UNIVERSITET Standard Transformations Review

Upload: others

Post on 18-Oct-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Transformations · Transformations David Carr Virtual Environments, Fundamentals Spring 2005 Based on Slides by E. Angel Jan-27-05 SMM009,Transformations 2 L Overview •Standard

1

Jan-27-05 SMM009, Transformations 1 L

INSTITUTIONEN FÖRSYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

Transformations

David Carr

Virtual Environments, Fundamentals

Spring 2005Based on Slides by E. Angel

Jan-27-05 SMM009, Transformations 2 L

Overview

• Standard transformations- Rotations, Translation, Scaling, Shear

- Homogeneous coordinate transformation matrices

- Constructing arbitrary transformation matrices from simpleones

• Transformations in OpenGL- Rotation, Translation , Scaling

- OpenGL matrix modes

+ Model-view, Projection

Jan-27-05 SMM009, Transformations 3 L

INSTITUTIONEN FÖRSYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

Standard Transformations

Review

Page 2: Transformations · Transformations David Carr Virtual Environments, Fundamentals Spring 2005 Based on Slides by E. Angel Jan-27-05 SMM009,Transformations 2 L Overview •Standard

2

Jan-27-05 SMM009, Transformations 4 L

General Transformations

• A transformation maps points to other points and/orvectors to other vectors

Q=T(P)

v=T(u)

Jan-27-05 SMM009, Transformations 5 L

Affine Transformations

• Line preserving

• Characteristic of many physically importanttransformations

- Rigid body transformations: rotation, translation

- Scaling, shear

• Importance in graphics is that:- Need only transform endpoints of line segments

- Let implementation draw line segment between thetransformed endpoints

Review

Jan-27-05 SMM009, Transformations 6 L

Pipeline Implementation

transformation rasterizer

u

u

v

T

T(u)

T(v)

T(u)T(u)

T(v)

T(v)

vertices vertices pixels

framebuffer

(from application program)

v

Review

Page 3: Transformations · Transformations David Carr Virtual Environments, Fundamentals Spring 2005 Based on Slides by E. Angel Jan-27-05 SMM009,Transformations 2 L Overview •Standard

3

Jan-27-05 SMM009, Transformations 7 L

Notation

• Work with both:- Coordinate-free representations of transformations

- Representations within a particular frame

• P,Q, R: points in an affine space

• u, v, w: vectors in an affine space• α, β, γ: scalars

• p, q, r: representations of points- array of 4 scalars in homogeneous coordinates [x y z 1]T

• u, v, w: representations of vectors- array of 4 scalars in homogeneous coordinates [x y z 0]T

Jan-27-05 SMM009, Transformations 8 L

Translation

• Move (translate, displace) a point to a new location

• Displacement determined by a vector d- Three degrees of freedom- P’=P+d

P

P’

d

Jan-27-05 SMM009, Transformations 9 Lobject translation: every point displaced

by same vector

How Many Ways?

• Although we can move a point to a new location ininfinite ways, when we move many points there isusually only one way

Page 4: Transformations · Transformations David Carr Virtual Environments, Fundamentals Spring 2005 Based on Slides by E. Angel Jan-27-05 SMM009,Transformations 2 L Overview •Standard

4

Jan-27-05 SMM009, Transformations 10 L

note that this expression is in four dimensions and expressesthat point = vector + point

Translation Using Representations

• Using the homogeneous coordinate representation insome frame- p=[ x y z 1]T

- p’=[x’ y’ z’ 1]T

- d=[dx dy dz 0]T

• Hence p’ = p + d or- x’=x+dx

- y’=y+dy

- z’=z+dz

Jan-27-05 SMM009, Transformations 11 L

!!!!

"

#

$$$$

%

&

1000

d100

d010

d001

z

y

x

Translation Matrix

• We can also express translation using a 4 x 4 matrix T inhomogeneous coordinates

p’=Tp where

T = T(dx, dy, dz) =

• This form is better for implementation because:- All affine transformations can be expressed this way- Multiple transformations can be concatenated together

Review

Jan-27-05 SMM009, Transformations 12 L

Rotation (2D)

• Consider rotation about the origin by θ degrees

- radius stays the same, angle increases by θ

x’=x cos θ –y sin θy’ = x sin θ + y cos θ

x = r cos φy = r sin φ

x = r cos (φ + θ)y = r sin (φ + θ)

Page 5: Transformations · Transformations David Carr Virtual Environments, Fundamentals Spring 2005 Based on Slides by E. Angel Jan-27-05 SMM009,Transformations 2 L Overview •Standard

5

Jan-27-05 SMM009, Transformations 13 L

• Rotation about z axis in three dimensions leaves allpoints with the same z- Equivalent to rotation in 2 dimensions in planes of constant z

- or in homogeneous coordinates

x’=x cos θ –y sin θy’ = x sin θ + y cos θz’ =z

Rotation About the Z-Axis

p’ = Rz (θ)p

Jan-27-05 SMM009, Transformations 14 L

Rotation Matrix

!!!!

"

#

$$$$

%

&

''

'('

1000

0100

00 cossin

00sin cos

R = Rz(θ) =

Review

Jan-27-05 SMM009, Transformations 15 L

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

R = Rx(θ) =

R = Ry(θ) =

!!!!

"

#

$$$$

%

&

''

''

1000

0 cos sin0

0 sin- cos0

0001

!!!!

"

#

$$$$

%

&

''

''

1000

0 cos0 sin-

0010

0 sin0 cos

Review

Page 6: Transformations · Transformations David Carr Virtual Environments, Fundamentals Spring 2005 Based on Slides by E. Angel Jan-27-05 SMM009,Transformations 2 L Overview •Standard

6

Jan-27-05 SMM009, Transformations 16 L!!!!

"

#

$$$$

%

&

1000

000

000

000

z

y

x

s

s

s

x’=sxxy’=syyz’=szz

p’=Sp

S = S(sx, sy, sz) =

Scaling

• Expand or contract along each axis (fixed point oforigin)

Jan-27-05 SMM009, Transformations 17 L

originalsx = -1 sy = 1

sx = -1 sy = -1 sx = 1 sy = -1

Reflection

• Corresponds to negative scale factors

Jan-27-05 SMM009, Transformations 18 L

Inverses

• Could compute inverse matrices by generalformulas,

• But, simple geometric observations are easier- Translation: T-1(dx, dy, dz) = T(-dx, -dy, -dz)- Rotation: R -1(θ) = R(-θ)

+Holds for any rotation matrix+Note that since cos(-θ) = cos(θ) and

sin(-θ)=-sin(θ), R -1(θ) = R T(θ)- Scaling: S-1(sx, sy, sz) = S(1/sx, 1/sy, 1/sz)

Page 7: Transformations · Transformations David Carr Virtual Environments, Fundamentals Spring 2005 Based on Slides by E. Angel Jan-27-05 SMM009,Transformations 2 L Overview •Standard

7

Jan-27-05 SMM009, Transformations 19 L

Concatenation

• Form arbitrary affine transformation matrices bymultiplying- Rotation, translation, and scaling matrices

• Same transformation is applied to many vertices:- Cost of forming a matrix M=ABCD is not significant

compared to the cost of computing Mp for many vertices p• Difficulty is forming a desired transformation

Jan-27-05 SMM009, Transformations 20 L

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 present

points. In terms of column matrices pT’ = pTCTBTAT

Jan-27-05 SMM009, Transformations 21 L

x

z

yv

R(θ) = Rz(θz) Ry(θy) Rx(θx)

θx θy θz are called the Euler angles

Note that rotations do not commuteWe can use rotations in another order butwith different angles

General Rotation About the Origin

• A rotation by θ about an arbitrary axis can bedecomposed into the concatenation of rotationsabout the x, y, and z axes

Page 8: Transformations · Transformations David Carr Virtual Environments, Fundamentals Spring 2005 Based on Slides by E. Angel Jan-27-05 SMM009,Transformations 2 L Overview •Standard

8

Jan-27-05 SMM009, Transformations 22 L

Rotation About a Fixed Point, Not the Origin

• Move fixed point to origin

• Rotate

• Move fixed point backM = T(-pf) R(θ) T(pf)

Jan-27-05 SMM009, Transformations 23 L

Instancing

• In modeling, we often start with:- A simple object- Centered at the origin- Oriented with the axes- At a standard size

• We apply an instance transform- ation to its vertices to- Scale- Orient- Locate

Jan-27-05 SMM009, Transformations 24 L

Shear

• Helpful to add one more basic transformation

• Equivalent to pulling faces in opposite directions

Page 9: Transformations · Transformations David Carr Virtual Environments, Fundamentals Spring 2005 Based on Slides by E. Angel Jan-27-05 SMM009,Transformations 2 L Overview •Standard

9

Jan-27-05 SMM009, Transformations 25 L

x’ = x + y cot θy’ = yz’ = z

!!!!

"

#

$$$$

%

& '

1000

0100

0010

00cot 1

H(θ) =

Shear Matrix

• Consider simple shear along the x-axis

Jan-27-05 SMM009, Transformations 26 L

INSTITUTIONEN FÖRSYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

Transformations in OpenGL

Jan-27-05 SMM009, Transformations 27 L

OpenGL Matrices

• In OpenGL matrices are part of the state

• Three types- Model-View (GL.GL_MODEL_VIEW)

- Projection (GL.GL_PROJECTION)

- Texture (GL.GL_TEXTURE) (ignore for now)

• Single set of functions for manipulation

• Select which to manipulated by- gl.glMatrixMode(GL_MODEL_VIEW);- gl.glMatrixMode(GL_PROJECTION);

Page 10: Transformations · Transformations David Carr Virtual Environments, Fundamentals Spring 2005 Based on Slides by E. Angel Jan-27-05 SMM009,Transformations 2 L Overview •Standard

10

Jan-27-05 SMM009, Transformations 28 LCTMvertices vertices

p p’=CpC

Current Transformation Matrix (CTM)

• Conceptually, there is a 4 x 4 homogeneous coordinatematrix, the current transformation matrix (CTM) that is:- Part of the state

- Applied to all vertices that pass down the pipeline

• The CTM is defined in the user program and loadedinto a transformation unit

Jan-27-05 SMM009, Transformations 29 L

Load an identity matrix: C ← ILoad an arbitrary matrix: C ← MLoad a translation matrix: C ← TLoad a rotation matrix: C ← RLoad a scaling matrix: C ← S

Post multiply by an arbitrary matrix: C ← CMPost multiply by a translation matrix: C ← CTPost multiply by a rotation matrix: C ← C RPost multiply by a scaling matrix: C ← C S

CTM operations

• The CTM can be altered either by- Loading a new CTM

- Post multiplication

Jan-27-05 SMM009, Transformations 30 L

Rotation about a Fixed Point

• Start with identity matrix: C ← I• Move fixed point to origin: C ← CT-1

• Rotate: C ← CR• Move fixed point back: C ← CT

• Result: C = T-1RT

• Each operation corresponds to one function call in the program.

• Note that the last operation specified is the first executed in theprogram

Page 11: Transformations · Transformations David Carr Virtual Environments, Fundamentals Spring 2005 Based on Slides by E. Angel Jan-27-05 SMM009,Transformations 2 L Overview •Standard

11

Jan-27-05 SMM009, Transformations 31 L

CTM in OpenGL

• OpenGL has a model-view and a projection matrix inthe pipeline which are concatenated together to formthe CTM

• Can manipulate each by first setting the matrix mode

Jan-27-05 SMM009, Transformations 32 L

Rotation, Translation, Scaling

• Load an identity matrixgl.glLoadIdentity()

• Multiply on rightgl.glRotatef(theta, vx, vy, vz)

- theta in degrees, (vx, vy, vz) define axis of rotationgl.glTranslatef(dx, dy, dz)gl.glScalef( sx, sy, sz)

• Each has a float (f) and double (d) format (e.g.,gl.glScaled)

Jan-27-05 SMM009, Transformations 33 L

• Rotation about z axis by 30 degrees with a fixed pointof (1.0, 2.0, 3.0)

• Remember that last matrix specified in the program isthe first applied

gl.glMatrixMode(GL_MODELVIEW);gl.glLoadIdentity();gl.glTranslatef(1.0, 2.0, 3.0);gl.glRotatef(30.0, 0.0, 0.0, 1.0);gl.glTranslatef(-1.0, -2.0, -3.0);

Example

Page 12: Transformations · Transformations David Carr Virtual Environments, Fundamentals Spring 2005 Based on Slides by E. Angel Jan-27-05 SMM009,Transformations 2 L Overview •Standard

12

Jan-27-05 SMM009, Transformations 34 L

Arbitrary Matrices

• Can load and multiply by matrices defined in theapplication program

• The matrix m is a one dimension array of 16 elementswhich are the components of the desired 4 x 4 matrixstored by columns

• In glMultMatrixf, m multiplies the existing matrix onthe right

gl.glLoadMatrixf(m)gl.glMultMatrixf(m)

Jan-27-05 SMM009, Transformations 35 L

Matrix Stacks

• In many situations we want to save transformation matricesfor use later- Traversing hierarchical data structures (Chapter 9)

- Avoiding state changes when executing display lists

• OpenGL maintains stacks for each type of matrix- Access present type (as set by glMatrixMode) by

gl.glPushMatrix()gl.glPopMatrix()

Jan-27-05 SMM009, Transformations 36 L

glGetIntegervglGetFloatvglGetBooleanvglGetDoublevglIsEnabled

double m[16];glGetFloatv(GL_MODELVIEW, m);

Reading Back Matrices

• Can also access matrices (and other parts of the state)by enquiry (query) functions

• For matrices, we use as

Page 13: Transformations · Transformations David Carr Virtual Environments, Fundamentals Spring 2005 Based on Slides by E. Angel Jan-27-05 SMM009,Transformations 2 L Overview •Standard

13

Jan-27-05 SMM009, Transformations 37 L

Using Transformations

• Example: use idle function to rotate a cube and mousefunction to change direction of rotation

• Start with a program that draws a cube (Cube.java) ina standard way- Centered at origin

- Sides aligned with axes

Jan-27-05 SMM009, Transformations 38 L

Method main()public class Cube { public static void main(String[] args) { Frame simpleFrame = new Frame("Cube from Chapter 4"); SimpleRenderer renderer = new SimpleRenderer(); GLCapabilities glc = new GLCapabilities();

glc.setDoubleBuffered(true); GLCanvas drawable = GLDrawableFactory.getFactory().createGLCanvas(glc); drawable.addGLEventListener(renderer); simpleFrame.add(drawable); simpleFrame.setLocation(100,100); simpleFrame.setSize(500, 522); final Animator animator = new Animator(drawable); simpleFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {animator.stop(); System.exit(0);} }); simpleFrame.show(); animator.start(); }

Jan-27-05 SMM009, Transformations 39 L

Renderer static class SimpleRenderer implements GLEventListener, MouseListener { final double XMIN = -2.0, XMAX = 2.0, YMIN = -2.0, YMAX = 2.0; double vertices[][] = { {-1.0,-1.0,-1.0}, {1.0,-1.0,-1.0},

{1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0}, {1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0} },

colors[][] = { {0.0,0.0,0.0}, {1.0,0.0,0.0}, {1.0,1.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0}, {1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0} }, theta[] = {0.0,0.0,0.0};

int axis = 2; boolean firstReshape = true;

public void displayChanged(GLDrawable drawable, boolean modeChanged, boolean deviceChanged) {}

Page 14: Transformations · Transformations David Carr Virtual Environments, Fundamentals Spring 2005 Based on Slides by E. Angel Jan-27-05 SMM009,Transformations 2 L Overview •Standard

14

Jan-27-05 SMM009, Transformations 40 L

Method reshape(…)

public void reshape(GLDrawable drawable, int x, int y, int w, int h) { GL gl = drawable.getGL(); double scale = (w <= h) ? (double) h / (double) w : (double) w / (double) h; gl.glViewport(x, y, w, h); gl.glMatrixMode(GL.GL_PROJECTION); // switch matrix mode gl. glLoadIdentity(); if(w<=h) gl.glOrtho(XMIN,XMAX,YMIN*scale,YMAX*scale,-10.0,10.0); else gl.glOrtho(XMIN*scale,XMAX*scale,YMIN,YMAX,-10.0,10.0); gl.glMatrixMode(GL.GL_MODELVIEW); // return to modelview mode gl.glClear(GL.GL_COLOR_BUFFER_BIT); if (firstReshape) firstReshape = false; else drawable.display();} Calling display on first pass

causes an exception!

Jan-27-05 SMM009, Transformations 41 L

Method init(…)

public void init(GLDrawable drawable) { GL gl = drawable.getGL(); // get interfaces

drawable.addMouseListener(this); // connect mouse gl.glClearColor(0f,0f,0f,0f); // set bg and fill color

gl.glMatrixMode(GL.GL_PROJECTION); // view intialization gl.glLoadIdentity(); gl.glOrtho(XMIN, XMAX, YMIN, YMAX, -10.0, 10.0); gl.glEnable(GL.GL_DEPTH_TEST); // enable z-buffer}

Jan-27-05 SMM009, Transformations 42 L

Method display(…)

public void display(GLDrawable drawable) { GL gl = drawable.getGL();

gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); gl.glRotated(theta[0], 1.0, 0.0, 0.0); gl.glRotated(theta[1], 0.0, 1.0, 0.0); gl.glRotated(theta[2], 0.0, 0.0, 1.0);

colorCube(gl); // draw cube spinCube(); // rotate for next draw

gl.glFlush();}

Page 15: Transformations · Transformations David Carr Virtual Environments, Fundamentals Spring 2005 Based on Slides by E. Angel Jan-27-05 SMM009,Transformations 2 L Overview •Standard

15

Jan-27-05 SMM009, Transformations 43 L

Drawing the Cube

private void cubeFace(GL gl, int a, int b, int c , int d) { gl.glBegin(GL.GL_POLYGON);

gl.glColor3dv(colors[a]); gl.glVertex3dv(vertices[a]); gl.glColor3dv(colors[b]); gl.glVertex3dv(vertices[b]); gl.glColor3dv(colors[c]); gl.glVertex3dv(vertices[c]); gl.glColor3dv(colors[d]); gl.glVertex3dv(vertices[d]);

gl.glEnd(); }

Jan-27-05 SMM009, Transformations 44 L

Drawing the Cube, …

private void colorCube(GL gl) { cubeFace(gl,0,3,2,1); cubeFace(gl,2,3,7,6); cubeFace(gl,0,4,7,3); cubeFace(gl,1,2,6,5); cubeFace(gl,4,5,6,7); cubeFace(gl,0,1,5,4); }

private void spinCube() { theta[axis] += 0.20; if( theta[axis] > 360.0 ) theta[axis] -= 360.0; }

Jan-27-05 SMM009, Transformations 45 L

Mouse Interface

public void mouseEntered(MouseEvent e) {}public void mouseExited(MouseEvent e) {}public void mouseClicked(MouseEvent e) {}public void mouseReleased(MouseEvent e) {}public void mousePressed(MouseEvent e) { int button = e.getButton();

switch (button) { case MouseEvent.BUTTON1: axis = 0; break;

case MouseEvent.BUTTON2: axis = 1; break;

case MouseEvent.BUTTON3: axis = 2; break; }}

Page 16: Transformations · Transformations David Carr Virtual Environments, Fundamentals Spring 2005 Based on Slides by E. Angel Jan-27-05 SMM009,Transformations 2 L Overview •Standard

16

Jan-27-05 SMM009, Transformations 46 L

Using the Model-View Matrix

• In OpenGL the model-view matrix is used to:- Position the camera

+ Can be done by rotations and translations but is often easier to usegluLookAt (Chapter 5)

- Build models of obejcts

• The projection matrix is used to:- Define the view volume- Select a camera lens

• Although both are manipulated by the same functions,- Be careful because incremental changes are always made by post-multiplication- For example, rotating model-view and projection matrices by the same matrix are

not equivalent operations.- Post-multiplication of the model-view matrix is equivalent to pre-multiplication

of the projection matrix

Jan-27-05 SMM009, Transformations 47 L

Smooth Rotation

• From a practical standpoint,- Often want to use transformations to move and reorient an

object smoothly- Problem: find a sequence of model-view matrices

M0,M1,…..,Mn so that when they are applied successivelyto one or more objects we see a smooth transition

• For orientating an object- Can use the fact that every rotation corresponds to part of a

great circle on a sphere- Find the axis of rotation and angle- Virtual trackball (see text)

Jan-27-05 SMM009, Transformations 48 L

Incremental Rotation

• Consider the two approaches

- For a sequence of rotation matrices R0,R1,…..,Rn , find theEuler angles for each and use Ri= Riz Riy Rix

+ Not very efficient

- Use the final positions to determine the axis and angle ofrotation, then increment only the angle

• Quaternions can be more efficient than either

Page 17: Transformations · Transformations David Carr Virtual Environments, Fundamentals Spring 2005 Based on Slides by E. Angel Jan-27-05 SMM009,Transformations 2 L Overview •Standard

17

Jan-27-05 SMM009, Transformations 49 L

Quaternions

• Extension of imaginary numbers from two to threedimensions

• Requires 1 real and 3 imaginary components i, j, k

• Quaternions can express rotations on sphere smoothlyand efficiently. Process:- Model-view matrix → quaternion

- Carry out operations with quaternions- Quaternion → Model-view matrix

q=q0+q1i+q2j+q3k, where i2=j2=k2=ijk=-1

Jan-27-05 SMM009, Transformations 50 L

!

r = cos"

2,sin

"

2v

#

$ %

&

' (

Quaternions and Rotations

• A rotation R=Rx(θ)Ry(t)Rz(φ) can be expressed as aquaternion:- r = rxryrz where

- v is the vector of rotation and

• Then p’ = rpr-1 is the rotation of the point p by R- If p = [x y z 1] then p = (0, [x y z])

• Quaternions can be linearly interpolated to give smooth angularanimation

• See 4.11.2 for more details

Jan-27-05 SMM009, Transformations 51 L

Interfaces

• One of the major problems in interactive computergraphics is how to- Use 2D devices such as a mouse to interface with 3D objects

• Example: how to form an instance matrix?• Some alternatives

- Virtual trackball- 3D input devices such as the space ball- Use areas of the screen

+ Distance from center controls angle, position, scaledepending on mouse button depressed

Page 18: Transformations · Transformations David Carr Virtual Environments, Fundamentals Spring 2005 Based on Slides by E. Angel Jan-27-05 SMM009,Transformations 2 L Overview •Standard

18

Jan-27-05 SMM009, Transformations 52 L

Questions