//
// Created by sebastian on 29.07.26.
//

#include "MeshRenderer.h"

#include "entities/PointLight.h"

void engine::MeshRenderer::render(RenderQueue &queue, const Camera &camera, const PointLight& light) {
    m_shader->start();
    m_shader->loadViewMatrix(camera.getViewMatrix());
    m_shader->loadProjectionMatrix(camera.getProjectionMatrix());
    m_shader->loadCameraPosition(camera.getPosition());
    m_shader->loadLight(light.position, light.color);

     for (const auto&[mesh, transformationMatrix, tintColor, tintIntensity] : queue.getRenderCommands()) {
         m_shader->loadTransformationMatrix(transformationMatrix);
         m_shader->loadHighlightEffect(tintColor, tintIntensity);
         mesh->bind();
         if (mesh->hasSections()) {
             for (const auto& section : mesh->getSections()) {
                 m_shader->loadMaterial(*section.material);
                 section.material->bind();
                 mesh->drawSectionRange(section.indexOffset, section.indexCount);
             }
         } else {
             mesh->draw();
         }

         mesh->unbind();
    }

    m_shader->stop();
}
