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

  1. /* Initializes the cubes and adds them to the object list. */
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include "polygon.h"
  5.  
  6. #define ROT_6  (M_PI / 30.0)     /* rotate 6 degrees at a time */
  7. #define ROT_3  (M_PI / 60.0)     /* rotate 3 degrees at a time */
  8. #define ROT_2  (M_PI / 90.0)     /* rotate 2 degrees at a time */
  9. #define NUM_CUBES 12             /* # of cubes */
  10. Point3 CubeVerts[NUM_CUBE_VERTS]; /* set elsewhere, from floats */
  11. /* Vertex indices for individual cube faces */
  12. static int Face1[] = {1,3,2,0};
  13. static int Face2[] = {5,7,3,1};
  14. static int Face3[] = {4,5,1,0};
  15. static int Face4[] = {3,7,6,2};
  16. static int Face5[] = {5,4,6,7};
  17. static int Face6[] = {0,2,6,4};
  18. static int *VertNumList[]={Face1, Face2, Face3, Face4, Face5, Face6};
  19. static int VertsInFace[]={ sizeof(Face1)/sizeof(int),
  20.    sizeof(Face2)/sizeof(int), sizeof(Face3)/sizeof(int),
  21.    sizeof(Face4)/sizeof(int), sizeof(Face5)/sizeof(int),
  22.    sizeof(Face6)/sizeof(int) };
  23. /* X, Y, Z rotations for cubes */
  24. static RotateControl InitialRotate[NUM_CUBES] = {
  25.    {0.0,ROT_6,ROT_6},{ROT_3,0.0,ROT_3},{ROT_3,ROT_3,0.0},
  26.    {ROT_3,-ROT_3,0.0},{-ROT_3,ROT_2,0.0},{-ROT_6,-ROT_3,0.0},
  27.    {ROT_3,0.0,-ROT_6},{-ROT_2,0.0,ROT_3},{-ROT_3,0.0,-ROT_3},
  28.    {0.0,ROT_2,-ROT_2},{0.0,-ROT_3,ROT_3},{0.0,-ROT_6,-ROT_6},};
  29. static MoveControl InitialMove[NUM_CUBES] = {
  30.    {0,0,80,0,0,0,0,0,-350},{0,0,80,0,0,0,0,0,-350},
  31.    {0,0,80,0,0,0,0,0,-350},{0,0,80,0,0,0,0,0,-350},
  32.    {0,0,80,0,0,0,0,0,-350},{0,0,80,0,0,0,0,0,-350},
  33.    {0,0,80,0,0,0,0,0,-350},{0,0,80,0,0,0,0,0,-350},
  34.    {0,0,80,0,0,0,0,0,-350},{0,0,80,0,0,0,0,0,-350},
  35.    {0,0,80,0,0,0,0,0,-350},{0,0,80,0,0,0,0,0,-350}, };
  36. /* Face colors for various cubes */
  37. static int Colors[NUM_CUBES][NUM_CUBE_FACES] = {
  38.    {15,14,12,11,10,9},{1,2,3,4,5,6},{35,37,39,41,43,45},
  39.    {47,49,51,53,55,57},{59,61,63,65,67,69},{71,73,75,77,79,81},
  40.    {83,85,87,89,91,93},{95,97,99,101,103,105},
  41.    {107,109,111,113,115,117},{119,121,123,125,127,129},
  42.    {131,133,135,137,139,141},{143,145,147,149,151,153} };
  43. /* Starting coordinates for cubes in world space */
  44. static int CubeStartCoords[NUM_CUBES][3] = {
  45.    {100,0,-6000},  {100,70,-6000}, {100,-70,-6000}, {33,0,-6000},
  46.    {33,70,-6000},  {33,-70,-6000}, {-33,0,-6000},   {-33,70,-6000},
  47.    {-33,-70,-6000},{-100,0,-6000}, {-100,70,-6000}, {-100,-70,-6000}};
  48. /* Delay counts (speed control) for cubes */
  49. static int InitRDelayCounts[NUM_CUBES] = {1,2,1,2,1,1,1,1,1,2,1,1};
  50. static int BaseRDelayCounts[NUM_CUBES] = {1,2,1,2,2,1,1,1,2,2,2,1};
  51. static int InitMDelayCounts[NUM_CUBES] = {1,1,1,1,1,1,1,1,1,1,1,1};
  52. static int BaseMDelayCounts[NUM_CUBES] = {1,1,1,1,1,1,1,1,1,1,1,1};
  53.  
  54. void InitializeCubes()
  55. {
  56.    int i, j, k;
  57.    PObject *WorkingCube;
  58.  
  59.    for (i=0; i<NUM_CUBES; i++) {
  60.       if ((WorkingCube = malloc(sizeof(PObject))) == NULL) {
  61.          printf("Couldn't get memory\n"); exit(1); }
  62.       WorkingCube->DrawFunc = DrawPObject;
  63.       WorkingCube->RecalcFunc = XformAndProjectPObject;
  64.       WorkingCube->MoveFunc = RotateAndMovePObject;
  65.       WorkingCube->RecalcXform = 1;
  66.       for (k=0; k<2; k++) {
  67.          WorkingCube->EraseRect[k].Left =
  68.             WorkingCube->EraseRect[k].Top = 0x7FFF;
  69.          WorkingCube->EraseRect[k].Right = 0;
  70.          WorkingCube->EraseRect[k].Bottom = 0;
  71.       }
  72.       WorkingCube->RDelayCount = InitRDelayCounts[i];
  73.       WorkingCube->RDelayCountBase = BaseRDelayCounts[i];
  74.       WorkingCube->MDelayCount = InitMDelayCounts[i];
  75.       WorkingCube->MDelayCountBase = BaseMDelayCounts[i];
  76.       /* Set the object->world xform to none */
  77.       for (j=0; j<3; j++)
  78.          for (k=0; k<4; k++)
  79.             WorkingCube->XformToWorld[j][k] = INT_TO_FIXED(0);
  80.       WorkingCube->XformToWorld[0][0] = 
  81.          WorkingCube->XformToWorld[1][1] =
  82.          WorkingCube->XformToWorld[2][2] =
  83.          WorkingCube->XformToWorld[3][3] = INT_TO_FIXED(1);
  84.       /* Set the initial location */
  85.       for (j=0; j<3; j++) WorkingCube->XformToWorld[j][3] =
  86.             INT_TO_FIXED(CubeStartCoords[i][j]);
  87.       WorkingCube->NumVerts = NUM_CUBE_VERTS;
  88.       WorkingCube->VertexList = CubeVerts;
  89.       WorkingCube->NumFaces = NUM_CUBE_FACES;
  90.       WorkingCube->Rotate = InitialRotate[i];
  91.       WorkingCube->Move.MoveX = INT_TO_FIXED(InitialMove[i].MoveX);
  92.       WorkingCube->Move.MoveY = INT_TO_FIXED(InitialMove[i].MoveY);
  93.       WorkingCube->Move.MoveZ = INT_TO_FIXED(InitialMove[i].MoveZ);
  94.       WorkingCube->Move.MinX = INT_TO_FIXED(InitialMove[i].MinX);
  95.       WorkingCube->Move.MinY = INT_TO_FIXED(InitialMove[i].MinY);
  96.       WorkingCube->Move.MinZ = INT_TO_FIXED(InitialMove[i].MinZ);
  97.       WorkingCube->Move.MaxX = INT_TO_FIXED(InitialMove[i].MaxX);
  98.       WorkingCube->Move.MaxY = INT_TO_FIXED(InitialMove[i].MaxY);
  99.       WorkingCube->Move.MaxZ = INT_TO_FIXED(InitialMove[i].MaxZ);
  100.       if ((WorkingCube->XformedVertexList =
  101.             malloc(NUM_CUBE_VERTS*sizeof(Point3))) == NULL) {
  102.          printf("Couldn't get memory\n"); exit(1); }
  103.       if ((WorkingCube->ProjectedVertexList =
  104.             malloc(NUM_CUBE_VERTS*sizeof(Point3))) == NULL) {
  105.          printf("Couldn't get memory\n"); exit(1); }
  106.       if ((WorkingCube->ScreenVertexList =
  107.             malloc(NUM_CUBE_VERTS*sizeof(Point))) == NULL) {
  108.          printf("Couldn't get memory\n"); exit(1); }
  109.       if ((WorkingCube->FaceList =
  110.             malloc(NUM_CUBE_FACES*sizeof(Face))) == NULL) {
  111.          printf("Couldn't get memory\n"); exit(1); }
  112.       /* Initialize the faces */
  113.       for (j=0; j<NUM_CUBE_FACES; j++) {
  114.          WorkingCube->FaceList[j].VertNums = VertNumList[j];
  115.          WorkingCube->FaceList[j].NumVerts = VertsInFace[j];
  116.          WorkingCube->FaceList[j].Color = Colors[i][j];
  117.       }
  118.       ObjectList[NumObjects++] = (Object *)WorkingCube;
  119.    }
  120. }
  121.