home *** CD-ROM | disk | FTP | other *** search
/ Graphics Programming Black Book (Special Edition) / BlackBook.bin / disk1 / source / chapter50 / l50-3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-18  |  2.7 KB  |  55 lines

  1. /* Transforms convex polygon Poly (which has PolyLength vertices),
  2.    performing the transformation according to Xform (which generally
  3.    represents a transformation from object space through world space
  4.    to view space), then projects the transformed polygon onto the
  5.    screen and draws it in color Color. Also updates the extent of the
  6.    rectangle (EraseRect) that's used to erase the screen later.
  7.    Tested with Borland C++ 4.02 in small model by Jim Mischel 12/16/94.
  8. */
  9. #include "polygon.h"
  10.  
  11. void XformAndProjectPoly(double Xform[4][4], struct Point3 * Poly,
  12.    int PolyLength, int Color)
  13. {
  14.    int i;
  15.    struct Point3 XformedPoly[MAX_POLY_LENGTH];
  16.    struct Point ProjectedPoly[MAX_POLY_LENGTH];
  17.    struct PointListHeader Polygon;
  18.  
  19.    /* Transform to view space, then project to the screen */
  20.    for (i=0; i<PolyLength; i++) {
  21.       /* Transform to view space */
  22.       XformVec(Xform, (double *)&Poly[i], (double *)&XformedPoly[i]);
  23.       /* Project the X & Y coordinates to the screen, rounding to the
  24.          nearest integral coordinates. The Y coordinate is negated to
  25.          flip from view space, where increasing Y is up, to screen
  26.          space, where increasing Y is down. Add in half the screen
  27.          width and height to center on the screen */
  28.       ProjectedPoly[i].X = ((int) (XformedPoly[i].X/XformedPoly[i].Z *
  29.             PROJECTION_RATIO*(SCREEN_WIDTH/2.0)+0.5))+SCREEN_WIDTH/2;
  30.       ProjectedPoly[i].Y = ((int) (XformedPoly[i].Y/XformedPoly[i].Z *
  31.             -1.0 * PROJECTION_RATIO * (SCREEN_WIDTH / 2.0) + 0.5)) +
  32.             SCREEN_HEIGHT/2;
  33.       /* Appropriately adjust the extent of the rectangle used to
  34.          erase this page later */
  35.          if (ProjectedPoly[i].X > EraseRect[NonDisplayedPage].Right)
  36.           if (ProjectedPoly[i].X < SCREEN_WIDTH)
  37.             EraseRect[NonDisplayedPage].Right = ProjectedPoly[i].X;
  38.           else EraseRect[NonDisplayedPage].Right = SCREEN_WIDTH;
  39.          if (ProjectedPoly[i].Y > EraseRect[NonDisplayedPage].Bottom)
  40.           if (ProjectedPoly[i].Y < SCREEN_HEIGHT)
  41.             EraseRect[NonDisplayedPage].Bottom = ProjectedPoly[i].Y;
  42.           else EraseRect[NonDisplayedPage].Bottom = SCREEN_HEIGHT;
  43.          if (ProjectedPoly[i].X < EraseRect[NonDisplayedPage].Left)
  44.           if (ProjectedPoly[i].X > 0)
  45.             EraseRect[NonDisplayedPage].Left = ProjectedPoly[i].X;
  46.           else EraseRect[NonDisplayedPage].Left = 0;
  47.          if (ProjectedPoly[i].Y < EraseRect[NonDisplayedPage].Top)
  48.           if (ProjectedPoly[i].Y > 0)
  49.             EraseRect[NonDisplayedPage].Top = ProjectedPoly[i].Y;
  50.           else EraseRect[NonDisplayedPage].Top = 0;
  51.    }
  52.    /* Draw the polygon */
  53.    DRAW_POLYGON(ProjectedPoly, PolyLength, Color, 0, 0);
  54. }
  55.