001package pipeline;
002import scene.*;
003
004/**
005   Project line segments from camera coordinates to the view plane.
006
007   For each Vertex object in each LineSgement object, project the
008   Vertex object into the view plane z = -1.
009*/
010public class Project
011{
012   /**
013      For each line segment in the model, project
014      each vertex to the view plane z = -1.
015
016      @param ls LineSegment to project onto the image plane
017      @param camera a reference to the Scene's Camera object
018   */
019   public static void project(LineSegment ls, Camera camera)
020   {
021      // For each vertex in the line segment,
022      // compute its projected (x,y) coordinates.
023      double v0_xp, v0_yp, v1_xp, v1_yp;
024
025      if ( camera.perspective )
026      {
027         // calculate the perspective projection
028         v0_xp = ls.v[0].x / -ls.v[0].z;  // xp = xv / -zv
029         v0_yp = ls.v[0].y / -ls.v[0].z;  // yp = yv / -zv
030         v1_xp = ls.v[1].x / -ls.v[1].z;
031         v1_yp = ls.v[1].y / -ls.v[1].z;
032      }
033      else
034      {
035         // calculate the parallel projection instead
036         v0_xp = ls.v[0].x;
037         v0_yp = ls.v[0].y;
038         v1_xp = ls.v[1].x;
039         v1_yp = ls.v[1].y;
040      }
041
042      // Mutate the LineSegment object so that it refers to
043      // new Vertex objects containing the projected vertices.
044      ls.v[0] = new Vertex( v0_xp, v0_yp, -1, ls.v[0].getColor() );
045      ls.v[1] = new Vertex( v1_xp, v1_yp, -1, ls.v[1].getColor() );
046   }
047}