home *** CD-ROM | disk | FTP | other *** search
/ Graphics Programming Black Book (Special Edition) / BlackBook.bin / disk1 / source / chapter52 / xsharp14.exe / L2.C < prev    next >
Text File  |  1991-12-01  |  2KB  |  45 lines

  1. /* Transforms all vertices in the specified polygon-based object into
  2.    view space, then perspective projects them to screen space and maps
  3.    them to screen coordinates, storing the results in the object.
  4.    Recalculates object->view transformation because only if the
  5.    transform changes would we bother to retransform the vertices. */
  6. #include <math.h>
  7. #include "polygon.h"
  8.  
  9. void XformAndProjectPObject(PObject * ObjectToXform)
  10. {
  11.    int i, NumPoints = ObjectToXform->NumVerts;
  12.    Point3 * Points = ObjectToXform->VertexList;
  13.    Point3 * XformedPoints = ObjectToXform->XformedVertexList;
  14.    Point3 * ProjectedPoints =
  15.          ObjectToXform->ProjectedVertexList;
  16.    Point * ScreenPoints = ObjectToXform->ScreenVertexList;
  17.  
  18.    /* Recalculate the object->view transform */
  19.    ConcatXforms(WorldViewXform, ObjectToXform->XformToWorld,
  20.          ObjectToXform->XformToView);
  21.    /* Apply that new transformation and project the points */
  22.    for (i=0; i<NumPoints; i++, Points++, XformedPoints++,
  23.          ProjectedPoints++, ScreenPoints++) {
  24.       /* Transform to view space */
  25.       XformVec(ObjectToXform->XformToView, (Fixedpoint *) Points,
  26.             (Fixedpoint *) XformedPoints);
  27.       /* Perspective-project to screen space */
  28.       ProjectedPoints->X =
  29.             FixedMul(FixedDiv(XformedPoints->X, XformedPoints->Z),
  30.             DOUBLE_TO_FIXED(PROJECTION_RATIO * (SCREEN_WIDTH/2)));
  31.       ProjectedPoints->Y =
  32.             FixedMul(FixedDiv(XformedPoints->Y, XformedPoints->Z),
  33.             DOUBLE_TO_FIXED(PROJECTION_RATIO * (SCREEN_WIDTH/2)));
  34.       ProjectedPoints->Z = XformedPoints->Z;
  35.       /* Convert to screen coordinates. The Y coord is negated to
  36.          flip from increasing Y being up to increasing Y being down,
  37.          as expected by the polygon filler. Add in half the screen
  38.          width and height to center on the screen */
  39.       ScreenPoints->X = ((int) ((ProjectedPoints->X +
  40.             DOUBLE_TO_FIXED(0.5)) >> 16)) + SCREEN_WIDTH/2;
  41.       ScreenPoints->Y = (-((int) ((ProjectedPoints->Y +
  42.             DOUBLE_TO_FIXED(0.5)) >> 16))) + SCREEN_HEIGHT/2;
  43.    }
  44. }
  45.