Docstoc

Polygon Scan Conversion _amp; Shading

Document Sample
Polygon Scan Conversion _amp; Shading Powered By Docstoc
					                                                   3D Rendering Pipeline (for direct illumination)
                                                           3D Primitives
                                                                       3D Modeling Coordinates
                                                             Modeling
                                                          Transformation
                                                                        3D World Coordinates

        Polygon Scan Conversion                              Lighting

                                                                       3D World Coordinates
                                                                                                                      P1
               & Shading                                     Viewing
                                                          Transformation
                                                                        3D Camera Coordinates
                                                            Projection
                       Greg Humphreys                     Transformation
                                                                        2D Screen Coordinates
                                                             Clipping
                                                                                                      P2
                    CS445: Intro Graphics                               2D Screen Coordinates

               University of Virginia, Fall 2004
                                                             Viewport
                                                          Transformation                                                     P3
                                                                        2D Image Coordinates               Scan Conversion
                                                             Scan
                                                           Conversion                                         & Shading
                                                                        2D Image Coordinates
                                                              Image




Overview                                           Scan Conversion
• Scan conversion                                  • Render an image of a geometric primitive
    Figure out which pixels to fill                 by setting pixel colors
• Shading
    Determine a color for each filled pixel                     void SetPixel(int x, int y, Color rgba)

                                                   • Example: Filling the inside of a triangle


                                                                                                      P1

                                                                                     P2

                                                                                                            P3




Scan Conversion                                    Triangle Scan Conversion
• Render an image of a geometric primitive         • Properties of a good algorithm
  by setting pixel colors                                 Symmetric
                                                          Straight edges
                                                          Antialiased edges
        void SetPixel(int x, int y, Color rgba)
                                                          No cracks between adjacent primitives
• Example: Filling the inside of a triangle               MUST BE FAST!


                                       P1                                                             P1
                                                                                    P2
                      P2
                                                                                                            P4
                                            P3
                                                                                                 P3




                                                                                                                                  1
Triangle Scan Conversion                                Simple Algorithm
• Properties of a good algorithm                        • Color all pixels inside triangle
      Symmetric
      Straight edges                                         void ScanTriangle(Triangle T, Color rgba){
      Antialiased edges                                        for each pixel P at (x,y){
                                                                   if (Inside(T, P))
      No cracks between adjacent primitives                          SetPixel(x, y, rgba);
                                                                }
      MUST BE FAST!                                          }



                                        P1                                                                      P1
                     P2                                                                      P2
                                             P4
                                                                                                                     P3
                                   P3




Line defines two halfspaces                             Inside Triangle Test
• Implicit equation for a line                          • A point is inside a triangle if it is in the
    On line:    ax + by + c = 0                          positive halfspace of all three boundary lines
    On right:   ax + by + c < 0                            Triangle vertices are ordered counter-clockwise
    On left:    ax + by + c > 0                            Point must be on the left side of every boundary line



                                                  P1
                                        L                                                    L1
                               P2                                                                      P   L3

                                                                                                  L2




Inside Triangle Test                                    Simple Algorithm
                                                        • What is bad about this algorithm?
       Boolean Inside(Triangle T, Point P)
       {
         for each boundary line L of T {                      void ScanTriangle(Triangle T, Color rgba){
            Scalar d = L.a*P.x + L.b*P.y + L.c;                 for each pixel P at (x,y){
            if (d < 0.0) return FALSE;                             if (Inside(T, P))
         }                                                            SetPixel(x, y, rgba);
         return TRUE;                                           }
       }                                                      }

                                        L1
                                                                                                                P1
                                                   L3
                                                                                             P2
                                             L2
                                                                                                                     P3




                                                                                                                          2
Triangle Sweep-Line Algorithm                                    Triangle Sweep-Line Algorithm
• Take advantage of spatial coherence
                                                                    void ScanTriangle(Triangle T, Color rgba){
    Compute which pixels are inside using horizontal spans            for each edge pair {
    Process horizontal spans in scan-line order                           initialize xL , xR;
                                                                           compute dxL/dyL and dxR/dyR;
                                                                           for each scanline at y
• Take advantage of edge linearity                                         for (int x = x L ; x <= xR; x++)
    Use edge slopes to update coordinates incrementally                            SetPixel(x, y, rgba);
                                                                           xL += dxL /dyL;
                                                                           xR += dxR/dyR;                     dxL
                                                                       }                                                     dxR
                                                                    }                                     dyL                   dyR
                                               dx
                                          dy
                                                                      Bresenham’s algorithm                    xL           xR
                                                                      works the same way,
                                                                      but uses only integer
                                                                      operations!




Polygon Scan Conversion                                          Polygon Scan Conversion
• Fill pixels inside a polygon                                   • Need better test for points inside polygon
      Triangle                                                     Triangle method works only for convex polygons
      Quadrilateral
      Convex
      Star-shaped
      Concave
                                                                              L5                                      L5
                                                                                              L4                                   L4
      Self-intersecting
      Holes                                                         L1                                   L1                     L3B
                                                                                              L3                                 L3A
                                                                                L2                                     L2
       What problems do we encounter with arbitrary polygons?
                                                                          Convex Polygon                            Concave Polygon




Inside Polygon Rule                                              Inside Polygon Rule
• What is a good rule for which pixels are inside?               • Odd-parity rule
                                                                    Any ray from P to infinity crosses odd number of edges




       Concave             Self-Intersecting        With Holes
                                                                      Concave              Self-Intersecting                With Holes




                                                                                                                                         3
