Dicewars-Siedler/src/engine/renderer/Renderer.cpp
Sebastian Böckelmann 738d424df9
All checks were successful
Tests / test (push) Successful in 2m33s
ADD: WindowResizeEvent on Window Resizing; remove dependency of Application for renderers
2026-04-20 10:12:46 +02:00

76 lines
2.3 KiB
C++

//
// Created by sebastian on 24.12.25.
//
#include "Renderer.h"
#include <ranges>
#include "../core/Application.h"
#include "../layer/entities/Light.h"
#include "../toolbox/MathUtils.h"
#include "glad/glad.h"
#include "glm/ext/matrix_clip_space.hpp"
#include "model/TexturedModel.h"
void Renderer::prepare(const Camera& camera, const Light& light, glm::mat4 projectionMatrix) {
const glm::mat4 viewMatrix = MathUtils::createViewMatrix(camera);
staticShader.start();
staticShader.loadLight(light.getPosition(), light.getColor());
staticShader.loadViewMatrix(viewMatrix);
staticShader.loadProjectionMatrix(projectionMatrix);
}
void Renderer::renderEntities(const std::unordered_map<TexturedModel *, std::vector<std::unique_ptr<Entity>>>& entities) {
for (const auto& [texturedModel, batch] : entities) {
for (const auto& subModel : texturedModel->getSubModels()) {
prepareTexturedModel(subModel);
for (const auto& entity : batch) {
prepareInstance(*entity);
glDrawElements(GL_TRIANGLES, subModel.rawModel->vertexCount, GL_UNSIGNED_INT, 0);
}
unbindTexturedModel();
}
}
}
void Renderer::prepareTexturedModel(const SubModel &subModel) {
auto rawModel = subModel.rawModel;
glBindVertexArray(rawModel->vaoID);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, subModel.texture->getTextureID());
}
void Renderer::unbindTexturedModel() {
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glBindVertexArray(0);
}
void Renderer::prepareInstance(const Entity &entity) {
glm::mat4 transformationMatrix = MathUtils::createTransformationMatrix(entity.getPosition(), entity.getRotX(), entity.getRotY(), entity.getRotZ(), entity.getScale());
staticShader.loadTransformationMatrix(transformationMatrix);
if (entity.getRenderState()) {
staticShader.loadGhostColor(entity.getRenderState()->ghostColor);
staticShader.loadGhostMode(entity.getRenderState()->ghostMode);
staticShader.loadPulse(entity.getRenderState()->pulse);
}
}
void Renderer::finalizeFrame() {
staticShader.stop();
}