home *** CD-ROM | disk | FTP | other *** search
/ Graphics Programming Black Book (Special Edition) / BlackBook.bin / disk1 / source / chapter52 / xsharp14.exe / POLYGON.H < prev    next >
Text File  |  1991-12-05  |  5KB  |  98 lines

  1. /* POLYGON.H: Header file for polygon-filling code, also includes
  2.    a number of useful items for 3D animation. */
  3. #define MAX_OBJECTS  100   /* max simultaneous # objects supported */
  4. #define MAX_POLY_LENGTH 4  /* four vertices is the max per poly */
  5. #define SCREEN_WIDTH 320
  6. #define SCREEN_HEIGHT 240
  7. #define PAGE0_START_OFFSET 0
  8. #define PAGE1_START_OFFSET (((long)SCREEN_HEIGHT*SCREEN_WIDTH)/4)
  9. #define NUM_CUBE_VERTS 8              /* # of vertices per cube */
  10. #define NUM_CUBE_FACES 6              /* # of faces per cube */
  11. /* Ratio: distance from viewpoint to projection plane / width of
  12.    projection plane. Defines the width of the field of view. Lower
  13.    absolute values = wider fields of view; higher values = narrower */
  14. #define PROJECTION_RATIO -2.0 /* negative because visible Z
  15.                                  coordinates are negative */
  16. /* Draws the polygon described by the point list PointList in color
  17.    Color with all vertices offset by (X,Y) */
  18. #define DRAW_POLYGON(PointList,NumPoints,Color,X,Y)          \
  19.    Polygon.Length = NumPoints; Polygon.PointPtr = PointList; \
  20.    FillConvexPolygon(&Polygon, Color, X, Y);
  21. #define INT_TO_FIXED(x) (((long)(int)x) << 16)
  22. #define DOUBLE_TO_FIXED(x) ((long) (x * 65536.0 + 0.5))
  23.  
  24. typedef long Fixedpoint;
  25. typedef Fixedpoint Xform[3][4];
  26. /* Describes a single 2D point */
  27. typedef struct { int X; int Y; } Point;
  28. /* Describes a single 3D point in homogeneous coordinates; the W
  29.    coordinate isn't present, though; assumed to be 1 and implied */
  30. typedef struct { Fixedpoint X, Y, Z; } Point3;
  31. typedef struct { int X; int Y; int Z; } IntPoint3;
  32. /* Describes a series of points (used to store a list of vertices that
  33.    describe a polygon; each vertex is assumed to connect to the two
  34.    adjacent vertices; last vertex is assumed to connect to first) */
  35. typedef struct { int Length; Point * PointPtr; } PointListHeader;
  36. /* Describes the beginning and ending X coordinates of a single
  37.    horizontal line */
  38. typedef struct { int XStart; int XEnd; } HLine;
  39. /* Describes a Length-long series of horizontal lines, all assumed to
  40.    be on contiguous scan lines starting at YStart and proceeding
  41.    downward (used to describe a scan-converted polygon to the
  42.    low-level hardware-dependent drawing code) */
  43. typedef struct { int Length; int YStart; HLine * HLinePtr;} HLineList;
  44. typedef struct { int Left, Top, Right, Bottom; } Rect;
  45. /* Structure describing one face of an object (one polygon) */
  46. typedef struct { int * VertNums; int NumVerts; int Color; }  Face;
  47. typedef struct { double RotateX, RotateY, RotateZ; } RotateControl;
  48. typedef struct { Fixedpoint MoveX, MoveY, MoveZ, MinX, MinY, MinZ,
  49.    MaxX, MaxY, MaxZ; } MoveControl;
  50. /* Fields common to every object */
  51. #define BASE_OBJECT                                              \
  52.    void (*DrawFunc)();     /* draws object */                    \
  53.    void (*RecalcFunc)();   /* prepares object for drawing */     \
  54.    void (*MoveFunc)();     /* moves object */                    \
  55.    int RecalcXform;        /* 1 to indicate need to recalc */    \
  56.    Rect EraseRect[2];      /* rectangle to erase in each page */
  57. /* Basic object */
  58. typedef struct { BASE_OBJECT } Object;
  59. /* Structure describing a polygon-based object */
  60. typedef struct {
  61.    BASE_OBJECT
  62.    int RDelayCount, RDelayCountBase; /* controls rotation speed */
  63.    int MDelayCount, MDelayCountBase; /* controls movement speed */
  64.    Xform XformToWorld;        /* transform from object->world space */
  65.    Xform XformToView;         /* transform from object->view space */
  66.    RotateControl Rotate;      /* controls rotation change over time */
  67.    MoveControl Move;          /* controls object movement over time */
  68.    int NumVerts;              /* # vertices in VertexList */
  69.    Point3 * VertexList;       /* untransformed vertices */
  70.    Point3 * XformedVertexList;   /* transformed into view space */
  71.    Point3 * ProjectedVertexList; /* projected into screen space */
  72.    Point * ScreenVertexList;     /* converted to screen coordinates */
  73.    int NumFaces;              /* # of faces in object */
  74.    Face * FaceList;           /* pointer to face info */
  75. } PObject;
  76.  
  77. extern void XformVec(Xform, Fixedpoint *, Fixedpoint *);
  78. extern void ConcatXforms(Xform, Xform, Xform);
  79. extern int FillConvexPolygon(PointListHeader *, int, int, int);
  80. extern void Set320x240Mode(void);
  81. extern void ShowPage(unsigned int);
  82. extern void FillRectangleX(int, int, int, int, unsigned int, int);
  83. extern void XformAndProjectPObject(PObject *);
  84. extern void DrawPObject(PObject *);
  85. extern void AppendRotationX(Xform, double);
  86. extern void AppendRotationY(Xform, double);
  87. extern void AppendRotationZ(Xform, double);
  88. extern near Fixedpoint FixedMul(Fixedpoint, Fixedpoint);
  89. extern near Fixedpoint FixedDiv(Fixedpoint, Fixedpoint);
  90. extern void InitializeFixedPoint(void);
  91. extern void RotateAndMovePObject(PObject *);
  92. extern void InitializeCubes(void);
  93. extern int DisplayedPage, NonDisplayedPage, RecalcAllXforms;
  94. extern int NumObjects;
  95. extern Xform WorldViewXform;
  96. extern Object *ObjectList[];
  97. extern Point3 CubeVerts[];
  98.