Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 59

Hochiminh city University of Technology

Faculty of Computer Science and Engineering

COMPUTER GRAPHICS

CHAPTER 06:

Transformations
OUTLINE
 Introduction
 Transformation in 2D
 Transformation in 3D
 Transformation in OpenGL

Faculty of Computer Science and Engineering - HCMUT Slide 2


Introduction

Faculty of Computer Science and Engineering - HCMUT Slide 3


Introduction
 General Transformations
A transformation maps points to other points and/or
vectors to other vectors
v=T(u)

Q=T(P)

Faculty of Computer Science and Engineering - HCMUT Slide 4


Introduction
 General Transformations
P = (Px, Py, 1); Q = (Qx, Qy, 1) (Q - image)
(Qx, Qy, 1) = T (Px, Py, 1) (T – transformation)
Q = T(P).

a) b) y
Q
y T
Q
P
0
P
x x
z

Faculty of Computer Science and Engineering - HCMUT Slide 5


Introduction
 General Transformations

a) b) y
after
y
after before

before
x x

Faculty of Computer Science and Engineering - HCMUT Slide 6


Introduction
 Affine Transformations

Qx = m11Px +m12Py +m13


Qy = m21Px +m22Py +m23

 Qx   m11 m12 m13  Px 


    
 Q y    m21 m22 m23  Py 
 1   0 0 1  1 
  
always (0, 0, 1)
T

Faculty of Computer Science and Engineering - HCMUT Slide 7


Introduction
 Affine Transformations
– Line preserving
– Characteristic of many physically important
transformations
• Rigid body transformations: rotation,
translation
• Scaling, shear
– Importance in graphics is that we need only transform
endpoints of line segments and let implementation
draw line segment between the transformed endpoints

Faculty of Computer Science and Engineering - HCMUT Slide 8


Introduction
 Pipeline Implementation
glBegin(GL_LINES);
glVertex3f(. . .);
glVertex3f(. . .);
glVertex3f(. . .);
glEnd();

P1, P2,... Q1, Q2,...


CT

y P3 y
P2

P1
z x z x

Faculty of Computer Science and Engineering - HCMUT Slide 9


Transformations in 2D
 Translation
 Qx   1 0 m13  Px 
Qx = Px + m13     
Qy = Py + m23  Q y    0 1 m23  Py 
 1   0 0 1  1 
    

y 1 0 2 y
 
0 1 1
0 0 1
 
1
x x
2

Faculty of Computer Science and Engineering - HCMUT Slide 10


Transformations in 2D
 Scaling
 Qx   S x 0 0  Px 
Qx = Sx Px     
Qy = Sy Py  Qy    0 Sy 0  Py 
 1 0 0 1  1 
  y
1.3 0 0 
y   uniform
 0 1 .3 0 
 0 0 1 
  x
y
x 1.5 0 0  Non-
  uniform
 0 0. 5 0 
 0 0 1 
 
x

Faculty of Computer Science and Engineering - HCMUT Slide 11


Transformations in 2D
 Scaling (Reflection)
y
  1 0 0
    1 0 0
 
 0 1 0  0 1 0
 0 0 1
 
 0 0 1
y   x
y

x   1 0 0
  x
 0  1 0
 0 0 1 
 

Faculty of Computer Science and Engineering - HCMUT Slide 12


Transformations in 2D
 Scaling (Reflection)

  1 0 0
 
 0 2 0
 0 0 1
 
Faculty of Computer Science and Engineering - HCMUT Slide 13
Transformations in 2D
 Rotation  cos( )  sin( ) 0 
Qx = Px cos() – Py sin()  
 sin( ) cos( ) 0 
Qy = Px sin() + Py cos()  0 
 0 1 
 0.5  3 / 2 0 
 
y  3/2 0.5 0 y
 
 0 0 1 

x 600 x

Faculty of Computer Science and Engineering - HCMUT Slide 14


Transformations in 2D
 Rotation
y
Q

R P

 x

Qx  R cos(   )
Q y  R sin(   )
Qx  R cos  cos   R sin  sin   Px cos   Py sin 
Q y  R sin  cos   R cos  sin   Px sin   Py cos 