Polygon Sweep-Line Algorithm                            Polygon Sweep-Line Algorithm
• Incremental algorithm to find spans,
                                                           void ScanPolygon(Triangle T, Color rgba){
  and determine insideness with odd parity rule              sort edges by maxy
   Takes advantage of scanline coherence                    make empty “active edge list”
                                                             for each scanline (top-to-bottom) {
                                                                insert/remove edges from “active edge list”
                                                                update x coordinate of every active edge
                                                                sort active edges by x coordinate
                                                                for each pair of active edges (left-to-right)
                                                                   SetPixels(xi, xi+1, y, rgba);
                                                                }
                                                           }

            xL           xR



              Triangle                        Polygon




Hardware Scan Conversion                                Hardware Antialiasing
• Convert everything into triangles                     • Supersample pixels
   Scan convert the triangles                             Multiple samples per pixel
                                                           Average subpixel intensities (box filter)
                                                           Trades intensity resolution for spatial resolution



                                                                                                      P1

                                                                                     P2

                                                                                                           P3




Overview                                                Shading
• Scan conversion                                       • How do we choose a color for each filled pixel?
   Figure out which pixels to fill                        Each illumination calculation for a ray from the eyepoint
                                                            through the view plane provides a radiance sample
• Shading                                                    » How do we choose where to place samples?
   Determine a color for each filled pixel                  » How do we filter samples to reconstruct image?




                                                            Emphasis on methods that can
                                                            be implemented in hardware




                                                                                                           Angel Figure 6.34




                                                                                                                               4
Ray Casting                                                                 Polygon Shading
• Simplest shading approach is to perform                                   • Can take advantage of spatial coherence
  independent lighting calculation for every pixel                             Illumination calculations for pixels covered by same
    When is this unnecessary?                                                  primitive are related to each other




     I = I E + K A I AL + !i ( K D ( N • Li ) I i + K S (V • Ri ) n I i )        I = I E + K A I AL + !i ( K D ( N • Li ) I i + K S (V • Ri ) n I i )




Polygon Shading Algorithms                                                  Polygon Shading Algorithms
• Flat Shading                                                              • Flat Shading
• Gouraud Shading                                                           • Gouraud Shading
• Phong Shading                                                             • Phong Shading




Flat Shading                                                                Flat Shading
• What if a faceted object is illuminated only by                           • One illumination calculation per polygon
  directional light sources and is either diffuse or                           Assign all pixels inside each polygon the same color
  viewed from infinitely far away




                                                                                                      N



     I = I E + K A I AL + !i ( K D ( N • Li ) I i + K S (V • Ri ) n I i )




                                                                                                                                                        5
Flat Shading                                                                Polygon Shading Algorithms
• Objects look like they are composed of polygons                           • Flat Shading
    OK for polyhedral objects
    Not so good for smooth surfaces
                                                                            • Gouraud Shading
                                                                            • Phong Shading




Gouraud Shading                                                             Gouraud Shading
• What if smooth surface is represented by                                  • Method 1: One lighting calculation per vertex
  polygonal mesh with a normal at each vertex?                                 Assign pixels inside polygon by interpolating colors
                                                                                computed at vertices




                                                Watt Plate 7

     I = I E + K A I AL + !i ( K D ( N • Li ) I i + K S (V • Ri ) n I i )




Gouraud Shading                                                             Gouraud Shading
• Bilinearly interpolate colors at vertices                                 • Smooth shading over adjacent polygons
  down and across scan lines                                                   Curved surfaces
                                                                               Illumination highlights
                                                                               Soft shadows




                                                                                           Mesh with shared normals at vertices
                                                                                                                                  Watt Plate 7




                                                                                                                                                 6
Gouraud Shading                                                             Polygon Shading Algorithms
• Produces smoothly shaded polygonal mesh                                   • Flat Shading
    Piecewise linear approximation
    Need fine mesh to capture subtle lighting effects
                                                                            • Gouraud Shading
                                                                            • Phong Shading




         Flat Shading                         Gouraud Shading




Phong Shading                                                               Phong Shading
• What if polygonal mesh is too coarse to capture                           • Method 2: One lighting calculation per pixel
  illumination effects in polygon interiors?                                   Approximate surface normals for points inside polygons by
                                                                                bilinear interpolation of normals from vertices




     I = I E + K A I AL + !i ( K D ( N • Li ) I i + K S (V • Ri ) n I i )




Phong Shading                                                               Polygon Shading Algorithms
• Bilinearly interpolate surface normals at vertices
  down and across scan lines                                                              Wireframe                Flat




                                                                                           Gouraud               Phong
                                                                                                                               Watt Plate 7




                                                                                                                                              7
Shading Issues                                                 Summary
• Problems with interpolated shading:                          • 2D polygon scan conversion
     Polygonal silhouettes                                       Paint pixels inside primitive
     Perspective distortion                                      Sweep-line algorithm for polygons
     Orientation dependence (due to bilinear interpolation)
     Problems computing shared vertex normals
                                                               • Polygon Shading Algorithms
                                                                    Flat
     Problems at T-vertices                                                                           Less expensive
                                                                    Gouraud
                                                                    Phong
                                                                    Ray casting
                                                                                                       More accurate
                                                               • Key ideas:
                                                                  Sampling and reconstruction
                                                                  Spatial coherence




                                                                                                                        8

				
DOCUMENT INFO