1 Screen Coordinate System W orld Coordinate System Problem s - - PDF document

1
SMART_READER_LITE
LIVE PREVIEW

1 Screen Coordinate System W orld Coordinate System Problem s - - PDF document

Announcem ents CS 4 7 3 1 : Com put e r Gr a phics Room: if few people drop class, I will request room change Le ct ur e 4 : 2 D Gr a phic Syst e m s Project 1 should work on any of the CCC unix/ Linux m achines Em m anuel Agu


slide-1
SLIDE 1

1

CS 4 7 3 1 : Com put e r Gr a phics Le ct ur e 4 : 2 D Gr a phic Syst e m s Em m anuel Agu

Announcem ents

  • Room: if few people drop class, I will request room change
  • Project 1 should work on any of the CCC unix/ Linux

m achines

  • Sim ply let TA know which m achine you worked on
  • m yWPI:
TA’s: Jim Nichols and Paolo Piselli SA: Brian Corcoran Treat what t hey post as “ official” .

2 D Graphics: Coordinate System s

  • Screen coordinate system
  • World coordinate system
  • World window
  • Viewport
  • Window to Viewport m apping

Screen Coordinate System

  • Screen: 2D coordinate

system (WxH)

  • 2D Regular Cartesian Grid
  • Origin (0,0) at lower left

corner (OpenGL convention)

  • Horizontal axis – x
  • Vertical axis – y
  • Pixels: grid intersections

(0,0) y x (2,2)

slide-2
SLIDE 2

2

Screen Coordinate System Insert screen dump from OHI O (0,0) W orld Coordinate System

  • Problem s with drawing in screen coordinates:
  • I nflexible
  • Difficult t o use
  • One m apping: not applicat ion specific
  • World Coordinate system : application- specific
  • Exam ple: drawing dim ensions m ay be in m eters, km , feet,

etc.

Definition: W orld W indow

  • World Window: rectangular region of drawing (in world

coordinates) to be drawn

  • Defined by W.L, W.R, W.B, W.T

W.L W.R W.B W.T

Definition: View port

  • Rectangular region in the screen used to display drawing
  • Defined in screen coordinate system

V.L V.R V.B V.T

slide-3
SLIDE 3

3

W indow to View port Mapping

  • Would like to:
Specify drawing in world coordinat es Display in screen coordinat es
  • Need som e sort of m apping
  • Called Window- to- viewport m apping
  • Basic W- to- V m apping steps:
Define a world window Define a viewport Com put e a m apping from window t o viewport

W indow to View port Mapping ( OpenGL W ay)

  • Define window (world coordinates):
gluOrtho2D(left, right, bottom, top) Side not e : gluOr t ho2D is m em ber of glu library
  • Define Viewport (screen coordinates):

glViewport(left, bottom, right -left, top -bottom)

  • All subsequent drawings are autom atically m apped
  • Do m apping before any drawing (glBegin( ), glEnd( ))
  • Two m ore calls you will encounter to set up m atrices:
glMatrixMode(GL_PROJECTION) glLoadIdentity( )
  • Type in as above for now, will explain later
  • Ref: Hill Practice exercise 3.2.1, pg 86

W indow to View port Mapping ( Our W ay)

  • How is window- to- viewport m apping done?
  • Trigonom etry: derive Window- to- Viewport m apping
  • Basic principles:
Calculat e rat io: proport ional m apping rat io ( NO dist ort ion) Account for offset s in window and viewport origins
  • You are given:
World Window: W.R, W.L, W.T, W.B Viewport : V. L, V. R, V. B, V. T A point ( x,y) in t he world
  • Required: Calculate corresponding point (s.x, s.y) in screen

coordinates

W indow to View port Mapping ( Our W ay)

(x,y) (sx,sy)

