// MidiVizSpinnerGroup
// Lew Hill II

#include "MidiVizSpinnerGroup.h"

MidiVizSpinnerGroup::MidiVizSpinnerGroup(){
  init();
}

void MidiVizSpinnerGroup::init(){

  for (int i = 0; i < 12; i++){
    noteHeight[i] = 0;
    deltaNoteHeight[i] = 0;
    noteSpin[i] = 0;
    deltaNoteSpin[i] = 0;
  }

  scale[0] = 0.7;
  scale[1] = 0.7;  
  scale[2] = 0.7;

  centerSpinSpeed = 0;
  centerSpinAngle = 0;

  position[0] = 0;
  position[1] = 0;  
  position[2] = 0;

  rotation[0] = 0;
  rotation[1] = 0;
  rotation[2] = 0;

}

void MidiVizSpinnerGroup::processMidiEvent(MidiEvent event){
  
  int note = event.getNote();
  int velocity = event.getVelocity();

  int note_index = note %12;

  deltaNoteSpin[note_index] += velocity;
  deltaNoteHeight[note_index] += velocity;
  noteValues[note_index] = (float) velocity/128.0;

}


void MidiVizSpinnerGroup::frame(){

  centerSpinSpeed = centerSpinSpeed * .90;

  for (int i = 0; i < 12; i++){
    noteHeight[i] += deltaNoteHeight[i]/17.0;
    noteSpin[i] += deltaNoteSpin[i]/10.0;
    
    centerSpinSpeed += noteSpin[i];
    
    deltaNoteHeight[i] *= 0.8;
    deltaNoteSpin[i] *= 0.80;

    noteHeight[i] *=0.85;
    noteSpin[i] *=0.85;

    noteValues[i] *=.97;
  }

  centerSpinAngle += centerSpinSpeed*.005;
  if (centerSpinAngle > 360){
    centerSpinAngle -= 360;
  }
  
}

void MidiVizSpinnerGroup::draw(){

  // frame turn rate is the sum of the note spins.

  glPushMatrix();
  glTranslatef(position[0], position[1], position[2]);
  glRotatef(rotation[2], 0.0, 0.0, 1.0); 
  glRotatef(rotation[1] + centerSpinAngle, 0.0, 1.0, 0.0); 
  glRotatef(rotation[0], 1.0, 0.0, 0.0); 
  glScalef(scale[0], scale[1], scale[2]);

  glColor3f(1.0, 1.0, 1.0);

  float attColor[3];

  for (int i = 0; i < 12; i++){

    float* colors = color_table.getValue(i);
    attColor[0] = colors[0] * (.09 + .91 * noteValues[i]);
    attColor[1] = colors[1] * (.09 + .91 * noteValues[i]);
    attColor[2] = colors[2] * (.09 + .91 * noteValues[i]);

    glPushMatrix();
    glRotatef(i*30,0,1,0);
    glColor3fv(attColor);
    glTranslatef(noteSpin[i]/17.0, noteHeight[i]/10.0, 0);
    glRotatef(noteSpin[i]*3, 1, 1, 1);
    glScalef(.11 + noteSpin[i]*.015, 
	     .11 + noteSpin[i]*.015, 
	     .11 + noteSpin[i]*.015);
    drawbox(0,1, 0, 1, 0, .25, GL_QUADS); 
    glPopMatrix();

    glPushMatrix();
    glRotatef(i*30,0,1,0);
    glColor3fv(attColor);
    glTranslatef(-noteSpin[i]/17.0, -noteHeight[i]/10.0, 0);
    glRotatef(-noteSpin[i]*3, 1, 1, 1);
    glScalef(.15 + noteSpin[i]*.02, 
	     .15 + noteSpin[i]*.02, 
	     .15 + noteSpin[i]*.02);
    drawbox(0,1, 0, 1, 0, .25, GL_QUADS); 
    glPopMatrix();

  }

  glPopMatrix();
  
}

void MidiVizSpinnerGroup::drawbox(GLdouble x0, GLdouble x1, GLdouble y0, GLdouble y1,
             GLdouble z0, GLdouble z1, GLenum type)
{
   static GLdouble n[6][3] = {
      {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
      {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0}
   };
   static GLint faces[6][4] = {
      { 0, 1, 2, 3}, { 3, 2, 6, 7}, { 7, 6, 5, 4},
      { 4, 5, 1, 0}, { 5, 6, 2, 1}, { 7, 4, 0, 3}
   };
   GLdouble v[8][3], tmp;
   GLint i;

   if (x0 > x1)
   {
      tmp = x0; x0 = x1; x1 = tmp;
   }
   if (y0 > y1)
   {
      tmp = y0; y0 = y1; y1 = tmp;
   }
   if (z0 > z1)
   {
      tmp = z0; z0 = z1; z1 = tmp;
   }
   v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
   v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
   v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
   v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
   v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
   v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;

   for (i = 0; i < 6; i++)
   {
      glBegin(type);
         glNormal3dv(&n[i][0]);
         glVertex3dv(&v[faces[i][0]][0]);
         glNormal3dv(&n[i][0]);
         glVertex3dv(&v[faces[i][1]][0]);
         glNormal3dv(&n[i][0]);
         glVertex3dv(&v[faces[i][2]][0]);
         glNormal3dv(&n[i][0]);
         glVertex3dv(&v[faces[i][3]][0]);
      glEnd();
   }
}

