#include "Camera.hpp"


#include <float.h>
#include <math.h>
#include "gl.hpp"
#include <GL/glu.h>

#define DEG2RAD(a) (((a)/180.0)*3.14159)


Camera::Camera(double polar, double azimut, double distance) :
m_polar(polar), m_azimut(azimut), m_distance(distance)
{
  m_lookAt[0] = 0.0;
  m_lookAt[1] = 0.0;
  m_lookAt[2] = 0.0;  

  update();  
}


Camera::~Camera(void)
{
}

void Camera::update(void) 
{
  m_position[0] = m_lookAt[0] + m_distance*cos(DEG2RAD(m_polar))*sin(DEG2RAD(-m_azimut));
  m_position[1] = m_lookAt[1] + m_distance*cos(DEG2RAD(m_polar))*cos(DEG2RAD(-m_azimut));
  m_position[2] = m_lookAt[2] + m_distance*sin(DEG2RAD(m_polar));
}


void Camera::setup(void)
{
  gluLookAt(
    m_position[0], // eyepoint 
    m_position[1], 
    m_position[2],
    
    m_lookAt[0], // center 
    m_lookAt[1], 
    m_lookAt[2],
    
    0.0, 0.0, 1.0  // up vector
  );
}


void Camera::setPolar(double polar) 
{ 
  if (polar >= 90.0)
    m_polar = 89.99;
  else if (polar <= -90.0)
    m_polar = -89.99;
  else 
    m_polar = polar; 

  update();
};

void Camera::setAzimut(double azimut) 
{  
  m_azimut = azimut - 360.0*floor(azimut/360.0);

  if (m_azimut < 0.0)
    m_azimut += 360.0; 

  update();
};

void Camera::setDistance(double distance)
{ 
  if (distance < FLT_EPSILON) 
    distance = FLT_EPSILON;

  m_distance = distance; 

  update();
};

