//
// Created by sfd on 25-5-28.
//

#include "ScenceCamera.h"

#include <glm/ext/matrix_clip_space.hpp>

namespace  Hazel
{
    ScenceCamera::ScenceCamera()
    {
        RecalculateProjection();
    }

    void ScenceCamera::SetOrthographic(const float size, const float nearClip, const float farClip)
    {
        m_ProjectionType = ProjectionType::Orthographic;
        m_OrthographicSize = size;
        m_OrthographicNear = nearClip;
        m_OrthographicFar = farClip;

        RecalculateProjection();
    }

    void ScenceCamera::SetPerspective(const float verticalFOV, const float nearClip, const float farClip)
    {
        m_ProjectionType = ProjectionType::Perspective;
        m_PerspectiveFOV = verticalFOV;
        m_PerspectiveNear = nearClip;
        m_PerspectiveFar = farClip;

        RecalculateProjection();
    }

    void ScenceCamera::SetViewPortSize(uint32_t width, uint32_t height)
    {
        m_AspectRatio = (float)width / (float)height;

        RecalculateProjection();
    }

    void ScenceCamera::RecalculateProjection()
    {
        if (m_ProjectionType == ProjectionType::Perspective)
        {
            m_Projection = glm::perspective(m_PerspectiveFOV, m_AspectRatio, m_PerspectiveNear, m_PerspectiveFar);
        }
        else
        {
            const float orthoLeft   = -m_OrthographicSize * m_AspectRatio * 0.5f;
            const float orthoRight  = m_OrthographicSize * m_AspectRatio * 0.5f;
            const float orthoBottom = -m_OrthographicSize * 0.5f;
            const float orthoTop    = m_OrthographicSize * 0.5f;


            m_Projection = glm::ortho(orthoLeft, orthoRight, orthoBottom, orthoTop, m_OrthographicNear,
                                      m_OrthographicFar);
        }
    }
}
