45
2 DIMENSIONAL 2 DIMENSIONAL VIEWING VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

Embed Size (px)

Citation preview

Page 1: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

2 DIMENSIONAL 2 DIMENSIONAL VIEWINGVIEWING

Ceng 477 Introduction to Computer Graphics

Fall 2010-2011

Computer EngineeringMETU

Page 2: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

Model

Model

Model

M1

M2

M3

3D WorldScene

3D ViewScene

V

P Clip Normalize2D/3D DeviceScene

2D Image

Projection

Rasterization

ModelingTransformations

ViewingTransformations

MCS WCS VCS

NDCS DCSSCS

Viewing Pipeline Revisited

Page 3: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

● Model coordinates to World coordinates:Modelling transformationsModel coordinates:1 circle (head),2 circles (eyes),1 line group (nose),1 arc (mouth),2 arcs (ears).With their relative coordinates and sizes

World coordinates:All shapes with theirabsolute coordinates and sizes.circle(0,0,2)circle(-.6,.8,.3) circle(.6,.8,.3)lines[(-.4,0),(-.5,-.3),(.5,.3),(.4,0)]arc(-.6,0,.6,0,1.8,180,360)arc(-2.2,.2,-2.2,-.2,.8,45,315)arc(2.2,.2,2.2,-.2,.8,225,135)

Page 4: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

● World coordinates to Viewing coordinates:Viewing transformations

World coordinates Viewing coordinates:Viewers position and view angle. i.e. rotated/translated

Page 5: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

● Projection: 3D to 2D. Clipping depends on viewing frame/volume. Normalization: device independent coordinates

Device Independent Coordinates:Invisible shapes deleted, others reduced to visible parts.3 arcs, 1 circle, 1 line group

Viewing coordinates:

Page 6: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

● Device Independent Coordinates to Device Coordinates. Rasterization

Unit measures Pixels

Device Independent Coordinates

Screen Coordinates orDevice Coordinates

Page 7: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

2D Viewing● World coordinates to Viewing coordinates

● Window to Viewport. Window: A region of the scene selected for viewing (also called clipping window)Viewport: A region on display device for mapping to

window

World Coordinates Viewing Coordinates

WindowWindow

Viewport

Page 8: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

Clipping Window vs. Viewport

● The clipping window selects what we want to see in our virtual 2D world.

● The viewport indicates where it is to be viewed on the output device (or within the display window)

● By default the viewport have the same location and dimensions of the GLUT display window you create– But it can be modified so that only a part of

the display window is used for OpenGL display

Page 9: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

The clipping window

xwmin xwmax

ywmin

ywmax

xvmin xvmax

yvmin

yvmax

xvmin xvmax

yvmin

yvmax

x0

y0

xview

yview

xworld

yworld

xview

yview

RectangularWindow

RotatedWindow

Page 10: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

World-coordinates to Viewing Coordinates

● Mwc,vc= R·T

x0

y0

xview

yview

xworld

yworld

xview

yviewxview

yview

xworld

