/*
** Name:      Spinning Sphere
** Version:   1.0
** Author:    Paul Manias
** Copyright: DreamWorld Productions (c) 1997.  Freely distributable.
** SAS/C:     sc Sphere.c
**
** Doc:       This is a demo of a spinning sphere consisting entirely of dots.
**            The object is pre-calculated then rotated in real time for speed,
**            although things could be faster than this.
**
*/

#include <proto/games.h>
#include <math.h>

extern struct GMSBase *GMSBase;
APTR   PREFSNAME = DEFAULT;

struct GameScreen *screen;

void Demo(void);

struct DotPixel { double X,Y,Z; };

#define AMTCOLOURS 32

ULONG palette[AMTCOLOURS] = {
  0x000000,0x101010,0x171717,0x202020,0x272727,0x303030,0x373737,0x404040,
  0x474747,0x505050,0x575757,0x606060,0x676767,0x707070,0x777777,0x808080,
  0x878787,0x909090,0x979797,0xa0a0a0,0xa7a7a7,0xb0b0b0,0xb7b7b7,0xc0c0c0,
  0xc7c7c7,0xd0d0d0,0xd7d7d7,0xe0e0e0,0xe0e0e0,0xf0f0f0,0xf7f7f7,0xffffff
};

/***********************************************************************************/

void main(void)
{
  if (screen = AddScreenTags(TAGS_GAMESCREEN,NULL,
     GSA_AmtColours, AMTCOLOURS,
     GSA_Palette,    palette,
     GSA_Attrib,     DBLBUFFER,
     TAGEND)) {

     ShowScreen(screen);
     InitJoyPorts();

     Demo();

  DeleteScreen(screen);
  }
}

/************************************************************************************
** Longtitude deterimines the position of the dot on the horizontal axis.
** Latitude determines the position of the dot on the vertical axis.
*/

#define AMTDOTS 500       /* The amount of dots in the object. */

void Demo(void)
{
  struct DotPixel *object; /* Pointer to our 3D object */
  double *sine;            /* Pointer to our sine table */
  double *cosine;          /* Pointer to our cosine table */
  WORD   i;
  WORD   offsetx = (screen->ScrWidth/2);
  WORD   offsety = (screen->ScrHeight/2);
  double temp;
  double angle=0;
  ULONG  colour;
  double Z2,X2,Y2;
  ULONG  jport1;
  LONG   scale=32;
  UWORD  anglex=0,angley=0,anglez=0;
  double longtitude;
  double latitude;

  object = AllocMemBlock(sizeof(struct DotPixel)*AMTDOTS,MEM_ANY);
  sine   = AllocMemBlock(sizeof(double)*360,MEM_ANY);
  cosine = AllocMemBlock(sizeof(double)*360,MEM_ANY);

  /* First calculate the X, Y and Z coordinates of our object */

  for (i=0; i<AMTDOTS; i++) {
    longtitude = FastRandom(100);
    latitude   = FastRandom(100);
    object[i].X  = sin(longtitude)*cos(latitude);
    object[i].Y  = sin(longtitude)*sin(latitude);
    object[i].Z  = cos(longtitude);
  }

  /* Now generate our cosine and sinus tables */

  for (i=0; i<360; i++) {
    cosine[i] = cos(angle);
    sine[i]   = sin(angle);
    angle    += 0.25;
  }

  /* Commence our main loop */

  do
  {
    jport1 = ReadJoyPort(JPORT1,JT_ZBXY);
    scale += GetY(jport1);
    if (scale < 1) scale = 1;
    if (scale > 100) scale = 100;

    ClearBitmap(screen->Bitmap);

    for (i=0; i<AMTDOTS; i++) {

      X2 = object[i].X;
      Y2 = object[i].Y;
      Z2 = object[i].Z;
 
      /* Rotate the X axis */

      temp = Z2;
      Z2 = Z2*cosine[anglex] - Y2*sine[anglex];
      Y2 = Y2*cosine[anglex] + temp*sine[anglex];

      /* Rotate the Y axis */

//      temp = Z2;
//      Z2 = Z2*cosine[angley] - X2*sine[angley];
//      X2 = X2*cosine[angley] + temp*sine[angley];

      /* Rotate the Z axis */

//      temp = X2;
//      X2 = X2*cosine[anglez] - Y2*sine[anglez];
//      Y2 = Y2*cosine[anglez] + temp*sine[anglez];

      /* Calculate colour based on Z position (-1 < Z < +1) */

      colour = ((Z2+1)*screen->AmtColours)/2;

      /* Finally scale the (x,y) coordinates to enlarge or shrink the object */

      X2 *= scale;
      Y2 *= scale;

      DrawPixel(screen->Bitmap,(WORD)X2+offsetx,(WORD)Y2+offsety,colour);
    }
    anglex++; if (anglex >= 360) anglex = 0;
    angley++; if (angley >= 360) angley = 0;
    anglez++; if (anglez >= 360) anglez = 0;

    WaitVBL();
    SwapBuffers(screen);
  } while(!(jport1 & MB_LMB));

  FreeMemBlock(object);
  FreeMemBlock(sine);
  FreeMemBlock(cosine);
}

