opengl geometric transformations. glmatrixmode(gl_modelview);

36
OpenGL Geometric Transformations

Upload: dominick-hawkins

Post on 02-Jan-2016

250 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

OpenGLGeometric Transformations

Page 2: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

OpenGL Geometric Transformations

• glMatrixMode(GL_MODELVIEW);

Page 3: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

OpenGL Geometric Transformations

• Basic Transpormation:– glLoadIdentity();– glTranslatef(tx, ty, tz);– glRotatef(theta, vx, vy, vz); angle-axis

•(vx, vy, vz) is automatically normalized– glScalef(sx, sy, sz);– glLoadMatrixf(Glfloat elems[16]);

• Multiplication– glMultMatrixf(Glfloat elems[16]);– The current matrix is postmultiplied by the matrix – Column major

Page 4: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

OpenGL Geometric Transformations

• Getting the current matrix value:– glGetFloatv (GL_MODELVIEW_MATRIX,

GLfloat elems[16]);• Column major 0 4 8 12

1 5 9 13

2 6 10 14

3 7 11 15

GLfloat mat [16];glGetFloatv (GL_MODELVIEW_MATRIX, mat);GLfloat mat [16];glGetFloatv (GL_MODELVIEW_MATRIX, mat);

Page 5: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

OpenGL Geometric Transformations

• Matrix Direct Manipulation:– glLoadMatrixf(GLfloat elems[16]);

• Column major

– glMultMatrixf(GLfloat elems[16]);• The current matrix is postmultiplied by the matrix

0 4 8 12

1 5 9 13

2 6 10 14

3 7 11 15

glLoadIdentity();glMultMatrixf (M1);glMultMatrixf (M2);

glLoadIdentity();glMultMatrixf (M1);glMultMatrixf (M2);

M = M1∙M2

Page 6: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

OpenGL GLUT Animation Function

• GLUT Idle Callback fuction: – Idling: when there is nothing to do.

• Redraw the scene:

glutIdleFunc ( MyIdle );glutIdleFunc ( MyIdle );

void MyIdle(){

… // things to do… // when Idling

}

void MyIdle(){

… // things to do… // when Idling

}

glutPostRedisplay ( );glutPostRedisplay ( );

Page 7: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

Smooth display for animation

• Double Buffering: Using two buffers

Page 8: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

Smooth display for animation

• OpenGL Double Buffering:– Enabling double buffering

– Swapping buffers after drawing

glutInitDisplayMode (GLUT_DOUBLE);glutInitDisplayMode (GLUT_DOUBLE);

glClear (GL_COLOR_BUFFER_BIT);……glutSwapBuffers ();glFlush();

glClear (GL_COLOR_BUFFER_BIT);……glutSwapBuffers ();glFlush();

Page 9: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

바람개비 ( 풍차 ) 만들기

Page 10: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

로보트 팔 만들기

Page 11: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

로보트 팔 만들기 2

Page 12: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

Hierarchical Modeling

• A hierarchical model is created by nesting the descriptions of subparts into one another to form a tree organization

Page 13: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

OpenGL Matrix Stacks

• Stack processing– The top of the stack is the “current” matrix– glPushMatrix(); // Duplicate the current matrix at the top

– glPopMatrix(); // Remove the matrix at the top

Page 14: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

Homework #2

• 다각형을 이용하여 움직이는 2 차원 아름다운 애니메이션 만들기

• Push/Pop Matrix 를 사용하여 2 단계 이상의 계층적인 움직임 디자인을 할 것– Ex): 태양계 시스템 ( 태양지구달 )

사람의 움직임 ( 몸어깨팔꿈치손목손 )

• 숙제제출 : 이메일 제출 (Screenshot, code, report)• 숙제마감 : 4 월 28 일 수요일 23 시 59 분까지

Page 15: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

Viewing Part I: Two-Dimensional Viewing

Sang Il Park

Sejong University

Lots of slides are stolen from Jehee Lee’s

Page 16: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

Viewing Pipeline

Page 17: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

Two-Dimensional Viewing

• Two dimensional viewing transformation– From world coordinate scene description to device

(screen) coordinates

Page 18: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

Normalization and Viewport Transformation

• World coordinate clipping window• Normalization square: usually [-1,1]x[-1,1]• Device coordinate viewport

Page 19: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

OpenGL 2D Viewing

• Projection Mode– glMatrixMode(GL_PROJECTION);

• GLU clipping-window function– gluOrtho2D(xwmin,xwmax,ywmin,ywmax);– Normalized to [-1,1]x[-1,1]

• OpenGL viewport function– glViewport(xvmin,xvmax,yvmin,yvmax);

Page 20: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