yworldyview

),( 00 yx T )(R

Page 11: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

Normalization

Page 12: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

● Coordinate transformation:Different sizes and/or height width ratios?

● For any point:

should hold.minmax

min

minmax

min

minmax

min

minmax

min

ywyw

ywyw=

yvyv

yvyv

xwxw

xwxw=

xvxv

xvxv

Page 13: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

● This can also be accomplished in 2 steps:

1. Scale over the fixed point:

2. Translate lower-left corner of the clipping window to the lower-left corner of the viewport

minmax

minmaxminmin

minmax

minmaxminmin

ywyw

yvyvywyw+yv=yv

xwxw

xvxvxwxw+xv=xv

yxminmin s,s,yw,xwS

minminminmin ywyv,xwxv T

minmax

min

minmax

min

minmax

min

minmax

min

ywyw

ywyw=

yvyv

yvyv

xwxw

xwxw=

xvxv

xvxv

Page 14: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

OpenGL 2D Viewing Functions

● OpenGL, GLU, and GLUT provide functions to specify clipping windows, viewports, and display windows within a video screen.

Page 15: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

Setting up a 2D Clipping-Window

● glMatrixMode (GL_PROJECTION)

● glLoadIdentity (); // reset, so that new viewing parameters are not combined

with old ones (if any)

● gluOrtho2D (xwmin, xwmax, ywmin, ywmax);

or● glOrtho (xwmin, xwmax, ywmin, ywmax, zwmin, zwmax);

● Objects within the clipping window are transformed to normalized coordinates (-1,1)

Page 16: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

Setting up a Viewport

● glViewport (xvmin, yvmin, vpWidth, vpHeight);

● All the parameters are given in integer screen coordinates relative to the lower-left corner of the display window.

● If we do not invoke this function, by default, a viewport with the same size and position of the display window is used (i.e., all of the GLUT window is used for OpenGL display)

Page 17: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

Creating a GLUT Display Window

● glutInitWindowPosition (xTopLeft, yTopLeft);

– the integer parameters are relative to the top-left corner of the screen

● glutInitWindowSize (dwWidth, dwHeight);

● glutCreateWindow (“Title of Display Window”);

● glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB)

– Specification of the buffer that will be used

● glClearColor (red, green, blue, alpha)

– Specify the background color

Page 18: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

Multiple GLUT windows● Multiple windows may be created within an

OpenGL program– Need window ids to manage multiple windows– windowID = glutCreateWindow(“Window1”);– glutDestroyWindow (windowID)

// to distroy the window● General functions (like glutInitDisplayMode)

are applied to the current display window. We can set the current window to a specific window with:– glutSetWindow (windowID);

Page 19: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

Other functions

● GLUT provide functions to relocate, resize, minimize, resize to fullscreen, change window title, hide, show, bring to front, or send to back, select a specific cursor for the current display window. (pages 309-311 in the textbook)

Page 20: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

Display callback function

● Each separate GLUT window can have its own function to specify what will be drawn inside. E.g.,

– glutSetWindow (w1);

– glutDisplayFunction (wireframeDisplay);

– glutSetWindow (w2);

– glutDisplayFunction (solidDisplay);

● Display callback functions are called only when GLUT determines that the display content should be renewed. To update the display manually call: glutPostRedisplay();

● glutIdleFunc (functionName) could be used in animations

Page 21: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

OpenGL 2D Viewing Example● 2 Viewports● One triangle is displayed

in two colors and orientations in 2 viewports

glClear (GL_COLOR_BUFFER_BIT);glColor3f(0.0, 0.0, 1.0);glViewport(0, 0, 300, 300);drawCenteredTriangle();

glColor3f(1.0, 0.0, 0.0);glViewport(300, 0, 300, 300);glRotatef(90.0, 0.0, 0.0, 1.0);drawCenteredTriangle();

glutInitWindowSize (600, 300);

Page 22: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

Clipping Algorithms

● Clipping: identifying the parts of the objects that will be inside of the window.

● Everything outside the clipping window is eliminated from the scene description (i.e., not scan converted) for efficiency.

● Point clipping:

maxmin

maxmin

ywyyw

xwxxw

Page 23: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

Line Clipping

● When both end points are inside all four boundaries (completely inside) or both end points are outside any one of the boundaries (completely outside) no extra processing need to be done. Otherwise:

– For each line determine the intersection with boundaries. Parametric line equations:

Find u for all boundary lines. Not very efficient. 10121

121

u,yyu+y=y

,xxu+x=x

Page 24: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

Liang-Barsky Line Clipping

● Use parametric equations for efficiency

k

k

max

min

max

min

kk

maxmin

maxmin

p

q=u

yy=qΔy,=p

ywy=qΔy,=p

xxw=qΔx,=p

xwx=qΔx,=p

=k,qup

u,ywyu+yyw

xwxu+xxw

144

133

122

111

1

1

1,2,3,4

10

