University of British Columbia News CPSC 314 Computer Graphics � midterm review Wednesday Jan-Apr 2005 � plus Kangaroo Hall of Fame � midterm Friday (be on time!) Tamara Munzner � covering through lighting/shading � not color or rasterization Rasterization � homework 1 solutions out � no more late homework accepted Week 6, Mon Feb 7 � program 2 writeup out � due Thu Feb 24 http://www.ugrad.cs.ubc.ca/~cs314/Vjan2005 2 Program 2: Terrain Navigation Navigating � make bumpy terrain � two flying modes: absolute and relative � 100x100 rectangular grid � absolute � vertex height varies randomly by 20% � keyboard keys to increment/decrement � vertex color varies randomly � x/y/z position of eye, lookat, up vectors � switch between per-face, per-vertex normals � relative � explicitly draw normals (hedgehog mode) � mouse drags � lighting and shading � incremental wrt current camera position � headlamp, plus at least one fixed light � forward/backward motion � switch between smooth and flat shading � roll, pitch, and yaw angles 3 4 Hint: Incremental Motion Reading � motion is wrt current camera coords � Color (reading from Friday) � maintaining cumulative angles wrt world coords � FCG Chap 17 Human Vision (pp 293-298) would be difficult � FCG Chap 18 Color (pp 301-311) � computation in coord system used to draw previous frame is simple � until Section 18.9 Tone Mapping � OpenGL modelview matrix has the info! � FCG Sec 3.2 Gamma Correction � but multiplying by new matrix gives p’=CIp � FCG Sec 3.3 RGB Color � you want to do p’=ICp � trick: � Rasterization � dump out modelview matrix � FCG Chap 3 Raster Algorithms (pp 49-67) � wipe the stack with glIdentity � apply incremental update matrix � FCG Section 2.11 Barycentric Coordinates � apply current camera coord matrix 5 6
FCG Errata Font Correction: Lighting in OpenGL glLightfv(GL_LIGHT0, GL_AMBIENT, amb_light_rgba ); � p 54 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif_light_rgba ); glLightfv(GL_LIGHT0, GL_SPECULAR, spec_light_rgba ); � triangle at bottom of figure shouldn’t have glLightfv(GL_LIGHT0, GL_POSITION, position); black outline glEnable(GL_LIGHT0); � p 63 glMaterialfv( GL_FRONT, GL_AMBIENT, ambient_rgba ); glMaterialfv( GL_FRONT, GL_DIFFUSE, diffuse_rgba ); � The test if numbers a [x] and b [y] have the glMaterialfv( GL_FRONT, GL_SPECULAR, specular_rgba ); same sign can be implemented as the test glMaterialfv( GL_FRONT, GL_SHININESS, n ); ab [xy] > 0. � warning: glMaterial is expensive and tricky � use cheap and simple glColor when possible � see OpenGL Pitfall #14 from Kilgard’s list http://www.opengl.org/resources/features/KilgardTechniques/oglpitfall/ 7 8 Correction/Review: Computing Normals Review: Trichromacy and Metamers � per-vertex normals by interpolating per-facet � three types of cones normals � color is combination � OpenGL supports both of cone stimuli � computing normal for a polygon � metamer: identically � three points form two vectors b perceived color (a-b) x (b-c) � cross: normal of plane direction caused by very � normalize: make unit length different spectra b-c c � which side of plane is up? a-b � counterclockwise point order convention a 9 10 Review: Color Constancy Review: Measured vs. CIE Color Spaces measured basis � transformed basis � monochromatic lights � � “imaginary” lights physical observations � � all positive, unit area negative lobes � � Y is luminance 11 12
Review: Device Color Gamuts Review: RGB Color Space � define colors with (r, g, b) � compare gamuts on CIE chromaticity diagram amounts of red, green, and � gamut mapping blue � used by OpenGL � RGB color cube sits within CIE color space � subset of perceivable colors 13 14 Review: HSV Color Space Review: YIQ Color Space � YIQ is the color model used for color TV � hue: dominant wavelength, in America. Y is brightness, I & Q are color “color” � saturation: how far from grey � same Y as CIE, backwards compatibility with black and white TV � value/brightness: how far from black/white � blue is more compressed Y 0 . 30 0 . 59 0 . 11 R � � � � � � � � � � � � I 0 . 60 0 . 28 0 . 32 G = � � � � � � � � Q 0 . 21 0 . 52 0 . 31 B � � � � � � � � � � � � � 15 16 Review: Gamma Correction � DS = � D (1/ � OS ) Rasterization 17 18
Scan Conversion - Rasterization Scan Conversion � convert continuous rendering primitives into � given vertices in DCS, fill in the pixels discrete fragments/pixels � start with lines � lines � Bresenham � triangles � flood fill � scanline � implicit formulation � interpolation 19 20 Basic Line Drawing Line ( x 1 , y 1 , x 2 , y 2 ) begin float dx , dy , x , y , slope ; y mx b dx � x 2 � x 1 ; = + dy � y 2 � y 1 ; ( y y ) � y 2 1 ( x x ) y Lines = � + slope � dy 1 1 ( x x ) dx ; � 2 1 y � y 1 � goals for x from x 1 to x 2 do � integer coordinates begin � thinnest line with no gaps PlotPixel ( x , Round ( y ) ) ; assume � 0 < dy dx < 1 x < x y � y + slope ; � , slope 1 2 end ; how can we do this quickly? � end ; 21 Midpoint Algorithm Making It Fast y+1 � moving incrementally along x direction � maintain error value y+e+m � test � draw at current y value, or move up to y+1? m y+e e+m � if (y+e+m) < y+.5 � check if midpoint between two possible pixel centers e � e+m < .5 y above or below line � if top pixel picked x x+1 � candidates � e = y+e+m-y = e+m � top pixel: (x+1,y+1) � if bottom pixel picked � bottom pixel: (x+1, y) � e = y+e+m-(y+1) = e+m-1 � midpoint: (x+1, y+.5) � convert to use only integer arithmetic (remember m=dy/dx) � test: multiply by 2*dx. then check if (2*e*dx+dy) < dx � check if midpoint above or below line � top: multiply by dx. then e*dx = e*dx+dy � below: top pixel � bottom: multiple by dx. then e*dx = e*dx+dy-1 � above: bottom pixel � E= e*dx � [demo] 23 24
Bresenham Line Drawing Algorithm Bresenham Line Drawing Algorithm y=y0; e=0; y=y0; e=0; for (x=x0; x <= x1; x++) { for (x=x0; x <= x1; x++) { draw(x,y); draw(x,y); � all integer arithmetic � all integer arithmetic if (2(e+dy) < dx) { if (2(e+dy) < dx) { � more speedups e = e+dy; e = e+dy; � left shift for multiply by two } else { } else { � avoid extra calculations y=y+1; y=y+1; e=e+dy-dx; e=e+dy-dx; y=y0; eps=0 }} }} for ( int x = x0; x <= x1; x++ ){ draw(x,y); eps += dy; if ( (eps << 1) >= dx ){ y++; eps -= dx; } } 25 26 Rasterizing Polygons/Triangles � basic surface representation in rendering � why? � lowest common denominator � can approximate any surface with arbitrary accuracy Polygons � all polygons can be broken up into triangles � guaranteed to be: � planar � triangles - convex � simple to render � can implement in hardware 27 28 Triangulation OpenGL Triangulation � convex polygons easily triangulated � simple convex polygons � break into triangles, trivial � glBegin(GL_POLYGON) ... glEnd() � concave polygons present a challenge � concave or non-simple polygons � break into triangles, more effort � gluNewTess(), gluTessCallback(), ... 29 30
Problem Flood Fill � input: closed 2D polygon � simple algorithm � problem: fill its interior with specified color on � draw edges of polygon graphics display � use flood-fill to draw interior � assumptions � simple - no self intersections � simply connected P � solutions � flood fill � scan conversion � implicit test 32 Flood Fill Flood Fill � draw edges � start with seed point � run: � recursively set all neighbors until boundary is hit FloodFill (Polygon P, int x , int y , Color C ) if not ( OnBoundary ( x , y ,P) or Colored ( x , y , C )) begin PlotPixel ( x , y , C ); FloodFill (P, x + 1, y , C ); FloodFill (P, x , y + 1, C ); FloodFill (P, x , y � 1, C ); FloodFill (P, x � 1, y , C ); end ; � drawbacks? 33 34 Flood Fill Drawbacks Scanline Algorithms � pixels visited up to 4 times to check if already set � scanline: a line of pixels in an image � need per-pixel flag indicating if set already � must clear for every polygon! 35 36
Recommend
More recommend