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

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