/**
 * <h1>My first processing.org 3D application</h1>
 * <p>by Darabos, Edvard Konrad &lt;nil@inf.elte.hu&gt;</p>
 * <h2>Known limitations:</h2>
 * <li>Requires OpenGL capable graphics adapter</li>
 * <li>ESC: Leads to freezing. (Use browser's Refresh button!)</li>
 */

class CubeMoon {
  public float placeA=0,placeB=0,placeC=0;
  public float oriA=0,oriB=0,oriC=0;
  public float distance = 100;
  public color c = color(255,0,0);
}

int CUBEMOON_COUNT = 40;
CubeMoon[] cubeMoons;

import processing.opengl.*;

void setup()
{
  size(800,600,OPENGL);
  cubeMoons = new CubeMoon[CUBEMOON_COUNT];
  for(int i=0; i<CUBEMOON_COUNT; ++i)
  {
    cubeMoons[i] = new CubeMoon();
    cubeMoons[i].placeA = random(100)/50*PI;
    cubeMoons[i].placeB = random(100)/50*PI;
    cubeMoons[i].placeC = random(100)/50*PI;
    cubeMoons[i].oriA = random(100)/50*PI;
    cubeMoons[i].oriB = random(100)/50*PI;
    cubeMoons[i].oriC = random(100)/50*PI;
  }
}

int distance = 300;

void draw()
{
  background(0);
  pushMatrix();
  rotateY(PI/4);
  rotateX(PI/4);
  lights();
  popMatrix();
  rotateY(mouseX/30.0);
  rotateX(mouseY/30.0);
  fill(0,0,255);
  noStroke();
  for(int i=0; i<CUBEMOON_COUNT; ++i)
  {
    pushMatrix();
    fill(cubeMoons[i].c);
    rotateX(cubeMoons[i].oriA);
    rotateY(cubeMoons[i].oriB);
    rotateZ(cubeMoons[i].oriC);
    translate(cubeMoons[i].distance,0,0);
    rotateX(cubeMoons[i].placeA);
    rotateY(cubeMoons[i].placeB);
    rotateZ(cubeMoons[i].placeC);
    box(20);
    popMatrix();
    cubeMoons[i].oriC -= PI/100;
    cubeMoons[i].placeC += PI/100;
    color c = cubeMoons[i].c;
    float r=red(c),g=green(c),b=blue(c);
    r += random(-10,10); if(r<0) r=0; if(r>255) r=255;
    g += random(-10,10); if(g<0) g=0; if(g>255) g=255;
    b += random(-10,10); if(b<0) b=0; if(b>255) b=255;
    float _max = max(max(r,g),b);
    if(_max<128)
    {
      r *= 128.0/_max;
      g *= 128.0/_max;
      b *= 128.0/_max;
    }
    cubeMoons[i].c = color(r,g,b);
  }
//  sphere(25);
//  popMatrix();
  fill(255,0,0);
  box(50);

  camera(
    0,0,distance
    ,0,0,0
    ,0,1,0);
}

void draw_0()
{
  background(128);
  translate(width/2,height/2);
  rotate(millis()/1000.0);
  rect(-25,-25,50,50);
}

void keyPressed()
{
  if(key=='a')
    distance*=0.8;
  if(key=='z')
    distance*=1.25;
}
