From ef03920fa62e7c92383d00e195dfcd23073c7418 Mon Sep 17 00:00:00 2001 From: Archie Hilton Date: Fri, 10 May 2024 22:23:35 +0100 Subject: [PATCH] Add rotation and translation uniforms --- src/main.cc | 72 +++++++++++++++++++++++++---------------- src/shaders/shader.vert | 11 +++---- 2 files changed, 50 insertions(+), 33 deletions(-) diff --git a/src/main.cc b/src/main.cc index 6c2c6af..f335856 100644 --- a/src/main.cc +++ b/src/main.cc @@ -8,10 +8,16 @@ #include #include +#include + // This has to come last. #define GLAD_GL_IMPLEMENTATION #include +#include +#include +#include + static void dbg_log(const char* str) { std::cout << str << std::endl; } @@ -27,7 +33,8 @@ static void key_callback(GLFWwindow *w, int key, int scancode, int action, int m GLuint g_program_id = 0; -static GLuint time_unif; +static GLuint translate_unif; +static GLuint rotate_unif; static void init_shaders() { dbg_log("Initialising shaders"); @@ -47,36 +54,55 @@ static void init_shaders() { glUseProgram(g_program_id); - glGetUniformLocation(g_program_id, "time"); + translate_unif = glGetUniformLocation(g_program_id, "world_pos"); + rotate_unif = glGetUniformLocation(g_program_id, "rotation"); } -static const GLfloat verts[6] = { - 0.0f, 0.0f, 0.0f, 0.5f, 0.5f, 0.0f +class Triangle { +public: + GLfloat data[6]; + float m_pos_x; + float m_pos_y; + float rotation = 0.2f; + + Triangle(std::array verts) { + for (std::size_t i = 0; i < 3; i++) { + data[i*2] = verts[i][0]; + data[i*2+1] = verts[i][1]; + } + } + + std::array get_world_projection() { + return std::array({m_pos_x, m_pos_y}); + } }; -static GLuint vertex_buf; -static GLuint array_buf; +static GLuint vertex_buf[2]; +static GLuint array_buf[2]; static const GLuint vertex_data_position = 0; +static Triangle o1(std::array({ + {0.0f, 0.0f}, + {0.5f, 0.0f}, + {0.5f, 0.5f} + })); + // Do the work of initialising opengl so we can draw stuff static void init_opengl() { dbg_log("Initialising opengl"); // Configure the context's capabilities glClearColor(0.0, 0.0, 0.0, 1.0); - glGenVertexArrays(1, &array_buf); - glBindVertexArray(array_buf); - - glGenBuffers(1, &vertex_buf); - glBindBuffer(GL_ARRAY_BUFFER, vertex_buf); - glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW); - - - init_shaders(); + glGenVertexArrays(2, array_buf); + glBindVertexArray(array_buf[0]); + glGenBuffers(2, vertex_buf); + glBindBuffer(GL_ARRAY_BUFFER, vertex_buf[0]); + glBufferData(GL_ARRAY_BUFFER, sizeof(Triangle::data), o1.data, GL_STATIC_DRAW); glVertexAttribPointer(vertex_data_position, 2, GL_FLOAT, GL_FALSE, 0, NULL); glEnableVertexAttribArray(vertex_data_position); + init_shaders(); } int main() { @@ -107,23 +133,15 @@ int main() { // Main Loop while(!glfwWindowShouldClose(w)) { - /* - int width, height; - glfwGetFramebufferSize(w, &width, &height); - - glViewport(0, 0, width, height); - glClear(GL_COLOR_BUFFER_BIT); - */ - float time = glfwGetTime(); glClear(GL_COLOR_BUFFER_BIT); - glUniform1f(time_unif, time); - glBindVertexArray(array_buf); - glDrawArrays(GL_TRIANGLES, 0, 3); + glUniform2fv(translate_unif, 1, o1.get_world_projection().data()); + glUniform1f(rotate_unif, o1.rotation); - // TODO Draw primitives + glBindVertexArray(array_buf[0]); + glDrawArrays(GL_TRIANGLES, 0, 3); glfwSwapBuffers(w); glfwPollEvents(); diff --git a/src/shaders/shader.vert b/src/shaders/shader.vert index 6958689..d98894b 100644 --- a/src/shaders/shader.vert +++ b/src/shaders/shader.vert @@ -2,15 +2,14 @@ layout (location = 0) in vec4 v_position; -uniform float time; +uniform vec2 world_pos; +uniform float rotation; // Simple passthrough shader for now. void main() { - vec2 new_xy = v_position.xy * mat2x2( - cos(time), -sin(time), - sin(time), cos(time) - ); - + 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;