refactor: extract uniform updates from triangle

This commit is contained in:
Cat Flynn 2023-08-02 23:35:52 +02:00
parent 025238c73f
commit f22fcee399
5 changed files with 53 additions and 51 deletions

View File

@ -35,7 +35,8 @@
// cmake ..
// cmake --build .
#include <glm/glm.hpp>
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
@ -81,6 +82,43 @@ int initGraphics(GLFWwindow** window)
return 0;
}
GLint getShaderUniformLocation(GLuint shaderProgram, const std::string& uniformName)
{
GLint location = glGetUniformLocation(shaderProgram, uniformName.c_str());
if (location == -1)
{
std::cerr << "Could not find uniform: " << uniformName << std::endl;
return -1;
}
return location;
}
void updateModelViewProjectionMatrix(GLuint shaderProgram, 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;
GLint mvpLocation = getShaderUniformLocation(shaderProgram, "_ModelViewProjectionMatrix");
glUniformMatrix4fv(mvpLocation, 1, GL_FALSE, &mvp[0][0]);
}
void updateTime(GLuint shaderProgram, float time)
{
GLint timeLocation = getShaderUniformLocation(shaderProgram, "_Time");
float timeValue = time;
glUniform1f(timeLocation, timeValue);
}
int main()
{
// Calculate period of ISS orbit around the Earth
@ -98,8 +136,8 @@ int main()
return -1;
GLuint shaderProgram = compileShaderProgram();
Triangle triangle(shaderProgram);
Icosphere sphere(shaderProgram, 0);
Triangle triangle;
Icosphere sphere(0);
// Wireframe
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
@ -110,7 +148,15 @@ int main()
glClearColor(0.2, 0.3, 0.3, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
// Render everything with the same shaders
glUseProgram(shaderProgram);
// Update uniforms
float time = glfwGetTime();
updateTime(shaderProgram, time);
updateModelViewProjectionMatrix(shaderProgram, time);
// Render objects
triangle.render(time);
sphere.render(time);

View File

@ -2,8 +2,7 @@
#include <iostream>
Icosphere::Icosphere(GLuint shaderProgram, int subdivisions)
: _shaderProgram(shaderProgram)
Icosphere::Icosphere(int subdivisions)
{
glGenVertexArrays(1, &_vao);
glGenBuffers(1, &_vbo);
@ -39,8 +38,6 @@ Icosphere::Icosphere(GLuint shaderProgram, int subdivisions)
void Icosphere::render(float time)
{
glUseProgram(_shaderProgram);
glBindVertexArray(_vao);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo);

View File

@ -9,7 +9,7 @@
class Icosphere
{
public:
Icosphere(GLuint shaderProgram, int subdividision);
Icosphere(int subdividision);
void render(float time);
~Icosphere();
@ -18,7 +18,6 @@ private:
GLuint _vbo;
GLuint _vao;
GLuint _ebo;
GLuint _shaderProgram;
std::vector<float> _vertices;
std::vector<unsigned int> _indices;
@ -49,6 +48,4 @@ private:
{7,10,3},{7,6,10},{7,11,6},{11,0,6},{0,1,6},
{6,1,10},{9,0,11},{9,11,2},{9,2,5},{7,2,11}
};
GLint getShaderUniformLocation(const std::string& uniformName);
};

View File

@ -2,11 +2,7 @@
#include <iostream>
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
Triangle::Triangle(GLuint shaderProgram)
: _shaderProgram(shaderProgram)
Triangle::Triangle()
{
glGenVertexArrays(1, &_vao);
glGenBuffers(1, &_vbo);
@ -25,44 +21,11 @@ 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 mvpLocation = getShaderUniformLocation("_ModelViewProjectionMatrix");
glUniformMatrix4fv(mvpLocation, 1, GL_FALSE, &mvp[0][0]);
GLint timeLocation = getShaderUniformLocation("_Time");
float timeValue = time;
glUniform1f(timeLocation, timeValue);
glBindVertexArray(_vao);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
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 -1;
}
return location;
}
Triangle::~Triangle()
{
glDeleteVertexArrays(1, &_vao);

View File

@ -8,7 +8,7 @@
class Triangle
{
public:
Triangle(GLuint shaderProgram);
Triangle();
void render(float time);
~Triangle();
@ -16,7 +16,6 @@ public:
private:
GLuint _vbo;
GLuint _vao;
GLuint _shaderProgram;
std::vector<float> _vertices =
{