L V R V L V Sx L W R W L W x . . . . . ) . ( − − = − − B V T V B V Sy B W T W B W y . . . . . ) . ( − − = − −

W.R-W.L W.R-W.L V.R-V.L V.R-V.L

slide-4
SLIDE 4

4

W indow to View port Mapping ( Our W ay)

L V R V L V Sx L W R W L W x . . . . . ) . ( − − = − −

B V T V B V Sy B W T W B W y . . . . . ) . ( − − = − −

Solve for S x, Sy in t er m s of x , y :

      − − − −       − − = L V L W L W R W L V R V x L W R W L V R V Sx . . . . . . . . . .       − − − −       − − = B V B W B W T W B V T V y B W T W B V T V Sy . . . . . . . . . .

W indow to View port Mapping ( Our W ay)

Solve, given t he form ulas:

      − − − − − − = L V L W L W R W L V R V x L W R W L V R V Sx . . . . . . . . . .       − − − − − − = B V B W B W T W B V T V y B W T W B V T V Sy . . . . . . . . . . ) 2 , , 4 , ( ) . , . , . , . ( = = T W B W R W L W W

What is ( S x, Sy) for point ( 3.4,1.2) in world coordinat es if:

) 240 , 80 , 380 , 60 ( ) . , . , . , . ( = = T V B V R V L V V

W indow to View port Mapping ( Our W ay)

Solution:

      − − − − − − = L V L W L W R W L V R V x L W R W L V R V Sx . . . . . . . . . .       − − − − − − = B V B W B W T W B V T V y B W T W B V T V Sy . . . . . . . . . . 332 60 80 = + = x Sx 176 80 80 = + = x Sy

Hence, point ( 3.4,1.2) in world = point ( 332,176) on screen

More W - t o-V Mapping

  • W- to- V Applications:
Zoom ing: in on a port ion of obj ect Tiling: W-t o-V in loop, adj acent viewports Flipping drawings
  • Mapping different window and viewport aspect ratios

( W/ H)

  • Exam ple:

Window Viewport I m portant: Please read on your own, section 3.2.2 on pg. 92 of Hill

slide-5
SLIDE 5

5

Tiling: Exam ple 3 .2 .4 of Hill ( pg. 8 8 )

  • Problem : want to tile dino.dat in 5x5 across screen
  • Code:

gluOrtho2D(0, 640.0, 0, 440.0); for(int i=0;i < 5;i++) { for( int j = 0;j < 5; j++) { glViewport(i * 64, j * 44; 64, 44); drawPolylineFile(dino .dat); } }

Zoom ing

  • Problem:
  • dino. dat is current ly drawn on ent ire screen.
User want s t o zoom int o j ust t he head Specifies select ion by clicking t op -left and bot t om -right

corners

  • Solution:
1: Program accept s t wo m ouse clicks as rect angle corners 2: Calculat e m apping A of current screen t o all of dino. dat 3: Use m apping A t o refer screen rect angle t o world 4: Set s w orld t o sm aller w orld rect angle 5: Rem aps sm all rect angle in w orld t o screen viewport

Using m ouse to select screen rectangle for zoom ing ( Exam ple 2 .4 .2 , pg 6 4 ) for zoom ing

void myMouse(int button, int state, int x, int y) { static GLint corner[2]; static int numCorners = 0; // initialize if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { corner[ numCorners ].x = x; corner[ numCorners ].y = y; numCorners++; if(numCorners == 2) { glViewport(corner[0], corner[1]); numCorners = 0; } } }

Cohen- Sutherland Clipping

  • Frequently want to view only a portion of the picture
  • For instance, in dino.d at, you can select to view/ zoom in
  • n only the dinosaur’s head
  • Clipping: elim inate portions not selected
  • OpenGL autom atically clips for you
  • We want algorithm for clipping
  • Classical algorithm : Cohen- Sutherland Clipping
  • Picture has 1000s of segm ents : efficiency is im portant
slide-6
SLIDE 6

6

Clipping Points

(xmin, ym in) (xmax, ymax)

  • Det erm ine whet her a point

( x,y) is inside or out side of t he world window? If ( xmin < = x < = xm ax ) and (ymin < = y < = ymax) then the point (x,y) is inside else the point is outside

Clipping Lines

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

(xmin, ym in) (xmax, ymax) 1 2 3

Clipping Lines: Trivial Accept

  • Case 1: All of line in
  • Test line endpoint s:
  • N ot e : sim ply com paring x,y

values of endpoint s t o x,y values of rect angle

  • Result : t rivially accept .
  • Draw line in com plet ely

(Xmin, Ymin) (Xmax, Ymax) p1 p2 Xmin < = P1.x, P2.x < = Xmax and Ymin < = P1.y, P2.y < = Ymax

Clipping Lines: Trivial Reject

  • Case 2: All of line out
  • Test line endpoint s:
  • N ot e : sim ply com paring x,y

values of endpoint s t o x,y values of rect angle

  • Result : t rivially rej ect .
  • Don’t dr aw line in

p1 p2

p1.x, p2.x < = Xmin

OR p1.x, p2.x > = Xmax OR p1.y, p2.y < = ymin OR p1.y, p2.y > = ym ax

slide-7
SLIDE 7

7

Clipping Lines: Non- Trivial Cases

  • Case 3: Part in, part out
  • Two variat ions:
One point in, other out Both points out, but part of

line cuts through viewport

  • Need t o find inside segm ent s
  • Use sim ilar t riangles t o figure
  • ut lengt h of inside segm ent s

e p2 p1 d delx dely

delx e dely d = Cohen- Sutherland pseudocode ( fig. 3 .2 3 )

int clipSegment (Point2& p1, Point2& p2, RealRect W) { do{ if(trivial accept) return 1; // whole line survives if(trivial reject) return 0; // no portion survives // now chop if(p1 is outside) // find surviving segment else(p2 is outside) // find surviving segment }while(1) }

Cohen- Sutherland I m plem entation

  • Breaks space int o 4-bit words
Trivial accept: both FFFF Trivial reject: T in same position Chop everything else
  • Syst em at ically chops against

four edges

  • Can use C/ C+ + bit operat ions
  • I m port ant : read Hill 3.3

FFFF TFFT FFFT FFTT TFFF TTFF FTFF FTTF FFTF

Param etric Equations

  • I m plicit form
  • Param etric form s:
point s specified based on single param et er value Typical param et er: t im e t
  • Som e algorithm s work in param etric form
Clipping: exclude line segm ent ranges Anim at ion: I nt erpolat e bet ween endpoint s by varying t

) , ( = y x F t P P P t P * ) ( ) (

1

− + = 1 ≤ ≤ t

slide-8
SLIDE 8

8

References

  • Hill, 3.1 – 3.3, 3.8