rewrite these inequalities:

The point where the lineintersects the borders

Page 25: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

● Problem: find the clipping interval [u1,u

2]

● If pk<0 u

1 = max(u

1,p

k/q

k)

If pk>0 u

2 = min(u

2,p

k/q

k)

2

1

for candidate n, transitiooutside toinside 0, if

for candidate n, transitioinside tooutside 0, if

10

u>p

u<p

u,qup

k

k

kk

u1

u 0

u 1u2

Page 26: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

Liang-Barsky Algorithm

yu+y=yx,u+x=x

yu+y=yx,u+x=x

rejecttotally outside, is line

u>u

speciallytreat line, parallel

rumin=u

p

q=r>p

rumax=u

p

q=r<p

p

=k

=u,=u

k

k

kkk

k

k

kkk

k

111111

212212

21

2,2

1,1

21

if

else

0 if else

0 if

find

1,2,3,4for

10

Page 27: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

8/7,3558/78,08/705,2/1655/28,05/20

8

7

8

7

5

75

77,075,

4

5

2

5

2

8

15

22,205,

3

8

71

8

78

77,078,

2

8

10

8

18

11,108,

1

58

21

2

444

1

333

2

222

1

111

=++=P=++=P

=,min=u

=r==q=p

=k

=,max=u

=r==q=p

=k

=,min=u

=r==q=p

=k

=,max=u

=r==q=p

=k

=y,=x

Example

1=x 7=x

6=y

2=y

0,01 =P

8,52 =P

8/11 =u

8/72 =u

5/21 =u

k

k

max

min

max

min

kk

maxmin

maxmin

p

q=u

yy=qΔy,=p

ywy=qΔy,=p

xxw=qΔx,=p

xwx=qΔx,=p

=k,qup

u,ywyu+yyw

xwxu+xxw

144

133

122

111

1

1

1,2,3,4

10

Page 28: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

28

L

L

L

NLN

(2,2) (6,2)

(6,6)(2,6)

P1(1,4)

LT

LR

LB

P2(6,7)

Page 29: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

29

NLN

● For example when P2 is in region LT if

Slope P1PTR < Slope P1P2 < Slope P1PTL

 

● Or

(YT - Y1)/(XR – X1)) < (Y2 - Y1)/(X2 – X1) < (YT – Y1)/(XL – X1)

● Ex:● (6-4)/(6-1) < (7-4)/(6-1) < (6-4)/(2-1)● 2/5 < 3/5 < 2

And we clip the entire line if

 (YT – Y1)(X2 – X1) < (XL – X1)(Y2 – Y1)

L

L

L

(2,2) (6,2)

(6,6)(2,6)

P1(1,4)

LT

LR

LB

P2(6,7)

Page 30: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

30

NLN

● From the parametric equations:

x = x1 + (x2 – x1)u

y = y1 + (y2 – y1)u

● An intersection position on the left boundary is x = xL with u = (xL – x1)/(x2 – x1) will give coordinate y = y1 +((y2 – y1)/(x2 – x1)) * (xL – x1)

● An intersection position on the top boundary has y = yT with u = (yT – y1)/(y2 – y1) will give coordinate x = x1 +((x2 – x1)/(y2 – y1)) * (yT – y1)

Page 31: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

2001. 7. 13 31

NLN Line Clipping(1/4)

● Three possible position for a line end point P1

– Equal position with rotation or translation

Page 32: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

2001. 7. 13 32

NLN Line Clipping (2/4)

● The four clipping region when P1 is inside the clip window

● The four clipping region when P1 is directly left of the clip window

● The two possible sets of clipping regions when P1 is above and to the left of the clip window

Page 33: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

2001. 7. 13 33

NLN Line Clipping (3/4)

● Region Determination

– P1 is left of the clipping rectangle, then P2 is in the region LT if

Slope P1PTR < slope P1P2 < slope P1PTL

or

– Clipping the entire line

1

1

12

12

1

1

xx

yy

xx

