Class Rasterize_Clip_AntiAlias_Line


  • public final class Rasterize_Clip_AntiAlias_Line
    extends Object
    Rasterize a projected LineSegment into shaded pixels in a FrameBuffer.Viewport and (optionally) anti-alias and gamma-encode the line at the same time. Also, (optionally) do not rasterize any part of the LineSegment that is not contained in the Camera's view rectangle.

    This pipeline stage takes a LineSegment whose vertices have been projected into the Camera's view plane coordinate system and rasterizes the LineSegment into shaded, anti-aliased pixels in a FrameBuffer.Viewport.

    In addition, this rasterizer has the option to "clip" the LineSegment by not rasterizing into the FrameBuffer.Viewport any part of the projected LineSegment that is not within the Camera's view rectangle.

    This rasterization algorithm is based on

         "Fundamentals of Computer Graphics", 3rd Edition,
          by Peter Shirley, pages 163-165.
    

    This rasterizer implements a simple version of Xiaolin_Wu's anti-aliasing algorithm. See https://en.wikipedia.org/wiki/Xiaolin_Wu's_line_algorithm

    Recall that a FrameBuffer.Viewport is a two-dimensional array of pixel data. So a viewport has an integer "coordinate system". That is, we locate a pixel in a viewport using two integers, which we think of as row and column. On the other hand, a Camera's view rectangle has a two-dimensional real number (not integer) coordinate system. Since a framebuffer's viewport and a camera's view rectangle have such different coordinate systems, rasterizing line segments from a two-dimensional real number coordinate system to an two-dimensional integer grid can be tricky. The "logical pixel-plane" and the "viewport transformation" try to make this rasterization step a bit easier.

    
                                     (0,0)
                                       +-------------------------------------------+
            y-axis                     |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
              |                        |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
              |  (+1,+1)               |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
        +-----|-----+                  |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
        |     |     |                  |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
        |     |     |                  |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
    ----------+----------- x-axis      |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
        |     |     |                  |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
        |     |     |                  |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
        +-----|-----+                  |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
     (-1,-1)  |                        |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
              |                        |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
                                       |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
     Camera's View Rectangle           +-------------------------------------------+
      (in the View Plane)                                                      (w-1,h-1)
                                                  FrameBuffer's Viewport
    

    The viewport transformation places the logical pixel-plane between the camera's view rectangle and the framebuffer's viewport. The pixel-plane has a real number coordinate system (like the camera's view plane) but is has dimensions more like the dimensions of the framebuffer's viewport.

    
                                                        (w+0.5, h+0.5)
               +----------------------------------------------+
               | . . . . . . . . . . . . . . . . . . . . . .  |
               | . . . . . . . . . . . . . . . . . . . . . .  |
               | . . . . . . . . . . . . . . . . . . . . . .  |
               | . . . . . . . . . . . . . . . . . . . . . .  |
               | . . . . . . . . . . . . . . . . . . . . . .  |  The "logical pixels"
               | . . . . . . . . . . . . . . . . . . . . . .  |  are the points in
               | . . . . . . . . . . . . . . . . . . . . . .  |  the pixel-plane with
               | . . . . . . . . . . . . . . . . . . . . . .  |  integer coordinates.
               | . . . . . . . . . . . . . . . . . . . . . .  |
               | . . . . . . . . . . . . . . . . . . . . . .  |
               | . . . . . . . . . . . . . . . . . . . . . .  |
               | . . . . . . . . . . . . . . . . . . . . . .  |
               | . . . . . . . . . . . . . . . . . . . . . .  |
               +----------------------------------------------+
          (0.5, 0.5)
                     pixel-plane's "logical viewport"
                       containing "logical pixels"
       

    Notice that we have two uses of the word "viewport",

    • The "logical viewport" is a rectangle in the pixel-plane (so its points have real number coordinates). The "logical pixels" are the points in the logical viewport with integer coordinates.
    • The "physical viewport" is part of the FrameBuffer's pixel array (so its entries have integer coordinates). The "physical pixels" are the entries in the physical viewport.