refactor: extract uniform updates from triangle
This commit is contained in:
parent
025238c73f
commit
f22fcee399
|
@ -35,7 +35,8 @@
|
||||||
// cmake ..
|
// cmake ..
|
||||||
// cmake --build .
|
// cmake --build .
|
||||||
|
|
||||||
#include <glm/glm.hpp>
|
#include "glm/glm.hpp"
|
||||||
|
#include "glm/gtc/matrix_transform.hpp"
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
|
@ -81,6 +82,43 @@ int initGraphics(GLFWwindow** window)
|
||||||
return 0;
|
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()
|
int main()
|
||||||
{
|
{
|
||||||
// Calculate period of ISS orbit around the Earth
|
// Calculate period of ISS orbit around the Earth
|
||||||
|
@ -98,8 +136,8 @@ int main()
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
GLuint shaderProgram = compileShaderProgram();
|
GLuint shaderProgram = compileShaderProgram();
|
||||||
Triangle triangle(shaderProgram);
|
Triangle triangle;
|
||||||
Icosphere sphere(shaderProgram, 0);
|
Icosphere sphere(0);
|
||||||
|
|
||||||
// Wireframe
|
// Wireframe
|
||||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||||
|
@ -110,7 +148,15 @@ int main()
|
||||||
glClearColor(0.2, 0.3, 0.3, 1.0);
|
glClearColor(0.2, 0.3, 0.3, 1.0);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
// Render everything with the same shaders
|
||||||
|
glUseProgram(shaderProgram);
|
||||||
|
|
||||||
|
// Update uniforms
|
||||||
float time = glfwGetTime();
|
float time = glfwGetTime();
|
||||||
|
updateTime(shaderProgram, time);
|
||||||
|
updateModelViewProjectionMatrix(shaderProgram, time);
|
||||||
|
|
||||||
|
// Render objects
|
||||||
triangle.render(time);
|
triangle.render(time);
|
||||||
sphere.render(time);
|
sphere.render(time);
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,7 @@
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
Icosphere::Icosphere(GLuint shaderProgram, int subdivisions)
|
Icosphere::Icosphere(int subdivisions)
|
||||||
: _shaderProgram(shaderProgram)
|
|
||||||
{
|
{
|
||||||
glGenVertexArrays(1, &_vao);
|
glGenVertexArrays(1, &_vao);
|
||||||
glGenBuffers(1, &_vbo);
|
glGenBuffers(1, &_vbo);
|
||||||
|
@ -39,8 +38,6 @@ Icosphere::Icosphere(GLuint shaderProgram, int subdivisions)
|
||||||
|
|
||||||
void Icosphere::render(float time)
|
void Icosphere::render(float time)
|
||||||
{
|
{
|
||||||
glUseProgram(_shaderProgram);
|
|
||||||
|
|
||||||
glBindVertexArray(_vao);
|
glBindVertexArray(_vao);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
|
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo);
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
class Icosphere
|
class Icosphere
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Icosphere(GLuint shaderProgram, int subdividision);
|
Icosphere(int subdividision);
|
||||||
void render(float time);
|
void render(float time);
|
||||||
|
|
||||||
~Icosphere();
|
~Icosphere();
|
||||||
|
@ -18,7 +18,6 @@ private:
|
||||||
GLuint _vbo;
|
GLuint _vbo;
|
||||||
GLuint _vao;
|
GLuint _vao;
|
||||||
GLuint _ebo;
|
GLuint _ebo;
|
||||||
GLuint _shaderProgram;
|
|
||||||
|
|
||||||
std::vector<float> _vertices;
|
std::vector<float> _vertices;
|
||||||
std::vector<unsigned int> _indices;
|
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},
|
{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}
|
{6,1,10},{9,0,11},{9,11,2},{9,2,5},{7,2,11}
|
||||||
};
|
};
|
||||||
|
|
||||||
GLint getShaderUniformLocation(const std::string& uniformName);
|
|
||||||
};
|
};
|
|
@ -2,11 +2,7 @@
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "glm/glm.hpp"
|
Triangle::Triangle()
|
||||||
#include "glm/gtc/matrix_transform.hpp"
|
|
||||||
|
|
||||||
Triangle::Triangle(GLuint shaderProgram)
|
|
||||||
: _shaderProgram(shaderProgram)
|
|
||||||
{
|
{
|
||||||
glGenVertexArrays(1, &_vao);
|
glGenVertexArrays(1, &_vao);
|
||||||
glGenBuffers(1, &_vbo);
|
glGenBuffers(1, &_vbo);
|
||||||
|
@ -25,44 +21,11 @@ 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);
|
|
||||||
|
|
||||||
GLint mvpLocation = getShaderUniformLocation("_ModelViewProjectionMatrix");
|
|
||||||
glUniformMatrix4fv(mvpLocation, 1, GL_FALSE, &mvp[0][0]);
|
|
||||||
|
|
||||||
GLint timeLocation = getShaderUniformLocation("_Time");
|
|
||||||
float timeValue = time;
|
|
||||||
glUniform1f(timeLocation, timeValue);
|
|
||||||
|
|
||||||
glBindVertexArray(_vao);
|
glBindVertexArray(_vao);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
|
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
|
||||||
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 -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return location;
|
|
||||||
}
|
|
||||||
|
|
||||||
Triangle::~Triangle()
|
Triangle::~Triangle()
|
||||||
{
|
{
|
||||||
glDeleteVertexArrays(1, &_vao);
|
glDeleteVertexArrays(1, &_vao);
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
class Triangle
|
class Triangle
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Triangle(GLuint shaderProgram);
|
Triangle();
|
||||||
void render(float time);
|
void render(float time);
|
||||||
|
|
||||||
~Triangle();
|
~Triangle();
|
||||||
|
@ -16,7 +16,6 @@ public:
|
||||||
private:
|
private:
|
||||||
GLuint _vbo;
|
GLuint _vbo;
|
||||||
GLuint _vao;
|
GLuint _vao;
|
||||||
GLuint _shaderProgram;
|
|
||||||
|
|
||||||
std::vector<float> _vertices =
|
std::vector<float> _vertices =
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue