Add a proper coordinate system
This commit is contained in:
parent
cc697b7a7c
commit
06ae06f0d9
26
src/Quad.cc
26
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 <glm/glm.hpp>
|
||||
#include <glm/matrix.hpp>
|
||||
#include <glm/ext.hpp>
|
||||
|
||||
#include <glad/gl.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <string.h>
|
||||
|
||||
Quad::Quad(std::span<glm::vec2> 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<float>(width), 0.0f, static_cast<float>(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);
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
#include <glad/gl.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
class Quad {
|
||||
public:
|
||||
|
@ -20,5 +21,5 @@ public:
|
|||
|
||||
glm::vec2 get_position_vector();
|
||||
|
||||
void draw();
|
||||
void draw(GLFWwindow *w);
|
||||
};
|
||||
|
|
|
@ -3,6 +3,5 @@
|
|||
#include <glad/gl.h>
|
||||
|
||||
struct Uniform {
|
||||
GLuint world_pos;
|
||||
GLuint rotation;
|
||||
GLuint mvp;
|
||||
};
|
||||
|
|
44
src/main.cc
44
src/main.cc
|
@ -24,6 +24,8 @@
|
|||
|
||||
#include <stb/stb_image.h>
|
||||
|
||||
static std::vector<Quad *> 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<Quad *> quads = {};
|
||||
|
||||
static std::array<glm::vec2, 6> 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<glm::vec2, 6> 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<glm::vec2>(verts), (Uniform){.world_pos = translate_unif, .rotation = rotate_unif}));
|
||||
quads.push_back(new Quad(std::span<glm::vec2>(verts2), (Uniform){.world_pos = translate_unif, .rotation = rotate_unif}));
|
||||
quads.push_back(new Quad(std::span<glm::vec2>(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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
gl_Position = mvp * vec4(v_position, 1.0);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue