From 06ae06f0d918fbc321102b7d1e4e128eaa243426 Mon Sep 17 00:00:00 2001 From: Archie Hilton Date: Tue, 14 May 2024 23:04:06 +0100 Subject: [PATCH] Add a proper coordinate system --- src/Quad.cc | 26 +++++++++++++++++++----- src/Quad.hh | 3 ++- src/Uniform.hh | 3 +-- src/main.cc | 44 +++++++++++++++++++---------------------- src/shaders/shader.vert | 15 ++++---------- 5 files changed, 48 insertions(+), 43 deletions(-) diff --git a/src/Quad.cc b/src/Quad.cc index 8bd1e8d..9bb2448 100644 --- a/src/Quad.cc +++ b/src/Quad.cc @@ -1,21 +1,37 @@ #include "Quad.hh" +#include "glm/detail/qualifier.hpp" +#include "glm/ext/matrix_transform.hpp" +#include "glm/gtc/type_ptr.hpp" +#include "glm/trigonometric.hpp" +#include +#include +#include #include #include +#include Quad::Quad(std::span verts, const Uniform &uniform_data) : m_mesh(verts) { - m_uniform_data.rotation = uniform_data.rotation; - m_uniform_data.world_pos = uniform_data.world_pos; + memcpy(&m_uniform_data, &uniform_data, sizeof(Uniform)); } glm::vec2 Quad::get_position_vector() { return glm::vec2(m_pos_x, m_pos_y); } -void Quad::draw(){ - glUniform2f(m_uniform_data.world_pos, m_pos_x, m_pos_y); - glUniform1f(m_uniform_data.rotation, m_rotation); +void Quad::draw(GLFWwindow *w){ + int width, height; + glfwGetWindowSize(w, &width, &height); + + glm::mat4 translate = glm::translate(glm::mat4(1.0f), glm::vec3(m_pos_x, m_pos_y, 0.0f)); + glm::mat4 rotate = glm::rotate(glm::mat4(1.0f), (float) glfwGetTime() * glm::radians(50.0f), glm::vec3(0.0f, 0.0f, 1.0f)); + glm::mat4 scale = glm::scale(glm::mat4(1.0f), glm::vec3(1.0f)); + glm::mat4 model = translate * rotate * scale; + glm::mat4 view = glm::mat4(1.0f); + glm::mat4 projection = glm::ortho(0.0f, static_cast(width), 0.0f, static_cast(height)); + glm::mat4 mvp = projection * view * model; + glUniformMatrix4fv(m_uniform_data.mvp, 1, GL_FALSE, glm::value_ptr(mvp)); m_mesh.bindVAO(); glDrawArrays(GL_TRIANGLES, 0, 6); diff --git a/src/Quad.hh b/src/Quad.hh index 065e931..30041ee 100644 --- a/src/Quad.hh +++ b/src/Quad.hh @@ -5,6 +5,7 @@ #include #include +#include class Quad { public: @@ -20,5 +21,5 @@ public: glm::vec2 get_position_vector(); - void draw(); + void draw(GLFWwindow *w); }; diff --git a/src/Uniform.hh b/src/Uniform.hh index 6d02e3c..1e6bea6 100644 --- a/src/Uniform.hh +++ b/src/Uniform.hh @@ -3,6 +3,5 @@ #include struct Uniform { - GLuint world_pos; - GLuint rotation; + GLuint mvp; }; diff --git a/src/main.cc b/src/main.cc index 69af39d..0d81913 100644 --- a/src/main.cc +++ b/src/main.cc @@ -24,6 +24,8 @@ #include +static std::vector quads = {}; + static void dbg_log(const char* str) { std::cout << str << std::endl; } @@ -48,13 +50,21 @@ static void key_callback(GLFWwindow *w, int key, int scancode, int action, int m } } +static void cursor_position_callback(GLFWwindow* window, double xpos, double ypos) +{ + int width, height; + glfwGetWindowSize(window, &width, &height); + quads[0]->m_pos_y= height-ypos; + quads[0]->m_pos_x= xpos; +} + #define VERTEX_SHADER_LOCATION "shaders/shader.vert" #define FRAGMENT_SHADER_LOCATION "shaders/shader.frag" GLuint g_program_id = 0; -static GLuint translate_unif; -static GLuint rotate_unif; + +static Uniform uniform_locations; void framebuffer_size_callback(GLFWwindow* window, int width, int height) { @@ -82,36 +92,22 @@ static void init_shaders() { glUseProgram(g_program_id); - translate_unif = glGetUniformLocation(g_program_id, "world_pos"); - rotate_unif = glGetUniformLocation(g_program_id, "rotation"); + uniform_locations.mvp = glGetUniformLocation(g_program_id, "mvp"); } static GLuint vertex_buf[2]; static GLuint array_buf[2]; static const GLuint vertex_data_position = 0; -static std::vector quads = {}; - static std::array verts({ // Tri 2 {0.0f, 0.0f}, - {0.5f, 0.0f}, - {0.5f, 0.5f}, + {20.0f, 0.0f}, + {20.0f, 20.0f}, // Tri 1 {0.0f, 0.0f}, - {0.5f, 0.5f}, - {0.0f, 0.5f}, -}); - -static std::array verts2({ - // Tri 2 - {1.0f, 0.0f}, - {1.5f, 0.0f}, - {1.5f, 0.5f}, - // Tri 1 - {1.0f, 0.0f}, - {1.5f, 0.5f}, - {1.0f, 0.5f}, + {20.0f, 20.0f}, + {0.0f, 20.0f}, }); // Do the work of initialising opengl so we can draw stuff @@ -127,8 +123,7 @@ static void init_opengl() { init_shaders(); - quads.push_back(new Quad(std::span(verts), (Uniform){.world_pos = translate_unif, .rotation = rotate_unif})); - quads.push_back(new Quad(std::span(verts2), (Uniform){.world_pos = translate_unif, .rotation = rotate_unif})); + quads.push_back(new Quad(std::span(verts), uniform_locations)); } int main() { @@ -155,6 +150,7 @@ int main() { // Set up the key handler glfwSetKeyCallback(w, key_callback); glfwSetFramebufferSizeCallback(w, framebuffer_size_callback); + glfwSetCursorPosCallback(w, cursor_position_callback); init_opengl(); @@ -163,7 +159,7 @@ int main() { glClear(GL_COLOR_BUFFER_BIT); for(auto q : quads) { - q->draw(); + q->draw(w); } glfwSwapBuffers(w); diff --git a/src/shaders/shader.vert b/src/shaders/shader.vert index d98894b..0af18a3 100644 --- a/src/shaders/shader.vert +++ b/src/shaders/shader.vert @@ -1,16 +1,9 @@ #version 430 core +layout (location = 0) in vec3 v_position; -layout (location = 0) in vec4 v_position; - -uniform vec2 world_pos; -uniform float rotation; +uniform mat4 mvp; // Simple passthrough shader for now. void main() { - mat2x2 rot_mat = mat2x2(cos(rotation), sin(rotation), -sin(rotation), cos(rotation)); - - vec2 new_xy = v_position.xy * rot_mat + world_pos; - vec4 new_position = vec4(new_xy.x, new_xy.y, 0.0, 1.0); - - gl_Position = new_position; -} \ No newline at end of file + gl_Position = mvp * vec4(v_position, 1.0); +}