49
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2008 Tamara Munzner http://www.ugrad.cs.ubc.ca/~cs314/Vjan2008 OpenGL, GLUT, Transformations I Week 2, Wed Jan 16

OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

University of British ColumbiaCPSC 314 Computer Graphics

Jan-Apr 2008

Tamara Munzner

http://www.ugrad.cs.ubc.ca/~cs314/Vjan2008

OpenGL, GLUT,Transformations I

Week 2, Wed Jan 16

Page 2: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

2

Review: Rendering Pipeline

GeometryDatabase

Model/ViewTransform. Lighting Perspective

Transform. Clipping

ScanConversion

DepthTestTexturing Blending

Frame-buffer

Page 3: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

3

OpenGL (briefly)

Page 4: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

4

OpenGL• API to graphics hardware

• based on IRIS_GL by SGI• designed to exploit hardware optimized for display and

manipulation of 3D graphics• implemented on many different platforms• low level, powerful flexible• pipeline processing

• set state as needed

Page 5: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

5

Graphics State

• set the state once, remains until overwritten• glColor3f(1.0, 1.0, 0.0) set color to yellow• glSetClearColor(0.0, 0.0, 0.2) dark blue bg• glEnable(LIGHT0) turn on light• glEnable(GL_DEPTH_TEST) hidden surf.

Page 6: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

6

Geometry Pipeline• tell it how to interpret geometry

• glBegin(<mode of geometric primitives>)• mode = GL_TRIANGLE, GL_POLYGON, etc.

• feed it vertices• glVertex3f(-1.0, 0.0, -1.0)• glVertex3f(1.0, 0.0, -1.0)• glVertex3f(0.0, 1.0, -1.0)

• tell it you’re done• glEnd()

Page 7: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

7

Open GL: Geometric Primitives

glPointSizeglPointSize( float size);( float size);glLineWidthglLineWidth( float width);( float width);glColor3f( float r, float g, float b);glColor3f( float r, float g, float b);........

Page 8: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

8

Code Samplevoid display(){glClearColor(0.0, 0.0, 0.0, 0.0);glClear(GL_COLOR_BUFFER_BIT);glColor3f(0.0, 1.0, 0.0);glBegin(GL_POLYGON); glVertex3f(0.25, 0.25, -0.5); glVertex3f(0.75, 0.25, -0.5); glVertex3f(0.75, 0.75, -0.5); glVertex3f(0.25, 0.75, -0.5);glEnd();glFlush();

}• more OpenGL as course continues

Page 9: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

9

GLUT

Page 10: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

10

GLUT: OpenGL Utility Toolkit• developed by Mark Kilgard (also from SGI)• simple, portable window manager

• opening windows• handling graphics contexts

• handling input with callbacks• keyboard, mouse, window reshape events

• timing• idle processing, idle events

• designed for small/medium size applications• distributed as binaries

• free, but not open source

Page 11: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

11

Event-Driven Programming

• main loop not under your control• vs. batch mode where you control the flow

• control flow through event callbacks• redraw the window now• key was pressed• mouse moved

• callback functions called from main loopwhen events occur• mouse/keyboard state setting vs. redrawing

Page 12: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

12

GLUT Callback Functions // you supply these kind of functions // you supply these kind of functions

void reshape(void reshape(int int w, w, int int h);h);void keyboard(unsigned char key, void keyboard(unsigned char key, int int x, x, int int y);y);void mouse(void mouse(int int but, but, int int state, state, int int x, x, int int y);y);void idle();void idle();void display();void display();

// register them with glut // register them with glut

glutReshapeFuncglutReshapeFunc(reshape);(reshape);glutKeyboardFuncglutKeyboardFunc(keyboard);(keyboard);glutMouseFuncglutMouseFunc(mouse);(mouse);glutIdleFuncglutIdleFunc(idle);(idle);glutDisplayFuncglutDisplayFunc(display);(display);

void void glutDisplayFunc glutDisplayFunc (void(void (*(*funcfunc)(void));)(void));voidvoid glutKeyboardFunc glutKeyboardFunc (void(void (*(*funcfunc)(unsigned)(unsigned charchar key,key, intint x,x, intint y));y));voidvoid glutIdleFunc glutIdleFunc (void(void (*(*funcfunc)());)());voidvoid glutReshapeFunc glutReshapeFunc (void(void (*(*funcfunc)()(intint width,width, intint height));height));

Page 13: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

13

