home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CreatingGames / Utilities / Misc / GMS / GMSDev / Source / C / 3DObjects / Sphere.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-12-16  |  4.2 KB  |  162 lines

  1. /* SAS/C: sc Sphere.c opt math=standard INCDIR=INCLUDES:
  2. **
  3. ** This is a demo of a spinning sphere consisting entirely of dots.  The
  4. ** object is pre-calculated then rotated in real time for speed, although
  5. ** things could be much faster than this (use integer math).
  6. */
  7.  
  8. #include <proto/dpkernel.h>
  9. #include <math.h>
  10.  
  11. BYTE *ProgName      = "Spinning Sphere";
  12. BYTE *ProgAuthor    = "Paul Manias";
  13. BYTE *ProgDate      = "December 1997";
  14. BYTE *ProgCopyright = "DreamWorld Productions (c) 1996-1997.  Freely distributable.";
  15. BYTE *ProgShort     = "3-Dimensional object demonstration.";
  16.  
  17. struct GScreen *screen;
  18. struct JoyData *jport1;
  19.  
  20. void Demo(void);
  21.  
  22. struct DotPixel { double X,Y,Z; };
  23.  
  24. #define AMTCOLOURS 32
  25.  
  26. LONG palette[AMTCOLOURS+2] = {
  27.   PALETTE,32,
  28.   0x000000L,0x101010L,0x171717L,0x202020L,0x272727L,0x303030L,0x373737L,0x404040L,
  29.   0x474747L,0x505050L,0x575757L,0x606060L,0x676767L,0x707070L,0x777777L,0x808080L,
  30.   0x878787L,0x909090L,0x979797L,0xa0a0a0L,0xa7a7a7L,0xb0b0b0L,0xb7b7b7L,0xc0c0c0L,
  31.   0xc7c7c7L,0xd0d0d0L,0xd7d7d7L,0xe0e0e0L,0xe0e0e0L,0xf0f0f0L,0xf7f7f7L,0xffffffL
  32. };
  33.  
  34. /***********************************************************************************/
  35.  
  36. void main(void)
  37. {
  38.   if (screen = InitTags(NULL,
  39.      TAGS_SCREEN,    NULL,
  40.        GSA_BitmapTags, NULL,
  41.        BMA_AmtColours, AMTCOLOURS,
  42.        TAGEND, NULL,
  43.      GSA_Palette,    palette,
  44.      GSA_Attrib,     DBLBUFFER,
  45.      TAGEND)) {
  46.  
  47.      if (jport1 = Init(Get(ID_JOYDATA),NULL)) {
  48.         Display(screen);
  49.         Demo();
  50.  
  51.      Free(jport1);
  52.      }
  53.   Free(screen);
  54.   }
  55. }
  56.  
  57. /************************************************************************************
  58. ** Longtitude deterimines the position of the dot on the horizontal axis.
  59. ** Latitude determines the position of the dot on the vertical axis.
  60. */
  61.  
  62. #define AMTDOTS 500       /* The amount of dots in the object. */
  63.  
  64. void Demo(void)
  65. {
  66.   struct DotPixel *object; /* Pointer to our 3D object */
  67.   double *sine;            /* Pointer to our sine table */
  68.   double *cosine;          /* Pointer to our cosine table */
  69.   WORD   i;
  70.   WORD   offsetx = (screen->Width/2);
  71.   WORD   offsety = (screen->Height/2);
  72.   double temp;
  73.   double angle=0;
  74.   ULONG  colour;
  75.   double Z2,X2,Y2;
  76.   LONG   scale=32;
  77.   UWORD  anglex=0,angley=0,anglez=0;
  78.   double longtitude;
  79.   double latitude;
  80.  
  81.   object = AllocMemBlock(sizeof(struct DotPixel)*AMTDOTS,MEM_DATA);
  82.   sine   = AllocMemBlock(sizeof(double)*360,MEM_DATA);
  83.   cosine = AllocMemBlock(sizeof(double)*360,MEM_DATA);
  84.  
  85.   /* First calculate the X, Y and Z coordinates of our object */
  86.  
  87.   for (i=0; i<AMTDOTS; i++) {
  88.     longtitude = FastRandom(100);
  89.     latitude   = FastRandom(100);
  90.     object[i].X  = sin(longtitude)*cos(latitude);
  91.     object[i].Y  = sin(longtitude)*sin(latitude);
  92.     object[i].Z  = cos(longtitude);
  93.   }
  94.  
  95.   /* Now generate our cosine and sinus tables */
  96.  
  97.   for (i=0; i<360; i++) {
  98.     cosine[i] = cos(angle);
  99.     sine[i]   = sin(angle);
  100.     angle    += 0.25;
  101.   }
  102.  
  103.   /* Commence our main loop */
  104.  
  105.   do
  106.   {
  107.     Query(jport1);
  108.     scale += jport1->YChange;
  109.     if (scale < 1) scale = 1;
  110.     if (scale > 100) scale = 100;
  111.  
  112.     ClearBitmap(screen->Bitmap);
  113.  
  114.     for (i=0; i<AMTDOTS; i++) {
  115.  
  116.       X2 = object[i].X;
  117.       Y2 = object[i].Y;
  118.       Z2 = object[i].Z;
  119.  
  120.       /* Rotate the X axis */
  121.  
  122.       temp = Z2;
  123.       Z2 = Z2*cosine[anglex] - Y2*sine[anglex];
  124.       Y2 = Y2*cosine[anglex] + temp*sine[anglex];
  125.  
  126.       /* Rotate the Y axis */
  127.  
  128. //      temp = Z2;
  129. //      Z2 = Z2*cosine[angley] - X2*sine[angley];
  130. //      X2 = X2*cosine[angley] + temp*sine[angley];
  131.  
  132.       /* Rotate the Z axis */
  133.  
  134. //      temp = X2;
  135. //      X2 = X2*cosine[anglez] - Y2*sine[anglez];
  136. //      Y2 = Y2*cosine[anglez] + temp*sine[anglez];
  137.  
  138.       /* Calculate colour based on Z position (-1 < Z < +1) */
  139.  
  140.       colour = ((Z2+1)*screen->Bitmap->AmtColours)/2;
  141.  
  142.       /* Finally scale the (x,y) coordinates to enlarge or shrink the object */
  143.  
  144.       X2 *= scale;
  145.       Y2 *= scale;
  146.  
  147.       DrawPixel(screen->Bitmap,(WORD)X2+offsetx,(WORD)Y2+offsety,colour);
  148.     }
  149.     anglex++; if (anglex >= 360) anglex = 0;
  150.     angley++; if (angley >= 360) angley = 0;
  151.     anglez++; if (anglez >= 360) anglez = 0;
  152.  
  153.     WaitVBL();
  154.     SwapBuffers(screen);
  155.   } while(!(jport1->Buttons & JD_LMB));
  156.  
  157.   FreeMemBlock(object);
  158.   FreeMemBlock(sine);
  159.   FreeMemBlock(cosine);
  160. }
  161.  
  162.