feat: orthographic triangle

This commit is contained in:
Cat Flynn 2023-07-31 23:30:35 +02:00
parent c44624f51e
commit 68d63edef7
3 changed files with 38 additions and 7 deletions

View File

@ -2,6 +2,9 @@
#include <iostream> #include <iostream>
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
Triangle::Triangle(GLuint shaderProgram) Triangle::Triangle(GLuint shaderProgram)
: _shaderProgram(shaderProgram) : _shaderProgram(shaderProgram)
{ {
@ -22,14 +25,24 @@ Triangle::Triangle(GLuint shaderProgram)
void Triangle::render(float time) 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); glUseProgram(_shaderProgram);
GLint timeLocation = glGetUniformLocation(_shaderProgram, "_Time"); GLint mvpLocation = getShaderUniformLocation("_ModelViewProjectionMatrix");
if (timeLocation == -1) glUniformMatrix4fv(mvpLocation, 1, GL_FALSE, &mvp[0][0]);
{
std::cerr << "Could not find uniform: _Time" << std::endl; GLint timeLocation = getShaderUniformLocation("_Time");
return;
}
float timeValue = time; float timeValue = time;
glUniform1f(timeLocation, timeValue); glUniform1f(timeLocation, timeValue);
@ -38,6 +51,18 @@ void Triangle::render(float time)
glDrawArrays(GL_TRIANGLES, 0, 3); 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() Triangle::~Triangle()
{ {
glDeleteVertexArrays(1, &_vao); glDeleteVertexArrays(1, &_vao);

View File

@ -3,6 +3,7 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <vector> #include <vector>
#include <string>
class Triangle class Triangle
{ {
@ -23,4 +24,6 @@ private:
0.5, -0.5, 0.0, // right 0.5, -0.5, 0.0, // right
0.0, 0.5, 0.0 // top 0.0, 0.5, 0.0 // top
}; };
GLint getShaderUniformLocation(const std::string& uniformName);
}; };

View File

@ -2,8 +2,11 @@
layout (location = 0) in vec3 aPos; layout (location = 0) in vec3 aPos;
uniform mat4x4 _ModelViewProjectionMatrix;
void main() 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;
} }