GLUT Example 1#include <GLUT/glut.h>void display(){ glClearColor(0,0,0,1); glClear(GL_COLOR_BUFFER_BIT); glColor4f(0,1,0,1); glBegin(GL_POLYGON); glVertex3f(0.25, 0.25, -0.5); glVertex3f(0.75, 0.25, -0.5); glVertex3f(0.75, 0.75, -0.5); glVertex3f(0.25, 0.75, -0.5); glEnd(); glutSwapBuffers();}

int main(int argc,char**argv){ glutInit( &argc, argv ); glutInitDisplayMode(

GLUT_RGB|GLUT_DOUBLE); glutInitWindowSize(640,480); glutCreateWindow("glut1"); glutDisplayFunc( display ); glutMainLoop(); return 0; // never reached}

Page 14: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

14

GLUT Example 2#include <GLUT/glut.h>void display(){ glRotatef(0.1, 0,0,1);

glClearColor(0,0,0,1); glClear(GL_COLOR_BUFFER_BIT); glColor4f(0,1,0,1); glBegin(GL_POLYGON); glVertex3f(0.25, 0.25, -0.5); glVertex3f(0.75, 0.25, -0.5); glVertex3f(0.75, 0.75, -0.5); glVertex3f(0.25, 0.75, -0.5); glEnd(); glutSwapBuffers();}

int main(int argc,char**argv){ glutInit( &argc, argv ); glutInitDisplayMode(

GLUT_RGB|GLUT_DOUBLE); glutInitWindowSize(640,480); glutCreateWindow("glut2"); glutDisplayFunc( display ); glutMainLoop(); return 0; // never reached}

Page 15: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

15

Redrawing Display

• display only redrawn by explicit request• glutPostRedisplay() function• default window resize callback does this

• idle called from main loop when no user input• good place to request redraw• will call display next time through event loop

• should return control to main loop quickly• continues to rotate even when no user action

Page 16: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

16

GLUT Example 3#include <GLUT/glut.h>void display(){ glRotatef(0.1, 0,0,1);

glClearColor(0,0,0,1); glClear(GL_COLOR_BUFFER_BIT); glColor4f(0,1,0,1); glBegin(GL_POLYGON); glVertex3f(0.25, 0.25, -0.5); glVertex3f(0.75, 0.25, -0.5); glVertex3f(0.75, 0.75, -0.5); glVertex3f(0.25, 0.75, -0.5); glEnd(); glutSwapBuffers();}

void idle() { glutPostRedisplay();}

