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 � 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 � Screen coordinate system � World coordinate system • Screen: 2D coordinate � World window system (WxH) � Viewport • 2D Regular Cartesian Grid � Window to Viewport m apping • Origin (0,0) at lower left corner (OpenGL convention) y • Horizontal axis – x • Vertical axis – y x (0,0) • Pixels: grid intersections (2,2) 1
Screen Coordinate System W orld Coordinate System • Problem s with drawing in screen coordinates: • I nflexible Insert screen • Difficult t o use dump from • One m apping: not applicat ion specific OHI O • World Coordinate system : application- specific • Exam ple: drawing dim ensions m ay be in m eters, km , feet, etc. (0,0) Definition: W orld W indow Definition: View port • World Window: rectangular region of drawing (in world • Rectangular region in the screen used to display drawing coordinates) to be drawn • Defined in screen coordinate system • Defined by W.L, W.R, W.B, W.T W.T V.T W.B V.B V.L V.R W.L W.R 2
W indow to View port Mapping W indow to View port Mapping ( OpenGL W ay) Would like to: � Define window (world coordinates): � � gluOrtho2D(left, right, bottom, top) � Specify drawing in world coordinat es � Side not e : gluOr t ho2D is m em ber of glu library � Display in screen coordinat es � Define Viewport (screen coordinates): � Need som e sort of m apping glViewport(left, bottom, right -left, top -bottom) � Called Window- to- viewport m apping � All subsequent drawings are autom atically m apped � Basic W- to- V m apping steps: � Do m apping before any drawing (glBegin( ), glEnd( )) � Define a world window Two m ore calls you will encounter to set up m atrices: � � Define a viewport � glMatrixMode(GL_PROJECTION) � Com put e a m apping from window t o viewport � 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) 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 W.R-W.L V.R-V.L Basic principles: (sx,sy) � (x,y) � Calculat e rat io: proport ional m apping rat io ( NO dist ort ion) � Account for offset s in window and viewport origins V.R-V.L W.R-W.L � You are given: � World Window: W.R, W.L, W.T, W.B − − ( x W . L ) Sx V . L � Viewport : V. L, V. R, V. B, V. T = − − � A point ( x,y) in t he world W . R W . L V . R V . L � Required: Calculate corresponding point (s.x, s.y) in screen coordinates − − ( y W . B ) Sy V . B = − − W . T W . B V . T V . B 3
W indow to View port Mapping ( Our W ay) W indow to View port Mapping ( Our W ay) Solve for S x, Sy in t er m s of x , y : Solve, given t he form ulas: − − − − V . R V . L V . R V . L ( x W . L ) Sx V . L = − − = Sx x W L V L . . − − − − W . R W . L V . R V . L W . R W . L W . R W . L − − − − ( y W . B ) Sy V . B V . T V . B V . T V . B = = − − Sy y W B V B . . − − − − W T W B V T V B . . . . W . T W . B W . T W . B − − V . R V . L V . R V . L What is ( S x, Sy) for point ( 3.4,1.2) in world coordinat es if: = − − Sx x W . L V . L − − W R W L W R W L . . . . = = W ( W . L , W . R , W . B , W . T ) ( 0 , 4 , 0 , 2 ) − − V . T V . B V . T V . B = − − Sy y W . B V . B = = V ( V . L , V . R , V . B , V . T ) ( 60 , 380 , 80 , 240 ) − − W T W B W T W B . . . . W indow to View port Mapping ( Our W ay) More W - t o-V Mapping Solution: � W- to- V Applications: − − � Zoom ing: in on a port ion of obj ect V . R V . L V . R V . L = − − Sx x W . L V . L � Tiling: W-t o-V in loop, adj acent viewports − − W . R W . L W . R W . L � Flipping drawings − − � Mapping different window and viewport aspect ratios V . T V . B V . T V . B = − − Sy y W B V B . . ( W/ H) − − W . T W . B W . T W . B Exam ple: � = + = = + = Sx 80 x 60 332 Sy 80 x 80 176 Hence, point ( 3.4,1.2) in world = point ( 332,176) on screen Viewport I m portant: Please read on your own, Window section 3.2.2 on pg. 92 of Hill 4
Tiling: Exam ple 3 .2 .4 of Hill ( pg. 8 8 ) Zoom ing Problem : want to tile dino.dat in 5x5 across screen Problem: � � Code: � dino. dat is current ly drawn on ent ire screen. � � User want s t o zoom int o j ust t he head gluOrtho2D(0, 640.0, 0, 440.0); � Specifies select ion by clicking t op -left and bot t om -right for(int i=0;i < 5;i++) corners { � Solution: for( int j = 0;j < 5; j++) � 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 glViewport(i * 64, j * 44; 64, 44); � 3: Use m apping A t o refer screen rect angle t o world drawPolylineFile(dino .dat); � 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 Cohen- Sutherland Clipping void myMouse(int button, int state, int x, int y) � Frequently want to view only a portion of the picture { static GLint corner[2]; � For instance, in dino.d at, you can select to view/ zoom in static int numCorners = 0; // initialize on only the dinosaur’s head if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) � Clipping: elim inate portions not selected { corner[ numCorners ].x = x; OpenGL autom atically clips for you � corner[ numCorners ].y = y; � We want algorithm for clipping numCorners++; if(numCorners == 2) � Classical algorithm : Cohen- Sutherland Clipping { � Picture has 1000s of segm ents : efficiency is im portant glViewport(corner[0], corner[1]); numCorners = 0; } } } 5
Clipping Points Clipping Lines � Det erm ine whet her a point � 3 cases: ( xmax, ymax ) ( x,y) is inside or out side of � Case 1: All of line in 2 (xmax, ymax) t he world window? � Case 2: All of line out � Case 3: Part in, part out 1 If ( xmin < = x < = xm ax ) 3 and (ymin < = y < = ymax) (xmin, ym in) then the point (x,y) is inside ( xmin, ym in ) else the point is outside Clipping Lines: Trivial Accept Clipping Lines: Trivial Reject � Case 1: All of line in � Case 2: All of line out (Xmax, Ymax) � Test line endpoint s: � Test line endpoint s: p1 p1 Xmin < = P1.x, P2.x < = Xmax � p1.x, p2.x < = Xmin OR and � p1.x, p2.x > = Xmax OR Ymin < = P1.y, P2.y < = Ymax � p1.y, p2.y < = ymin OR p2 � p1.y, p2.y > = ym ax � N ot e : sim ply com paring x,y values of endpoint s t o x,y (Xmin, Ymin) values of rect angle � N ot e : sim ply com paring x,y p2 � Result : t rivially accept . values of endpoint s t o x,y � Draw line in com plet ely values of rect angle � Result : t rivially rej ect . � Don’t dr aw line in 6
Clipping Lines: Non- Trivial Cases Cohen- Sutherland pseudocode ( fig. 3 .2 3 ) int clipSegment (Point2& p1, Point2& p2, RealRect W) { � Case 3: Part in, part out p2 do{ d if(trivial accept) return 1; // whole line survives � Two variat ions: e if(trivial reject) return 0; // no portion survives dely � One point in, other out // now chop � Both points out, but part of p1 delx line cuts through viewport if(p1 is outside) // find surviving segment � Need t o find inside segm ent s else(p2 is outside) // find surviving segment � Use sim ilar t riangles t o figure out lengt h of inside segm ent s }while(1) d e = } dely delx Cohen- Sutherland I m plem entation Param etric Equations � I m plicit form = � Breaks space int o 4-bit words F ( x , y ) 0 � Trivial accept: both FFFF Param etric form s: � TTFF FTFF FTTF � Trivial reject: T in same position � point s specified based on single param et er value � Chop everything else � Typical param et er: t im e t � Syst em at ically chops against TFFF FFFF FFTF = + − ≤ t ≤ P ( t ) P ( P P ) * t 0 1 four edges 0 1 0 � Can use C/ C+ + bit operat ions TFFT FFFT FFTT � Som e algorithm s work in param etric form � Clipping: exclude line segm ent ranges � I m port ant : read Hill 3.3 � Anim at ion: I nt erpolat e bet ween endpoint s by varying t 7
References Hill, 3.1 – 3.3, 3.8 � 8
Recommend
More recommend