• Tidak ada hasil yang ditemukan

Transformasi Obyek (Kasus 2D)

N/A
N/A
Protected

Academic year: 2021

Membagikan "Transformasi Obyek (Kasus 2D)"

Copied!
58
0
0

Teks penuh

(1)

Transformasi Obyek

(Kasus 2D)

Grafika Komputer

Semester Ganjil 2008

(2)

Kompetensi

1.

Mampu membangun tool untuk

mentransformasi obyek

2.

Mampu memahami konsep transformasi affine

yang merupakan kombinasi dari rotasi,

penskalaan, dan translasi

3.

Mampu mengimplementasikan konsep

(3)

Referensi

Computer Graphics using OpenGL, 3rd

Edition, by: F.S. Hill, Jr. and Stephen M. Kelley

– Chapter 5

Computer Graphics with OpenGL, 3rd Edition,

by: Donald Hearn and M.Pauline Baker –

Chapter 5

(4)

Pokok Bahasan

1.

Transformasi 2D Dasar

1.

Translasi

2.

Rotasi

3.

Penskalaan

2.

Kombinasi Transformasi

3.

Transformasi Affine

(5)

Kegunaan Transformasi

Me-reposisi ataupun me-resize obyek

Pada proses viewing, untuk mengubah

world-coordinate menjadi display untuk suatu output device

Untuk aplikasi-aplikasi tertentu:

CAD

mengatur orientasi dan ukuran suatu komponen

pada suatu rancangan

Animasi

menggerakkan letak kamera atau obyek pada

(6)

Jenis-jenis Transformasi

Transformasi Geometri

Diterapkan pada deskripsi geometri suatu obyek

Gunanya untuk mengubah posisi, orientasi, atau ukuran suatu obyek

Transformasi modeling

Digunakan untuk membentuk sebuah layar atau membuat deskripsi

hierarki suatu obyek kompleks yang terdiri dari beberapa bagian

Contoh: pesawat terdiri dari sayap, ekor, mesin, dan lain-lain, dimana

masing-masing dapat dispesifikasikan dalam komponen-komponen level

ke-2 dan seterusnya.

Transformasi modeling mendeskripsikan bagaimana setiap komponen

(7)

2D Modeling Transformations

Scale

Rotate

Translate

Scale

Translate

x

y

Modeling

Coordinates

(8)

2D Modeling Transformations

x

y

Modeling

Coordinates

Let’s look

at this in

detail…

(9)

2D Modeling Transformations

x

y

Modeling

Coordinates

Initial location

at (0, 0) with

x- and y-axes

aligned

(10)

2D Modeling Transformations

x

y

Modeling

Coordinates

Scale .3, .3

Rotate -90

Translate 5, 3

(11)

2D Modeling Transformations

x

y

Modeling

Coordinates

Scale .3, .3

Rotate -90

Translate 5, 3

(12)

2D Modeling Transformations

x

y

Modeling

Coordinates

Scale .3, .3

Rotate -90

Translate 5, 3

(13)

Transformasi Geometri 2D

Translasi 2D

x’ = x + t

x

, y’ = y + t

y

(t

x

, t

y

) disebut vektor translasi atau pergeseran

Dalam bentuk matriks:

Persamaan translasi 2D:

P’ = P + T

Line, polygon

translasi titik endpoints, render

Untuk menghapus obyek yang asli

display it in background color before

translating it

Circle, ellipse, spline curve

translasi titik-titik yang mendefinisikan (misalnya

koordinat titik pusat), render obyek hasil translasi di posisi yang baru

y x

t

t

T

y

x

P

y

x

P

'

'

'

(14)

Transformasi Geometri 2D

Scaling 2D

x’ = x· s

x

, y’ = y· s

y

Dalam bentuk matriks:

Uniform scaling

berarti skalar pengali sama untuk semua komponen:

y

x

s

s

y

x

y x

0

0

'

'

2

(15)

Non-uniform scaling

: skalar yang berbeda untuk tiap

komponen:

Transformasi Geometri 2D

X 2,

Y 0.5

(16)

Transformasi Geometri 2D

Rotasi 2D

x’ = r cos (Φ + )

y’ = r sin (Φ + )

Trig Identity…

x’ = r cos(f) cos( ) – r sin(f) sin( )

y’ = r sin(f) cos( ) + r cos(f) sin( )

Original point…

x = r cos (f)

y = r sin (f)

Hasil substitusi…

x’ = x

cos

( ) - y

sin

( )

y’ = x

sin

