61
2D Transformations 3D Transformations OpenGL Transformation

2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

  • Upload
    others

  • View
    26

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

2D Transformations3D TransformationsOpenGL Transformation

Page 2: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

The most basic ones Translation Scaling Rotation Shear Shear And others, e.g., perspective transform, projection, etc

Basic types of transformations

Rigid-body: preserves length and angle

Affine: preserves parallel lines, not angles or lengths

Free-form: anything goes

Page 3: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

Basic Transformations Homogeneous coordinate system Composition of transformations Composition of transformations

Page 4: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

(4,5) (7,5)

Y Y

XBefore Translation

1

*

100

10

01

1

y

x

d

d

y

x

TPPd

dT

y

xP

y

xP

y

x

y

x

Form sHomogeniou

x’ = x + dx

y’ = y + dy

(7,1) (10,1)

XTranslation by (3,-4)

Page 5: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

(4,5) (7,5)

Y

X(2,5/4) (7/2,5/4)

X

Y

Before Scaling Scaling by (1/2, 1/4)

Types of Scaling:

Differential ( sx != sy )Uniform ( sx = sy )

X XBefore Scaling Scaling by (1/2, 1/4)

y

x

y

x

y

x

sy

sx

y

x

s

s

PPS

ysy

xsx

*

**

0

0

*

*

*

1

*

100

00

00

1

Form sHomogeniou

y

x

s

s

y

x

y

x

Page 6: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

cosr

v

sin

cos

r

rv

sin

cos

r

rv

cossinsincos

sinsincoscos

rry

rrx expand

cossin

sincos

sin

cos

yxy

yxx

ry

rx

but

Page 7: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

Y

(2.1,4.9)

(4.9,7.8)

YBefore Rotation Rotation of 45 deg. w.r.t. origin

(5,2) (9,2)X X

1

*

100

0cossin

0sincos

1

Form sHomogeniou

y

x

y

x

cos*sin*

sin*cos**

cossin

sincos

*

yx

yx

y

x

PPR

yyx

xyx

cos*sin*

sin*cos*

Page 8: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

(1,1)

Y

X

(-1,1) (1,1)

X

Y

100

010

001

axis-Xabout Reflection

xM

yyxx

100

010

001

axis-Yabout Reflection

yM

yyxx

(1,-1)

Page 9: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

100

01

01

100

01

001

100

010

01

b

a

SHbSH

a

SH xyyx

100100100

unit cubeSheared in X

directionSheared in Y

directionSheared in both X

and Y direction

Page 10: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

-

(-θ-(θ

(-dx,-dy)-

(dx,dy)

SS

RR

TT

1

)1)

1

: Sclaing

: Rotation

:nTranslaito

y-y

x-x

),(-(sx,sy)

MM

MM

SSsysx

1

1

1

:RefMirror

: Sclaing 11

Page 11: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

Translation, scaling and rotation are expressed (non-homogeneously) as: translation: P = P + T

Scale: P = S · PScale: P = S · P

Rotate: P = R · P Composition is difficult to express, since translation

not expressed as a matrix multiplication Homogeneous coordinates allow all three to be

expressed homogeneously, using multiplication by 3 3 matrices

W is 1 for affine transformations in graphics

Page 12: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

P2d is a projection of Ph onto the w = 1 plane So an infinite number of points correspond to :

they constitute the whole line (tx, ty, tw)

x

y

w Ph(x,y,w)

P2d(x,y,1)

w=1

Page 13: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

1. Rigid-body Transformation

Preserves parallelism of lines

Preserves angle and length

e.g. any sequence of R() and T(dx,dy)

2. Affine Transformation2. Affine Transformation

Preserves parallelism of lines

Doesn’t preserve angle and length

e.g. any sequence of R(), S(sx,sy) and T(dx,dy)

unit cube 45 deg rotaton Scale in X not in Y

Page 14: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

1002221

1211

y

x

trr

trr

The following Matrix is Orthogonal if the upper left 2X2 matrix has the following properties

1.A) Each row are unit vector.

sqrt(r11* r11 + r12* r12) = 1

B) Each column are unit vector.

sqrt(c11* c11 + c12* c12) = 1

2.A) Rows will be perpendicular to each other

