Docsity

2D Clipping - Computer Graphics - Lecture Slides, Slides of Computer Graphics

Partial preview of the text

Download 2D Clipping - Computer Graphics - Lecture Slides and more Slides Computer Graphics in PDF only on Docsity!

Computer Graphics Tiling, Zooming and 2D Clipping

Applications of W-to-V Mapping

n W-to-V Applications:

n Zooming: in on a portion of object n Tiling: W-to-V in loop, adjacent viewports n Flipping drawings

n Mapping different window and viewport aspect ratios

(W/H)

Zooming

n Problem:

n dino.dat is currently drawn on entire screen. n User wants to zoom into just the head n Specifies selection by clicking top-left and bottom-right corners

Zooming

Step 1 : Calculate mapping A, that maps

dino.dat (world window) to viewport

my first attempt

Step 2: Calculate reverse mapping Aโ€™ of current viewport back to the entire world window (dino.dat)

World window

Viewport

Aโ€™

A

Sx = Ax โˆ’ ( A ( W. L ) โˆ’ V. L )

Sy = By โˆ’ ( B ( W. B ) โˆ’ V. B )

Example mapping A

Zooming

n Zooming (pseudocode):

1. Calculate mapping A of from world (entire dino.dat) **to current viewport

  1. Derive reverse mapping Aโ€™ from viewport to world
  2. Program accepts two mouse clicks as rectangle** **corners
  3. Use mapping Aโ€™ to refer screen rectangle to world
  4. Sets world to smaller world rectangle (gluOrtho2D on** **selected rectangle in world coordinates)
  5. Remaps small rectangle in world to screen viewport**

What if Window and Viewport have different

Aspect Ratios?

n Aspect ratio: is ratio R = Width/Height

n What if window and viewport have different aspect ratios?

n If different, two possible cases:

n Case A (R > W/H): map a wide window to a tall viewport?

Aspect ratio R

Viewport

W

glOrtho(left, right, bottom, top ); R = (right โ€“ left)/(top โ€“ bottom); If(R > W/H) glViewport(0, 0, W, W/R);

H

W/R

Window

reshape( ) function that maintains aspect

ratio

// glOrtho(left, right, bottom, top )is done previously, // probably in your draw function // function assumes variables left, right, top and bottom // are declared and updated globally

void myReshape(double W, double H ){ R = (right โ€“ left)/(top โ€“ bottom);

if(R > W/H) glViewport(0, 0, W, W/R); else if(R < W/H) glViewport(0, 0, HR, H); else glViewport(0, 0, W, H); // equal aspect ratios*

Cohen-Sutherland Clipping

n Frequently want to view only a portion of the picture

n For instance, in dino.dat, you can select to view/zoom in

on only the dinosaurโ€™s head

n Clipping: eliminate portions not selected

n OpenGL automatically clips for you

n We want algorithm for clipping

n Classical algorithm: Cohen-Sutherland Clipping

n Picture has 1000s of segments : efficiency is important

Clipping Lines

n 3 cases: n Case 1: All of line in n Case 2: All of line out n Case 3: Part in, part out

(xmin, ymin)

(xmax, ymax)

Clipping Lines: Trivial Accept

n Case 1: All of line in n Test line endpoints:

n Note: simply comparing x,y values of endpoints to x,y values of rectangle n Result: trivially accept. n Draw line in completely

(Xmin, Ymin)

(Xmax, Ymax)

p

p

Xmin <= P1.x, P2.x <= Xmax and Ymin <= P1.y, P2.y <= Ymax

Clipping Lines: Non-Trivial Cases

n Case 3: Part in, part out

n Two variations: n One point in, other out n Both points out, but part of line cuts through viewport

n Need to find inside segments

n Use similar triangles to figure out length of inside segments

e

p

p

d

delx

dely

delx

e dely

d

Clipping Lines: Calculation example

n If chopping window has (left, right, bottom, top) = (30, 220, 50, 240), what happens when the following lines are chopped?

n (a) p1 = (40,140), p2 = (100, 200)

n (b) p1 = (20,10), p2 = (20, 200)

n (c) p1 = (100,180), p2 = (200, 250)

e

p

p

d

delx

dely

delx

e dely

d

Cohen-Sutherland pseudocode (fig. 3.23)

else // p2 is outside // find surviving segment { if(p2 is to the left) chop against left edge else if(p2 is to right) chop against right edge else if(p2 is below) chop against the bottom edge else if(p2 is above) chop against the top edge } }while(1); }

Cohen-Sutherland Implementation

n Need quick efficient comparisons to get quick accepts, rejects, chop n Can use C/C++ bit operations n Breaks space into 4-bit words n Trivial accept: both FFFF n Trivial reject: T in same position n Chop everything else

n Systematically chops against four edges

n Important: read Hill 3.

FFFF

TFFT FFFT FFTT

TFFF

TTFF FTFF FTTF

FFTF