yy

xx

yy

L

T

R

T

))(())(( 121121 yyxxxxyy LT

Page 34: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

2001. 7. 13 34

NLN Line Clipping (4/4)

● Intersection Position Calculation

– Left Boundary

– Top Boundary

uyyyy

uxxxx

)(

)(

121

121

)/()( 121 xxxxu

xx

L

L

)/()( 121 yyyyu

yy

T

T

)( 112

121 xx

xx

yyyy L

)( 112

121 yy

yy

xxxx T

Page 35: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

Polygon Clipping

● Find the vertices of the new polygon(s) inside thewindow.

● Sutherland-HodgmanPolygon Clipping:Check each edge of the polygonagainst all window boundaries. Modify the vertices based on transitions. Transfer the new edges to the next clipping boundary.

Page 36: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

Shutherland-Hodgman Polygon Clipping

● Traverse edges for borders; 4 cases:

– V1 outside, V2 inside: take V1' and V2

– V1 inside, V2 inside: take V1 and V2

– V1 inside, V2 outside: take V1 and V2'

– V1 outside, V2 outside: take none

V2

V1

V1'

V2

V1

V2

V1

V2'

V2

V1

Page 37: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

v1v2

v3'

v4v5

v6

● Left border:v1 v2 both inside v1 v2v2 v3 both inside v2 v3..... “ “ ........v1,v2,v3,v4v5,v6,v1

● Bottom Border:v1 v2 both inside v1 v2v2 v3 v2 i, v3 o v2 v3'v3 v4 both outside nonev4 v5 both outside nonev5 v6 v5 o, v6 i v5' v6v6 v1 both inside v6 v1v1,v2,v3',v5',v6,v1

v3

v5'

Page 38: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

v1v2

v3

v4

v5

v6

● v1,v2,v3',v5',v6,v1

● Right border:v1 v2 v1 i, v2 o v1 v2'v2 v3' v2 o, v3'i v2'' v3'v3' v5' both inside v3' v5'v5' v6 both inside v5' v6v6 v1 both inside v6 v1v1,v2',v2'',v3',v5',v6,v1

● Top Border:v1 v2' both outside nonev2' v2'' v2' o, v2'' i v2''' v2''v2'' v3' both inside v2'' v3'v3' v5' both inside v3' v5'v5' v6 both inside v5' v6v6 v1 v6 i, v1 o v6 v1'v2''',v2'',v3',v5',v6,v1'

v3'v5'

v2'

v2''

v2'''

v1'

Page 39: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

v1v2

v3

v4

v5

v6

v3'v5'

v2'

v2''

v2'''

v1'

v6

v3'v5'

v2''

v2'''v1'

Page 40: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

Other Issues in Clipping

● Problem in Shutherland-Hodgman.Weiler-Atherton has a solution

● Clipping other shapes:Circle, Ellipse, Curves.

● Clipping a shape against another shape

● Clipping the interior.

Page 41: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

Weiler-Atherton Polygon Clipping

● When using Sutherland-Hodgeman, concavities can end up linked

● A different clipping algorithm, the Weiler-Atherton algorithm, creates separate polygons

Remember this?

Page 42: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

Weiler-Atherton Polygon Clipping

● Example:

Out -> InAdd clip vertexAdd end vertex

In -> InAdd end vertex

In -> OutAdd clip vertexCache old direction

Follow clip edge until(a) new crossing found(b) reach vertex already added

Page 43: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

Weiler-Atherton Polygon Clipping

● Example (cont’d):

Continue fromcached vertex and direction

Out -> InAdd clip vertexAdd end vertex

In -> OutAdd clip vertexCache old direction

Follow clip edge until(a) new crossing found(b) reach vertex already added

Page 44: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU

Weiler-Atherton Polygon Clipping

● Example (cont’d):

Continue fromcached vertex and direction

Nothing addedFinished

Final Result:2 unconnectedpolygons

Page 45: 2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall 2010-2011 Computer Engineering METU