#include "Camera.h"
#include "GL/glut.h"

Camera::Camera(float eyeX, float eyeY, float eyeZ, float centerX, float centerY, float centerZ, float upX, float upY, float upZ)
{
	this->eye = Vec3f(eyeX, eyeY, eyeZ);
	this->center = Vec3f(centerX, centerY, centerZ);
	this->up = Vec3f(upX, upY, upZ);

	viewVec = Vec3f(&eye, &center);
	viewVec.normalize();
}

Camera::Camera(Vec3f eye, Vec3f centerAt, Vec3f up)
{
	this->eye = eye;
	this->center = center;
	this->up = up;

	viewVec = Vec3f(&center, &eye);
	viewVec.normalize();
}

Camera::~Camera()
{

}

void Camera::set(Vec3f eye, Vec3f center, Vec3f up)
{
	this->eye = eye;
	this->center = center;
	this->up = up;

	viewVec = Vec3f(&center, &eye);
	viewVec.normalize();
}

Vec3f Camera::getPosition()
{
	return eye;
}

void Camera::rotateX(float angle)
{
up.rotate(angle * radToDeg, &viewVec);
}

void Camera::rotateY(float angle)
{
	viewVec.rotate(angle * radToDeg, &up);
	center = eye;
	center.add(viewVec);
}

void Camera::rotateZ(float angle)
{
	Vec3f right = viewVec;
	right.crossToSelf(&up);
	viewVec.rotate(angle * radToDeg, &right);
	center = eye;
	center.add(viewVec);
}

void Camera::move(float x, float y, float z)
{
	Vec3f right = viewVec;
	right.crossToSelf(&up);

	Vec3f delta = Vec3f(viewVec.x * z + up.x * y + right.x *x,
						viewVec.y * z + up.y * y + right.y *x,
						viewVec.z * z + up.z * y + right.z *x);
	eye.add(delta);
	center.add(delta);

	Vec3f newViewVec = Vec3f(&eye, &center);
	newViewVec.normalize();
	viewVec = newViewVec;
}


void Camera::render()
{
	gluLookAt(	eye.x, eye.y, eye.z, 
				center.x, center.y, center.z, 
				up.x, up.y, up.z);
}

