200 likes | 419 Views
CS430 Computer Graphics. Rendering Pipeline and Primitive Rasterization. Topics. Introduction Viewport Transformation Primitive Rasterization Primitive Clipping. Introduction. Scan conversion Convert specification of a primitive into pixels in the frame buffer Simple rendering pipeline.
E N D
CS430 Computer Graphics Rendering Pipeline and Primitive Rasterization Chi-Cheng Lin, Winona State University
Topics • Introduction • Viewport Transformation • Primitive Rasterization • Primitive Clipping
Introduction • Scan conversion • Convert specification of a primitive into pixels in the frame buffer • Simple rendering pipeline Viewport Transformation Scan Conversion Pixels on display Model
Viewport Transformation • Problem • When we define a 2D model, we want to use whatever coordinate values and units convenient to use in the 2D Cartesian system. • BUT, the coordinate values on the display (or in the screen window) are always nonnegative measured in pixels. • ALSO, we might want to show different parts of our model in different places on the display (or in the screen window) • Solution: viewport transformation
World Window and Viewport • World coordinate • Space in which objects are described • World window • A rectangle area specifies which part of the “world” should be drawn • Viewport • A rectangle area specifies which part on the display (or in the window) the world window is mapped to
World Window and Viewport • Example
Window-to-Viewport Mapping • (x, y) in world window should be mapped to (sx, sy) in viewport proportionally distortion is possible
Window-to-Viewport Mapping • Every vertex used in window to define an object should be mapped to viewport (sx,sy) (x,y) x - W.l sx - V.l sy - V.b y - W.b
How does OpenGL do it? • Set up world window • glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(left, right, bottom, top); • We can write a function setWindow() • setWindow(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(left, right, bottom, top); }
How does OpenGL do it? • Set up viewport • glViewport(left, bottom, width, height); or glViewport(left, bottom, right-left, top-bottom); • Default viewport = screen window size • Parameters to set up a window are doubles while those to set up a viewport are ints. (Why?)
Example: Tiling with Motif setWindow(0,640.0,0,440.0); // set a fixed window for (int i=0; i<5; i++) // for each column for (int j=0; j<5; j++) { // for each row glViewport(i*64, j*44, 64, 44); // set the next viewport drawPolylineFile(“dino.dat ”); // draw it again }
Example: Tiling with Motif for (int i = 0; i < 5; i++) for (int j = 0; j < 5; j++) { if ((i+j)%2 = 1) // if (i+j) is odd setWindow(0.0, 640.0, 0.0, 440.0); // right-side-up window else setWindow(0.0, 640.0, 440.0, 0.0); // upside-down window glViewport(i*64, j*44, 64, 44); // set the next viewport drawPolylineFile("dino.dat"); // draw it again }
Zooming and Panning • Zooming • Scale the world window with respect to the center of the world window with viewport fixed • Panning • Shift the world window with viewport fixed
Mapping without Distortion • To prevent from distortion, a viewport with the same aspect ratio of world window is required. • Aspect ratio: width/height • How do you draw the largest undistorted picture in the screen window?
Mapping without Distortion • If aspect ratio of world window = R • Two cases glViewport(0,0,H*R,H); glViewport(0,0,W,W/R);
Resize Screen Window • glutReshapeFunc(myReshape) • Prototype of myReshape • void myReshape(GLsizei W, Glsizei H); • W and H: new width and height • Making a matched viewport • void myReshape(GLsizei W,GLsizei H) { if (R >W/H) //use (global)window aspect ratio setViewport(0, W, 0, W/R); else setViewport(0, H*R, 0, H); }
Primitives Rasterization • Line • Brute force • DDA • Midpoint algorithm • Circle • Brute force • Polar coordinates • Midpoint algorithm • Polyline and polygon
Primitive Clipping • Problem • Not everything defined in the world will be included in the world window • Solution • Clipping • Line clipping • Polygon clipping • OpenGL does clipping for you