( ) + y

cos

( )

(x, y)

(x’, y’)

r

r

y

x

y

x

cos

sin

sin

cos

'

'

(17)

Transformasi Geometri 2D

Shear 2D

Shear pada arah x relatif terhadap sumbu

x

: x’ = x + sh

x

· y, y’ =

y

Parameter sh

x

dapat berupa bilangan real

Dalam bentuk matriks:

Shear pada arah y relatif terhadap sumbu

y

: x’ = x, y’ = y + sh

y

·

x

Dalam bentuk matriks:

y

x

sh

y

x

x

1

0

1

'

'

y

x

sh

y

x

y

1

0

1

'

'

(18)

Transformasi Geometri 2D

Reflection 2D

Pencerminan terhadap garis

y

= 0 (sumbu

x

): x’ = x, y’ = -y

Dalam bentuk matriks:

Pencerminan terhadap garis

x

= 0 (sumbu

y

): x’ = -x, y’ = y

Dalam bentuk matriks:

Pencerminan terhadap titik pusat (0, 0): x’ = -x, y’ = -y

Dalam bentuk matriks:

y

x

y

x

1

0

0

1

'

'

y

x

y

x

1

0

0

1

'

'

y

x

y

x

1

0

0

1

'

'

(19)

Basic 2D Transformations

Translation:

x’ = x + t

x

y’ = y + t

y

Scale:

x’ = x * s

x

y’ = y * s

y

Rotation:

x’ = x*cos(θ) -

y*sin(θ)

y’ = x*sin(θ) +

y*cos(θ)

Transformations

can be combined

(with simple algebra)

(20)

Basic 2D Transformations

Translation:

x’ = x + t

x

y’ = y + t

y

Scale:

x’ = x * s

x

y’ = y * s

y

Rotation:

x’ = x*cos(θ) -

y*sin(θ)

y’ = x*sin(θ) +

y*cos(θ)

(21)

Basic 2D Transformations

Translation:

x’ = x + t

x

y’ = y + t

y

Scale:

x’ = x

* s

x

y’ = y

* s

y

Rotation:

x’ = x*cos(θ) -

y*sin(θ)

y’ = x*sin(θ) +

y*cos(θ)

x’ = x

*s

x

y’ = y

*s

y

(x,y)

(x’,y’)

(22)

Basic 2D Transformations

x’ = (x*s

x

)

*cos

θ)

-

(y*s

y

)

*sin

(

θ

)

y’ = (x*s

x

)

*sin

(θ)

+

(y*s

y

)

*cos

(θ)

(x’,y’)

Translation:

x’ = x + t

x

y’ = y + t

y

Scale:

x’ = x * s

x

y’ = y * s

y

Rotation:

x’ = x

*cos(θ)

-

y

*sin(θ)

y’ = x

*sin(θ)

+

y

*cos(θ)

(23)

Basic 2D Transformations

Translation:

x’ = x

+ t

x

y’ = y

+ t

y

Scale:

x’ = x * s

x

y’ = y * s

y

Rotation:

x’ = x

*cos(θ)

-

y

*sin(θ)

y’ = x

*sin(θ)

+

y

*cos(θ)

x’ = ((x*s

x

)*cos

(θ)

- (y*s

y

)*sin

(θ)

)

+ t

x

y’ = ((x*s

x

)*sin

(θ)

+ (y*s

y

)*cos

(θ)

)

+ t

y

(24)

Basic 2D Transformations

Translation:

x’ = x + t

x

y’ = y + t

y

Scale:

x’ = x * s

x

y’ = y * s

y

Rotation:

x’ = x

*cos(θ)

-

y

*sin(θ)

y’ = x

*sin(θ)

+

y

*cos(θ)

x’ = ((x*s

x

)*cos

(θ)

- (y*s

y

)*sin

(θ)

) + t

x

y’ = ((x*s

x

)*sin

(θ)

+ (y*s

y

)*cos

(θ)

) + t

y

(25)

Matrix Representation

Represent 2D transformation by a matrix

Multiply matrix by column vector

apply transformation to point

y

x

d

c

b

a

y

x

'

'

d

c

b

a

dy

cx

y

by

ax

x

'

'

(26)

Matrix Representation

Transformations combined by multiplication

y

x

l

k

j

i

h

g

f

e

d

c

b

a

y

x

'

'

Matrices are a convenient and efficient way

to represent a sequence of transformations!

(27)

2x2 Matrices

What types of transformations can be

represented with a 2x2 matrix?