(r11 , r12 ) . ( r21 , r22) = 0

B) Columns will be perpendicular to each other

(c11 , c12 ) . (c21 ,c22) = 0

e.g. Rotation matrix is orthogonal

100

0cossin

0sincos

• Orthogonal Transformation Rigid-Body Transformation• For any orthogonal matrix B B-1 = BT

Page 15: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

• In general matrix multiplication is not commutative• For the following special cases commutativity holds i.e.

M1.M2 = M2.M1

M1 M2

Translate TranslateTranslate Translate

Scale Scale

Rotate Rotate

Uniform Scale Rotate

• Some non-commutative Compositions: Non-uniform scale, Rotate Translate, Scale Rotate, Translate

OriginalTransitional

Final

Page 16: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

Create new affine transformations by multiplying sequences of the above basic transformations.

q = CBAp

q = ( (CB) A) p = (C (B A))p = C (B (Ap) ) etc.

matrix multiplication is associative.

But to transform many points, best to do

M = CBA

then do q = Mp for any point p to be rendered.

To transform just a point, better to do q = C(B(Ap))

For geometric pipeline transformation, define M and set it up with the model-view matrix and apply it to any vertex subsequently defined to its setting.

Page 17: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

R =

Step 1: Translate P(h,k) to origin

T(-h ,-k)

Step 2: Rotate w.r.t to origin

R *

Step 3: Translate (0,0) to P(h,k0)

T(h ,k) *

P3(h,k)

Q3(x’+h, y’ +k)

R,P=

Q(x,y)

P(h,k)

T(-h ,-k)

Q1(x’,y’)

P1 (0,0)

R*

Q2(x’,y’)

P2 (0,0)

T(h ,k) *

Page 18: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

S =

Step 1: Translate P(h,k) to origin

Step 2: Scale S(sx,sy) w.r.t origin

(7,1)

Step 3: Translate (0,0) to P(h,k)(7,2)

(1,1)T(-h ,-k)S(sx,sy)*T(h ,k) *Ssx,sy,P=

(4,3)

(1,1) (4,1)

S3/2,1/2,(1,1)

(4,2)

(0,0) (4,0)

T(-1,-1)

(6,1)

(6,0)(0,0)

S(3/2,1/2)

(7,1)(1,1)

T(1,1)

Page 19: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

Step 1: Translate (0,b) to origin

Step 2: Rotate - degrees

YYYYYY

T(0 ,-b)ML =

Step 3: Mirror reflect about X-axis

R(-) *T(0 ,b) *

Step 4: Rotate degrees

Step 5: Translate origin to (0,b)

M x*R() *

(0,b)

X

t

O XO XO XO XO

(0,b)

X

t

O

Page 20: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

Schaum’s outline series:

Problems:

4.1

4.2

4.3, 4.4, 4.5 => R,P

4.6, 4.7, 4.8 => S 4.6, 4.7, 4.8 => Ssx,sy,P

4.9, 4.10, 4.11, 4.21 => ML

4.12 => Shearing

Page 21: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

1. www.willamette.edu/~gorr/classes/GeneralGraphics/Transforms/transforms2d.htm2. http://www.cs.sfu.ca/~torsten/Teaching/Cmpt361/LectureNotes/PDF/06_2Dtrans.pdf

Page 22: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

Basics of 3D geometryBasic 3D TransformationsComposite Transformations

Page 23: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

Thumb points to +ve Z-axis Fingers show +ve rotation from X to Y

axis

Y YY

X

Z (out of page)

Y

X

Z (larger z areaway from viewer)

Right-handed orentation Left-handed orentation

Page 24: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

Transformation – is a function that takes a point (or vector) and maps that point (or vector) into another point (or vector).

A coordinate transformation of the form:

x’ = axx x + axy y + axz z + bx ,

y’ = ayx x + ayy y + ayz z + by ,

'

'

'

z

y

x

baaa

baaa

baaa

z

y

x

yyzyyyx

xxzxyxx

z’ = azx x + azy y + azz z + bz ,

is called a 3D affine transformation.

11000

' zbaaa

w

z zzzzyzx

The 4th row for affine transformation is always [0 0 0 1].

