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

  1. /* Draws all visible faces (faces pointing toward the viewer) in the specified 
  2. object. The object must have previously been transformed and projected, so 
  3. that the ScreenVertexList array is filled in.
  4. Tested with Borland C++ 4.02 in small model by Jim Mischel 12/16/94.
  5. */
  6. #include "polygon.h"
  7.  
  8. void DrawVisibleFaces(struct Object * ObjectToXform)
  9. {
  10.    int i, j, NumFaces = ObjectToXform->NumFaces, NumVertices;
  11.    int * VertNumsPtr;
  12.    struct Face * FacePtr = ObjectToXform->FaceList;
  13.    struct Point * ScreenPoints = ObjectToXform->ScreenVertexList;
  14.    long v1,v2,w1,w2;
  15.    struct Point Vertices[MAX_POLY_LENGTH];
  16.    struct PointListHeader Polygon;
  17.  
  18.    /* Draw each visible face (polygon) of the object in turn */
  19.    for (i=0; i<NumFaces; i++, FacePtr++) {
  20.       NumVertices = FacePtr->NumVerts;
  21.       /* Copy over the face's vertices from the vertex list */
  22.       for (j=0, VertNumsPtr=FacePtr->VertNums; j<NumVertices; j++)
  23.          Vertices[j] = ScreenPoints[*VertNumsPtr++];
  24.       /* Draw only if outside face showing (if the normal to the
  25.          polygon points toward the viewer; that is, has a positive
  26.          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 page later */
  35.          for (j=0; j<NumVertices; j++) {
  36.             if (Vertices[j].X > EraseRect[NonDisplayedPage].Right)
  37.                if (Vertices[j].X < SCREEN_WIDTH)
  38.                   EraseRect[NonDisplayedPage].Right = Vertices[j].X;
  39.                else EraseRect[NonDisplayedPage].Right = SCREEN_WIDTH;
  40.             if (Vertices[j].Y > EraseRect[NonDisplayedPage].Bottom)
  41.                if (Vertices[j].Y < SCREEN_HEIGHT)
  42.                   EraseRect[NonDisplayedPage].Bottom = Vertices[j].Y;
  43.                else EraseRect[NonDisplayedPage].Bottom=SCREEN_HEIGHT;
  44.             if (Vertices[j].X < EraseRect[NonDisplayedPage].Left)
  45.                if (Vertices[j].X > 0)
  46.                   EraseRect[NonDisplayedPage].Left = Vertices[j].X;
  47.                else EraseRect[NonDisplayedPage].Left = 0;
  48.             if (Vertices[j].Y < EraseRect[NonDisplayedPage].Top)
  49.                if (Vertices[j].Y > 0)
  50.                   EraseRect[NonDisplayedPage].Top = Vertices[j].Y;
  51.                else EraseRect[NonDisplayedPage].Top = 0;
  52.          }
  53.          /* Draw the polygon */
  54.          DRAW_POLYGON(Vertices, NumVertices, FacePtr->Color, 0, 0);
  55.       }
  56.    }
  57. }
  58.  
  59.