/*
	Trampoline Sim.
	Copyright (c) 2009, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL DARRAGH COY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

//=====================================================================================================================
/// @brief		Represents a 3D camera.
//=====================================================================================================================

#include "Precompiled.h"
#include "Graphics\Camera3D.h"
#include "Windows\Window.h"

using namespace Game;

//=====================================================================================================================
/// @brief		Creates a 3D camera
/// @param		window - window the camera's view will be in
/// @param		position - initial position of the camera
/// @param		z_near - near plane z value
/// @param		z_far - far plane z value
/// @pre		Given window cannot be null
//=====================================================================================================================

Camera3D::Camera3D( Window * window , const Vec3f & position , float z_near , float z_far )
{
	// Make sure ok:

	ENSURE( window , "Given window cannot be null" );

	// Save all params:

	m_window	= window;
	m_z_near	= z_near;
	m_z_far		= z_far;

	// Initialize orientation matrix to identity

	m_orientation = Matrix4f::Identity();

	// Set our position:

	SetPosition(position);
}

//=====================================================================================================================
/// @brief		Annouces the type name for this object.
/// @returns	Type name of this object
//=====================================================================================================================

const char * Camera3D::GetTypeName() const
{
	return "Camera3D";
}

//=====================================================================================================================
/// @brief	Interpolates the camera towards the given position
/// @param	position - position to interpolate to
/// @param	amount - amount of interpolation to do from 0-1
//=====================================================================================================================

void Camera3D::LerpTo( const Vec3f & position , float amount )
{
	// Do the lerp:

	Vec3f p = GetPosition();
	
	p *= 1.0f		- amount; 
	p += position	* amount;

	// Set the new position

	SetPosition(p);
}

//=====================================================================================================================
/// @brief		Rotates the camera about the world x-axis:
/// @param		amount - amount to rotate by in radians
//=====================================================================================================================

void Camera3D::RotateX( float amount )
{
	// Do it
	
	m_orientation = Matrix4f::RotationX(amount) * m_orientation;
}

//=====================================================================================================================
/// @brief		Rotates the camera about the world y-axis:
/// @param		amount - amount to rotate by in radians
//=====================================================================================================================

void Camera3D::RotateY( float amount )
{
	// Do it
	
	m_orientation = Matrix4f::RotationY(amount) * m_orientation;
}

//=====================================================================================================================
/// @brief		Rotates the camera about the world z-axis:
/// @param		amount - amount to rotate by in radians
//=====================================================================================================================

void Camera3D::RotateZ( float amount )
{
	// Do it
	
	m_orientation = Matrix4f::RotationZ(amount) * m_orientation;
}

//=====================================================================================================================
/// @brief		Rotates the camera about it's local x-axis:
/// @param		amount - amount to rotate by in radians
//=====================================================================================================================

void Camera3D::RotateLocalX( float amount )
{
	// Do it
	
	m_orientation = m_orientation * Matrix4f::RotationX(amount);
}

//=====================================================================================================================
/// @brief		Rotates the camera about the it's local y-axis:
/// @param		amount - amount to rotate by in radians
//=====================================================================================================================

void Camera3D::RotateLocalY( float amount )
{
	// Do it
	
	m_orientation = m_orientation * Matrix4f::RotationY(amount);
}

//=====================================================================================================================
/// @brief		Rotates the camera about the it's local z-axis:
/// @param		amount - amount to rotate by in radians
//=====================================================================================================================

void Camera3D::RotateLocalZ( float amount )
{
	// Do it
	
	m_orientation = m_orientation * Matrix4f::RotationZ(amount);
}

//=====================================================================================================================
/// @brief		Moves the camera in world coordinates
/// @param		amount - amount to move by
//=====================================================================================================================

void Camera3D::Move( const Vec3f & amount )
{
	// Move:

	SetPosition( GetPosition() + amount );
}
//=====================================================================================================================
/// @brief		Moves the camera relative to it's direction
/// @pre		amount - amount to move by
//=====================================================================================================================

void Camera3D::MoveLocal( const Vec3f & amount )
{
	// Get the three axes of the camera
	
	Vec3f x = m_orientation.GetRow(0).ToVec3();
	Vec3f y = m_orientation.GetRow(1).ToVec3();
	Vec3f z = m_orientation.GetRow(2).ToVec3();
	
	// Dot product with the movement amount:
	
	float mx = x.Dot(amount);
	float my = y.Dot(amount);
	float mz = z.Dot(amount);
	
	// Move in world coords
	
	Move(Vec3f(mx,my,mz));
}

//=====================================================================================================================
/// @brief		Looks at the given position:
/// @param		position - position to look at
//=====================================================================================================================

void Camera3D::LookAt( const Vec3f & position )
{
	// Do up the orientational part

	m_orientation = Matrix4f::LookAt( m_position , position , m_orientation.GetCol(1).ToVec3() );

	// Clear the translational part
	
	m_orientation.SetRow(3,Vec4f::UnitW());
}

//=====================================================================================================================
/// @brief		Gets the view matrix used by the camera
/// @returns	View matrix used by the camera
//=====================================================================================================================

Matrix4f Camera3D::GetViewMatrix() const
{
	// Return our viewing matrix

	return Matrix4f::Translation(-m_position) * m_orientation;
}

//=====================================================================================================================
/// @brief		Gets the projection matrix used by the camera
/// @returns	Projection matrix used by the camera
//=====================================================================================================================

Matrix4f Camera3D::GetProjectionMatrix() const
{
	// Get window size
	
	Vec2u size = m_window->GetClientSize();

	// Get the aspect ratio:
	
	float a = float(size.x) / float(size.y);

	// Return the projection matrix:

	return Matrix4f::Perspective(a,1.0f,m_z_near,m_z_far);
}

//=====================================================================================================================
/// @brief		Returns a combined world and projection matrix for the camera
/// @returns	Combined world and view matrix for the camera
//=====================================================================================================================

Matrix4f Camera3D::GetViewProjectionMatrix() const
{
	// Get the view and projection matrices:
	
	Matrix4f p = GetProjectionMatrix();

	// Concatenate and return answer
	
	return GetViewMatrix() * p;
}
