#include "ZGo.h"



//---- Scene1 ----------------------------------------------------------------//

//---- Scene1

Scene1::Scene1(ZGo *zgo, ZGoPARAMS *input, ZGoPARAMS *output)
{
  this->zgo = zgo;
  this->input = input->setInput(this);
  this->output = output->setOutput(this);

  this->fnType = new ZGoNAME(zgo, "Scene1");

  //  Parameters

  time = (ZGoREAL *)input->get("time");
  result = (ZGoIMAGE *)output->get("result");

  //  Internal data

  (position = new ZGoVECTOR(zgo))->setOutput(this);
  (point_at = new ZGoVECTOR(zgo))->setOutput(this);
  (scene = new ZGo3DOBJECT(zgo))->setOutput(this);

  //  Tree data

  ZGoCAMERA *camera = new ZGoCAMERA(zgo);

  //  Internal functions

  new ZGoPointCamera(zgo,
        (new ZGoPARAMS(zgo))->add("position", position)->add("point_at", point_at),
        (new ZGoPARAMS(zgo))->add("camera", camera)
      );

  new ZGoRender(zgo,
        (new ZGoPARAMS(zgo))->add("object", scene)->add("camera", camera),
        (new ZGoPARAMS(zgo))->add("result", result)
      );

}



//---- process

void Scene1::process()
{
  time->process();

  int i, j;

  //- Initialise

  static bool init = false;
  static float x[8]={0,0,0,0,0,0,0,0}, z[8]={0,0,0,0,0,0,0,0};
  static float zz = 0;

  float sox = -1.5, soy = 0, soz = 2;

  if(!init)
  {
    init = true;

    scene->plane(1);
    scene->trans->shiftMatrix(sox, soy, soz);

    for(i=0; i<8; i++)
    {
      x[i] = i-3.5;
      z[i] = i-3.5;

      ZGo3DOBJECT *obj = new ZGo3DOBJECT(zgo);
      obj->cube();
      obj->trans->shiftMatrix(x[i], 1.5, z[i]);

      scene->addObject(obj);
    }
    zz = 0;
  }

  //- Calculate time difference

  static float oldt = 0;
  float dt = time->value - oldt;
  oldt = time->value;

  float dz = dt * 1.1;

  zz += dz;

  //- Position camera

  point_at->x = 0;
  point_at->y = 0;
  point_at->z = 0 + zz;

  position->x = 0.25*2.5;
  position->y = 1.75*2.5;
  position->z = -0.5*2.5 + zz;

  //- Move objects

  static int randseed = 627383;
  randseed = (randseed*7367834) & 0xFFFFFF;

  for(i=0; i<8; i++)
  {
    while(z[i] < zz-5)
    {
      z[i] += 10;
      x[i] = (rand()%8) - 3.5;
    }

    scene->object[i]->trans->shiftMatrix(x[i], 1.5, z[i]-zz);
  }

  //- Move base

  scene->trans->shiftMatrix(sox, soy, soz + ((int)(zz*0.5))*2);

  //- Done

  position->updated();
  point_at->updated();

  scene->updated();
}



//---- end of file -----------------------------------------------------------//
