OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is...

65
OpenGL Geometric Transformations

Transcript of OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is...

Page 1: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

OpenGLGeometric Transformations

Page 2: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

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 3: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

로보트 팔 만들기

Page 4: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Hierarchical Modeling

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

Page 5: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Homework #2

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

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

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

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

Page 6: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Viewing Part I: Two-Dimensional Viewing

Sang Il Park

Sejong University

Lots of slides are stolen from Jehee Lee’s

Page 7: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Two-Dimensional Viewing

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

(screen) coordinates

Page 8: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Normalization and Viewport Transformation

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

Page 9: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

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 10: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

GLUT window Functions:

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

• See the text book pp 346~354

Page 11: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

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 12: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

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 13: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

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 14: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

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 15: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

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 16: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Cohen-Sutherland Line Clipping

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

Page 17: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

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 18: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

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 19: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

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 20: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

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 21: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

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 22: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

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 23: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Liang-Barsky Line Clipping

T

B

RL

tT

tB

Page 24: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

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 25: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Nicholl-Lee-Nicholl Line Clipping

• Divide more cases and reduce the computation.

(1)

(2)(3)

Page 26: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

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

Page 27: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Polygon Fill-Area Clipping

• Polyline vs polygon fill-area

• Early rejection is useful

Clipping Window

Bounding box of polygon fill area

Page 28: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Sutherland-Hodgman Polygon Clipping

• Clip against 4 infinite clip edges in succession

Page 29: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Sutherland-Hodgman Polygon Clipping

• Accept a series of vertices (polygon) and outputs another series of vertices

• Four possible outputs

Page 30: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Sutherland-Hodgman Polygon Clipping

• The algorithm correctly clips convex polygons, but may display extraneous lines for concave polygons

Page 31: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Weiler-Atherton Polygon Clipping

• For an outside-to-inside pair of vertices, follow the polygon boundary

• For an inside-to-outside pair of vertices, follow the window boundary in a clockwise direction

Page 32: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Weiler-Atherton Polygon Clipping

• Polygon clipping using nonrectangular polygon clip windows

Page 33: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Text Clipping

• All-or-none text clipping– Using boundary box for the entire text

• All-or-non character clipping– Using boundary box for each individual character

• Character clipping– Vector font: Clip boundary polygons or curves– Bitmap font: Clip individual pixels

Page 34: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Three-Dimensional Viewing

Sang Il ParkSejong University

Lots of slides are stolen from Jehee Lee’s

Page 35: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Viewing Pipeline

Page 36: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Virtual Camera Model

• Viewing Transformation– The camera position and orientation is determined

• Projection Transformation– The selected view of a 3D scene is projected onto a

view plane

Page 37: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

General 3D Viewing Pipeline

• Modeling coordinates (MC)• World coordinates (WC)• Viewing coordinates (VC)• Projection coordinates (PC)• Normalized coordinates (NC)• Device coordinates (DC)

Page 38: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Viewing-Coordinate Parameters

• View point (eye point or viewing position)

