#include "OGLPlatformSpecific.h"
#include "cCamera.h"
#include <math.h>

cCamera::cCamera(cBall* ball)
{
    ball_ = ball;
    phi_ = 3.1415;
    theta_ = -3.1415/4;
    radius_ = 100;
    lookAt_ = ball_->position_;
    position_ = lookAt_ + cVector(	-radius_*cos(theta_)*sin(phi_), 
                                        -radius_*sin(theta_),
                                        radius_*cos(phi_)*cos(theta_));
    upDir_ = cVector(0,1,0);
}

void cCamera::setBall(cBall* ball)
{
    ball_ = ball;
    lookAt_ = ball_->position_;
    position_ = lookAt_ + cVector(	-radius_*cos(theta_)*sin(phi_), 
                                        -radius_*sin(theta_),
                                        radius_*cos(phi_)*cos(theta_));
}

void cCamera::update(float dt)
{
    
    cout << theta_ << " " << phi_ << endl;
    lookAt_ = (ball_->position_ + 15 * lookAt_) / 16;

    position_ = lookAt_ + cVector(	-radius_*cos(theta_)*sin(phi_), 
                                        -radius_*sin(theta_),
                                        radius_*cos(phi_)*cos(theta_));
    cVector lookAtToPos = (position_ - lookAt_);
    lookAtToPos.normalize();
    upDir_ = cVector(0,1,0) - lookAtToPos;
    upDir_.normalize();
    }

void cCamera::look()
{
    glLoadIdentity(); // clear the last transformation
    gluLookAt(	position_.x_, position_.y_, position_.z_,
                lookAt_.x_, lookAt_.y_, lookAt_.z_,
                upDir_.x_, upDir_.y_, upDir_.z_);
}
