#include "ZGo.h"



//---- ZGoPointCamera --------------------------------------------------------//

//---- ZGoPointCamera

ZGoPointCamera::ZGoPointCamera(ZGo *zgo, ZGoPARAMS *input, ZGoPARAMS *output)
{
  this->zgo = zgo;
  this->input = input->setInput(this);
  this->output = output->setOutput(this);

  this->fnType = new ZGoNAME(zgo, "ZGoPointCamera");

  //  Parameters

  position = (ZGoVECTOR *)input->get("position");
  point_at = (ZGoVECTOR *)input->get("point_at");
  camera = (ZGoCAMERA *)output->get("camera");
}



//---- process

void ZGoPointCamera::process()
{
  position->process();
  point_at->process();

  //- Create camera matrix

  ZGoVECTOR *forward = new ZGoVECTOR(zgo);

  point_at->sub(position, forward);
  forward->unit();

  ZGoVECTOR *up = new ZGoVECTOR(zgo, 0, 1, 0);

  ZGoVECTOR *right = new ZGoVECTOR(zgo);

  up->cross(forward, right);
  right->unit();

  right->cross(forward, up);

  up->unit();

  ZGoMATRIX *m = new ZGoMATRIX(zgo);

  m->m00 = right->x;
  m->m10 = right->y;
  m->m20 = right->z;
  m->m01 = up->x;
  m->m11 = up->y;
  m->m21 = up->z;
  m->m02 = forward->x;
  m->m12 = forward->y;
  m->m22 = forward->z;
  m->m03 = position->x;
  m->m13 = position->y;
  m->m23 = position->z;

  m->inverse(camera->trans);

  delete m;

  delete up;
  delete right;
  delete forward;
}



//---- end of file -----------------------------------------------------------//
