//
// Created by sebastian on 08.02.26.
//

#include "MasterRenderer.h"

#include "../core/Application.h"
#include "glm/ext/matrix_clip_space.hpp"

void MasterRenderer::render(const Light &light, const Camera &camera) {
    glEnable(GL_DEPTH_TEST);
    glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    entityRenderer->prepare(camera, light);
    entityRenderer->renderEntities(entities);
    entityRenderer->finalizeFrame();
    entities.clear();

    terrainRenderer->prepare(camera, light);
    terrainRenderer->renderTerrainTiles(terrainTiles);
    terrainRenderer->finalizeFrame();
    terrainTiles.clear();

}

void MasterRenderer::submitEntity(Entity *entity) {
    TexturedModel* entityModel = entity->getModel().get();
    entities[entityModel].push_back(entity);
}

void MasterRenderer::submitTerrainTile(HexRenderData *tile) {
    terrainTiles[tile->model.get()].push_back(tile);
}


glm::mat4 MasterRenderer::createProjectionMatrix() {
    float aspectRatio = static_cast<float>(Application::getInstance().getWindow().GetWidth()) / static_cast<float>(Application::getInstance().getWindow().GetHeight());

    glm::mat4 projection = glm::perspective(
        glm::radians(FOV),
        aspectRatio,
        NEAR_PLANE,
        FAR_PLANE
    );

    return projection;
}
