home *** CD-ROM | disk | FTP | other *** search
/ Graphics Programming Black Book (Special Edition) / BlackBook.bin / disk1 / source / chapter57 / xsharp21.exe / INITBALL.C < prev    next >
Text File  |  1992-06-10  |  13KB  |  311 lines

  1. /* Initializes the ball(s) and adds them to the object list. */
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include "polygon.h"
  5.  
  6. #define NUM_BALLS 1              /* # of balls to initialize */
  7.  
  8. #include "balvert0.inc" /* face and vertex definitions for ball */
  9.  
  10. /* X, Y, Z rotations for balls, in tenths of degrees (angles) */
  11. static RotateControl InitialRotate[NUM_BALLS] = {
  12.    {0,10,0},};
  13.  
  14. /* Face colors for balls, in RGB forms, with each color component expressed
  15.    as a fraction of full intensity in the range 0.0 to 1.0 */
  16. static ModelColor Colors[NUM_BALLS][NUM_FACES] = {
  17.   {{CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  18.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  19.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  20.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  21.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  22.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  23.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  24.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  25.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  26.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  27.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  28.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  29.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  30.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  31.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  32.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  33.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  34.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  35.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  36.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  37.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  38.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  39.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  40.    {CCOMP(1),CCOMP(1),CCOMP(0)}, {CCOMP(0),CCOMP(0),CCOMP(0)},
  41.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  42.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  43.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  44.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  45.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  46.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  47.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  48.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  49.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  50.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  51.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)},
  52.    {CCOMP(1),CCOMP(1),CCOMP(1)}, {CCOMP(1),CCOMP(1),CCOMP(1)}},
  53. };
  54.  
  55. /* Shading types for faces of balls */
  56. static int FaceShadings[NUM_BALLS][NUM_FACES] = {
  57.    DIFFUSE_SHADING | AMBIENT_SHADING,
  58.    DIFFUSE_SHADING | AMBIENT_SHADING,
  59.    DIFFUSE_SHADING | AMBIENT_SHADING,
  60.    DIFFUSE_SHADING | AMBIENT_SHADING,
  61.    DIFFUSE_SHADING | AMBIENT_SHADING,
  62.    DIFFUSE_SHADING | AMBIENT_SHADING,
  63.    DIFFUSE_SHADING | AMBIENT_SHADING,
  64.    DIFFUSE_SHADING | AMBIENT_SHADING,
  65.    DIFFUSE_SHADING | AMBIENT_SHADING,
  66.    DIFFUSE_SHADING | AMBIENT_SHADING,
  67.    DIFFUSE_SHADING | AMBIENT_SHADING,
  68.    DIFFUSE_SHADING | AMBIENT_SHADING,
  69.    DIFFUSE_SHADING | AMBIENT_SHADING,
  70.    DIFFUSE_SHADING | AMBIENT_SHADING,
  71.    DIFFUSE_SHADING | AMBIENT_SHADING,
  72.    DIFFUSE_SHADING | AMBIENT_SHADING,
  73.    DIFFUSE_SHADING | AMBIENT_SHADING,
  74.    DIFFUSE_SHADING | AMBIENT_SHADING,
  75.    DIFFUSE_SHADING | AMBIENT_SHADING,
  76.    DIFFUSE_SHADING | AMBIENT_SHADING,
  77.    DIFFUSE_SHADING | AMBIENT_SHADING,
  78.    DIFFUSE_SHADING | AMBIENT_SHADING,
  79.    DIFFUSE_SHADING | AMBIENT_SHADING,
  80.    DIFFUSE_SHADING | AMBIENT_SHADING,
  81.    DIFFUSE_SHADING | AMBIENT_SHADING,
  82.    DIFFUSE_SHADING | AMBIENT_SHADING,
  83.    DIFFUSE_SHADING | AMBIENT_SHADING,
  84.    DIFFUSE_SHADING | AMBIENT_SHADING,
  85.    DIFFUSE_SHADING | AMBIENT_SHADING,
  86.    DIFFUSE_SHADING | AMBIENT_SHADING,
  87.    DIFFUSE_SHADING | AMBIENT_SHADING,
  88.    DIFFUSE_SHADING | AMBIENT_SHADING,
  89.    DIFFUSE_SHADING | AMBIENT_SHADING,
  90.    DIFFUSE_SHADING | AMBIENT_SHADING,
  91.    DIFFUSE_SHADING | AMBIENT_SHADING,
  92.    DIFFUSE_SHADING | AMBIENT_SHADING,
  93.    DIFFUSE_SHADING | AMBIENT_SHADING,
  94.    DIFFUSE_SHADING | AMBIENT_SHADING,
  95.    DIFFUSE_SHADING | AMBIENT_SHADING,
  96.    TEXTURE_MAPPED_SHADING,
  97.    TEXTURE_MAPPED_SHADING,
  98.    DIFFUSE_SHADING | AMBIENT_SHADING,
  99.    DIFFUSE_SHADING | AMBIENT_SHADING,
  100.    DIFFUSE_SHADING | AMBIENT_SHADING,
  101.    DIFFUSE_SHADING | AMBIENT_SHADING,
  102.    DIFFUSE_SHADING | AMBIENT_SHADING,
  103.    NO_SHADING,
  104.    TEXTURE_MAPPED_SHADING,
  105.    DIFFUSE_SHADING | AMBIENT_SHADING,
  106.    DIFFUSE_SHADING | AMBIENT_SHADING,
  107.    DIFFUSE_SHADING | AMBIENT_SHADING,
  108.    DIFFUSE_SHADING | AMBIENT_SHADING,
  109.    DIFFUSE_SHADING | AMBIENT_SHADING,
  110.    DIFFUSE_SHADING | AMBIENT_SHADING,
  111.    DIFFUSE_SHADING | AMBIENT_SHADING,
  112.    DIFFUSE_SHADING | AMBIENT_SHADING,
  113.    DIFFUSE_SHADING | AMBIENT_SHADING,
  114.    DIFFUSE_SHADING | AMBIENT_SHADING,
  115.    DIFFUSE_SHADING | AMBIENT_SHADING,
  116.    DIFFUSE_SHADING | AMBIENT_SHADING,
  117.    DIFFUSE_SHADING | AMBIENT_SHADING,
  118.    DIFFUSE_SHADING | AMBIENT_SHADING,
  119.    DIFFUSE_SHADING | AMBIENT_SHADING,
  120.    DIFFUSE_SHADING | AMBIENT_SHADING,
  121.    DIFFUSE_SHADING | AMBIENT_SHADING,
  122.    DIFFUSE_SHADING | AMBIENT_SHADING,
  123.    DIFFUSE_SHADING | AMBIENT_SHADING,
  124.    DIFFUSE_SHADING | AMBIENT_SHADING,
  125.    DIFFUSE_SHADING | AMBIENT_SHADING,
  126.    DIFFUSE_SHADING | AMBIENT_SHADING,
  127.    DIFFUSE_SHADING | AMBIENT_SHADING,
  128.    DIFFUSE_SHADING | AMBIENT_SHADING,
  129. };
  130.  
  131. /* Bear bitmap for texture mapping. Note: texture bitmaps must conform
  132.    to the pixel type of the destination; they are not device independent */
  133. char BearBits[] = {
  134.    60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,
  135.    60,60,20,20,20,60,60,60,60,60,60,60,60,60,60,20,20,20,60,60,
  136.    60,20,20,20,20,20,60,60,60,60,60,60,60,60,20,20,20,20,20,60,
  137.    60,20,20,20,20,20,60,60,20,20,20,20,60,60,20,20,20,20,20,60,
  138.    60,20,20,20,20,20,60,20,20,20,20,20,20,60,20,20,20,20,20,60,
  139.    60,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,60,
  140.    60,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,60,
  141.    60,60,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,60,60,
  142.    60,60,60,20,20,20, 3, 3, 3,20,20, 3, 3, 3,20,20,20,60,60,60,
  143.    60,60,20,20,20,20, 3, 3, 3,20,20, 3, 3, 3,20,20,20,20,60,60,
  144.    60,60,20,20,20,20, 3, 3, 3,20,20, 3, 3, 3,20,20,20,20,60,60,
  145.    60,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,60,
  146.    60,20,20,20,20,20,20,20, 0, 0, 0, 0,20,20,20,20,20,20,20,60,
  147.    60,20,20,20,20,20,20,20, 0, 0, 0, 0,20,20,20,20,20,20,20,60,
  148.    60,20,20,20,20,20,20,20, 0, 0, 0, 0,20,20,20,20,20,20,20,60,
  149.    60,20,20,20,20,20,20,20, 0, 0, 0, 0,20,20,20,20,20,20,20,60,
  150.    60,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,60,
  151.    60,60,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,60,60,
  152.    60,60,60,20,20,20,20,20,20,20,20,20,20,20,20,20,20,60,60,60,
  153.    60,60,60,60,60,20,20,20,20,20,20,20,20,20,20,60,60,60,60,60,
  154. };
  155.  
  156. /* Pattern bitmap for texture mapping. Note: texture bitmaps must conform
  157.    to the pixel type of the destination; they are not device independent. */
  158. char PatBits[] = {
  159.    12,12,12,12,12,12,12,12,12,
  160.    12,12, 3, 3, 3, 3,12,12,12,
  161.    12, 3,12,12,12,12, 3,12,12,
  162.    12,12, 3,12,12, 3,12,12,12,
  163.    12,12,12, 3, 3,12,12,12,12,
  164.    12,12,12,12,12,12,12,12,12,
  165. };
  166.  
  167. /* Descriptor for bear texture mapping bitmap */
  168. TextureMap BearTex = { 20, BearBits };
  169.  
  170. /* Descriptor for bear texture mapping bitmap */
  171. TextureMap PatTex = { 9, PatBits };
  172.  
  173. /* Vertices for first face's area of BearBits. Note that the texture bitmap
  174.    is set up with a boundary row column around it; this is so that if fixed-
  175.    point imprecision causes the source pointer to wander just off the
  176.    bitmap, appropriate pixels will be found (the boundary pixels are
  177.    duplicates of their neighbors) */
  178. Point BearVerts1[] = {
  179.    { 0, 0 }, {19, 0}, {19, 19}, {0, 19}
  180. };
  181.  
  182. /* Vertices for first face's area of PatBits */
  183. Point PatVerts1[] = {
  184.    { 0, 0 }, {4, 0}, {4, 5}, {0, 5}
  185. };
  186.  
  187. /* Vertices for second face's area of PatBits */
  188. Point PatVerts2[] = {
  189.    { 4, 0 }, {8, 0}, {8, 5}, {4, 5}
  190. };
  191.  
  192. typedef struct {
  193.    FaceNumber;       /* index of face with the following texture */
  194.    TextureMap * TexMap; /* pointer to bitmap for texture mapping, if any */
  195.    Point * TexVerts; /* pointer to list of this polygon's vertices, in
  196.                         TextureMap coordinates. Index n must map to index
  197.                         n + 1 in VertNums, (the + 1 is to skip over the unit
  198.                         normal endpoint in VertNums) */
  199. } TexMap;
  200.  
  201. TexMap TexMaps[] = {
  202.    { 39, &PatTex,  PatVerts1 },
  203.    { 40, &PatTex,  PatVerts2 },
  204.    { 47, &BearTex, BearVerts1 },
  205. };
  206.  
  207. #define NUM_TEX_MAPS (sizeof(TexMaps)/sizeof(TexMap))
  208.  
  209. /* Starting coordinates for balls in world space */
  210. static int BallStartCoords[NUM_BALLS][3] = {
  211.    {0,0,-300},
  212. };
  213. /* Delay counts (speed control) for balls */
  214. static int InitRDelayCounts[NUM_BALLS] = {1};
  215. static int BaseRDelayCounts[NUM_BALLS] = {1};
  216. static int InitMDelayCounts[NUM_BALLS] = {1};
  217. static int BaseMDelayCounts[NUM_BALLS] = {1};
  218.  
  219. void InitializeBalls()
  220. {
  221.    int i, j, k, TexMapFound;
  222.    PObject *WorkingBall;
  223.  
  224.    for (i=0; i<NUM_BALLS; i++) {
  225.       if ((WorkingBall = malloc(sizeof(PObject))) == NULL) {
  226.          printf("Couldn't get memory\n"); exit(1); }
  227.       WorkingBall->DrawFunc = DrawPObject;
  228.       WorkingBall->RecalcFunc = XformAndProjectPObject;
  229.       WorkingBall->MoveFunc = RotateAndMoveBall;
  230.       WorkingBall->RecalcXform = 1;
  231.       for (k=0; k<2; k++) {
  232.          WorkingBall->EraseRect[k].Left =
  233.             WorkingBall->EraseRect[k].Top = 0x7FFF;
  234.          WorkingBall->EraseRect[k].Right = 0;
  235.          WorkingBall->EraseRect[k].Bottom = 0;
  236.       }
  237.       WorkingBall->RDelayCount = InitRDelayCounts[i];
  238.       WorkingBall->RDelayCountBase = BaseRDelayCounts[i];
  239.       WorkingBall->MDelayCount = InitMDelayCounts[i];
  240.       WorkingBall->MDelayCountBase = BaseMDelayCounts[i];
  241.       /* Set the object->world xform to none */
  242.       for (j=0; j<3; j++)
  243.          for (k=0; k<4; k++)
  244.             WorkingBall->XformToWorld[j][k] = INT_TO_FIXED(0);
  245.       WorkingBall->XformToWorld[0][0] = 
  246.          WorkingBall->XformToWorld[1][1] =
  247.          WorkingBall->XformToWorld[2][2] = INT_TO_FIXED(1);
  248.       /* Set the initial location */
  249.       for (j=0; j<3; j++) {
  250.           WorkingBall->XformToWorld[j][3] =
  251.                 INT_TO_FIXED(BallStartCoords[i][j]);
  252.       }
  253.       /* Initial center coordinates */
  254.       WorkingBall->CenterInView.X = WorkingBall->XformToWorld[0][3];
  255.       WorkingBall->CenterInView.Y = WorkingBall->XformToWorld[1][3];
  256.       WorkingBall->CenterInView.Z = WorkingBall->XformToWorld[2][3];
  257.       WorkingBall->NumVerts = NUM_VERTS;
  258.       WorkingBall->NumRealVerts = NUM_REAL_VERTS;
  259.       WorkingBall->VertexList = Verts;
  260.       WorkingBall->NumFaces = NUM_FACES;
  261.       WorkingBall->Rotate = InitialRotate[i];
  262.       if ((WorkingBall->XformedVertexList =
  263.             malloc(NUM_VERTS*sizeof(Point3))) == NULL) {
  264.          printf("Couldn't get memory\n"); exit(1); }
  265.       if ((WorkingBall->ProjectedVertexList =
  266.             malloc(NUM_VERTS*sizeof(Point3))) == NULL) {
  267.          printf("Couldn't get memory\n"); exit(1); }
  268.       if ((WorkingBall->ScreenVertexList =
  269.             malloc(NUM_VERTS*sizeof(Point))) == NULL) {
  270.          printf("Couldn't get memory\n"); exit(1); }
  271.       if ((WorkingBall->FaceList =
  272.             malloc(NUM_FACES*sizeof(Face))) == NULL) {
  273.          printf("Couldn't get memory\n"); exit(1); }
  274.       /* Initialize the faces */
  275.       for (j=0; j<NUM_FACES; j++) {
  276.          WorkingBall->FaceList[j].VertNums = VertNumList[j];
  277.          WorkingBall->FaceList[j].NumVerts = VertsInFace[j];
  278.          WorkingBall->FaceList[j].FullColor.Red = Colors[i][j].Red;
  279.          WorkingBall->FaceList[j].FullColor.Green = Colors[i][j].Green;
  280.          WorkingBall->FaceList[j].FullColor.Blue = Colors[i][j].Blue;
  281.          if ((WorkingBall->FaceList[j].ShadingType = FaceShadings[i][j])
  282.                == NO_SHADING) {
  283.             /* If no shading is in effect, convert the color from the model
  284.                color to a color index, which we can then use directly */
  285.             WorkingBall->FaceList[j].ColorIndex =
  286.                   ModelColorToColorIndex(&Colors[i][j]);
  287.  
  288.          } else if (WorkingBall->FaceList[j].ShadingType ==
  289.                TEXTURE_MAPPED_SHADING) {
  290.  
  291.             /* Search TexMaps for this face's texture mapping info */
  292.             TexMapFound = 0;
  293.             for (k=0; (k<NUM_TEX_MAPS) && !TexMapFound; k++) {
  294.                if (TexMaps[k].FaceNumber == j) {
  295.                   WorkingBall->FaceList[j].TexMap = TexMaps[k].TexMap;
  296.                   WorkingBall->FaceList[j].TexVerts = TexMaps[k].TexVerts;
  297.                   TexMapFound = 1;
  298.                }
  299.             }
  300.  
  301.             if (!TexMapFound) {
  302.                printf("Error: Texture mapping not found for face %d", j);
  303.                exit(1);
  304.             }
  305.  
  306.          }
  307.       }
  308.       AddObject((Object *)WorkingBall);
  309.    }
  310. }
  311.