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

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