ADD: Apply Projection matrices

This commit is contained in:
sebastian 2026-02-07 18:19:25 +01:00
parent 6f0d8852fa
commit 0871202db9
7 changed files with 39 additions and 2 deletions

View File

@ -6,9 +6,10 @@ in vec2 textureCoords;
out vec2 pass_textureCoords; out vec2 pass_textureCoords;
uniform mat4 transformationMatrix; uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
void main() void main()
{ {
gl_Position = transformationMatrix * vec4(position, 1.0f); gl_Position = projectionMatrix * transformationMatrix * vec4(position, 1.0f);
pass_textureCoords = textureCoords; pass_textureCoords = textureCoords;
} }

View File

@ -22,6 +22,7 @@ public:
void close() {running = false;} void close() {running = false;}
static Application& getInstance(); static Application& getInstance();
[[nodiscard]] Window& getWindow() const {return *window;}
private: private:
bool running = true; bool running = true;
std::unique_ptr<Window> window; std::unique_ptr<Window> window;

View File

@ -36,7 +36,7 @@ void GameLayer::onAttach()
RawModel model = loader.loadToVAO(vertices, textureCoords, indices); RawModel model = loader.loadToVAO(vertices, textureCoords, indices);
ModelTexture texture = ModelTexture(loader.loadTexture("assets/textures/texture.png")); ModelTexture texture = ModelTexture(loader.loadTexture("assets/textures/texture.png"));
texturedModel = TexturedModel(std::make_shared<RawModel>(model), std::make_shared<ModelTexture>(texture)); texturedModel = TexturedModel(std::make_shared<RawModel>(model), std::make_shared<ModelTexture>(texture));
entity = std::make_unique<Entity>(Entity(std::make_shared<TexturedModel>(texturedModel), glm::vec3(-1,0,0), 0,0,0, 1.f)); entity = std::make_unique<Entity>(Entity(std::make_shared<TexturedModel>(texturedModel), glm::vec3(0,0,-1), 0,0,0, 1.f));
} }
void GameLayer::onDetach() void GameLayer::onDetach()
@ -46,6 +46,7 @@ void GameLayer::onDetach()
void GameLayer::onUpdate() void GameLayer::onUpdate()
{ {
entity->increasePosition(0,0,-0.002f);
renderer.renderFrame(*entity); renderer.renderFrame(*entity);
} }

View File

@ -4,8 +4,10 @@
#include "Renderer.h" #include "Renderer.h"
#include "../core/Application.h"
#include "../toolbox/MathUtils.h" #include "../toolbox/MathUtils.h"
#include "glad/glad.h" #include "glad/glad.h"
#include "glm/ext/matrix_clip_space.hpp"
#include "model/TexturedModel.h" #include "model/TexturedModel.h"
void Renderer::prepare() { void Renderer::prepare() {
@ -38,3 +40,16 @@ void Renderer::renderRawModel(const Entity& entity) {
glBindVertexArray(0); 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;
}

View File

@ -12,11 +12,23 @@
class Renderer { class Renderer {
public: public:
Renderer() : projectionMatrix(createProjectionMatrix()) {
staticShader.start();
staticShader.loadProjectionMatrix(projectionMatrix);
staticShader.stop();
};
void prepare(); void prepare();
void renderFrame(const Entity &entity); void renderFrame(const Entity &entity);
private: private:
void renderRawModel(const Entity &entity); void renderRawModel(const Entity &entity);
StaticShader staticShader; StaticShader staticShader;
glm::mat4 projectionMatrix;
constexpr static float FOV = 70.0f;
constexpr static float NEAR_PLANE = 0.1f;
constexpr static float FAR_PLANE = 1000.0f;
static glm::mat4 createProjectionMatrix();
}; };

View File

@ -13,6 +13,10 @@ void StaticShader::loadTransformationMatrix(glm::mat4 matrix) {
loadMatrix(location_transformationMatrix, matrix); loadMatrix(location_transformationMatrix, matrix);
} }
void StaticShader::loadProjectionMatrix(glm::mat4 matrix) {
loadMatrix(location_projectionMatrix, matrix);
}
void StaticShader::bindAttributes() const { void StaticShader::bindAttributes() const {
bindAttribute(0, "position"); bindAttribute(0, "position");
bindAttribute(1, "textureCoords"); bindAttribute(1, "textureCoords");
@ -20,6 +24,7 @@ void StaticShader::bindAttributes() const {
void StaticShader::getAllUniformLocations() { void StaticShader::getAllUniformLocations() {
location_transformationMatrix = getUniformLocation("transformationMatrix"); location_transformationMatrix = getUniformLocation("transformationMatrix");
location_projectionMatrix = getUniformLocation("projectionMatrix");
} }

View File

@ -12,11 +12,13 @@ class StaticShader: public ShaderProgram
public: public:
StaticShader(); StaticShader();
void loadTransformationMatrix(glm::mat4 matrix); void loadTransformationMatrix(glm::mat4 matrix);
void loadProjectionMatrix(glm::mat4 matrix);
private: private:
inline static const std::string VERTEX_FILE = "assets/shaders/vertexShader.glsl"; inline static const std::string VERTEX_FILE = "assets/shaders/vertexShader.glsl";
inline static const std::string FRAGMENT_FILE = "assets/shaders/fragmentShader.glsl"; inline static const std::string FRAGMENT_FILE = "assets/shaders/fragmentShader.glsl";
int location_transformationMatrix; int location_transformationMatrix;
int location_projectionMatrix;
protected: protected:
void bindAttributes() const override; void bindAttributes() const override;