2D Identity?

y

y

x

x

'

'

y

x

y

x

1

0

0

1

'

'

2D Scale around (0,0)?

y

s

y

x

s

x

x

*

'

*

'

y

x

s

s

y

x

x

0

0

'

'

(28)

2x2 Matrices

What types of transformations can be

represented with a 2x2 matrix?

2D Rotate around (0,0)?

y

x

y

y

x

x

*

cos

*

sin

'

*

sin

*

cos

'

y

x

y

x

cos

sin

sin

cos

'

'

2D Shear?

y

x

sh

y

y

sh

x

x

y

x

*

'

*

'

y

x

sh

sh

y

x

x

1

1

'

'

(29)

2x2 Matrices

What types of transformations can be

represented with a 2x2 matrix?

2D Mirror about Y axis?

y

y

x

x

'

'

y

x

y

x

1

0

0

1

'

'

2D Mirror over (0,0)?

x

x

'

x

'

1

0

x

(30)

2x2 Matrices

What types of transformations can be

represented with a 2x2 matrix?

2D Translation?

y

x

t

y

y

t

x

x

'

'

Only linear 2D transformations

can be represented with a 2x2 matrix

(31)

Linear Transformations

Linear transformations are combinations of …

Scale,

Rotation,

Shear, and

Mirror

Properties of linear transformations:

Satisfies:

Origin maps to origin

Lines map to lines

Parallel lines remain parallel

Ratios are preserved

)

(

)

(

)

(

s

1

p

1

s

2

p

2

s

1

T

p

1

s

2

T

p

2

T

y

x

d

c

b

a

y

x

'

'

(32)

Homogeneous Coordinates

Q: How can we represent translation as a

3x3 matrix?

y

x

t

y

y

t

x

x

'

'

(33)

Homogeneous Coordinates

Homogeneous coordinates

represent coordinates in 2

dimensions with a

3-vector

1

coords

s

homogeneou

y

x

y

x

Homogeneous coordinates seem unintuitive, but

(34)

Homogeneous Coordinates

Q: How can we represent translation as a 3x3

matrix?

A: Using the rightmost column:

1

0

0

1

0

0

1

y

x

t

t

ranslation

T

y

x

t

y

y

t

x

x

'

'

(35)

Translation

Example of translation

1

1

1

0

0

1

0

0

1

1

'

'

y x y x

t

y

t

x

y

x

t

t

y

x

t

x

= 2

Homogeneous Coordinates

(36)

Homogeneous Coordinates

Add a 3rd coordinate to every 2D point

(x, y, w) represents a point at location (x/w, y/w)

(x, y, 0) represents a point at infinity

(0, 0, 0) is not allowed

Convenient

coordinate system to

represent many

useful

transformations

1 2

1

2

(2,1,1) or (4,2,2) or (6,3,3)

x

y

(37)

Basic 2D Transformations

Basic 2D transformations as 3x3 matrices

1

1

0

0

0

cos

sin

0

sin

cos

1

'

'

y

x

y

x

1

1

0

0

1

0

0

1

1

'

'

y

x

t

t

y

x

y x

1

1

0

0

0

1

0

1

1

'

'

y

x

sh

sh

y

x

y x

Translate

1

1

0

0

0

0

0

0

1

'

'

y

x

s

s

y

x

y x

Scale

(38)

Affine Transformations

Affine transformations are combinations of …

Linear transformations, and

Translations

Properties of affine transformations:

Origin does not necessarily map to origin

Lines map to lines

Parallel lines remain parallel

Ratios are preserved

w

y

x

f

e

d

c

b

a

w

y

x

1

0

0

'

'

(39)

Matrix Composition

Transformations can be combined by

matrix multiplication

w

y

x

sy

sx

ty

tx

w

y

x

1

0

0

0

0

0

0

1

0

0

0

cos

sin

0

sin

cos

1

0

0

1

0

0

1

'

'

'

p

’ = T(t

x

,t

y

) R( ) S(s

x

,s

y

)

p

(40)

Matrix Composition

Matrices are a convenient and efficient way to

represent a sequence of transformations

General purpose representation

Hardware matrix multiply

p

’ = (T * (R * (S*

p

) ) )

(41)

Matrix Composition

Be aware: order of transformations matters

Matrix multiplication is not commutative

p

’ = T * R * S *

p

(42)

Matrix Composition

What if we want to rotate

and

translate?

Ex:

Rotate line segment by 45 degrees about

endpoint

a and lengthen

(43)

Multiplication Order – Wrong

Way

Our line is defined by two endpoints

Applying a rotation of 45 degrees, R(45), affects both points

We could try to translate both endpoints to return endpoint

a

to its original position, but by how much?

Wrong

Correct

(44)

Multiplication Order - Correct

Isolate endpoint a from rotation effects

First translate line so

a

is at origin: T (-3)

Then rotate line 45 degrees: R(45)

Then translate back so

a

is where it was: T(3)

a

a

a

(45)

Will this sequence of operations work?

Matrix Composition

1

'

'

1

1

0

0

0

1

0

3

0

1

1

0

0

0

)

