/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

#include "MorphableCamera.hpp"

namespace OpenVulkano::Scene
{
	namespace
	{
		Math::Matrix4f BlendMatrices(const Math::Matrix4f& mat1, const Math::Matrix4f& mat2, float t)
		{
			Math::Matrix4f result;
			for (int i = 0; i < 4; ++i)
			{
				result[i] = Math::Utils::mix(mat1[i], mat2[i], t);
			}
			return result;
		}
	}
	void MorphableCamera::UpdateProjectionMatrix()
	{
		if (m_morphState == 0) PerspectiveCamera::UpdateProjectionMatrix();
		if (m_morphState != 0)
		{
			const float scale = 0.5f * m_contentScaleFactor * m_zoom;
			const float widthHalf = m_width * scale, heightHalf = m_height * scale;
			Math::Matrix4f orthoMatrix = Math::Utils::orthoRH_ZO(-widthHalf, widthHalf, -heightHalf, heightHalf, m_nearPlane, m_farPlane);
			if (m_morphState == 1)
			{
				SetProjectionMatrix(orthoMatrix);
			}
			else
			{
				Math::Matrix4f projection = Math::Utils::perspectiveRH_ZO(m_fov, m_aspect, m_nearPlane, m_farPlane);
				SetProjectionMatrix(BlendMatrices(projection, orthoMatrix, m_morphState));
			}
		}
	}
}
