41
CSCI 420: Computer Graphics Hao Li http://cs420.hao-li.com Fall 2018 2.2 Transformations 1

Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

  • Upload
    others

  • View
    10

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

CSCI 420: Computer Graphics

Hao Lihttp://cs420.hao-li.com

Fall 2018

2.2 Transformations

1

Page 2: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

OpenGL Transformations Matrices

Model-view Projection

vertices in canonical 3D world coordinate

system vertices in 3D

vertices in 2D

• Model-view matrix (4x4 matrix)

• Projection matrix (4x4 matrix)

2

Page 3: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

vertices in 2D

vertices in 3D

vertices in canonical 3D world coordinate

system

• Translate, rotate, scale objects

• Position the camera

Model-view Projection

4x4 Model-view Matrix (this lecture)

3

Page 4: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

vertices in 2D

vertices in 3D

vertices in canonical 3D world coordinate

system

• Projection from 3D to 2D

Model-view Projection

4x4 Model-view Matrix (next lecture)

4

Page 5: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Model-view Projection

• Manipulated separately in OpenGL

(must set matrix mode) :

glMatrixMode (GL_MODELVIEW); glMatrixMode (GL_PROJECTION;

OpenGL Transformation Matrices

5

Page 6: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Setting the Current Model-view Matrix

• Load or post-multiply

glMatrixMode (GL_MODELVIEW); glLoadIdentity(); // very common usagefloat m[16] = { … }; glLoadMatrixf(m); // rare, advanced glMultMatrixf(m); // rare, advanced

• Use library functions

glTranslatef(dx, dy, dz); glRotatef(angle, vx, vy, vz); glScalef(sx, sy, sz);

6

Page 7: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Translated, rotated, scaled object

world

7

Page 8: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

The rendering coordinate system

world

Initially (after glLoadIdentity()) :

rendering coordinate system =world coordinate system

8

Page 9: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

The rendering coordinate system

rendering coordinate system

glTranslatef(x, y, z);

world

[x, y ,z]

9

Page 10: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

The rendering coordinate system

rendering coordinate

syste

m

glRotatef(angle,ax, ay, az);

world

10

Page 11: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

The rendering coordinate system

rendering coordinate

syste

m

glScalef(sx, sy, sz);

world

11

Page 12: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

OpenGL code

rendering coordinate

syste

m

glMatrixMode (GL_MODELVIEW); glLoadIdentity(); glTranslatef(x, y, z); glRotatef(angle, ax, ay, az); glScalef(sx, sy, sz); renderBunny();

world

12

Page 13: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Rendering more objects

rendering coordinate

syste

mworld

How to obtain this frame?

13

Page 14: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Solution 1

Find glTranslate(…), glRotatef(…), glScalef(…)

world

How to obtain this frame?

14

Page 15: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Solution 2: gl{Push,Pop}Matrix

glMatrixMode (GL_MODELVIEW); glLoadIdentity();

// render first bunny glPushMatrix(); // store current matrix glTranslate3f(…); glRotatef(…); renderBunny(); glPopMatrix(); // pop matrix // render second bunny glPushMatrix(); // store current matrix glTranslate3f(…); glRotatef(…); renderBunny(); glPopMatrix(); // pop matrix

15

Page 16: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Recall: Linear Algebra

16

Page 17: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Scalars

• Scalars , , from a scalar field

• Operations , , , , ,

• “Expected” laws apply

• Examples: rationals or reals with addition and

multiplication

17

↵ � �

↵+ � ↵� 0 1 �↵ ()�1

Page 18: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Vectors

• Vectors from a vector space

• Vector addition , subtraction

• Zero vector

• Scalar multiplication

18

u,v,w

u+ v u� v

0

↵v

Page 19: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Euclidean Space

• Vector space over real numbers

• Three-dimensional in computer graphics

• Dot product:

• ,

• are orthogonal if

• defines , the length of

19

↵ = u>v = u1v1 + u2v2 + u3v3

0>0 = 0

u>v = 0u,v

kvk2 = v>v kvk v

Page 20: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Lines and Line Segments

• Parametric form of line:

• Line segment between and : for

20

p(↵) = p0 + ↵d

q r

p(↵) = (1� ↵)q+ ↵r 0 ↵ 1

Page 21: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Convex Hull

• Convex hull defined by

for and

21

p = ↵1p1 + . . .+ ↵npn

↵1 + . . .+ ↵n = 1

0 ↵1 1, i = 1 . . . n

Page 22: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Projection

• Dot product projects one vector onto another vector

22

u>v = u1v1 + u2v2 + u3v3 = kukkvk cos(✓)

⇡v(u) = (u>v)v/kvk2

Page 23: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Cross Product

• Cross product is perpendicular to both and

• Right-hand rule

23

ka⇥ bk = |a||b|| sin(✓)|

a b

Page 24: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Plane

• Plane defined by point and vectors and

• and should not be parallel

• Parametric form:

• is the normal

• if and only if lies in plane

24

p0

u v

u v

t(↵,�) = p0 + ↵u+ �v

n = u⇥ v/ku⇥ vk

n>(p� p0) = 0 p

Page 25: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Coordinate Systems

• Let be three linearly independent

vectors in a 3-dimensional vector space

• Can write any vector as

for some scalars

25

v1,v2,v3

w = ↵1v1 + ↵2v2 + ↵3v3

w

↵1,↵2,↵3

Page 26: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Frames

• Frame = origin + coordinate system

• Any point

26

p0

p = p0 + ↵1v1 + ↵2v2 + ↵3v3

Page 27: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

In Practice, Frames are Often Orthogonal

27

Page 28: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Change of Coordinate System

• Bases { } and { }

• Express basis vectors in terms of

• Represent in matrix form:

28

u1,u2,u3 v1,v2,v3

ui vj

u1 = �11v1 + �12v2 + �13v3

u2 = �21v1 + �22v2 + �23v3

u3 = �31v1 + �32v2 + �33v3

2

4u>1

u>2

u>3

3

5 = M

2

4v>1

v>2

v>3

3

5 M

Page 29: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Representing3D transformations

(and model-view matrices)

29

Page 30: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Linear Transformations

• 3 x 3 matrices represent linear transformations

• Can represent rotation, scaling, and reflection

• Cannot represent translation

30

a = Mb

M

Page 31: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Homogeneous Coordinates

• In order to represent rotations, scales AND translations

• Augment by adding a fourth component (1):

• Homogeneous property:

, for any scalar

31

[↵1,↵2,↵3]>

p = [↵1,↵2,↵3, 1]>

p = [↵1,↵2,↵3, 1]> = [↵1,↵2,↵3]

> 6= 0

Page 32: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Homogeneous Coordinates

• Homogeneous coordinates are transformed by 4x4 matrices

world4-vector 4-vector

4x4 matrix 32

pq

q = Ap

Page 33: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Affine Transformations (4x4 matrices)

• Translation

• Rotation

• Scaling

• Any composition of the above

• Later: projective (perspective) transformations -Also expressible as 4 x 4 matrices!

33

Page 34: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Translation

• where ,

• ,

• ,

• Express in matrix form and solve for

34

q = p+ d d = [↵x,↵y,↵z, 0]>

p = [x, y, z, 1]>

q = [x0, y0, z0, 1]>

q = Tp T

Page 35: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Scaling

• Express as and solve for

35

x0 = �xx

y0 = �yy

z0 = �zz

q = Sp S

Page 36: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Rotation in 2 Dimensions

• Rotation by about the origin

• Express in matrix form:

• Note that the determinant is

36

x0 = x cos(✓)� y sin(✓)

y0 = x sin(✓) + y cos(✓)

1

Page 37: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Rotation in 3 Dimensions

• Orthogonal matrices:

• Affine transformation:

37

RR> = R>R = I

det(R) = 1

Page 38: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Affine Matrices are Composedby Matrix Multiplication

• Applied from right to left

• When calling glTranslate3f, glRotatef, or glScalef,OpenGL forms the corresponding 4x4 matrix, and multiplies the current modelview matrix with it.

38

A = A1A2A3

Ap = (A1A2A3)p = A1(A2(A3p))

Page 39: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Summary

• OpenGL Transformation Matrices

• Vector Spaces

• Frames

• Homogeneous Coordinates

• Transformation Matrices

39

Page 40: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

Next Time: Viewing & Projection

40

Page 41: Lecture02 - Hao Li · vertices in 2D vertices in 3D vertices in canonical 3D world coordinate system • Translate, rotate, scale objects • Position the camera Model-view Projection

http://cs420.hao-li.com

Thanks!

41