45

cos(

)

45

sin(

0

)

45

sin(

)

45

cos(

1

0

0

0

1

0

3

0

1

y

x

y

x

a

a

a

a

(46)

Matrix Composition

After correctly ordering the matrices

Multiply matrices together

What results is one matrix –

store it (on stack)!

Multiply this matrix by the vector of each vertex

All vertices easily transformed with one matrix

(47)

Reverse Rotations

Q: How do you undo a rotation of R( )?

A: Apply the inverse of the rotation… R

-1

( ) = R(- )

How to construct R-1( ) = R(- )

Inside the rotation matrix: cos( ) = cos(- )

The cosine elements of the inverse rotation matrix are unchanged

The sign of the sine elements will flip

(48)
(49)

#include <windows.h> #include <GL/glut.h> #include <stdlib.h> #include <math.h>

GLsizei winWidth=600, winHeight=600; //set initial display window size GLfloat xwcMin=0.0,xwcMax=225.0; //set range for world coordinates GLfloat ywcMin=0.0,ywcMax=225.0;

class wcPt2D {

public: GLfloat x,y; };

typedef GLfloat Matrix3x3 [3][3]; Matrix3x3 matComposite;

const GLdouble pi=3.14159; void init(void) {

glClearColor(1.0,1.0,1.0,0.0); //set color of display window to white }

/* Construct the 3 by 3 identity matrix */

void matrix3x3SetIdentity (Matrix3x3 matIdent3x3){ GLint row, col;

for(row=0;row<3;row++)

for(col=0;col<3;col++)

matIdent3x3[row][col]=(row==col); }

/* Premultiply matrix m1 times matrix m2, store result in m2. */ void matrix3x3PreMultiply (Matrix3x3 m1, Matrix3x3 m2) {

GLint row, col; Matrix3x3 matTemp; for(row=0;row<3;row++)

(50)

void translate2D(GLfloat tx, GLfloat ty){ Matrix3x3 matTrans1;

matrix3x3SetIdentity(matTrans1); //initialize translation matrix to identity matTrans1[0][2]=tx;

matTrans1[1][2]=ty;

matrix3x3PreMultiply(matTrans1,matComposite);//concatenate matTrans1 with composite matrix }

void rotate2D(wcPt2D pivotPt, GLfloat theta){ Matrix3x3 matRot;

matrix3x3SetIdentity(matRot); //initialize rotation matrix to identity matRot[0][0]=cos(theta);

matRot[0][1]=-sin(theta);

matRot[0][2]=pivotPt.x * (1-cos(theta))+pivotPt.y * sin(theta); matRot[1][0]=sin(theta);

matRot[1][1]=cos(theta);

matRot[1][2]=pivotPt.y * (1-cos(theta))-pivotPt.x * sin(theta);

matrix3x3PreMultiply(matRot,matComposite);//concatenate matRot with the composite matrix }

void scale2D(GLfloat sx, GLfloat sy, wcPt2D fixedPt){ Matrix3x3 matScale;

matrix3x3SetIdentity(matScale); //initialize scaling matrix to identity matScale[0][0]=sx;

matScale[0][2]=(1-sx)*fixedPt.x; matScale[1][1]=sy;

matScale[1][2]=(1-sy)*fixedPt.y;

matrix3x3PreMultiply(matScale,matComposite);//concatenate matScale with the composite matrix }

/* using the composite matrix, calculate transformed coordinates. */ void transformVerts2D(GLint nVerts, wcPt2D * verts){

GLint k; GLfloat temp; for(k=0;k<nVerts;k++){ temp=matComposite[0][0]*verts[k].x+matComposite[0][1]*verts[k].y+matComposite[0][2]; verts[k].y=matComposite[1][0]*verts[k].x+matComposite[1][1]*verts[k].y+matComposite[1][ 2];

(51)

void triangle(wcPt2D *verts) { GLint k; glBegin(GL_TRIANGLES); for(k=0;k<3;k++) glVertex2f(verts[k].x,verts[k].y); glEnd(); } void displayFcn(void){

GLint nVerts=3; //define initial position for triangle wcPt2D verts[3]={{50.0,25.0},{150.0,25.0},{100.0,100.0}}; wcPt2D centroidPt; //calculate position of triangle centroid GLint k, xSum=0, ySum=0;

for(k=0;k<nVerts;k++) { xSum+=verts[k].x; ySum+=verts[k].y; } centroidPt.x=GLfloat(xSum)/GLfloat(nVerts); centroidPt.y=GLfloat(ySum)/GLfloat(nVerts);

wcPt2D pivPt,fixedPt; // set geometric transformation parameters pivPt=centroidPt;

fixedPt=centroidPt;

GLfloat tx=0.0, ty=100.0; GLfloat sx=0.5, sy=0.5; GLdouble theta=pi/2.0;

glClear(GL_COLOR_BUFFER_BIT); //clear display window glColor3f(0.0,0.0,1.0); //set initial fill color to blue triangle(verts); //display blue triangle

matrix3x3SetIdentity(matComposite);//initialize composite matrix to identity /* construct composite matrix for transformation sequence */

scale2D(sx,sy,fixedPt); // first transformation: Scale. rotate2D(pivPt,theta); // second transformation: Rotate. translate2D(tx,ty); // final transformation: Translate.

(52)

void winReshapeFcn(GLint newWidth, GLint newHeight){ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(xwcMin,xwcMax,ywcMin,ywcMax); glClear(GL_COLOR_BUFFER_BIT); }

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

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowPosition(50,50);

glutInitWindowSize(winWidth,winHeight);

glutCreateWindow("Geometric Transformation Sequence"); init();

glutDisplayFunc(displayFcn); glutReshapeFunc(winReshapeFcn); glutMainLoop();

(53)
(54)
(55)

Tugas Kelas C

Tulis sebuah program animasi yang

mengimplementasikan prosedur rotasi 2D.

Inputnya sebuah polygon yang dirotasi terhadap sebuah

pivot point menggunakan sudut yang kecil.

Untuk meningkatkan kecepatan gunakan aproksimasi

dari fungsi sin dan cos, dan untuk menghindari

akumulasi error yang berlebihan reset nilai koordinat

awal pada setiap putaran baru.

(56)

Tugas Kelas X

Modifikasi contoh program sebelumnya supaya:

Parameter transformasi dapat dispesifikasikan

sebagai input dari user

Dapat diaplikasikan pada semua polygon dengan

verteks-verteksnya dispesikasikan oleh input dari

user

Urutan transformasi geometrik ditentukan oleh input

(57)

Tugas Kelas A

Implementasi program untuk membuat gambar:

Untuk membuatnya, buat sebuah fungsi yang

menggambar polygon gambar (b) (seperlima dari

bintang).

Lalu transformasikan dengan rotasi untuk

(58)

Tugas Kelas B

Implementasi program untuk membuat gambar:

Untuk membuatnya, buat sebuah fungsi yang

menggambar sebuah motif (gambar kiri).

Lalu transformasikan dengan refleksi untuk

mendapatkan sebuah spoke yang utuh, lalu rotasi untuk

menggambar snowflake secara keseluruhan.

Referensi

Dokumen terkait

Pelaksanaan kebijaksanaan harga sebagaimana dimaksud dalam ayat (2) tidak mengurangi tanggung jawab sosial Pemerintah terhadap golongan masyarakat tertentu. Akibat

Dari uraian tersebut dapat dipahami bahwa kegiatan ekstrakurikuler rohani Islam merupakan kegiatan tambahan yang dilaksanakan di luar jam pelajaran di sekolah sebagai penunjang

Dalam strategi ini, seorang praktisi public relations dapat membangun hubungan personal yang baik dengan orang-orang yang berada di institusi media maupun dengan

Dengan mempertimbangkan hal-hal tersebut, seperti potensi sumber daya alam Indonesia yang cukup besar untuk menghasilkan rimpang temulawak, perlunya proses

 Hubungan antara karyawan dengan pihak ketiga : Adanya pertemuan rahasia yang Hubungan antara karyawan dengan pihak ketiga : Adanya pertemuan rahasia yang

7. Menemukan latar cerita rakyat dengan mengutip kalimat atau paragraf yang mendukung. Menemukan amanat dalam cerita rakyat... 9. Menuliskan kembali isi cerita dengan