Faculty of Computer Science and Engineering - HCMUT Slide 15


Transformations in 2D
y
 Shear  1 h 0
 
 0 1 0
 0 0 1 x
 
y
y  1 0 0
 
 g 1 0
 0 0 1
x   x

x
Faculty of Computer Science and Engineering - HCMUT Slide 16
Transformations in 2D
 Concatenation

y Q
T1()
T2()
P
W
T()
x

Faculty of Computer Science and Engineering - HCMUT Slide 17


Transformations in 2D
 Concatenation
– We can form arbitrary affine transformation matrices
by multiplying together rotation, translation, and
scaling matrices
– Because the same transformation is applied to
many vertices, the cost of forming a matrix
M=ABCD is not significant compared to the cost of
computing Mp for many vertices p

Faculty of Computer Science and Engineering - HCMUT Slide 18


Transformations in 2D
 Concatenation
– Note that matrix on the right is the first applied
– Mathematically, the following are equivalent
• W = T2 Q = T2 (T1 P) = (T2 T1 )P

y Q
T1()
T2()
P
W
T()
x

Faculty of Computer Science and Engineering - HCMUT Slide 19


Transformations in 2D
 Concatenation
Q
- Move fixed point to origin
- Rotate Q’  P
V
- Move fixed point back
 P’
- M = T(pf) R(q) T(-pf)
 1 0 Vx  cos( )  sin( ) 0  1 0  Vx 
   
 0 1 V y  sin( ) cos( ) 0  0 1  V y 
 0 0 1  0 0 1  0 0 1 
 
 cos( )  sin( ) d x 
 
  sin( ) cos( ) d y 
 0 0 1 
 
Faculty of Computer Science and Engineering - HCMUT Slide 20
Transformations in 2D

Faculty of Computer Science and Engineering - HCMUT Slide 21


Transformations in 3D
 General Formula
 m11 m12 m13 m14 
 
 m21 m22 m23 m24 
M 
m m32 m33 m34 
 31 
 0 0 0 1 

 Qx   Px 
   
Qy   Py 
Q   M  P 
 z   z 
 1 1

Faculty of Computer Science and Engineering - HCMUT Slide 22


Transformations in 3D
 Translation
1 0 0 m14 
 
0 1 0 m24 
0 0 1 m34 
 
0 0 0 1 
 Scaling
y y
 Sx 0 0 0
 
0 Sy 0 0
0 0 Sz 0
  z x z x
0 0 0 1

Faculty of Computer Science and Engineering - HCMUT Slide 23


Transformations in 3D
 Shear
1 0 0 0
 
f 1 0 0 Q = (Px, fPx + Py, Pz )
0 0 1 0
 
0 0 0 1
 Rotation
y  x-roll, y-roll, z-roll
 when angle = 900:
P’ Q’
z-roll: xy
Q’’
Q x-roll: yz
P
P’’
z x y-roll: zx

Faculty of Computer Science and Engineering - HCMUT Slide 24


Transformations in 3D
 Rotation
1 0 0 0  c 0 s 0 c  s 0 0
     
0 c  s 0  0 1 0 0 s c 0 0
Rx (  )    Ry (  )   Rz (  )  
0 s c 0 s 0 c 0  0 0 1 0
     
0 0 0 1  0 0 0 1 0 0 0 1

a) object b) Rotate about Ox c) Rotate about Oy d) Rotate aboute


(- 700) (300) Oz (- 900)
y y y y

z x z x z x z x

Faculty of Computer Science and Engineering - HCMUT Slide 25


Transformations in 3D
 Rotation about an Arbitrary Axis
y u
 u  x.
 Rotate about x axis with angle .
Q  recover u.

P

z  x

Faculty of Computer Science and Engineering - HCMUT Slide 26


Transformations in 3D

y
u u  x
Q – Rotate u around y (+)

– Then rotate around z (-)
P
 Rotate around x (+)

 Recover u
z 
x – Rotate around z (+)
– Rotate around y (-)

