From de5a6d1bfd6024a471a758f192243a9daaebb09f Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 29 Jul 2023 18:47:40 +0200 Subject: [PATCH] Finished transformations section --- src/Engine/main.cpp | 14 ++++++++++---- src/Engine/shaders/Shader.h | 19 +++++++++++++------ src/Engine/shaders/default/default.vert | 4 +++- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/Engine/main.cpp b/src/Engine/main.cpp index 01b1cda..e7e35f8 100644 --- a/src/Engine/main.cpp +++ b/src/Engine/main.cpp @@ -10,8 +10,8 @@ #include "shaders/Shader.h" #include "util/stb_image.h" -// Continue: https://learnopengl.com/Getting-started/Transformations -// Chapter: GLM +// Continue: https://learnopengl.com/Getting-started/Coordinate-Systems +// Chapter: not started // // TODO: look at project->properties->VC++ Directories-> change everything to the Environment var // For the "real" Nebulix Engine setup everything so it can be compiled/used with VSCode/CLion and not just VS (so basically having an "external" folder which has open gl, GLAD, etc. in it or something like this) @@ -165,9 +165,15 @@ int main() ProcessInput(window); + glm::mat4 transform = glm::mat4(1.0f); + transform = glm::rotate(transform, (float)glfwGetTime(), glm::vec3(0, 0, 1.0f)); + transform = glm::translate(transform, glm::vec3(glm::sin((float)glfwGetTime()), 0, 0)); + //transform = glm::scale(transform, glm::vec3(glm::sin((float)glfwGetTime()))); + shader->Use(); - shader->setInt("ourTexture1", 0); - shader->setInt("ourTexture2", 1); + shader->SetInt("ourTexture1", 0); + shader->SetInt("ourTexture2", 1); + shader->SetMatrix("transform", transform); //float time = glfwGetTime(); //float greenValue = (sin(time) / 2.0f) + 0.5f; //shader->setFloat("ourColour", 0.0f, greenValue, 0.0f, 0.0f); diff --git a/src/Engine/shaders/Shader.h b/src/Engine/shaders/Shader.h index ffe3b20..ff42f4c 100644 --- a/src/Engine/shaders/Shader.h +++ b/src/Engine/shaders/Shader.h @@ -3,6 +3,8 @@ #include #include #include +#include + #include "Enums.h" @@ -23,31 +25,36 @@ namespace Nebulix /// void Use() { glUseProgram(shaderId); } - void setBool(const std::string& name, bool value) const + void SetBool(const std::string& name, bool value) const { glUniform1i(glGetUniformLocation(shaderId, name.c_str()), (int)value); } - void setInt(const std::string& name, int value) const + void SetInt(const std::string& name, int value) const { glUniform1i(glGetUniformLocation(shaderId, name.c_str()), value); } - void setFloat(const std::string& name, float x) const + void SetFloat(const std::string& name, float x) const { glUniform1f(glGetUniformLocation(shaderId, name.c_str()), x); } - void setFloat(const std::string& name, float x, float y) const + void SetFloat(const std::string& name, float x, float y) const { glUniform2f(glGetUniformLocation(shaderId, name.c_str()), x, y); } - void setFloat(const std::string& name, float x, float y, float z) const + void SetFloat(const std::string& name, float x, float y, float z) const { glUniform3f(glGetUniformLocation(shaderId, name.c_str()), x, y, z); } - void setFloat(const std::string& name, float x, float y, float z, float w) const + void SetFloat(const std::string& name, float x, float y, float z, float w) const { glUniform4f(glGetUniformLocation(shaderId, name.c_str()), x, y, z, w); } + void SetMatrix(const std::string& name, glm::mat4 mat) const + { + glUniformMatrix4fv(glGetUniformLocation(shaderId, name.c_str()), 1, GL_FALSE, glm::value_ptr(mat)); + } + private: std::string &_vertexFile, &_fragmentFile; GLuint shaderId; diff --git a/src/Engine/shaders/default/default.vert b/src/Engine/shaders/default/default.vert index ec51c36..56b8b19 100644 --- a/src/Engine/shaders/default/default.vert +++ b/src/Engine/shaders/default/default.vert @@ -6,9 +6,11 @@ layout (location = 2) in vec2 aTexCoord; out vec3 ourColour; out vec2 texCoord; +uniform mat4 transform; + void main() { - gl_Position = vec4(aPos, 1.0); + gl_Position = transform * vec4(aPos, 1.0); ourColour = aColour; texCoord = aTexCoord; }