001package pipeline;
002import scene.*;
003import framebuffer.*;
004
005/**
006   Transform each line segment's endpoints from viewplane coordinates
007   to viewport coordinates so that the view rectangle in the view plane
008   with
009        -1 <= x <= 1,
010        -1 <= y <= 1,
011   transforms into a viewport where
012       0.5 <= x < w + 0.5
013       0.5 <= y < h + 0.5
014   where w = number of horizontal pixels in the viewport,
015         h = number of vertical pixels in the viewport.
016
017   The goal of this transformation is to put a logical pixel with
018   integer coordinates at the center of each square physical pixel.
019   The logical pixel with integer coordinates (m, n) represents the
020   square pixel with
021     m - 0.5 <= x < m + 0.5,
022     n - 0.5 <= y < n + 0.5.
023   Notice that logical pixel integer coordinates (m.n) have
024     1 <= m <= w
025     1 <= n <= h.
026*/
027public class Viewport
028{
029   /**
030      Transform the line segment's endpoints from
031      viewplane coordinates to viewport coordinates.
032
033      @param ls LineSegment to transform to viewport coordinates
034      @param fb FrameBuffer that holds the current viewport
035   */
036   public static void viewport(LineSegment ls, FrameBuffer fb)
037   {
038      // Get the viewport dimensions.
039      int w = fb.getWidthVP();
040      int h = fb.getHeightVP();
041
042      // Transform the line segment's endpoints from
043      // viewplane coordinates to viewport coordinates.
044      double x0 = 0.5 + w/2.001 * (ls.v[0].x + 1.0);
045      double y0 = 0.5 + h/2.001 * (ls.v[0].y + 1.0);
046      double x1 = 0.5 + w/2.001 * (ls.v[1].x + 1.0);
047      double y1 = 0.5 + h/2.001 * (ls.v[1].y + 1.0);
048      // NOTE: Notice the 2.001 fudge factor in the last four equations.
049      // This is explained on page 142 of
050      //    "Jim Blinn's Corner: A Trip Down The Graphics Pipeline"
051      //     by Jim Blinn, 1996, Morgan Kaufmann Publishers.
052
053      // Mutate the LineSegment object so that it refers to new
054      // Vertex objects containing the viewport coordinates.
055      ls.v[0] = new Vertex( x0, y0, ls.v[0].z, ls.v[0].getColor() );
056      ls.v[1] = new Vertex( x1, y1, ls.v[1].z, ls.v[1].getColor() );
057   }
058}