Computer Graphics - The graphics pipeline - Geometry Modelview Geometry Processing Lighting Perspective Clipping Scan Conversion Texturing Fragment Tests Blending Framebuffer Fragment Processing - So far, projection of vertices into view space (done in the geometry stage) - Now, conversion of geometric primitives into fragments - Fragment: - Represents a point of geometry - Has color, texcoord, depth etc. - Associated with a pixel - But not yet a pixel (not stored in the framebuffer) - Scan conversion of geometric primitives - For each covered pixel a fragment is generated - Attributes like color, depth, texture coordinates etc. are generated from projected vertex attributes - During fragment generation OpenGL states, e.g. shading model, line width, polygon mode etc. are taken into account - Pixel: - Color of a pixel in the framebuffer - of geometric data Geometry processing Vertex transformation Lighting Clipping... Screen coord (X,Y) Depth (z) Color (RGBα) TexCoords (u,v,w)... Fragment Processing Texturing Per-fragment tests Per-fragment comparison Blending... - of triangles - Using OpenGL, everything is broken into triangles - Find all fragments covered by a triangle // most simple rasterizer Rasterize(vertex v[3]) { int x, y; bbox b; getbbox(v,, &b); for (y= b.ymin; ; y < b.ymax; ; y++) for (x= b.xmin; ; x < b.xmax; ; x++) if (inside(v,, x, y)) fragment(x,y); }
- Scanline algorithm - Edge table: - Stores list of all edges in increasing y min -order y min, x min, y max, dx/dy - Active edge table: - Contains all edges that intersect current scanline in increasing x-order x, y max, dx/dy - Scanline algorithm scanconvert(vertex v0, v,... vn) { AEL = NULL; foreach(scanline y) { remove edges from AEL with y max y; foreach(edge AEL) x = x + dx/dy; insert edges into AEL with y min == y; sort AEL by increasing x; draw scanline segments xx, x3x4,... } } - Once a fragment is generated, per-vertex attributes need to be assigned properly - Determination of color and other properties for all pixels along a scanline is called Shading - This is done via interpolation (see exercise for more information) wire frame scanline v 0 v v - Flat shading - Per surface (polygon) lighting - Take normal of one distinct vertex as triangle normal - Constant color for polygon - Mach band effect flat shading
- Smooth or Gouraud shading (97) - Idea: better quality by respecting color variations on the polygon - Per vertex lighting - Linear interpolation of color using barycentric interpolation in triangles - In object space during ray-tracing - In screen space during scan conversion (OpenGL) - How to determine barycentric coordinates efficiently for every pixel A a D a C P E B a D = A + ( C A) a + a a a = A C a a + + a + a a a = A + C a + a a + a - Similar - Similar C C A a a b b b D P E E = B + C b + b b + b b B A a a b c c D P E P = D + E c + c c + c c c b B Gouraud shading Polygon shading - Does this algorithm work for polygons, i.e. non- triangles? 3
- Problems of Gouraud shading: - Loosing surface structure - No internal highlights - Phong shading (975): higher quality shading by interpolating normals rather than colors - Evaluates lighting model for every pixel Has to be done in object space Not supported by OpenGL Phong shading - Shading problems - Smooth surfaces, but polygonal borders (silhouette) - Inconsistencies in vertices - T-vertex (hanging node) - OpenGL shading Phong lighting Gouraud shading Phong lighting and shading - Scan conversion of primitives scanline v 0 Interpolation of per-vertex attributes along scanlines v v z Depth values: non-linear in [Z near, Z far ] due to perspective projection Z near Z far 4
- For perspective views,, linear interpolation of vertex parameters like color, texcoords, normals in screen space leads to wrong results - Linear (left( left) ) and rational linear (right) interpolation of texture coordinates in screen space Wrong interpolation in screen space P a A P A=0.5 Line with parameters P 0,P at vertices viewpoint P b B P =0.5 (P + P ) viewplane P B=0.5 P - Texture interpolation without perspective correction - Refinement in,4,64 Quads (=,8,8 Tris) - Relationship between spaces in CG Object-affine spaces Screen-affine spaces Object space World space Texture space (u,v) Homogeneous screen space (xw,yw,w) divby/w divbyw Homogeneous texture space (u/w,v/w,/w) Screen space (x,y) - Perspective correction (given:a,b; looking for A,B) a b P ' P = P + P ; P = a + b a + b w A B Aw + Bw P ' = P ' + P ' w = A + B A + B A + B ap ' / w + bp ' / w AP ' + BP ' P = = A = a + b Aw + Bw a / wp ' + b / w P ' P ' = a / w + b / w a w, B = b w Interpolate x/w, y/w, z/w, /w and divide through /w per pixel - (Wrong) algorithm for polygon rendering with linear interpolation - Associate parameters p,...,p n with each vertex - vertex object space coords to homogeneous screen space coords (xw,yw,zw,w) - Perform homogeneous division (x,y,z,) - Scan convert in screen space by linear interpolation of all parameters 5
- (Correct) algorithm for polygon rendering with linear interpolation Next: fragment processing - Operations on fragments before the become pixels - Associate parameters p,...,p n with each vertex - vertex object space coords to homogeneous screen space coords (xw,yw,zw,w) - At each vertex construct variable list (x,y,z,s,...,s n+ ) with s i =p i /w, s n+ =/w - Scan convert in screen space by linear interpolation of all parameters; ; at each pixel compute p i =s i /s /s n+ Geometry Scan Conversion Modelview Texturing Geometry Processing Lighting Fragment Tests Perspective Blending Clipping Framebuffer Fragment Processing 6