From af1e96ce6d167d9f5f7fe1044a87aff94993590b Mon Sep 17 00:00:00 2001 From: miaurizius Date: Thu, 16 Apr 2026 02:04:32 +0200 Subject: [PATCH] added perspective --- CMakeLists.txt | 1 + shaders/basic.vert | 4 ++-- src/main.cpp | 21 +++++++++++++++------ 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b8fa5d7..02a17ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,7 @@ add_executable(${PROJECT_NAME} src/main.cpp target_compile_definitions(${PROJECT_NAME} PRIVATE $<$:_DEBUG>) target_include_directories(${PROJECT_NAME} PRIVATE include) +target_include_directories(${PROJECT_NAME} PRIVATE libs) target_include_directories(${PROJECT_NAME} PRIVATE shaders) add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD diff --git a/shaders/basic.vert b/shaders/basic.vert index 4906e06..a4210e6 100644 --- a/shaders/basic.vert +++ b/shaders/basic.vert @@ -7,10 +7,10 @@ layout(location = 2) in vec4 a_color; out vec4 v_color; out vec2 v_texCoord; -uniform mat4 u_model; +uniform mat4 u_modelViewProj; void main() { - gl_Position = u_model * vec4(a_position, 1.0f); + gl_Position = u_modelViewProj * vec4(a_position, 1.0f); v_color = a_color; v_texCoord = a_texCoord; } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index b236918..553a4ad 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,9 +9,10 @@ #include "VertexBuffer.hpp" #include "Shader.hpp" #define STB_IMAGE_IMPLEMENTATION -#include "../libs/stb_image.hpp" -#include "../libs/glm/glm.hpp" -#include "../libs/glm/ext/matrix_transform.hpp" +#include "stb_image.hpp" +#include "glm/glm.hpp" +#include "glm/ext/matrix_transform.hpp" +#include "glm/gtc/matrix_transform.hpp" void openGLDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam) { if (severity == GL_DEBUG_SEVERITY_NOTIFICATION) return; //ignore notifications (mostly performance information) @@ -165,10 +166,17 @@ int main() { uint64 lastCounter = SDL_GetPerformanceCounter(); float32 delta = 0.0f; - glm::mat4 model = glm::mat4(1.0f); + auto model = glm::mat4(1.0f); model = glm::scale(model, glm::vec3(1.2f)); - int modelMatrixLocation = glGetUniformLocation(shader.getShaderId(), "u_model"); + auto projection = glm::ortho(-2.0f, 2.0f, -2.0f, 2.0f, -10.0f, 100.0f); + projection = glm::perspective(glm::radians(45.0f), 4.0f/3.0f, 0.1f, 100.0f); + + auto view = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -3.0f)); + + auto modelViewProj = projection * view * model; + + int modelViewProjMatrixLocation = glGetUniformLocation(shader.getShaderId(), "u_modelViewProj"); // WIREFRAME //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); @@ -183,6 +191,7 @@ int main() { time += delta; model = glm::rotate(model, 1.0f*delta, glm::vec3(0.0f, 1.0f, 0.0f)); + modelViewProj = projection * view * model; if (colorUniformLocation != 1) { glUniform4f(colorUniformLocation, sinf(time)*sinf(time), 0.0f, 1.0f, 1.0f); @@ -191,7 +200,7 @@ int main() { // FRAME GENERATION vertexBuffer.bind(); indexBuffer.bind(); - glUniformMatrix4fv(modelMatrixLocation, 1, GL_FALSE, &model[0][0]); + glUniformMatrix4fv(modelViewProjMatrixLocation, 1, GL_FALSE, &modelViewProj[0][0]); //glActiveTexture(GL_TEXTURE0); //glBindTexture(GL_TEXTURE_2D, textureId); glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, 0);