import java.*;
import javax.media.opengl.*;
import processing.opengl.*;
import mri.*;

float rotX = -1.05;
float rotY = -0.6;
float  zoom = 0;

//PFont verdana10;

V3dsScene monsterBody;
V3dsScene monsterEye;
mouseWheel wheel;

void setup()
{
  size(800,600, OPENGL);
  hint(DISABLE_OPENGL_2X_SMOOTH);
  hint( ENABLE_OPENGL_4X_SMOOTH );
  smooth();

  wheel = new mouseWheel();
  //verdana10 = loadFont("Verdana-10.vlw");
  //textFont(verdana10);

  monsterBody = new V3dsScene( this, "monsterBody.3ds" );
  monsterEye = new V3dsScene( this, "eye.3ds" );
  //randomSeed(0);
  // textMode(SCREEN);
  //  text("Drag mouse to change angle", 0, 0);
  // text("Scroll to zoom in and out", 0, 15); 
  // text("Monster with tracking eyes", 0, 30);
}

void draw()
{
  background(0);

  fill(255);
  //int xOff = 0;
  // int yOff = 500;
  //int zOff = -800;
  //  text("Drag mouse to change angle", 0+xOff, 0+yOff, 0+zOff);
  //  text("Scroll to zoom in and out", 0+xOff, 15+yOff, 0+zOff); 
  //  text("Monster with tracking eyes", 0+ xOff, 30+yOff, 0+zOff);


  perspective( PI*0.25, 4.0/3.0, 1, 5000 );
  camera( 0, -10, 7+zoom, 0, -2, 0, 0, 1, 0 ); //cam_pos.x, cam_pos.y, cam_pos.z, cam_tar.x, cam_tar.y, cam_tar.z, cam_up.x, cam_up.y, cam_up.z );
  scale( -1, -1, 1 );

  float time = millis() * 0.001;

  // setup light. to do correct lighting you need to set it before any world transformations
  GL _gl = ((PGraphicsOpenGL)g).beginGL();
  setupLight1( _gl, new float[]{ 0, 5, 5  } , 1 );
  setupLight2( _gl, new float[]{ 0, 5, 15 } , 1 );
  ((PGraphicsOpenGL)g).endGL();

  pushMatrix();
  rotateY(rotX);

  rotateY( radians(1*time) );
  rotateX(rotY);
  print("y");
  println(rotY);
  print("x");
  println(rotX);


  for(int deg=0; deg<360; deg+=30){
    float angle = radians(deg);
    float x = (cos(angle)*10);
    float z= (sin(angle)*8);
    drawMonster(x,0,z);
  }

  popMatrix();

}

void drawMonster(float xPos,float  yPos, float zPos){
  drawBody(0+xPos,0+yPos,0+zPos);
  drawEye(-1.161+xPos,3.779+yPos,-0.238+zPos);
  drawEye(1.199+xPos, 3.78+yPos,-0.254+zPos);
}


void drawEye(float xPos, float yPos, float zPos){
  pushMatrix();
  translate(xPos, yPos, zPos);
  float x = map(mouseX, 0, getWidth(), 20, -20);
  float y = map(mouseY, 0, getHeight(), -20, 20);
  rotateY(radians(x));
  rotateX(radians(y));
  monsterEye.draw();
  popMatrix();
}

void drawBody (float posX, float posY, float posZ){
  pushMatrix();
  translate(posX,posY,posZ);
  monsterBody.draw();
  popMatrix();
}

void setupLight1( GL g, float[] pos, float val )
{
  float[] light_ambient = { 
    0.22f, 0.22f, 0.22f, 1   };
  float[] light_diffuse = { 
    1.0f, 1.0f, 1.0f, 1.0f   };
  float[] light_specular = { 
    1.0f, 1.0f, 1.0f, 1.0f   };  
  float[] light_position = { 
    pos[0], pos[1], pos[2], val   };  

  g.glLightfv ( GL.GL_LIGHT1, GL.GL_AMBIENT, light_ambient, 0 );
  g.glLightfv ( GL.GL_LIGHT1, GL.GL_DIFFUSE, light_diffuse, 0 );
  g.glLightfv ( GL.GL_LIGHT1, GL.GL_SPECULAR, light_specular, 0 );
  g.glLightfv ( GL.GL_LIGHT1, GL.GL_POSITION, light_position, 0 );  
  g.glEnable( GL.GL_LIGHT1 );
  g.glEnable( GL.GL_LIGHTING );
  g.glEnable( GL.GL_COLOR_MATERIAL );
}  

void setupLight2( GL g, float[] pos, float val )
{
  float[] light_ambient = { 
    0.22f, 0.22f, 0.22f, 1   };
  float[] light_diffuse = { 
    1.0f, 1.0f, 1.0f, 1.0f   };
  float[] light_specular = { 
    1.0f, 1.0f, 1.0f, 1.0f   };  
  float[] light_position = { 
    pos[0], pos[1], pos[2], val   };  

  g.glLightfv ( GL.GL_LIGHT1, GL.GL_AMBIENT, light_ambient, 0 );
  g.glLightfv ( GL.GL_LIGHT1, GL.GL_DIFFUSE, light_diffuse, 0 );
  g.glLightfv ( GL.GL_LIGHT1, GL.GL_SPECULAR, light_specular, 0 );
  g.glLightfv ( GL.GL_LIGHT1, GL.GL_POSITION, light_position, 0 );  
  g.glEnable( GL.GL_LIGHT1 );
  g.glEnable( GL.GL_LIGHTING );
  g.glEnable( GL.GL_COLOR_MATERIAL );
}  

void mouseDragged()
{
  rotX += (mouseX - pmouseX) * 0.01;
  rotY -= (mouseY - pmouseY) * 0.01;
}

public class mouseWheel implements MouseWheelListener {
  public mouseWheel() {
    addMouseWheelListener(this);
  }
  public void mouseWheelMoved(MouseWheelEvent e) {
    String message;
    zoom = zoom + e.getWheelRotation()*0.5;
  }
}






