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

  1. /* Draws all visible faces in specified polygon-based object. Object must have 
  2. previously been transformed and projected, so that ScreenVertexList array is 
  3. filled in.
  4. Tested with Borland C++ 4.02 in small model by Jim Mischel 12/16/94.
  5. */
  6.  
  7. #include "polygon.h"
  8.  
  9. void DrawPObject(PObject * ObjectToXform)
  10. {
  11.    int i, j, NumFaces = ObjectToXform->NumFaces, NumVertices;
  12.    int * VertNumsPtr;
  13.    Face * FacePtr = ObjectToXform->FaceList;
  14.    Point * ScreenPoints = ObjectToXform->ScreenVertexList;
  15.    long v1, v2, w1, w2;
  16.    Point Vertices[MAX_POLY_LENGTH];
  17.    PointListHeader Polygon;
  18.  
  19.    /* Draw each visible face (polygon) of the object in turn */
  20.    for (i=0; i<NumFaces; i++, FacePtr++) {
  21.       NumVertices = FacePtr->NumVerts;
  22.       /* Copy over the face's vertices from the vertex list */
  23.       for (j=0, VertNumsPtr=FacePtr->VertNums; j<NumVertices; j++)
  24.          Vertices[j] = ScreenPoints[*VertNumsPtr++];
  25.       /* Draw only if outside face showing (if the normal to the
  26.          polygon points toward viewer; that is, has a positive Z component) */
  27.       v1 = Vertices[1].X - Vertices[0].X;
  28.       w1 = Vertices[NumVertices-1].X - Vertices[0].X;
  29.       v2 = Vertices[1].Y - Vertices[0].Y;
  30.       w2 = Vertices[NumVertices-1].Y - Vertices[0].Y;
  31.       if ((v1*w2 - v2*w1) > 0) {
  32.          /* It is facing the screen, so draw */
  33.          /* Appropriately adjust the extent of the rectangle used to
  34.             erase this object later */
  35.          for (j=0; j<NumVertices; j++) {
  36.             if (Vertices[j].X >
  37.                   ObjectToXform->EraseRect[NonDisplayedPage].Right)
  38.                if (Vertices[j].X < SCREEN_WIDTH)
  39.                   ObjectToXform->EraseRect[NonDisplayedPage].Right =
  40.                         Vertices[j].X;
  41.                else ObjectToXform->EraseRect[NonDisplayedPage].Right =
  42.                      SCREEN_WIDTH;
  43.             if (Vertices[j].Y >
  44.                   ObjectToXform->EraseRect[NonDisplayedPage].Bottom)
  45.                if (Vertices[j].Y < SCREEN_HEIGHT)
  46.                   ObjectToXform->EraseRect[NonDisplayedPage].Bottom =
  47.                         Vertices[j].Y;
  48.                else ObjectToXform->EraseRect[NonDisplayedPage].Bottom=
  49.                      SCREEN_HEIGHT;
  50.             if (Vertices[j].X <
  51.                   ObjectToXform->EraseRect[NonDisplayedPage].Left)
  52.                if (Vertices[j].X > 0)
  53.                   ObjectToXform->EraseRect[NonDisplayedPage].Left =
  54.                         Vertices[j].X;
  55.                else ObjectToXform->EraseRect[NonDisplayedPage].Left=0;
  56.             if (Vertices[j].Y <
  57.                   ObjectToXform->EraseRect[NonDisplayedPage].Top)
  58.                if (Vertices[j].Y > 0)
  59.                   ObjectToXform->EraseRect[NonDisplayedPage].Top =
  60.                         Vertices[j].Y;
  61.                else ObjectToXform->EraseRect[NonDisplayedPage].Top=0;
  62.          }
  63.          /* Draw the polygon */
  64.          DRAW_POLYGON(Vertices, NumVertices, FacePtr->Color, 0, 0);
  65.       }
  66.    }
  67. }
  68.  
  69.