Announcem ent CS 4 7 3 1 : Com put e r Gr a phics Project 5 help session today, 5- 6pm in FL 320 � Le ct ur e 2 2 : Ra st e r Gr a phics Pa r t 3 Paolo has im plem ented project, will go over it, answer � questions Em m anuel Agu So Far… Defining and Filling Regions of Pixels � Raster graphics: � First, understand how to define and fill any defined regions � I m age m anipulat ion � Next, how to fill regions bounded by a polygon � Line drawing algorit hm s ( sim ple, Bresenham ’s) � Today: � Defining and filling regions � Polygon drawing and filling � Antialiasing 1
Defining and Filling Regions of Pixels Pixel- Defined Regions Methods of defining region Definit ion: Region R is the set of all pixels having color C � � that are connected to a given pixel S � Pixel- defined: specifies pixels in color or geom et ric range � Sym bolic: provides propert y pixels in region m ust have � 4 - adj acent: pixels that lie next to each other horizontally or vertically, NOT diagonally � Exam ples of sym bolic: • Closeness to some pixel � 8 - adj acent: pixels that lie next to each other horizontally, • Within circle of radius R vertically OR diagonally • Within a specified polygon � 4 - connect ed: if there is unbroken path of 4- adjacent pixels connecting them � 8 - connect ed: unbroken path of 8- adjacent pixels connecting them Recursive Flood-Fill Algorithm Recursive Flood-Fill Algorithm � Recursive algorithm � Note: getPixel(x,y) used to interrogate pixel color at (x, y) Starts from initial pixel of color, intColor � Recursively set 4- connected neighbors to newColor void floodFill(short x, short y, short intColor) � { � Flood- Fill : floods region with newColor if(getPixel(x, y) == intColor) � Basic idea: { � st art at “ seed” pixel ( x, y) � I f ( x, y) has color intColor, change it t o newColor setPixel(x, y); � Do sam e recursively for all 4 neighbors floodFill(x – 1, y, intColor); // left pixel floodFill(x + 1, y, intColor); // right pixel floodFill(x, y + 1, intColor); // down pixel floodFill(x, y – 1, intColor); // fill up } } 2
Recursive Flood-Fill Algorithm Region Filling Using Coherence This version defines region using intColor Example: start at s, initial seed � � Can also have version defining region by boundary � Recursive flood- fill is som ewhat blind and som e pixels m ay � be retested several tim es before algorithm term inates � Region coherence is likelihood that an interior pixel m ostly likely adjacent to another interior pixel � Coherence can be used to im prove algorithm perform ance � A run is a group of adjacent pixels lying on sam e � Exploit runs(adjacent, on sam e scan line) of pixels Region Filling Using Coherence Filling Polygon- Defined Regions � Pseudocode: � Problem : Region defined by Polygon P with vertices Pi = (Xi, Yi), for i – 1… N, specifying sequence of P’s vertices Push address of seed pixel onto stack while(stack is not empty) P2 P1 { Pop the stack to provide next seed P7 P3 Fill in the run defined by the seed In the row above find the reachable interior runs Push the address of their rightmost pixels Do the same for row below current run P5 } Note: algorithm m ost efficient if there is span coherence (pixels P6 P4 on scanline have sam e value) and scan - line coherence (consecutive scanlines are similar) 3
Filling Polygon- Defined Regions Filling Polygon- Defined Regions Solut ion: Progress through frame buffer scan line by scan Pseudocode: � � line, filling in appropriate portions of each line � Filled portions defined by intersection of scan line and for(each scan Line L) polygon edges { � Runs lying between edges inside P are filled Find intersections of L with all edges of P Sort the intersections by increasing x-value Fill pixel runs between all pairs of intersections } Filling Polygon- Defined Regions Filling Polygon- Defined Regions � Exam ple: scan line y = 3 int ersect s 4 edges e3, e4, e5, e6 � What if two polygons A, B share an edge? � Sort x values of int ersect ions and fill runs in pairs � Algorithm behavior could result in: � N ot e : at each int ersect ion, inside -out side ( parit y) , or vice versa � set t ing edge first in one color and t he anot her P2 Drawing edge t wice t oo bright P1 � � Make Rule: when two polygons share edge, each polygon owns its left and bottom edges P7 P3 � E.g. below draw shared edge with color of polygon B e6 e3 B P5 3 A e4 e5 P6 P4 Read: Hill: 10.7.1, pg 571 4
Filling Polygon- Defined Regions Antialiasing � How t o handle cases where scan line int ersect s wit h polygon Raster displays have pixels as rectangles � endpoints? Aliasing: Discrete nature of pixels introduces “jaggies” � � Solut ion: Discard int ersect ions wit h horizont al edges and wit h upper endpoint of any edge Hill: 10.7.1, pg. 572 Antialiasing Prefiltering � Aliasing effects: � Basic idea: � Dist ant obj ect s m ay disappear ent irely � com put e area of polygon coverage � Obj ects can blink on and off in anim ations � use proport ional int ensit y value � Antialiasing techniques involve som e form of blurring to � Exam ple: if polygon covers ¼ of the pixel reduce contrast, sm oothen im age � use ¼ polygon color � Three antialiasing techniques: � add it t o ¾ of adj acent region color Cons: com puting pixel coverage can be tim e consum ing � Prefiltering � � Post filt ering � Supersam pling 5
Supersam pling Postfiltering Useful if we can com pute color of any (x,y) value on the Supersam pling uses average � � screen Gives all sam ples equal im portance � � I ncrease frequency of sam pling Post- filtering: use weighting (different levels of im portance) � � I nstead of (x,y) sam ples in increm ents of 1 � Com pute pixel value as weighted average � Sam ple (x,y) in fractional (e.g. ½ ) increm ents � Samples close to pixel center given more weight � Find average of sam ples � Exam ple: Double sam pling = increm ents of ½ = 9 color Sa m ple w e igh t in g values averaged for each pixel 1/ 16 1/ 16 1/ 16 Average 9 (x, y) values 1/ 16 1/ 2 1/ 16 to find pixel color 1/ 16 1/ 16 1/ 16 Antialiasing in OpenGL Antialiasing in OpenGL � Many alternatives � First initialize: � glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | � Sim plest: accum ulation buffer GLUT_ACCUM | GLUT_DEPTH); � Accum ulation buffer: extra storage, sim ilar to fram e buffer � Zero out accum ulation buffer � Sam ples are accum ulated � glClear (GLUT_ACCUM_BUFFER_BIT); � When all slightly perturbed sam ples are done, copy results Add sam ples to accum ulation buffer using � to fram e buffer and draw � glAccum ( ) 6
Antialiasing in OpenGL References Sam ple code Hill, chapter 10 � � jitter[ ] stores random ized slight displacem ents of cam era, � factor, f controls am ount of overall sliding � glClear(GL_ACCUM_BUFFER_BIT); for(int i=0;i < 8; i++) { cam.slide(f*jitter[i], f*jitter[i].y, 0); display( ); glAccum(GL_ACCUM, 1/8.0); } glAccum(GL_RETURN, 1.0); 7
Recommend
More recommend