Add rotation and translation uniforms

This commit is contained in:
Archie Hilton 2024-05-10 22:23:35 +01:00
parent 267bd2216c
commit ef03920fa6
2 changed files with 50 additions and 33 deletions

View File

@ -8,10 +8,16 @@
#include <string>
#include <vector>
#include <array>
// This has to come last.
#define GLAD_GL_IMPLEMENTATION
#include <glad/gl.h>
#include <glm/glm.hpp>
#include <glm/ext.hpp>
#include <glm/mat2x2.hpp>
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<glm::vec2, 3> 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<float, 2> get_world_projection() {
return std::array<float, 2>({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<glm::vec2, 3>({
{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();

View File

@ -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;