Properties of affine transformation:– translation, scaling, shearing, rotation (or any combination of them)

are examples affine transformations.

– Lines and planes are preserved.

– parallelism of lines and planes are also preserved, but not angles and length.

Page 25: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

z

y

x

dzz

dyy

dxx

PPdddT

dz

dy

dx

z

y

x

d

d

d

zyx

z

y

x

z

y

x

*),,(

11

*

1000

100

010

001

Page 26: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

Original scale Y axiszsz

ysy

xsx

z

y

x

*

*

*

1

*

*

*

1

*

1000

000

000

000

*),,(

z

y

x

z

y

x

zyx

sz

sy

sx

z

y

x

s

s

s

PPsssS

scale all axes

Page 27: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

For 3D-Rotation 2 parameters are needed

Angle of rotation

Axis of rotation

Rotation about z-axis:

1

cos*sin*

sin*cos*

1

*

1000

0100

00cossin

00sincos

*,

z

yx

yx

z

y

x

PPR k

Page 28: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

cos*sin*

sin*cos*

*0cos0sin

0010

0sin0cos

*,

zx

y

zx

z

y

x

PPR jAbout y-axis

111000

1

cos*sin*

sin*cos*

1

*

1000

0cossin0

0sincos0

0001

*,

zy

zy

x

z

y

x

PPR iAbout x-axis

Page 29: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

*),( PPshshSH yxxy

y

z

x

1

*

*

1

*

1000

0100

010

001

z

shzy

shzx

z

y

x

sh

sh

y

x

y

x

Page 30: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

Some of the composite transformations to be studied are:

AV,N = aligning a vector V with a vector N R = rotation about an axis L( V, P ) R,L = rotation about an axis L( V, P ) Ssx,sy,P= scaling w.r.t. point P

Page 31: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

z

22λ

cos

λsin

by axis-about x Rotate :1 Step

cbc

b

b

z

( 0, b,c)b

z

( a, 0, )

( 0, 0, )

( 0, b,c)

Av = R,i

V = aI + bJ + cK

x

yb

a

c

k

λcos

|V|

x

yb

a

k

|V|

x

y

a

k

|V|

( a, 0, )

Page 32: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

22λ

cos

λsin

by axis-about x Rotate :1 Step

cbc

b

b

z

( a, 0, )( 0, b,c)b

z( 0, 0, |V|)

( 0, b,c)a

Av = R,iR-,j *

λcos

222

|V|

|V|

λ)cos(

|V|)sin(

-by axis-yabout V Rotate :2 Step

cba

a

P( a, b, c)

x

yb

a

c

k

|V|

( a, 0, )

x

yb

a

c

|V|

Page 33: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

AV-1 = AV

T

AV,N = AN-1 * AV

0--λ acab

1000

0

00

0

λλ

λ-

λ-λ

Vc

Vb

Va

bc

Vac

Vab

V

VA

Page 34: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

Let the axis L be represented by vector V and passing through point P

1. Translate P to the origin P

Q

L

z

2. Align V with vector k

3. Rotate about k

4. Reverse step 2

5. Reverse step 1

R,L = T-PAV *R,k *AV-1 *T-P

-1 *

V

Q

Q'

x

y

k

Page 35: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

Let the plane be represented by plane normal N and a point P in that plane

z

x

y

Page 36: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

Let the plane be represented by plane normal N and a point P in that plane

1. Translate P to the origin z

MN,P = T-P

x

y

Page 37: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

Let the plane be represented by plane normal N and a point P in that plane

1. Translate P to the origin z

2. Align N with vector k

MN,P = T-PAN *

x

y

Page 38: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

Let the plane be represented by plane normal N and a point P in that plane

1. Translate P to the origin z

2. Align N with vector k

3. Reflect w.r.t xy-plane

MN,P = T-PAN *S1,1,-1 *

x

y

Page 39: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

z

Let the plane be represented by plane normal N and a point P in that plane

1. Translate P to the origin

x

y

2. Align N with vector k

3. Reflect w.r.t xy-plane

4. Reverse step 2

MN,P = T-PAN *S1,1,-1 *AN-1 *

Page 40: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

Let the plane be represented by plane normal N and a point P in that plane

1. Translate P to the origin z

2. Align N with vector k

3. Reflect w.r.t xy-plane

4. Reverse step 2

5. Reverse step 1

MN,P = T-PAN *S1,1,-1 *AN-1 *T-P

-1 *

x

y

Page 41: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

The Composite Transform must have

Translate points in fig. 1 into points in fig 2 such that:

– P1 is at Origin

– P1P2 is along positive z-axis

– P1P3 lies in positive y-axis half of yz plane

– Translation of P1 to Origin T

z

x

y

3P

1P

2PT

– Some Combination of Rotations R

R

x

y

z 2P

3P 1P

z

x

y3P

1P2P

Fig. 1 Fig. 2

Page 42: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

zzz

yyy

xxx

zRyRxR

zRyRxR

zRyRxR

rrr

rrr

rrr

R

R

Transform body-Rigid is R

be Let

...

...

...

333231

232221

131211

xx

zyx

zyx

Rx.x R

RRR

RRR

vextor of component :Note

other each to

larperpendicu are ii)

