From 3fb766de7cd13c6f57443a9626a115a69e5b7d74 Mon Sep 17 00:00:00 2001 From: miaurizius Date: Thu, 16 Apr 2026 01:41:31 +0200 Subject: [PATCH] added rotation --- shaders/basic.frag | 2 +- shaders/basic.vert | 4 +++- src/main.cpp | 23 ++++++++++++++--------- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/shaders/basic.frag b/shaders/basic.frag index 7cc5906..7a4c151 100644 --- a/shaders/basic.frag +++ b/shaders/basic.frag @@ -10,5 +10,5 @@ uniform vec4 u_color; void main() { vec4 texColor = texture(u_texture, v_texCoord); - f_color = texColor; + f_color = v_color; } \ No newline at end of file diff --git a/shaders/basic.vert b/shaders/basic.vert index 1e0c942..4906e06 100644 --- a/shaders/basic.vert +++ b/shaders/basic.vert @@ -7,8 +7,10 @@ layout(location = 2) in vec4 a_color; out vec4 v_color; out vec2 v_texCoord; +uniform mat4 u_model; + void main() { - gl_Position = vec4(a_position, 1.0f); + gl_Position = u_model * 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 5505cf2..b236918 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,8 @@ #include "Shader.hpp" #define STB_IMAGE_IMPLEMENTATION #include "../libs/stb_image.hpp" +#include "../libs/glm/glm.hpp" +#include "../libs/glm/ext/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) @@ -105,22 +107,17 @@ int main() { Vertex{-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f}, - Vertex{-0.5f, 0.5f, 0.0f, + Vertex{0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0, 1.0f, 0.0f, 1.0f}, - Vertex{0.5f, -0.5f, 0.0f, + Vertex{0.0f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f}, - - Vertex{0.5f, 0.5f, 0.0f, - 1.0f, 1.0f, - 1.0f, 0.0f, 0.0f, 1.0f}, }; uint32 numVertices = sizeof(vertices) / sizeof(Vertex); uint32 indices[] = { 0, 1, 2, - 1, 2, 3 }; uint32 numIndices = sizeof(indices) / sizeof(uint32); @@ -168,6 +165,11 @@ int main() { uint64 lastCounter = SDL_GetPerformanceCounter(); float32 delta = 0.0f; + glm::mat4 model = glm::mat4(1.0f); + model = glm::scale(model, glm::vec3(1.2f)); + + int modelMatrixLocation = glGetUniformLocation(shader.getShaderId(), "u_model"); + // WIREFRAME //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); @@ -180,6 +182,8 @@ int main() { glClear(GL_COLOR_BUFFER_BIT); time += delta; + model = glm::rotate(model, 1.0f*delta, glm::vec3(0.0f, 1.0f, 0.0f)); + if (colorUniformLocation != 1) { glUniform4f(colorUniformLocation, sinf(time)*sinf(time), 0.0f, 1.0f, 1.0f); } @@ -187,8 +191,9 @@ int main() { // FRAME GENERATION vertexBuffer.bind(); indexBuffer.bind(); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, textureId); + glUniformMatrix4fv(modelMatrixLocation, 1, GL_FALSE, &model[0][0]); + //glActiveTexture(GL_TEXTURE0); + //glBindTexture(GL_TEXTURE_2D, textureId); glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, 0); vertexBuffer.unbind(); indexBuffer.unbind();