Ru()=Ry(-)Rz()Rx()Rz(-)Ry()

Faculty of Computer Science and Engineering - HCMUT Slide 27


Transformations in 3D
y u

Q

P
 Ru()=Ry(-)Rz()Rx()Rz(-)Ry()
z 
x

 c  (1  c )u x2 (1  c )u y u x  su z (1  c )uz u x  su y 0
 
 (1  c )u x u y  su z c  (1  c )u 2y (1  c )uz u y  su x 0
 (1  c )u u  su (1  c )u y uz  su x c  (1  c )uz2 0
 x z y 
 0 0 0 1 

Faculty of Computer Science and Engineering - HCMUT Slide 28


Transformations in OpenGL
 How to draw this picture (the red letter H)

Faculty of Computer Science and Engineering - HCMUT Slide 29


Transformations in OpenGL
 How to draw the blue H

Faculty of Computer Science and Engineering - HCMUT Slide 30


Transformations in OpenGL
 How to draw the blue H

Faculty of Computer Science and Engineering - HCMUT Slide 31


Transformations in OpenGL
 How to draw the purple H

Faculty of Computer Science and Engineering - HCMUT Slide 32


Transformations in OpenGL
 How to draw the purple H

Faculty of Computer Science and Engineering - HCMUT Slide 33


Transformations in OpenGL
 How to draw the blue H

Faculty of Computer Science and Engineering - HCMUT Slide 34


Transformations in OpenGL

Faculty of Computer Science and Engineering - HCMUT Slide 35


Transformations in OpenGL
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); CTM = I
drawFigure0();

CTM: Current Transf. Matrix


Sy
V Q S S
CTM WV

V Sx
Q

Faculty of Computer Science and Engineering - HCMUT Slide 36


Transformations in OpenGL
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); CTM = I

glColor3f(1, 0, 0); CTM = CTM* =


drawFigure0();

glColor3f(0, 0, 1);
glTranslatef(4, 5, 0); CTM = CTM* =
glRotatef(60, 0, 0, 1);
drawFigure0();

Faculty of Computer Science and Engineering - HCMUT Slide 37


Transformations in OpenGL
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

drawGrid();

glColor3f(0, 0, 1);
glTranslatef(4, 5, 0);
glRotatef(60, 0, 0, 1);
drawFigure0();

glColor3f(1, 0, 0);
drawFigure0();

Faculty of Computer Science and Engineering - HCMUT Slide 38


Transformations in OpenGL
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); CTM = I

glColor3f(0, 0, 1);
glTranslatef(4, 5, 0); CTM =
glRotatef(60, 0, 0, 1);
drawFigure0();

glColor3f(1, 0, 0);
drawFigure0();

Faculty of Computer Science and Engineering - HCMUT Slide 39


Transformations in OpenGL
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); CTM = I I

glPushMatrix();
glColor3f(0, 0, 1);//The blue H
glTranslatef(4, 5, 0); CTM =
glRotatef(60, 0, 0, 1);
drawFigure0();
glPopMatrix();

glColor3f(1, 0, 0);//The red H CTM = I


drawFigure0();

Faculty of Computer Science and Engineering - HCMUT Slide 40


Transformations in OpenGL

Faculty of Computer Science and Engineering - HCMUT Slide 41


Transformations in OpenGL
 Draw the blue H

Faculty of Computer Science and Engineering - HCMUT Slide 42


Transformations in OpenGL

Faculty of Computer Science and Engineering - HCMUT Slide 43


Transformations in OpenGL

 Qx = Px + hPy (Qx, Qy) = (0.6, 2.0); (Px, Py) = (0, 2.0)


0.6 = 0 + 2h  h = 0.3

Faculty of Computer Science and Engineering - HCMUT Slide 44


Transformations in OpenGL

Faculty of Computer Science and Engineering - HCMUT Slide 45


Transformations in OpenGL
 glMultMatrixf

Faculty of Computer Science and Engineering - HCMUT Slide 46


Transformations in OpenGL
 Load an identity matrix:
– glLoadIdentity()
 Rotation, Translation, Scaling