vectors unit are i)

Transform body-Rigid is R

,,

,,

Page 43: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

TT RRkR

21 PPˆ 1

x

y

3P

1P

2P Rz

kPP

PPR

axis-z along PP aligns R

21

21

21

ˆ

z

z

z

z

zyx

zyx

zyx

TT

R

zR

yR

xR

zRzRzR

yRyRyR

xRxRxR

RRkR

21

21

21

21

21

21

PP

PP

PP

PP

PP

PP

.

.

.

1

0

0

...

...

...

ˆ 1

R

z1

x

y

z 2P

3P 1P

k

Page 44: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

x

y

3P

1P 2P

T2131T RRPPPP

iR

ˆ 1 Rx

iR

R

ˆ

2131

2131

2131

PPPP

PPPP

axis-x along PPPP aligns

Rz

R

x

y

z 2P

3P 1P

z

x

2131

2131

x

x

x

2131

2131

zyx

zyx

zyx

T

2131

2131T

RPPPP

PPPP

zR

yR

xR

PPPP

PPPP

zRzRzR

yRyRyR

xRxRxR

RRPPPP

iR

.

.

.

0

0

1

...

...

...

ˆ 1 Rx

ik

Page 45: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

jR

R

ˆ

xz

xz

RR

axis- yalong RR aligns

x

y

3P

1P 2P

R

Rz

Ry

yxz

y

y

y

xz

zyx

zyx

zyx

Txz

T

RRR

zR

yR

xR

RR

zRzRzR

yRyRyR

xRxRxR

RRRRjR

.

.

.

0

1

0

...

...

...

ˆ 1R

x

y

z 2P

3P 1P

z Rx

ik

j

Page 46: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

Schaum’s outline series:

Problems:

6.1

6.2, 6.5, 6.9, 6.10, 6.11, 6.12 Av

6.3, 6.4 R,L

6.6, 6.7, 6.8 M 6.6, 6.7, 6.8 MN,P

Page 47: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

OpenGL transformation commandsOpenGL transformation commandsTransformation OrderHierarchical Modeling

Page 48: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

OpenGL uses 3 stacks to maintain transformation matrices:

Model & View transformation matrix stack

Projection matrix stack

Texture matrix stack Texture matrix stack

You can load, push and pop the stack The top most matrix from each stack is applied to all graphics primitive until it is changed

M N

Model-ViewMatrix Stack

ProjectionMatrix Stack

GraphicsPrimitives(P)

OutputN•M•P

Page 49: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

Specify current matrix (stack) : void glMatrixMode(GLenum mode)

▪ Mode : GL_MODELVIEW, GL_PROJECTION, GL_TEXTURE

Initialize current matrix. void glLoadIdentity(void)

ABC

glL

oad

Mat

rix(

M) void glLoadIdentity(void)

▪ Sets the current matrix to 4X4 identity matirx

void glLoadMatrix{f|d}(cost TYPE *M)▪ Sets the current matrix to 4X4 matrix specified by M

Note: current matrix Top most matrix of the current

matrix stack

ABI

ABM

glL

oad

Mat

rix(

M)

Page 50: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

