/*
** Name:      Spinning Helix
** Version:   1.0
** Author:    Paul Manias
** Copyright: DreamWorld Productions (c) 1997
** SAS/C:     sc Helix.c
**
** Doc:       This is a demo of a spinning helix consisting entirely of dots.
**            The object is pre-calculated then rotated in real time for speed,
**            although things could be faster than this.  Move the mouse to scale
**            the object to any size.
**
*/

#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);

     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. */
#define MAXZ    6.3926    /* MAXZ  */

void Demo(void)
{
  struct DotPixel *object;
  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=16;
  UWORD  anglex=0, angley=0, anglez=0;
  double u,v;
  double *sine;       /* Pointer to our sine table */
  double *cosine;     /* Pointer to our cosine table */

  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++) {
    u = ((double)FastRandom(12566-1)+1)/1000;  /*  0 < u < 4*PI  */
    v = ((double)FastRandom(6283-1)+1)/1000;   /*  0 < v < 2*PI  */
    object[i].X  = cos(u)*(2+cos(v));
    object[i].Y  = sin(u)*(2+cos(v));
    object[i].Z  = (u-2*3.14159)+sin(v);
  }

  /* Now generate our cosine and sinus tables */

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

  /* Go into our main loop */

  InitJoyPorts();

  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.96 < Z < +1.96) */

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

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

      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);
}

