Add rotation and translation uniforms
This commit is contained in:
parent
267bd2216c
commit
ef03920fa6
72
src/main.cc
72
src/main.cc
|
@ -8,10 +8,16 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
|
||||||
// This has to come last.
|
// This has to come last.
|
||||||
#define GLAD_GL_IMPLEMENTATION
|
#define GLAD_GL_IMPLEMENTATION
|
||||||
#include <glad/gl.h>
|
#include <glad/gl.h>
|
||||||
|
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <glm/ext.hpp>
|
||||||
|
#include <glm/mat2x2.hpp>
|
||||||
|
|
||||||
static void dbg_log(const char* str) {
|
static void dbg_log(const char* str) {
|
||||||
std::cout << str << std::endl;
|
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;
|
GLuint g_program_id = 0;
|
||||||
|
|
||||||
static GLuint time_unif;
|
static GLuint translate_unif;
|
||||||
|
static GLuint rotate_unif;
|
||||||
|
|
||||||
static void init_shaders() {
|
static void init_shaders() {
|
||||||
dbg_log("Initialising shaders");
|
dbg_log("Initialising shaders");
|
||||||
|
@ -47,36 +54,55 @@ static void init_shaders() {
|
||||||
|
|
||||||
glUseProgram(g_program_id);
|
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] = {
|
class Triangle {
|
||||||
0.0f, 0.0f, 0.0f, 0.5f, 0.5f, 0.0f
|
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 vertex_buf[2];
|
||||||
static GLuint array_buf;
|
static GLuint array_buf[2];
|
||||||
static const GLuint vertex_data_position = 0;
|
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
|
// Do the work of initialising opengl so we can draw stuff
|
||||||
static void init_opengl() {
|
static void init_opengl() {
|
||||||
dbg_log("Initialising opengl");
|
dbg_log("Initialising opengl");
|
||||||
// Configure the context's capabilities
|
// Configure the context's capabilities
|
||||||
glClearColor(0.0, 0.0, 0.0, 1.0);
|
glClearColor(0.0, 0.0, 0.0, 1.0);
|
||||||
|
|
||||||
glGenVertexArrays(1, &array_buf);
|
glGenVertexArrays(2, array_buf);
|
||||||
glBindVertexArray(array_buf);
|
glBindVertexArray(array_buf[0]);
|
||||||
|
|
||||||
glGenBuffers(1, &vertex_buf);
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_buf);
|
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
|
|
||||||
|
|
||||||
|
|
||||||
init_shaders();
|
|
||||||
|
|
||||||
|
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);
|
glVertexAttribPointer(vertex_data_position, 2, GL_FLOAT, GL_FALSE, 0, NULL);
|
||||||
glEnableVertexAttribArray(vertex_data_position);
|
glEnableVertexAttribArray(vertex_data_position);
|
||||||
|
|
||||||
|
init_shaders();
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
@ -107,23 +133,15 @@ int main() {
|
||||||
|
|
||||||
// Main Loop
|
// Main Loop
|
||||||
while(!glfwWindowShouldClose(w)) {
|
while(!glfwWindowShouldClose(w)) {
|
||||||
/*
|
|
||||||
int width, height;
|
|
||||||
glfwGetFramebufferSize(w, &width, &height);
|
|
||||||
|
|
||||||
glViewport(0, 0, width, height);
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
|
||||||
*/
|
|
||||||
|
|
||||||
float time = glfwGetTime();
|
float time = glfwGetTime();
|
||||||
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
glUniform1f(time_unif, time);
|
glUniform2fv(translate_unif, 1, o1.get_world_projection().data());
|
||||||
glBindVertexArray(array_buf);
|
glUniform1f(rotate_unif, o1.rotation);
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
|
||||||
|
|
||||||
// TODO Draw primitives
|
glBindVertexArray(array_buf[0]);
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||||
|
|
||||||
glfwSwapBuffers(w);
|
glfwSwapBuffers(w);
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
|
|
@ -2,15 +2,14 @@
|
||||||
|
|
||||||
layout (location = 0) in vec4 v_position;
|
layout (location = 0) in vec4 v_position;
|
||||||
|
|
||||||
uniform float time;
|
uniform vec2 world_pos;
|
||||||
|
uniform float rotation;
|
||||||
|
|
||||||
// Simple passthrough shader for now.
|
// Simple passthrough shader for now.
|
||||||
void main() {
|
void main() {
|
||||||
vec2 new_xy = v_position.xy * mat2x2(
|
mat2x2 rot_mat = mat2x2(cos(rotation), sin(rotation), -sin(rotation), cos(rotation));
|
||||||
cos(time), -sin(time),
|
|
||||||
sin(time), cos(time)
|
|
||||||
);
|
|
||||||
|
|
||||||
|
vec2 new_xy = v_position.xy * rot_mat + world_pos;
|
||||||
vec4 new_position = vec4(new_xy.x, new_xy.y, 0.0, 1.0);
|
vec4 new_position = vec4(new_xy.x, new_xy.y, 0.0, 1.0);
|
||||||
|
|
||||||
gl_Position = new_position;
|
gl_Position = new_position;
|
||||||
|
|
Loading…
Reference in New Issue