import processing.core.*; 
import processing.xml.*; 

import processing.core.*; 
import processing.opengl.*; 
import processing.video.*; 
import jp.nyatla.nyar4psg.*; 
import javax.media.opengl.*; 
import mri.*; 
import pogg.*; 
import ddf.minim.*; 

import jp.nyatla.nyartoolkit.core.*; 
import jp.nyatla.nyartoolkit.utils.*; 
import jp.nyatla.nyartoolkit.core.labeling.artoolkit.*; 
import jp.nyatla.nyartoolkit.core.raster.*; 
import mri.*; 
import jp.nyatla.nyartoolkit.core.types.stack.*; 
import jp.nyatla.nyartoolkit.core.utils.*; 
import jp.nyatla.nyartoolkit.core.pickup.*; 
import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.*; 
import jp.nyatla.nyartoolkit.core.labeling.rlelabeling.*; 
import jp.nyatla.nyartoolkit.core.analyzer.raster.*; 
import com.jcraft.jorbis.*; 
import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2gs.*; 
import ru.cleoag.MultiARToolKit.*; 
import jp.nyatla.nyartoolkit.core.match.*; 
import jp.nyatla.nyartoolkit.sample.*; 
import jp.nyatla.nyartoolkit.core.param.*; 
import net.sourceforge.jartoolkit.core.*; 
import jp.nyatla.nyartoolkit.core.types.matrix.*; 
import jp.nyatla.nyartoolkit.core.transmat.optimize.*; 
import jp.nyatla.nyartoolkit.core.transmat.rotmatrix.*; 
import com.fluendo.jkate.*; 
import jp.nyatla.nyartoolkit.nyidmarker.data.*; 
import com.fluendo.codecs.*; 
import jp.nyatla.nyartoolkit.core.rasterfilter.gs2bin.*; 
import jp.nyatla.nyartoolkit.core.squaredetect.*; 
import pogg.*; 
import jp.nyatla.nyartoolkit.detector.*; 
import com.fluendo.jheora.*; 
import jp.nyatla.nyartoolkit.core.rasterreader.*; 
import com.jcraft.jogg.*; 
import jp.nyatla.nyartoolkit.core.transmat.solver.*; 
import jp.nyatla.nyartoolkit.core.pca2d.*; 
import jp.nyatla.nyartoolkit.core.raster.rgb.*; 
import jp.nyatla.nyartoolkit.core.analyzer.raster.threshold.*; 
import com.fluendo.utils.*; 
import jp.nyatla.nyartoolkit.nyidmarker.*; 
import jp.nyatla.nyartoolkit.core.labeling.*; 
import jp.nyatla.nyartoolkit.utils.j2se.*; 
import jp.nyatla.nyartoolkit.core.transmat.optimize.artoolkit.*; 
import com.fluendo.player.*; 
import com.fluendo.examples.*; 
import jp.nyatla.nyar4psg.*; 
import com.fluendo.jtiger.*; 
import mri.v3ds.*; 
import jp.nyatla.nyartoolkit.core.analyzer.histogram.*; 
import jp.nyatla.nyartoolkit.core.rasterfilter.*; 
import jp.nyatla.nyartoolkit.core.transmat.*; 
import jp.nyatla.nyartoolkit.*; 
import jp.nyatla.nyartoolkit.core.types.*; 
import pogg.plugin.*; 
import jp.nyatla.nyartoolkit.processor.*; 

import java.applet.*; 
import java.awt.Dimension; 
import java.awt.Frame; 
import java.awt.event.MouseEvent; 
import java.awt.event.KeyEvent; 
import java.awt.event.FocusEvent; 
import java.awt.Image; 
import java.io.*; 
import java.net.*; 
import java.text.*; 
import java.util.*; 
import java.util.zip.*; 
import java.util.regex.*; 

public class sistemasolar extends PApplet {




 //cam
 //AR

