home *** CD-ROM | disk | FTP | other *** search
/ Graphics Programming Black Book (Special Edition) / BlackBook.bin / disk1 / source / chapter51 / l51-2.c < prev    next >
C/C++ Source or Header  |  1997-06-18  |  2KB  |  40 lines

  1. /* Transforms all vertices in the specified object into view space, then 
  2. perspective projects them to screen space and maps them to screen coordinates, 
  3. storing the results in the object.
  4. Tested with Borland C++ 4.02 in small model by Jim Mischel 12/16/94
  5. */
  6. #include <math.h>
  7. #include "polygon.h"/
  8.  
  9. void XformAndProjectPoints(double Xform[4][4],
  10.    struct Object * ObjectToXform)
  11. {
  12.    int i, NumPoints = ObjectToXform->NumVerts;
  13.    struct Point3 * Points = ObjectToXform->VertexList;
  14.    struct Point3 * XformedPoints = ObjectToXform->XformedVertexList;
  15.    struct Point3 * ProjectedPoints =
  16.          ObjectToXform->ProjectedVertexList;
  17.    struct Point * ScreenPoints = ObjectToXform->ScreenVertexList;
  18.  
  19.    for (i=0; i<NumPoints; i++, Points++, XformedPoints++,
  20.          ProjectedPoints++, ScreenPoints++) {
  21.       /* Transform to view space */
  22.       XformVec(Xform, (double *)Points, (double *)XformedPoints);
  23.       /* Perspective-project to screen space */
  24.       ProjectedPoints->X = XformedPoints->X / XformedPoints->Z *
  25.             PROJECTION_RATIO * (SCREEN_WIDTH / 2.0);
  26.       ProjectedPoints->Y = XformedPoints->Y / XformedPoints->Z *
  27.             PROJECTION_RATIO * (SCREEN_WIDTH / 2.0);
  28.       ProjectedPoints->Z = XformedPoints->Z;
  29.       /* Convert to screen coordinates. The Y coord is negated to
  30.          flip from increasing Y being up to increasing Y being down,
  31.          as expected by the polygon filler. Add in half the screen
  32.          width and height to center on the screen */
  33.       ScreenPoints->X = ((int) floor(ProjectedPoints->X + 0.5)) +
  34.                SCREEN_WIDTH/2;
  35.       ScreenPoints->Y = (-((int) floor(ProjectedPoints->Y + 0.5))) +
  36.                SCREEN_HEIGHT/2;
  37.    }
  38. }
  39.  
  40.