GLUT window Functions:

• glutInitWindowPosition• glutInitWindowSize• glutCreateWindow• glutDestroyWindow• glutSetWindow/glutGetWindow• ….. And more!

• See the text book pp 346~354

Page 21: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

Clipping

• Remove portion of output primitives outside clipping window

• Two approaches– Clip during scan conversion: Per-pixel bounds check– Clip analytically, then scan-convert the

modified primitives

Page 22: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

Two-Dimensional Clipping

• Point clipping – trivial• Line clipping

– Cohen-Sutherland– Cyrus-beck– Liang-Barsky

• Fill-area clipping– Sutherland-Hodgeman– Weiler-Atherton

• Curve clipping• Text clipping

Page 23: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

Line Clipping

• Basic calculations:– Is an endpoint inside or outside the clipping window?– Find the point of intersection, if any, between a line

segment and an edge of the clipping window.

Both endpoints inside:

trivial accept

One inside: find

intersection and clip

Both outside: either

clip or reject

Page 24: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

Cohen-Sutherland Line Clipping

• One of the earliest algorithms for fast line clipping• Identify trivial accepts and rejects by bit operations

< Region code for each endpoint >

above below right leftBit 4 3 2 1

0000

10001001

0001

0101 0100 0110

0010

1010Clipping window

Page 25: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

Cohen-Sutherland Line Clipping

• Compute region codes for two endpoints• If (both codes = 0000 ) trivially accepted• If (bitwise AND of both codes 0000) trivially rejected• Otherwise, divide line into two segments

– test intersection edges in a fixed order.

(e.g., top-to-bottom, right-to-left)

0000

10001001

0001

0101 0100 0110

0010

1010Clipping window

Page 26: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

Cohen-Sutherland Line Clipping

• Fixed order testing and clipping cause needless clipping (external intersection)

Page 27: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

Cohen-Sutherland Line Clipping

• This algorithm can be very efficient if it can accept and reject primitives trivially– Clip window is much larger than scene data

• Most primitives are accepted trivially– Clip window is much smaller than scene data

• Most primitives are rejected trivially

• Good for hardware implementation

Page 28: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

Cyrus-Beck Line Clipping

• Use a parametric line equation

• Reduce the number of calculating intersections by exploiting the parametric form

• Notations– Ei : edge of the clipping window

– Ni : outward normal of Ei

– An arbitrary point PEi on edge Ei

10 ),()( 010 tPPtPtP

Page 29: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

Cyrus-Beck Line Clipping

halfplane outside in thepoint a0))((

edge thecontaining line on thepoint a0))((

halfplane inside in thepoint a0))((

i

i

i

Ei

Ei

Ei

PtPN

PtPN

PtPN

Page 30: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

Cyrus-Beck Line Clipping

• Solve for the value of t at the intersection of P0P1 with the edge– Ni · [P(t) - PEi] = 0 and P(t) = P0 + t(P1 - P0)

– letting D = (P1 - P0),

– Where

• Ni 0

• D 0 (that is, P0 P1)

• Ni · D 0 (if not, no intersection)

DN

PPNt

i

Eii

][ 0

Page 31: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

Cyrus-Beck Line Clipping• Given a line segment P0P1, find intersection

points against four edges– Discard an intersection point if t [0,1] – Label each intersection point either PE

(potentially entering) or PL (potentially leaving)– Choose the smallest (PE, PL) pair that defines the

clipped line

PL0

PE0

10

10

PPN

PPN

i

i

Page 32: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

Cyrus-Beck Line Clipping

• Cyrus-Beck is efficient when many line segments need to be clipped

• Can be extended easily to convex polygon (rather than upright rectangle) clip windows

Page 33: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

Liang-Barsky Line Clipping

• Liang-Barsky optimized Cyrus-Beck for upright rectangular clip windows

),(1

),(0

22

11

yxQt

yxPt

tdyyyytyy

tdxxxxtxx

1121

1121

)(

)(

P(x1,y1)

Q(x2.y2)

tB

tT

tR

tL

Page 34: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

Liang-Barsky Line Clipping

T

B

RL

tT

tB

Page 35: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

Nicholl-Lee-Nicholl Line Clipping

• Divide more cases and reduce the computation.

(1)

(2)(3)

Page 36: OpenGL Geometric Transformations. glMatrixMode(GL_MODELVIEW);

General Clipping Window

• Line clipping using nonrectangular polygon clip windows– Convex polygon

• Cyrus-Beck algorithm can be readily extended– Concave polygon

• Split the concave polygon into convex polygons

Vector Method for Concave Splitting

• Calculate edge-vector cross products in a counterclockwise order

• If any z component turns out to be negative, the polygon is concave