61 lines
1.8 KiB
C++
61 lines
1.8 KiB
C++
//
|
|
// Created by sebastian on 24.12.25.
|
|
//
|
|
|
|
#include "Renderer.h"
|
|
|
|
#include "../core/Application.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) {
|
|
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
const glm::mat4 viewMatrix = MathUtils::createViewMatrix(camera);
|
|
staticShader.start();
|
|
staticShader.loadViewMatrix(viewMatrix);
|
|
staticShader.stop();
|
|
}
|
|
|
|
void Renderer::renderFrame(const Entity &entity, const Camera& camera) {
|
|
prepare(camera);
|
|
staticShader.start();
|
|
renderRawModel(entity);
|
|
staticShader.stop();
|
|
}
|
|
|
|
void Renderer::renderRawModel(const Entity& entity) {
|
|
auto texturedModel = entity.getModel();
|
|
auto model = texturedModel->getRawModel();
|
|
glBindVertexArray(model->vaoID);
|
|
glEnableVertexAttribArray(0);
|
|
glEnableVertexAttribArray(1);
|
|
|
|
glm::mat4 transformationMatrix = MathUtils::createTransformationMatrix(entity.getPosition(), entity.getRotX(), entity.getRotY(), entity.getRotZ(), entity.getScale());
|
|
staticShader.loadTransformationMatrix(transformationMatrix);
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
glBindTexture(GL_TEXTURE_2D, texturedModel->getTexture()->getTextureID());
|
|
glDrawElements(GL_TRIANGLES , model->vertexCount, GL_UNSIGNED_INT, 0);
|
|
glDisableVertexAttribArray(0);
|
|
glDisableVertexAttribArray(1);
|
|
glBindVertexArray(0);
|
|
|
|
}
|
|
|
|
glm::mat4 Renderer::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;
|
|
}
|