From 68d63edef7a80b64152a1415ff41d20b81867d78 Mon Sep 17 00:00:00 2001 From: Cat Flynn Date: Mon, 31 Jul 2023 23:30:35 +0200 Subject: [PATCH] feat: orthographic triangle --- src/triangle.cpp | 37 +++++++++++++++++++++++++++++++------ src/triangle.hpp | 3 +++ vert.glsl | 5 ++++- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/triangle.cpp b/src/triangle.cpp index 41c56f9..976e2be 100644 --- a/src/triangle.cpp +++ b/src/triangle.cpp @@ -2,6 +2,9 @@ #include +#include "glm/glm.hpp" +#include "glm/gtc/matrix_transform.hpp" + Triangle::Triangle(GLuint shaderProgram) : _shaderProgram(shaderProgram) { @@ -22,14 +25,24 @@ Triangle::Triangle(GLuint shaderProgram) void Triangle::render(float time) { + // Calculate matrices + float left = -1.0, right = 1.0, bottom = -1.0, top = 1.0, near = -1.0, far = 1.0; + glm::mat4 projection = glm::ortho(left, right, bottom, top, near, far); + + constexpr float angle = glm::radians(50.0); + glm::vec3 axis = glm::vec3(0.0, 1.0, 0.0); + glm::mat4 model = glm::rotate(glm::mat4(1.0), angle * time, axis); + + glm::mat4 view = glm::mat4(1.0); + + glm::mat4 mvp = projection * model * view; + glUseProgram(_shaderProgram); - GLint timeLocation = glGetUniformLocation(_shaderProgram, "_Time"); - if (timeLocation == -1) - { - std::cerr << "Could not find uniform: _Time" << std::endl; - return; - } + GLint mvpLocation = getShaderUniformLocation("_ModelViewProjectionMatrix"); + glUniformMatrix4fv(mvpLocation, 1, GL_FALSE, &mvp[0][0]); + + GLint timeLocation = getShaderUniformLocation("_Time"); float timeValue = time; glUniform1f(timeLocation, timeValue); @@ -38,6 +51,18 @@ void Triangle::render(float time) glDrawArrays(GL_TRIANGLES, 0, 3); } +GLint Triangle::getShaderUniformLocation(const std::string& uniformName) +{ + GLint location = glGetUniformLocation(_shaderProgram, uniformName.c_str()); + if (location == -1) + { + std::cerr << "Could not find uniform: " << uniformName << std::endl; + return 0; + } + + return location; +} + Triangle::~Triangle() { glDeleteVertexArrays(1, &_vao); diff --git a/src/triangle.hpp b/src/triangle.hpp index 4e02e4f..9ffea5d 100644 --- a/src/triangle.hpp +++ b/src/triangle.hpp @@ -3,6 +3,7 @@ #include #include +#include class Triangle { @@ -23,4 +24,6 @@ private: 0.5, -0.5, 0.0, // right 0.0, 0.5, 0.0 // top }; + + GLint getShaderUniformLocation(const std::string& uniformName); }; \ No newline at end of file diff --git a/vert.glsl b/vert.glsl index aaa01ad..96316d1 100644 --- a/vert.glsl +++ b/vert.glsl @@ -2,8 +2,11 @@ layout (location = 0) in vec3 aPos; +uniform mat4x4 _ModelViewProjectionMatrix; + void main() { - gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); + vec4 pos = vec4(aPos.x, aPos.y, aPos.z, 1.0); + gl_Position = _ModelViewProjectionMatrix * pos; }