// MidiVizTowers
// Lew Hill II

#include "MidiVizTowers.h"

MidiVizTowers::MidiVizTowers(){
  init();
}

void MidiVizTowers::init(){

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

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

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

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

}

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

  int note_index = note %12;

  deltaNoteHeight[note_index] += velocity;

}


void MidiVizTowers::frame(){

  for (int i = 0; i < 12; i++){
    noteHeight[i] += deltaNoteHeight[i]/50.0;
    
    deltaNoteHeight[i] *= 0.93;
    noteHeight[i] *=0.6;

  }

}

void MidiVizTowers::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], 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 attColors[3];

  for (int i = 0; i < 12; i++){
    
    glPushMatrix();
    
    float* colors = color_table.getValue(i);
    attColors[0] = colors[0]*deltaNoteHeight[i]/127.0;
    attColors[1] = colors[1]*deltaNoteHeight[i]/127.0;
    attColors[2] = colors[2]*deltaNoteHeight[i]/127.0;
    
    glColor3fv(attColors);

    glTranslatef((i-6)*1, 0.0, 0.0);

    drawbox(-.30,.30, 0, noteHeight[i], -0.30, .30, GL_QUADS); 
    
    glColor3fv(colors);
    glBegin(GL_LINE_LOOP);
    glVertex3f(-.33, 0, -.33);
    glVertex3f(.33, 0, -.33);
    glVertex3f(.33, 0, .33);
    glVertex3f(-.33, 0, .33);
    glEnd();
    glPopMatrix();
  }

  glPopMatrix();
  
}

void MidiVizTowers::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();
   }
}

