////////////////////////////////////////////////////////////////////////////////
// Filename: cameraclass.cpp
////////////////////////////////////////////////////////////////////////////////
#include "camera_class.h"

camera_class::camera_class()
{
	position_x_ = 0.0f;
	position_y_ = 0.0f;
	position_z_ = 0.0f;

	rotation_x_ = 0.0f;
	rotation_y_ = 0.0f;
	rotation_z_ = 0.0f;
}


camera_class::camera_class(const camera_class& other)
{
}


camera_class::~camera_class()
{
}

void camera_class::set_position(float x, float y, float z)
{
	position_x_ = x;
	position_y_ = y;
	position_z_ = z;
	return;
}


void camera_class::set_rotation(float x, float y, float z)
{
	rotation_x_ = x;
	rotation_y_ = y;
	rotation_z_ = z;
	return;
}

XMFLOAT3 camera_class::get_position()
{
	return XMFLOAT3(position_x_, position_y_, position_z_);
}


XMFLOAT3 camera_class::get_rotation()
{
	return XMFLOAT3(rotation_x_, rotation_y_, rotation_z_);
}

void camera_class::render()
{
	XMFLOAT3 up, position, lookAt;
	XMVECTOR upVector, positionVector, lookAtVector;
	float yaw, pitch, roll;
	XMMATRIX rotationMatrix;


	// Setup the vector that points upwards.
	up.x = 0.0f;
	up.y = 1.0f;
	up.z = 0.0f;

	// Load it into a XMVECTOR structure.
	upVector = XMLoadFloat3(&up);

	// Setup the position of the camera in the world.
	position.x = position_x_;
	position.y = position_y_;
	position.z = position_z_;

	// Load it into a XMVECTOR structure.
	positionVector = XMLoadFloat3(&position);

	// Setup where the camera is looking by default.
	lookAt.x = 0.0f;
	lookAt.y = 0.0f;
	lookAt.z = 1.0f;

	// Load it into a XMVECTOR structure.
	lookAtVector = XMLoadFloat3(&lookAt);

	// Set the yaw (Y axis), pitch (X axis), and roll (Z axis) rotations in radians.
	pitch = rotation_x_ * 0.0174532925f;
	yaw = rotation_y_ * 0.0174532925f;
	roll = rotation_z_ * 0.0174532925f;

	// Create the rotation matrix from the yaw, pitch, and roll values.
	rotationMatrix = XMMatrixRotationRollPitchYaw(pitch, yaw, roll);

	// Transform the lookAt and up vector by the rotation matrix so the view is correctly rotated at the origin.
	lookAtVector = XMVector3TransformCoord(lookAtVector, rotationMatrix);
	upVector = XMVector3TransformCoord(upVector, rotationMatrix);

	// Translate the rotated camera position to the location of the viewer.
	lookAtVector = XMVectorAdd(positionVector, lookAtVector);

	// Finally create the view matrix from the three updated vectors.
	view_matrix_ = XMMatrixLookAtLH(positionVector, lookAtVector, upVector);

	return;
}

XMMATRIX camera_class::get_view_matrix(XMMATRIX& view_matrix) const
{
	view_matrix = view_matrix_;
	return view_matrix;
}

void camera_class::render_reflection(float height)
{
	XMFLOAT3 up, position, lookAt;
	XMVECTOR upVector, positionVector, lookAtVector;
	float yaw, pitch, roll;
	XMMATRIX rotationMatrix;


	// Setup the vector that points upwards.
	up.x = 0.0f;
	up.y = 1.0f;
	up.z = 0.0f;

	// Load it into a XMVECTOR structure.
	upVector = XMLoadFloat3(&up);

	// Setup the position of the camera in the world.
	position.x = position_x_;
	position.y = -position_y_ + (height * 2.0f);
	position.z = position_z_;

	// Load it into a XMVECTOR structure.
	positionVector = XMLoadFloat3(&position);

	// Setup where the camera is looking by default.
	lookAt.x = 0.0f;
	lookAt.y = 0.0f;
	lookAt.z = 1.0f;

	// Load it into a XMVECTOR structure.
	lookAtVector = XMLoadFloat3(&lookAt);

	// Set the yaw (Y axis), pitch (X axis), and roll (Z axis) rotations in radians.
	pitch = (-1.0f * rotation_x_) * 0.0174532925f;  // Invert for reflection
	yaw = rotation_y_ * 0.0174532925f;
	roll = rotation_z_ * 0.0174532925f;

	// Create the rotation matrix from the yaw, pitch, and roll values.
	rotationMatrix = XMMatrixRotationRollPitchYaw(pitch, yaw, roll);

	// Transform the lookAt and up vector by the rotation matrix so the view is correctly rotated at the origin.
	lookAtVector = XMVector3TransformCoord(lookAtVector, rotationMatrix);
	upVector = XMVector3TransformCoord(upVector, rotationMatrix);

	// Translate the rotated camera position to the location of the viewer.
	lookAtVector = XMVectorAdd(positionVector, lookAtVector);

	// Finally create the view matrix from the three updated vectors.
	reflection_view_matrix_ = XMMatrixLookAtLH(positionVector, lookAtVector, upVector);

	return;
}

void camera_class::get_reflection_view_matrix(XMMATRIX& reflectionViewMatrix) const
{
	reflectionViewMatrix = reflection_view_matrix_;
	return;
}