• View-plane normal vector N

),,( 0000 zyxP

Page 39: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Viewing-Coordinate Parameters

• Look-at point Pref

• View-up vector V– N and V are specified in the world coordinates– V should be perpendicular to N, but it can be difficult

to a direction for V that is precisely perpendicular to N

refPPN 0

Page 40: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Viewing-Coordinate Reference Frame

• The camera orientation is determined by the uvn reference frame

),,(

),,(

),,(

zyx

zyx

zyx

vvv

uuu

nnn

unv

V

nVu

N

Nn

u

nv

Page 41: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

World-to-Viewing Transformation

• Transformation from world to viewing coordinates– Translate the viewing-coordinate origin to the world-

coordinate origin– Apply rotations to align the u, v, n axes with the world

xw, yw, zw axes, respectively

u

nv

Page 42: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

World-to-Viewing Transformation

1000

100

010

001

1000

0

0

0

0

0

0

z

y

x

nnn

vvv

uuu

zyx

zyx

zyx

TR

10000

0

0

, Pn

Pv

Pu

TRMzyx

zyx

zyx

vcwc nnn

vvv

uuu

Page 43: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Perspective Projection

• Pin-hold camera model– Put the optical center (Center Of Projection) at the origin– Put the image plane (Projection Plane) in front of the COP– The camera looks down the negative z axis

• we need this if we want right-handed-coordinates

Page 44: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Parallel Projection

• Special case of perspective projection– Distance from the COP to the PP is infinite

– Also called “parallel projection”

– What’s the projection matrix?

Image World

Slide by Steve Seitz

Page 45: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Taxonomy of Geometric Projections

geometric projections

parallel perspective

orthographic axonometric oblique

trimetric

dimetric

isometric

cavalier cabinet

single-point two-point three-point

Page 46: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Orthographic Transformation

• Preserves relative dimension• The center of projection at infinity• The direction of projection is parallel to a principle axis• Architectural and engineering drawings

Page 47: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Axonometric Transformation

• Orthogonal projection that displays more than one face of an object– Projection plane is not normal to a principal axis, but

DOP is perpendicular to the projection plane– Isometric, dimetric, trimetric

Page 48: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Oblique Parallel Projections

• Projection plane is not normal to a principal axis, but DOP is perpendicular to the projection plane

• Only faces of the object parallel to the projection plane are shown true size and shape

Page 49: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Oblique Parallel Projections

www.maptopia.com

Page 50: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Oblique Parallel Projections

Page 51: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Oblique Parallel Projections

• Typically, is either 30˚ or 45˚• L1 is the length of the projected side edge

– Cavalier projections• L1 is the same as the original length

– Cabinet projections• L1 is the half of the original length

Page 52: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Oblique Parallel Projections

• Cavalier projections

• Cabinet projections

Page 53: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Perspective Projection

• Pin-hold camera model– Put the optical center (Center Of Projection) at the origin– Put the image plane (Projection Plane) in front of the COP– The camera looks down the negative z axis

• we need this if we want right-handed-coordinates

Page 54: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Perspective Projection

• Projection equations– Compute intersection with PP of ray from (x,y,z) to COP– Derived using similar triangles (on board)

– We get the projection by throwing out the last coordinate:

Page 55: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Homogeneous coordinates

• Is this a linear transformation?

Page 56: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Homogeneous coordinates

• Trick: add one more coordinate:

• Converting from homogeneous coordinates

homogeneous projection coordinates

homogeneous viewing coordinates

Page 57: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Perspective Projection• Projection is a matrix multiply using homogeneous coordinates:

divide by third coordinate

• This is known as perspective projection– The matrix is the projection matrix– Can also formulate as a 4x4

divide by fourth coordinate

Page 58: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Perspective Projection

• The projection matrix can be much involved, if the COP is different from the origin of the uvn coordinates– See the textbook for the detailed matrix

Page 59: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Traditional Classification of Projections

• Three principle axes of the object is assumed– The front, top, and side face of the scene is apparent

Page 60: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Traditional Classification of Projections

Page 61: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Perspective-Projection View Volume

• Viewing frustum– Why do we need near and far clipping plane ?

Page 62: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Normalizing Transformation

• Transform an arbitrary perspective-projection view volume into the canonical view volume

• Step 1: from frustum to parallelepiped

Page 63: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

Normalizing Transformation

• Transform an arbitrary perspective-projection view volume into the canonical view volume

• Step 2: from parallelepiped to normalized

Page 64: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

OpenGL 3D Viewing Functions

• Viewing-transformation function– glMatrixMode(GL_MODELVIEW);– gluLookAt(x0,y0,z0,xref,yref,zref,vx,vy,vz);– Default: gluLookAt(0,0,0, 0,0,-1, 0,1,0);

• OpenGL orthogonal-projection function– glMatrixMode(GL_PROJECTION);– gluOrtho(xwmin,xwmax, ywmin,ywmax, dnear,dfar);– Default: gluOrtho(-1,1, -1,1, -1,1);– Note that

• dnear and dfar must be assigned positive values• znear=-dnear and zfar=-dfar• The near clipping plane is the view plane

Page 65: OpenGL Geometric Transformations. OpenGL Matrix Stacks Stack processing –The top of the stack is the “current” matrix –glPushMatrix(); // Duplicate the.

OpenGL 3D Viewing Functions

• OpenGL perspective-projection function– The projection reference point is the viewing-coordinate origin– The near clipping plane is the view plane– Symmetric: gluPerspective(theta,aspect,dnear,dfar)– General: glFrustum(xwmin,xwmax,ywmin,ywmax,dnear,dfar)