 //3d
 //video
 //audio

Minim minim1;
AudioPlayer musica;

TheoraMovie myMovie;

Capture cam;
NyARMultiBoard nya;
PFont font;

V3dsScene sol;
V3dsScene mercurio;
V3dsScene venus;
V3dsScene tierra;
V3dsScene marte;
V3dsScene jupiter;
V3dsScene saturno;
V3dsScene urano;
V3dsScene neptuno;
V3dsScene luna;

float angSol,angMercurio,angVenus,angTierra,angMarte,angJupiter,angSaturno,angUrano,angNeptuno,angLuna;
float angRotSol,angRotMercurio,angRotVenus,angRotTierra,angRotMarte,angRotJupiter,angRotSaturno,angRotUrano,angRotNeptuno,angRotLuna;
float Rot;

int interruptor = 0;

public void setup() {
  //Camera on
  try {
    quicktime.QTSession.open();
  } catch (quicktime.QTException qte) {
    qte.printStackTrace();
  }//END Camera on

  size(640,480,OPENGL);
  // I'm using the GSVideo capture stack
  cam=new Capture(this,width,height,24);
  // array de marcadores
  String[] patts = {"marcadores/ss","marcadores/sol2","marcadores/mercurio2","marcadores/venus2","marcadores/tierra2","marcadores/marte2","marcadores/jupiter2","marcadores/saturno2","marcadores/urano2","marcadores/neptuno2","marcadores/video"};
  // array de los tama\u00f1os en mm
  double[] widths = {80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80};

  // inicializar el NyARMultiBoard
  nya=new NyARMultiBoard(this,width,height,"camera_para.dat",patts,widths);

  nya.gsThreshold=110;//(0<n<255) default=110
  nya.cfThreshold=0.4f;//(0.0<n<1.0) default=0.4


  try
  {
      //cargar modelos 3D
      sol = new V3dsScene(this,"planetas/sol.3ds");
      mercurio = new V3dsScene(this,"planetas/mercurio.3ds");
      venus = new V3dsScene(this,"planetas/venus.3ds");
      tierra = new V3dsScene(this,"planetas/tierra.3ds");
      marte = new V3dsScene(this,"planetas/marte.3ds");
      jupiter = new V3dsScene(this,"planetas/jupiter.3ds");
      saturno = new V3dsScene(this,"planetas/saturno.3ds");
      urano = new V3dsScene(this,"planetas/urano.3ds");
      neptuno = new V3dsScene(this,"planetas/neptuno.3ds");
      luna = new V3dsScene(this,"planetas/luna.3ds");
      sol.useMaterial(true);
      mercurio.useMaterial(true);
      venus.useMaterial(true);
      tierra.useMaterial(true);
      marte.useMaterial(true);
      jupiter.useMaterial(true);
      saturno.useMaterial(true);
      urano.useMaterial(true);
      neptuno.useMaterial(true);
      luna.useMaterial(true);
  }
  catch( Exception e )
  {
    println( e );
    System.exit( 0 );
  }
  //video
  myMovie = new TheoraMovie(this, "SistemaSolar2.ogg");
  myMovie.loop();
  //audio
  minim1 = new Minim(this);
  musica=minim1.loadFile("SistemaSolar2.mp3",2048);   
  
}//END void setup()

public void drawMarkerPos(int[][] pos2d)
{

  stroke(100,0,0);
  fill(100,0,0);
  
  // Aqu\u00ed estan los puntos en las esquinas de los marcadores
  for(int i=0;i<4;i++){
    ellipse(pos2d[i][0], pos2d[i][1],5,5);
  }
  
  fill(0,255,0);
  for(int i=0;i<4;i++){
    text(pos2d[i][0]+","+pos2d[i][1],pos2d[i][0],pos2d[i][1]);
  }
  noStroke();
}
public void draw() {
  background(200);
  if (cam.available() )
  {
    cam.read();
  }
  
  background(255);

  hint(DISABLE_DEPTH_TEST);
  image(cam,0,0,800,600);
  hint(ENABLE_DEPTH_TEST);

  if (nya.detect(cam))
  {
    hint(DISABLE_DEPTH_TEST);
    for (int i=0; i < nya.markers.length; i++)
    {
      if (nya.markers[i].detected)
      {
        translate(60,50,70);
        //drawMarkerPos(nya.markers[i].pos2d);
      }
    }
    
    hint(ENABLE_DEPTH_TEST);

    PGraphicsOpenGL pgl = (PGraphicsOpenGL) g;
    for (int i=0; i < nya.markers.length; i++)
    {
      if (nya.markers[i].detected)
      {
        nya.markers[i].beginTransform(pgl);
        /*translate(0,0,20);
        rotateY(PI/2);
        rotateZ(PI/2);*/
        translate(20,-20,0);
        if ( i == 0 )
        {
            println("marker sistema solar");
            ss();
        }
        else if ( i == 1)
        {
            println("marker sol");
            sol();
        }
        else if ( i == 2)
        {
            println("marker mercurio");
            mercurio();
        }
        else if ( i == 3)
        {
            println("marker Venus");
            venus();
        }
        else if ( i == 4)
        {
            println("marker tierra");
            tierra();
        }
        else if ( i == 5)
        {
            println("marker marte");
            marte();
        }
        else if ( i == 6)
        {
            println("marker jupiter");
            jupiter();
        }
       else if ( i == 7)
        {
            println("marker saturno");
            saturno();
        }
       else if ( i == 8)
        {
            println("marker urano");
            urano();
        }
       else if ( i == 9)
        {
            println("marker neptuno");
            neptuno();
        }
       else if ( i == 10)
        {
            println("marker video");
            video();
        }        

        nya.markers[i].endTransform();
      }
    }
  }
}
public void sombra(int radio){
    pushMatrix();
    translate(0,0,-50);
    fill(0,50);
    ellipse(0,0,radio,radio); 
    popMatrix();  
}
public void video(){
  if(interruptor==0){
    musica.play();
    musica.loop(10);
    interruptor = 1;
  }
    translate(-(myMovie.width/4),(myMovie.height/4),0);
    scale(0.7f);
    rotateX(-PI);
    //rotateY(-1);    
    myMovie.read();
    image(myMovie, 0, 0);  
  
}
public void sol(){
    translate(0,0,40);
    sombra(80);
    scale(1);
    rotateZ(Rot += 0.01f);  
    rotateX(HALF_PI);
    sol.draw();
    rotateX(-HALF_PI);   
}
public void mercurio(){
    translate(0,0,50);
    sombra(80);
    scale(30);
    rotateZ(Rot += 0.01f);  
    rotateX(HALF_PI);
    mercurio.draw();
    rotateX(-HALF_PI);  
}
public void venus(){
    translate(0,0,50);
    sombra(80);
    scale(23);
    rotateZ(Rot += 0.01f);  
    rotateX(HALF_PI);
    venus.draw();
    rotateX(-HALF_PI);    
}
public void tierra(){
    translate(0,0,50);
    sombra(80);
    scale(23);
    rotateZ(Rot += 0.01f);  
    rotateX(HALF_PI);
    tierra.draw();
    rotateX(-HALF_PI);
}
public void marte(){
    translate(0,0,50);
    sombra(80);
    scale(30);
    rotateZ(Rot += 0.01f);  
    rotateX(HALF_PI);
    marte.draw();
    rotateX(-HALF_PI);    
}
public void jupiter(){
    translate(0,0,50);
    sombra(100);
    scale(4);
    rotateZ(Rot += 0.01f);  
    rotateX(HALF_PI);
    jupiter.draw();
    rotateX(-HALF_PI);   
}
public void saturno(){
    translate(0,0,50);
    sombra(80);
    scale(4);
    rotateZ(Rot += 0.01f);  
    rotateX(HALF_PI);
    saturno.draw();
    rotateX(-HALF_PI);     
}
public void urano(){
    translate(0,0,50);
    sombra(30);
    scale(5);
    rotateZ(Rot += 0.01f);  
    rotateX(HALF_PI);
    urano.draw();
    rotateX(-HALF_PI);     
}
public void neptuno(){
    translate(0,0,50);
    sombra(80);
    scale(10);
    rotateZ(Rot += 0.01f);  
    rotateX(HALF_PI);
    neptuno.draw();
    rotateX(-HALF_PI);    
}

public void ss(){
  //lights();
  //directionalLight(126, 126, 126, 0, 0, 1);
  //pointLight(126, 126, 126, 0, 0, 0);

  //sombra sol
  fill(0,50);
  ellipse(0,0,80,80);
  
  translate(0, 0, 50); // elevamos el sol sobre el marcador
  rotateX(HALF_PI);
  scale(0.75f);
  sol.draw();
  scale(1.5f);  
  rotateX(-HALF_PI);

  translate(7, 0, 0); //correcci\u00f3n centro esfera del sol

  //radios
  stroke(0,50);
  noFill();
  ellipse(0,0,100,100); 
  ellipse(0,0,120,120);   
  ellipse(0,0,160,160); 
  ellipse(0,0,200,200);  
  ellipse(0,0,240,240);
    ellipse(0,0,340,340);
    ellipse(0,0,400,400);
    ellipse(0,0,440,440);    
  noStroke();

  
  //MERCURIO
  pushMatrix();
  
  rotateZ(angMercurio += 0.414f);
  translate(0, 50, 0);

  rotateX(HALF_PI);
  mercurio.draw();
  rotateX(-HALF_PI);

sombra(3);
    
  popMatrix();
 
 //VENUS
  pushMatrix();
  rotateZ(angVenus += 0.1626f);  
  translate(0, 60, 0);
  
  rotateX(HALF_PI);
  venus.draw();
  rotateX(-HALF_PI);

sombra(3);
  popMatrix();
  
  //TIERRA
  pushMatrix();
  rotateZ(angTierra += 0.1f);  
  translate(0, 80, 0);
  
    pushMatrix();
    rotateZ(angRotTierra += 3.65f);  
    rotateX(HALF_PI);
    tierra.draw();
    popMatrix();
    
    sombra(4);
    
  //radios

  stroke(0,50);
  noFill();
  ellipse(0,0,20,20);  
  noStroke();
  
  //Luna
  pushMatrix();
  rotateZ(angLuna += 0.274f);  
  translate(0, 10, 0);
  scale(0.3f);
  rotateX(HALF_PI);
  luna.draw();
  rotateX(-HALF_PI);
      sombra(2);
  popMatrix();  
 
  popMatrix();

 //Marte
  pushMatrix();
  rotateZ(angMarte += 0.0532f);  
  translate(0, 100, 0);
  
  rotateX(HALF_PI);
  marte.draw();
  rotateX(-HALF_PI);

  sombra(3);
  popMatrix();
  
 //Jupiter
  pushMatrix();
  rotateZ(angJupiter += 0.0084f);  
  translate(0, 125, 0);
    pushMatrix();
    rotateZ(angRotJupiter += 0.1f);  
    rotateX(HALF_PI);
    jupiter.draw();
    popMatrix();

    sombra(25);
    
  popMatrix();
 //Saturno
  pushMatrix();
  rotateZ(angSaturno += 0.0034f);  
  translate(0, 170, 0);
    pushMatrix();
    rotateZ(angRotSaturno += 0.1f);  
    rotateX(2);
    saturno.draw();
    popMatrix();

    sombra(22);
    
  popMatrix();  
 //Urano
  pushMatrix();
  rotateZ(angUrano += 0.00119f);  
  translate(0, 200, 0);
    pushMatrix();
    rotateZ(angRotUrano += 0.1f);  
    rotateX(HALF_PI);
    urano.draw();
    popMatrix();

    sombra(10);
    
  popMatrix();
   //Neptuno
  pushMatrix();
  rotateZ(angNeptuno += 0.000606f);  
  translate(0, 220, 0);
    pushMatrix();
    rotateZ(angRotNeptuno += 0.1f);  
    rotateX(HALF_PI);
    neptuno.draw();
    popMatrix();

    sombra(7);
    
  popMatrix();  
}
  static public void main(String args[]) {
    PApplet.main(new String[] { "--bgcolor=#F0F0F0", "sistemasolar" });
  }
}
