84 lines
2.6 KiB
C++
84 lines
2.6 KiB
C++
//
|
|
// Created by sebastian on 14.02.26.
|
|
//
|
|
|
|
#include "MinimapRenderer.h"
|
|
|
|
#include "../../game/GameMode.h"
|
|
#include "../../game/hexWorld/MapGenerator.h"
|
|
#include "../core/ECS/ModelComponent.h"
|
|
#include "../core/ECS/TransformComponent.h"
|
|
#include "../toolbox/MathUtils.h"
|
|
#include "glm/ext/matrix_clip_space.hpp"
|
|
#include "glm/ext/matrix_transform.hpp"
|
|
|
|
|
|
void MinimapRenderer::render(const std::vector<MinimapRenderData> &renderData, std::unordered_map<PlayerID, glm::vec3> colorMapping) {
|
|
minimapFBO->bind();
|
|
glViewport(0,0, width, height);
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
glDisable(GL_DEPTH_TEST);
|
|
|
|
|
|
prepareShader();
|
|
for (const MinimapRenderData& renderDatum : renderData) {
|
|
prepareInstance(renderDatum, colorMapping);
|
|
|
|
for (const auto& submodel : renderDatum.modelComponent->getActiveModel()->getSubModels()) {
|
|
glBindVertexArray(submodel.rawModel->vaoID);
|
|
glEnableVertexAttribArray(0);
|
|
|
|
glDrawElements(GL_TRIANGLES, submodel.rawModel->vertexCount, GL_UNSIGNED_INT, 0);
|
|
|
|
glDisableVertexAttribArray(0);
|
|
glBindVertexArray(0);
|
|
}
|
|
|
|
}
|
|
finalizeShader();
|
|
minimapFBO->unbind();
|
|
|
|
glViewport(0,0, Application::getInstance().getWindow().GetWidth(), Application::getInstance().getWindow().GetHeight());
|
|
glEnable(GL_DEPTH_TEST);
|
|
}
|
|
|
|
GLuint MinimapRenderer::getMinimapTexture() {
|
|
return minimapFBO->getColorTexture();
|
|
}
|
|
|
|
void MinimapRenderer::prepareShader() {
|
|
minimapShader.start();
|
|
|
|
MapBounds bounds = MapGenerator::getMapBounds();
|
|
|
|
|
|
float mapWidth = bounds.maxX - bounds.minX;
|
|
float mapHeight = bounds.maxZ - bounds.minZ;
|
|
glm::vec3 mapCenter(
|
|
bounds.minX + mapWidth * 0.5f,
|
|
50.0f,
|
|
bounds.minZ + mapHeight * 0.5f
|
|
);
|
|
|
|
glm::mat4 view = glm::lookAt(mapCenter + glm::vec3(0,50,0), mapCenter, glm::vec3(0,0,1));
|
|
glm::mat4 proj = glm::ortho(-mapWidth * 0.5f, mapWidth * 0.5f, -mapHeight * 0.5f, mapHeight * 0.5f, -100.f, 100.0f);
|
|
|
|
minimapShader.loadViewProjectionMatrix(proj * view);
|
|
}
|
|
|
|
void MinimapRenderer::finalizeShader() {
|
|
minimapShader.stop();
|
|
}
|
|
|
|
void MinimapRenderer::prepareInstance(const MinimapRenderData &data, const std::unordered_map<PlayerID, glm::vec3>& colorMapping) {
|
|
glm::mat4 model = MathUtils::createTransformationMatrix(data.transform->position, data.transform->rotation.x, data.transform->rotation.y, data.transform->rotation.z, data.transform->scale);
|
|
minimapShader.loadTransformationMatrix(model);
|
|
|
|
auto color = glm::vec3(0.5f);
|
|
if (data.owner) {
|
|
color = colorMapping.at(data.owner->playerID);
|
|
}
|
|
minimapShader.loadColor(color);
|
|
}
|