– glRotatef(theta, vx, vy, vz)
• theta in degrees, (vx, vy, vz) define axis of rotation
– glTranslatef(dx, dy, dz)
– glScalef( sx, sy, sz)

Faculty of Computer Science and Engineering - HCMUT Slide 47


Transformations in OpenGL
 Can load and multiply by matrices defined in the
application program
– glLoadMatrixf(m)
– glMultMatrixf(m)
 The matrix m is a one dimension array of 16 elements
which are the components of the desired 4 x 4 matrix
stored by columns
 In glMultMatrixf, m multiplies the existing matrix on
the right

Faculty of Computer Science and Engineering - HCMUT Slide 48


Transformations in OpenGL
 Reading Back Matrices
– Can also access matrices (and other parts of the state) by
query functions
• glGetIntegerv
• glGetFloatv
• glGetBooleanv
• glGetDoublev
• glIsEnabled
– For matrices, we use as
• float m[16];
• glGetFloatv(GL_MODELVIEW_MATRIX, m);

Faculty of Computer Science and Engineering - HCMUT Slide 49


Transformations in OpenGL

B’ C’

C
A’ D’
B

D
A

Tìm ma trận biến đổi hình bình hành thành hình chữ nhật

Faculty of Computer Science and Engineering - HCMUT Slide 50


Transformations in OpenGL

1 0 0  1
0 1 0 1 
M1   
0 0 1 0
 
0 0 0 1

Faculty of Computer Science and Engineering - HCMUT Slide 51


Transformations in OpenGL
 1 0 0
 
 g 1 0
 0 0 1
 
Qy = gPx + Py
(2, 1)  (2, 0)
0 = 2g + 1  g = -0.5

 1 0 0 0
  0.5 1 0 0
M2   
 0 0 1 0
 
 0 0 0 1
Faculty of Computer Science and Engineering - HCMUT Slide 52
Transformations in OpenGL

5 / 2 0 0 0
 0 3/ 4 0 0 
M3  
 0 0 1 0
 
 0 0 0 1

Faculty of Computer Science and Engineering - HCMUT Slide 53


Transformations in OpenGL

1 0 0  5
0 1 0 6
M4   
0 0 1 0
 
0 0 0 1

Faculty of Computer Science and Engineering - HCMUT Slide 54


Transformations in OpenGL
Tịnh tiến (M1)
Trượt theo trục y (M2)
Tỷ lệ (M3)
Tịnh tiến (M4)

Faculty of Computer Science and Engineering - HCMUT Slide 55


Transformations in OpenGL
1 0 0  1  1 0 0 0
0 1 0  1   0.5 1 0 0
M1    M2   
0 0 1 0  0 0 1 0
   
0 0 0 1  0 0 0 1

5 / 2 0 0 0 1 0 0  5
 0 3/ 4 0 0  0 1 0 6 
M3   M4   
 0 0 1 0 0 0 1 0
   
 0 0 0 1 0 0 0 1
M = M4*M3*M2*M1

Faculty of Computer Science and Engineering - HCMUT Slide 56


Transformations in OpenGL
M = M4*M3*M2*M1

C’  5/ 2 0 0  15 / 2
B’  3 / 8 3 / 4 0 45 / 8 
M 
C  0 0 1 0 
A’ D’  
B  0 0 0 1 

D
A

Faculty of Computer Science and Engineering - HCMUT Slide 57


Transformations in OpenGL

C’ Qx = m11Px +m12Py +m13


B’
Qy = m21Px +m22Py +m23
C -5 = m11 + m12 + m13
A’ D’ 6 = m21 + m22 + m23
B
0 = 3m11+ 6m12 + m13
D 9 = 3m21 + 6m22 + m23
A
0 = 3m11 + 2m12 + m13
6 = 3m21 + 2m22 + m23

m11 = 5/2, m12 = 0, m13 = -15/2


m21 = -3/8, m22= 3/4,m23 = 45/8

Faculty of Computer Science and Engineering - HCMUT Slide 58


Transformations in OpenGL

Faculty of Computer Science and Engineering - HCMUT Slide 59

You might also like