Add the glm math library and use matrices to transform the object

This commit is contained in:
Fries 2024-04-28 16:00:58 -07:00
parent 9801282409
commit 4395bf8a9f
6 changed files with 28 additions and 2 deletions

View file

@ -2,5 +2,6 @@ project('opengl-learning', 'cpp', 'c', default_options: ['cpp_std=c++20'])
glfw = dependency('glfw3') glfw = dependency('glfw3')
libepoxy = dependency('epoxy') libepoxy = dependency('epoxy')
glm = dependency('glm')
subdir('src') subdir('src')

View file

@ -1,5 +1,7 @@
#include <epoxy/gl.h> #include <epoxy/gl.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <iostream> #include <iostream>
#include <format> #include <format>
@ -202,6 +204,10 @@ int main() {
shader.setInt("texture1", 0); shader.setInt("texture1", 0);
shader.setInt("texture2", 1); shader.setInt("texture2", 1);
// glm::mat4 trans = glm::mat4(1.0f);
// trans = glm::rotate(trans, glm::radians(90.0f), glm::vec3(0.0, 0.0, 1.0));
// trans = glm::scale(trans, glm::vec3(0.5, 0.5, 0.5));
// Create a render loop, which keeps the program open until glfw tells the loop that the window should close. // Create a render loop, which keeps the program open until glfw tells the loop that the window should close.
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
// Process input. // Process input.
@ -224,6 +230,15 @@ int main() {
shader.activate(); shader.activate();
// create the identity matrix
glm::mat4 trans(1);
// translate it by this vector3
trans = glm::translate(trans, glm::vec3(0.5f, -0.5f, 0.0f));
// rotate by the z axis with the angle being controlled by time
trans = glm::rotate(trans, (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));
shader.setMat4("transform", trans);
if (!rectangleToggled) { if (!rectangleToggled) {
// Bind the state to use the vertexArrayObject object so OpenGL knows what to do with the verticies. // Bind the state to use the vertexArrayObject object so OpenGL knows what to do with the verticies.
glBindVertexArray(triangleRenderObjs.vertexArrayObject); glBindVertexArray(triangleRenderObjs.vertexArrayObject);

View file

@ -12,5 +12,5 @@ vertex = custom_target('vertex', output: 'vertex.glsl', input: 'vertex.glsl', co
container = custom_target('container', output: 'container.jpg', input: 'container.jpg', command: [copy, '@INPUT@', '@OUTPUT@']) container = custom_target('container', output: 'container.jpg', input: 'container.jpg', command: [copy, '@INPUT@', '@OUTPUT@'])
awesomeface = custom_target('awesomeface', output: 'awesomeface.png', input: 'awesomeface.png', command: [copy, '@INPUT@', '@OUTPUT@']) awesomeface = custom_target('awesomeface', output: 'awesomeface.png', input: 'awesomeface.png', command: [copy, '@INPUT@', '@OUTPUT@'])
executable('main', [sources, fragment, vertex, container, awesomeface], dependencies: [glfw, libepoxy]) executable('main', [sources, fragment, vertex, container, awesomeface], dependencies: [glfw, libepoxy, glm])
executable('fixedfunction', ['fixedfunction.cc'], dependencies: [glfw, libepoxy]) executable('fixedfunction', ['fixedfunction.cc'], dependencies: [glfw, libepoxy])

View file

@ -103,3 +103,7 @@ void Shader::setInt(const char* name, int value) {
void Shader::setFloat(const char* name, float value) const { void Shader::setFloat(const char* name, float value) const {
glUniform1f(glGetUniformLocation(id, name), value); glUniform1f(glGetUniformLocation(id, name), value);
} }
void Shader::setMat4(const char* name, glm::mat4 matrix) {
glUniformMatrix4fv(glGetUniformLocation(id, name), 1, GL_FALSE, glm::value_ptr(matrix));
}

View file

@ -2,6 +2,9 @@
#define SHADER_HH #define SHADER_HH
#include <string> #include <string>
#include <epoxy/gl.h> #include <epoxy/gl.h>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
class Shader { class Shader {
public: public:
// The Shader ID. // The Shader ID.
@ -17,6 +20,7 @@ class Shader {
void setBool(const char* name, bool value) const; void setBool(const char* name, bool value) const;
void setInt(const char* name, int value) ; void setInt(const char* name, int value) ;
void setFloat(const char* name, float value) const; void setFloat(const char* name, float value) const;
void setMat4(const char* name, glm::mat4 matrix);
private: private:
uint compileShader(std::string code, GLenum type, std::string shaderTypeName); uint compileShader(std::string code, GLenum type, std::string shaderTypeName);
uint compileVertexShader(std::string code); uint compileVertexShader(std::string code);

View file

@ -7,8 +7,10 @@ layout (location = 2) in vec2 aTexCoord;
out vec3 ourColor; out vec3 ourColor;
out vec2 TexCoord; out vec2 TexCoord;
uniform mat4 transform;
void main() { void main() {
gl_Position = vec4(aPos, 1.0); gl_Position = transform * vec4(aPos, 1.0);
ourColor = aColor; ourColor = aColor;
TexCoord = aTexCoord; TexCoord = aTexCoord;
} }