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 "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);

View File

@ -3,6 +3,7 @@
#include <GL/glew.h>
#include <vector>
#include <string>
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);
};

View File

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