// MidiVizField
// Lew Hill II

#include "MidiVizField.h"

MidiVizField::MidiVizField(){
  init();
}

void MidiVizField::init(){

  for (int i = 0; i < NOTE_ARRAY_LENGTH; i++){
    noteScale[i] = 0;
  }

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

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

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

}

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

  int note_index = note-MIDI_LOW;

  noteScale[note_index] += velocity/127.0;

}


void MidiVizField::frame(){

  for (int i = 0; i < NOTE_ARRAY_LENGTH; i++){
    noteScale[i] *=.9;
  }
  
}

void MidiVizField::draw(){

  glLineWidth(1.0);
  
  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]);
  
  float *colors;
  float attColor[3];

  for (int i = 0; i < NOTE_ARRAY_LENGTH; i++){
    
    glPushMatrix();
    
    colors = color_table.getValue((i+MIDI_LOW)%12);
    
    attColor[0] = colors[0] * (.1 + .9 * noteScale[i]);
    attColor[1] = colors[1] * (.1 + .9 * noteScale[i]);
    attColor[2] = colors[2] * (.1 + .9 * noteScale[i]);
    
    glColor3fv(attColor);
    //    glColor3fv(colors);
    
    int j = i/12;
    
    glTranslatef(i%12,0 , j);
    float noteScale_2 = noteScale[i]/2.0;
    
    drawbox(noteScale_2, -noteScale_2, 
	    noteScale_2, -noteScale_2, 
	    noteScale_2, -noteScale_2, GL_QUADS); 
       


    if (noteScale[i] > .1){
      glColor3f(1.0f, 1.0f, 1.0f);
    } else {
      glColor3fv(attColor);
    }
    
    glBegin(GL_LINE_LOOP);
    glVertex3f(-.49, 0, -.49);
    glVertex3f(.49, 0, -.49);
    glVertex3f(.49, 0, .49);
    glVertex3f(-.49, 0, .49);
    glEnd();
    glPopMatrix();
  }
  
  glPopMatrix();
  
}

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