Concatenate Current Matrix: void glMultMatrix(const TYPE *M)▪ Multiplies current matrix C, by M. i.e. C = C*M

Caveat: OpenGL matrices are stored in column major order.column major order.

Best use utility function glTranslate, glRotate, glScale for common transformation tasks.

161284

151173

141062

13951

mmmm

mmmm

mmmm

mmmm

Page 51: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

Each time an OpenGL transformation M is called the current MODELVIEW matrix C is altered:

Cvv CMvv

glTranslatef(1.5, 0.0, 0.0);glRotatef(45.0, 0.0, 0.0, 1.0);

CTRvv

Note: v is any vertex placed in rendering pipeline v’ is the transformed vertex from v.

Page 52: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

glMatrixMode(GL_MODELVIEW);glLoadIdentity();glTranslatef(...);glRotatef(...);glScalef(...);gluCylinder(...);glScalef(...);gluCylinder(...);

Page 53: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

There is a World Coordinate System where: All objects are defined

Transformations are in World Coordinate space

Two Different Views

As a Global System Objects moves but

coordinates stay the same

Think of transformation in reverse order as they appear in code

As a Local System Objects moves and

coordinates move with it Think of transformation

in same order as they appear in code

Page 54: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

Local View

Translate Object

Then Rotate

glLoadIdentity();

glMultiMatrixf( T);

glMultiMatrixf( R);

draw_ the_ object( v);

v’ = ITRv

Global View

Rotate Object

Then Translate

Effect is same, but perception is different

Page 55: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

glLoadIdentity();

glMultiMatrixf( R);

glMultiMatrixf( T);

draw_ the_ object( v);

v’ = ITRv

Local View

Rotate Object

Then Translate

Global View

Translate Object

Then Rotate

Effect is same, but perception is different

Page 56: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

Many graphical objects are structured Exploit structure for

Efficient rendering

Concise specification of model parameters

Physical realism Often we need several instances of an object Often we need several instances of an object

Wheels of a car

Arms or legs of a figure

Chess pieces Encapsulate basic object in a function Object instances are created in “standard” form Apply transformations to different instances Typical order: scaling, rotation, translation

Page 57: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

– void glPushMatrix(void);

– void glPoipMatrix(void);

Some of the OpenGL functions helpful for hierarchical modeling are:

ABCC

AB

– void glPoipMatrix(void);

ABC C

m

glGetFloatv

– void glGetFloatv(GL_MODELVIEW_MATRIX, *m);

ABC

Page 58: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

A scene graph is a hierarchical representation of a scene We will use trees for representing hierarchical objects such

that: Nodes represent parts of an object

Topology is maintained using parent-child relationship Topology is maintained using parent-child relationship

Edges represent transformations that applies to a part and all the subparts connected to that part

typedef struct treenode {

GLfloat m[16]; // Transformation

void (*f) ( ); // Draw function

struct treenode *sibling;

struct treenode *child;

} treenode;

Scene

Sun Star X

Earth Venus Saturn

Moon Ring

Page 59: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

Initializing transformation matrix for nodetreenode torso, head, ...;/* in init function */glLoadIdentity();glRotatef(...);glGetFloatv(GL_MODELVIEW_MATRIX, torso.m);glGetFloatv(GL_MODELVIEW_MATRIX, torso.m);

Initializing pointerstorso.f = drawTorso;torso.sibling = NULL;torso.child = &head;

Page 60: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

To render the hierarchy:

Traverse the scene graph depth-first

Going down an edge:

▪ push the top matrix onto the stack

▪ apply the edge's transformation(s)

At each node, render with the top matrix

Going up an edge:

▪ pop the top matrix off the stack

Page 61: 2D Transformations 3D Transformations OpenGL Transformationsabbirsaleh.weebly.com/.../1/0/4510327/4_transformations.pdf · 2018. 9. 6. · 3D Transformations OpenGL Transformation

Recursive definitionvoid traverse (treenode *root) {

if (root == NULL) return;glPushMatrix();glMultMatrixf(root->m);root->f();glMultMatrixf(root->m);root->f();if (root->child != NULL) traverse(root->child);glPopMatrix();if (root->sibling != NULL) traverse(root->sibling);

}

C is really not the right language for this !!