int main(int argc,char**argv){ glutInit( &argc, argv ); glutInitDisplayMode(

GLUT_RGB|GLUT_DOUBLE); glutInitWindowSize(640,480); glutCreateWindow("glut1"); glutDisplayFunc( display ); glutIdleFunc( idle ); glutMainLoop(); return 0; // never reached}

Page 17: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

17

Keyboard/Mouse Callbacks• again, do minimal work• consider keypress that triggers animation

• do not have loop calling display in callback!• what if user hits another key during animation?

• instead, use shared/global variables to keeptrack of state• yes, OK to use globals for this!

• then display function just uses currentvariable value

Page 18: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

18

GLUT Example 4#include <GLUT/glut.h>

bool animToggle = true;float angle = 0.1;

void display() {glRotatef(angle, 0,0,1);

...}void idle() { glutPostRedisplay();}int main(int argc,char**argv){ ... glutKeyboardFunc( doKey ); ...}

void doKey(unsigned char key, int x, int y) {

if ('t' == key) { animToggle = !animToggle; if (!animToggle)

glutIdleFunc(NULL); else glutIdleFunc(idle); } else if ('r' == key) { angle = -angle; } glutPostRedisplay();}

Page 19: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

19

Transformations

Page 20: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

20

Transformations

• transforming an object = transforming all itspoints

• transforming a polygon = transforming itsvertices

Page 21: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

21

Matrix Representation• represent 2D transformation with matrix

• multiply matrix by column vectorapply transformation to point

• transformations combined by multiplication

• matrices are efficient, convenient way to representsequence of transformations!

!"

#$%

&!"

#$%

&=!

"

#$%

&

y

x

dc

ba

y

x

'

'

dycxy

byaxx

+=

+=

'

'

!"

#$%

&!"

#$%

&!"

#$%

&!"

#$%

&=!

"

#$%

&

y

x

kj

ih

gf

ed

dc

ba

y

x

'

'

Page 22: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

22

Scaling

• scaling a coordinate means multiplying eachof its components by a scalar

• uniform scaling means this scalar is the samefor all components:

× 2

Page 23: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

23

• non-uniform scaling: different scalars percomponent:

• how can we represent this in matrix form?

Scaling

X × 2,Y × 0.5

Page 24: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

24

Scaling

• scaling operation:

• or, in matrix form:

!"

#$%

&=!

"

#$%

&

by

ax

y

x

'

'

!"

#$%

&!"

#$%

&=!

"

#$%

&

y

x

b

a

y

x

0

0

'

'

scaling matrix

Page 25: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

25

2D Rotation

θ

(x, y)

(x', y')

x' = x cos(θ) - y sin(θ)y' = x sin(θ) + y cos(θ)

• counterclockwise

• RHS

Page 26: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

26

2D Rotation From Trig Identities

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

Trig Identity…x' = r cos(φ) cos(θ) – r sin(φ) sin(θ)y' = r sin(φ) cos(θ) + r cos(φ) sin(θ)

Substitute…x' = x cos(θ) - y sin(θ)y' = x sin(θ) + y cos(θ)

θ

(x, y)

(x', y')

φ

Page 27: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

27

2D Rotation Matrix

• easy to capture in matrix form:

• even though sin(q) and cos(q) are nonlinearfunctions of q,• x' is a linear combination of x and y• y' is a linear combination of x and y

( ) ( )( ) ( ) !

"

#$%

&!"

#$%

& '=!

"

#$%

&

y

x

y

x

((

((

cossin

sincos

'

'

Page 28: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

28

2D Rotation: Another Derivation

!!

!!

cossin'

sincos'

yxy

yxx

+=

"=(x',y')

θθ

(x,y)

Page 29: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

29

2D Rotation: Another Derivation

!!

!!

cossin'

sincos'

yxy

yxx

+=

"=(x',y')

(x,y)

θθ

xy

Page 30: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

30

2D Rotation: Another Derivation

!!

!!

cossin'

sincos'

yxy

yxx

+=

"=(x',y')

(x,y)

θθ

xy

θθ

x

y

Page 31: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

31

2D Rotation: Another Derivation

!!

!!

cossin'

sincos'

yxy

yxx

+=

"=

xx'

!

x'= A " B

BB

AA

(x',y')

(x,y)

Page 32: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

32

2D Rotation: Another Derivation

!!

!!

cossin'

sincos'

yxy

yxx

+=

"=

xx

xx'

!

x'= A " B

BB

AA

(x',y')

(x,y)

!

A = x cos"

θθ

xx

Page 33: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

33

2D Rotation: Another Derivation

!!

!!

cossin'

sincos'

yxy

yxx

+=

"=

xx!

x'= A " B

BB

AA

!

A = x cos"

yy

yy

θθ

θθ

(x',y')

(x,y)

θθxx'

!sinyB =θθ

Page 34: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

34

Shear

• shear along x axis• push points to right in proportion to height

x

y

x

y

!"

#$%

&+!"

#$%

&!"

#$%

&=!

"

#$%

&

'

'

?

?

??

??

y

x

y

x

Page 35: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

35

Shear

• shear along x axis• push points to right in proportion to height

x

y

x

y

!"

#$%

&+!"

#$%

&!"

#$%

&=!

"

#$%

&

'

'

0

0

10

1

y

xsh

y

x x

Page 36: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

36

Reflection

x x

!"

#$%

&+!"

#$%

&!"

#$%

&=!

"

#$%

&

'

'

?

?

??

??

y

x

y

x

• reflect across x axis• mirror

Page 37: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

37

Reflection• reflect across x axis

• mirror!"

#$%

&+!"

#$%

&!"

#$%

&

'=!

"

#$%

&

(

(

0

0

10

01

y

x

y

x

x x

Page 38: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

38

2D Translation

!"

#$%

&=!

"

#$%

&

+

+=!

"

#$%

&+!"

#$%

&

'

'

y

x

by

ax

b

a

y

x),( ba(x,y)

(x’,y’)

Page 39: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

39

2D Translation

!"

#$%

&=!

"

#$%

&

+

+=!

"

#$%

&+!"

#$%

&

'

'

y

x

by

ax

b

a

y

x),( ba(x,y)

(x’,y’)

( ) ( )( ) ( ) !

"

#$%

&!"

#$%

& '=!

"

#$%

&

y

x

y

x

((

((

cossin

sincos

'

'

!"

#$%

&!"

#$%

&=!

"

#$%

&

y

x

b

a

y

x

0

0

'

'

scaling matrix rotation matrix

Page 40: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

40

2D Translation

!"

#$%

&=!

"

#$%

&

+

+=!

"

#$%

&+!"

#$%

&

'

'

y

x

by

ax

b

a

y

x),( ba(x,y)

(x’,y’)

( ) ( )( ) ( ) !

"

#$%

&!"

#$%

& '=!

"

#$%

&

y

x

y

x

((

((

cossin

sincos

'

'

!"

#$%

&!"

#$%

&=!

"

#$%

&

y

x

b

a

y

x

0

0

'

'

scaling matrix rotation matrix

vector addition

matrix multiplicationmatrix multiplication

Page 41: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

41

2D Translation

!"

#$%

&=!

"

#$%

&

+

+=!

"

#$%

&+!"

#$%

&

'

'

y

x

by

ax

b

a

y

x),( ba(x,y)

(x’,y’)

( ) ( )( ) ( ) !

"

#$%

&!"

#$%

& '=!

"

#$%

&

y

x

y

x

((

((

cossin

sincos

'

'

!"

#$%

&!"

#$%

&=!

"

#$%

&

y

x

b

a

y

x

0

0

'

'

scaling matrix rotation matrix

!"

#$%

&=!

"

#$%

&!"

#$%

&

'

'

y

x

y

x

dc

ba

translation multiplication matrix??

vector addition

matrix multiplicationmatrix multiplication

Page 42: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

42

Linear Transformations• linear transformations are combinations of

• shear• scale• rotate• reflect

• properties of linear transformations• satisifes T(sx+ty) = s T(x) + t T(y)• origin maps to origin• lines map to lines• parallel lines remain parallel• ratios are preserved• closed under composition

!"

#$%

&!"

#$%

&=!

"

#$%

&

y

x

dc

ba

y

x

'

'

dycxy

byaxx

+=

+=

'

'

Page 43: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

43

Challenge• matrix multiplication

• for everything except translation• how to do everything with multiplication?

• then just do composition, no special cases• homogeneous coordinates trick

• represent 2D coordinates (x,y) with 3-vector (x,y,1)

Page 44: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

44

Homogeneous Coordinates• our 2D transformation matrices are now 3x3:

!!!

"

#

$$$

%

& '

=

100

0)cos()sin(

0)sin()cos(

((

((

otationR

!!!

"

#

$$$

%

&

=

100

00

00

b

a

caleS

!!!

"

#

$$$

%

&

=

100

10

01

y

x

T

T

ranslationT

!!!

"

#

$$$

%

&

+

+

=

!!!

"

#

$$$

%

&

'+'

'+'

=

!!!

"

#

$$$

%

&

!!!

"

#

$$$

%

&

11

11

11

1100

10

01

by

ax

by

ax

y

x

b

a

• use rightmost column

Page 45: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

45

Homogeneous Coordinates Geometrically

• point in 2D cartesian

xxyy

Page 46: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

46

Homogeneous Coordinates Geometrically

• point in 2D cartesian + weight w =point P in 3D homog. coords

• multiples of (x,y,w)• form a line L in 3D• all homogeneous points on L

represent same 2D cartesian point• example: (2,2,1) = (4,4,2) =

(1,1,0.5)

),,( wyx

homogeneoushomogeneous

),(w

y

w

x

cartesiancartesian

/ w/ w

xxyy

ww

w=w=11

Page 47: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

47

Homogeneous Coordinates Geometrically

• homogenize to convert homog. 3Dpoint to cartesian 2D point:• divide by w to get (x/w, y/w, 1)• projects line to point onto w=1 plane• like normalizing, one dimension up

• when w=0, consider it as direction• points at infinity• these points cannot be homogenized• lies on x-y plane

• (0,0,0) is undefined

),,( wyx

homogeneoushomogeneous

),(w

y

w

x

cartesiancartesian

/ w/ w

xxyy

ww

w=w=11

Page 48: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

48

Affine Transformations• affine transforms are combinations of

• linear transformations• translations

• 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

!!!

"

#

$$$

%

&

!!!

"

#

$$$

%

&

=

!!!

"

#

$$$

%

&

w

y

x

fed

cba

w

y

x

100

'

'

Page 49: OpenGL, GLUT, Transformations I Week 2, Wed Jan 16tmm/courses/314/Vjan2008/... · GLUT: OpenGL Utility Toolkit •developed by Mark Kilgard (also from SGI) ... •free, but not open

49

Homogeneous Coordinates Summary

• may seem unintuitive, but they makegraphics operations much easier

• allow all affine transformations to beexpressed through matrix multiplication• we’ll see even more later...

• use 3x3 matrices for 2D transformations• use 4x4 matrices for 3D transformations