/************************************************
*
* Project Information: Classe Caméra
*
* Filename: CCamera.cpp
* Version : 0.0.2-dev
* Last Update: 23-01-2004
*
* Author:  Romain Gaucher -- nEUrOO
* E-mail:  neuroo@kouette.com
* Date  :  23-01-2004
*
* Comment: Utilisation d'une caméra dans OpenGL
*
***********************************************/

#include <cwchar>
#include <cstdio>
#include <cstdlib>
#include <GL/glu.h>

#include "CCamera.hpp"
#include "CMaths.hpp"

#ifndef __TRIGO_H
        #include <cmath>
#endif



void CCamera::Init()
{
  EyeDir            = MakeVect3(0.0f,0.0f,-1.0f),
  RightVector       = MakeVect3(1.0f,0.0f,0.0f),
  UpVector          = MakeVect3(0.0f,1.0f,0.0f),
  Position          = NULL_VECTOR,
  RotX    = (0.0f),
  RotY    = (0.0f),
  inertie = (0.0f),
  masse   = (10.0f),
  RotZ    = (0.0f);
}

// Faire tourner la caméra par rapport à l'axe
// des 'x'.
// Le faire tourner de 'angle'

void CCamera::RotateX(float angle)
{
	RotX += angle;
	// On fait tourner EyeDir autours de RIGHT_VECTOR
	EyeDir = Normalize(EyeDir*m_cos(angle*M_PIsur180) + UpVector*m_sin(angle*M_PIsur180));
	// On calcul le nouveau vecteur orthogonal
	UpVector = CrossProduct(&EyeDir, &RightVector)*-1;
}

void CCamera::RotateY(float angle)
{
	RotY += angle;
	EyeDir = Normalize(EyeDir*m_cos(angle*M_PIsur180) - RightVector*m_sin(angle*M_PIsur180));
	RightVector = CrossProduct(&EyeDir, &UpVector);
}

void CCamera::RotateZ(float angle)
{
	RotZ += angle;
	RightVector = Normalize(RightVector*m_cos(angle*M_PIsur180) + UpVector*m_sin(angle*M_PIsur180));
	UpVector = CrossProduct(&EyeDir, &RightVector)*-1;
}

/*
void CCamera::RotateX(float angle)
{
	RotX += angle;
	// On fait tourner EyeDir autours de RIGHT_VECTOR
	EyeDir = Normalize(EyeDir*cos(angle*M_PIsur180) + UpVector*sin(angle*M_PIsur180));
	// On calcul le nouveau vecteur orthogonal
	UpVector = CrossProduct(&EyeDir, &RightVector)*-1;
}

void CCamera::RotateY(float angle)
{
	RotY += angle;
	EyeDir = Normalize(EyeDir*cos(angle*M_PIsur180) - RightVector*sin(angle*M_PIsur180));
	RightVector = CrossProduct(&EyeDir, &UpVector);
}

void CCamera::RotateZ(float angle)
{
	RotZ += angle;
	RightVector = Normalize(RightVector*cos(angle*M_PIsur180) + UpVector*sin(angle*M_PIsur180));
	UpVector = CrossProduct(&EyeDir, &RightVector)*-1;
}
*/

// Faire le rendu à l'ecran grace a gluLookAt
void CCamera::Rendering()
{
	CVector3 ViewPoint = Position+EyeDir;

	gluLookAt(	Position.x,Position.y,Position.z,      // eye
				      ViewPoint.x,ViewPoint.y,ViewPoint.z,   // centre
				      UpVector.x,UpVector.y,UpVector.z);     // up
}

// Ne pas oublier que c caméra face a objet fixe ...
// enfin, supposé fixe pendant le mouvement !
// oué, je m'etais gourré :-(
void CCamera::MoveForward(float len)
{
	Position = Position + (EyeDir*(-len));
}

void CCamera::StrafeRight (float len)
{
	Position = Position + (RightVector*len);
}

void CCamera::MoveUpward(float len)
{
	Position = Position + (UpVector*(-len));
}

void CCamera::StrafeLeft (float len)
{
	Position = Position + (RightVector*(-len));
}

void CCamera::Move (CVector3 dir)
{
	Position = Position + dir;
}

void CCamera::GetEyeDir(float &u, float &v, float &w, float &z)
{
  Position.GetValues(u,v,w);
  z = sqrt(u*u+v*v+w*w);
}

/************************************************
*
* 23-01-2004: Creation du source (Romain)
*             Debugage des rotations
*             (fonction m_sin/m_cos p-ê pas assez
*             précises)
*
***********************************************/
