From 73ff2b56196e75392cad9cfff1547adefb4bf5d2 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Mon, 24 Oct 2022 15:27:53 +0200 Subject: [PATCH] Transformations etc --- .gitmodules | 3 +++ CMakeLists.txt | 14 ++++++++--- headers/shaders.hpp | 5 ++++ headers/textures.hpp | 2 +- lib/glm | 1 + shaders/vertex.glsl | 9 ++++++- src/main.cpp | 58 ++++++++++++++++++++++++++++++-------------- src/shaders.cpp | 17 ++++++++++--- src/textures.cpp | 2 +- 9 files changed, 83 insertions(+), 28 deletions(-) create mode 160000 lib/glm diff --git a/.gitmodules b/.gitmodules index 2121d34..a816dfb 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,3 +7,6 @@ [submodule "extern/stb"] path = extern/stb url = https://github.com/nothings/stb.git +[submodule "lib/glm"] + path = lib/glm + url = https://github.com/g-truc/glm.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 6cb33b5..f34a33c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,10 +2,13 @@ cmake_minimum_required(VERSION 3.24.2) project(Hohmann VERSION 1.0) +#### Sources file(GLOB SOURCES src/*.cpp headers/*.hpp) add_executable(Hohmann ${SOURCES}) -# GLFW submodule + +#### Libs +# GLFW set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE) set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE) set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) @@ -14,10 +17,13 @@ add_subdirectory(lib/glfw) # GLAD add_subdirectory(lib/glad) -# External headers & sources +# GLM +add_subdirectory(lib/glm) + +#### External headers & sources file(GLOB EXTERN_SOURCES extern/*) include_directories(${EXTERN_SOURCES}) link_directories(${EXTERN_SOURCES}) -# Linking -target_link_libraries(Hohmann PRIVATE glfw glad) +#### Linking +target_link_libraries(Hohmann PRIVATE glfw glad glm) diff --git a/headers/shaders.hpp b/headers/shaders.hpp index 1dfc1fa..95ebfd5 100644 --- a/headers/shaders.hpp +++ b/headers/shaders.hpp @@ -1,3 +1,4 @@ +#include "glm/fwd.hpp" #include #include #include @@ -18,5 +19,9 @@ namespace Shaders { void setBool(const std::string &name, bool val) const; void setInt(const std::string &name, int val) const; void setFloat(const std::string &name, float val) const; + void setMat4(const std::string &name, glm::mat4 val) const; + + private: + unsigned int uniLocation(const std::string &name) const; }; } diff --git a/headers/textures.hpp b/headers/textures.hpp index 17c0033..0bdb916 100644 --- a/headers/textures.hpp +++ b/headers/textures.hpp @@ -7,7 +7,7 @@ namespace Textures { unsigned int id; Texture2D(const char* t_src); void load(); - void use(); + void bind(); private: const char* texture_src; int width, height, nrChannels; diff --git a/lib/glm b/lib/glm new file mode 160000 index 0000000..0a6d333 --- /dev/null +++ b/lib/glm @@ -0,0 +1 @@ +Subproject commit 0a6d3334ea747b0023d232572d3aff61dc6b48e6 diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl index ad44138..db110e6 100644 --- a/shaders/vertex.glsl +++ b/shaders/vertex.glsl @@ -6,8 +6,15 @@ layout (location = 2) in vec2 aTexCoord; out vec4 VertexColor; out vec2 TexCoord; +uniform mat4 transform = mat4( + 1.0, 0.0, 0.0, 0.0, + 0.0, 1.0, 0.0, 0.0, + 0.0, 0.0, 1.0, 0.0, + 0.0, 0.0, 0.0, 1.0 + ); + void main() { - gl_Position = vec4(aPos, 1.0); + gl_Position = transform * vec4(aPos, 1.0); VertexColor = vec4(aColor, 1.0); TexCoord = aTexCoord; } diff --git a/src/main.cpp b/src/main.cpp index 8f98da9..bfc3fe6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,8 @@ #include #include +#include +#include +#include #include #include #include @@ -68,35 +71,30 @@ RenderObj preRenderCallback(unsigned int indices[], unsigned int indices_count, glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)(6*sizeof(float))); glEnableVertexAttribArray(2); - Shaders::Shader shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE); Textures::Texture2D texture(RUSTY_METAL_TEXTURE); return RenderObj {EBO, shader, texture}; } -void renderCallback(RenderObj ro) { - glClearColor(0.0f, 0.0f, 0.0f, 1.0f); +void renderCallback() { + // Make background + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); +} - float time = glfwGetTime(); - float gVal = (sin(time) / 1.5f) + 0.5f; - - - ro.shader.use(); - ro.shader.setFloat("r", gVal); - ro.shader.setFloat("g", gVal); - ro.shader.setFloat("b", gVal); +void renderObject(RenderObj ro, glm::mat4 T) { + // Bind the texture + ro.texture.bind(); - ro.texture.use(); + ro.shader.setMat4("transform", T); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ro.EBO); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); } int main() { - glfwInit(); - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); @@ -116,9 +114,9 @@ int main() { float verts[] = { 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, - 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, - -0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f, -1.0f, -1.0f, - -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, -1.0f + 0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, + -0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f, + -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f }; // Vert struc: x y z r g b tx ty @@ -140,7 +138,31 @@ int main() { processInput(win); // rendering - renderCallback(ro); + renderCallback(); + + /* OBJECT RENDERING */ + float time = glfwGetTime(); + float gVal = (sin(time) / 1.5f) + 0.5f; + + // Coloring uniform + ro.shader.use(); + // ro.shader.setFloat("r", gVal); + // ro.shader.setFloat("g", gVal); + // ro.shader.setFloat("b", gVal); + + // Transformation + float rotang = time; + + glm::mat4 T = glm::mat4(1.0f); + T = glm::rotate(T, rotang, glm::vec3(0.0, 0.707, 0.707)); + T = glm::scale(T, glm::vec3(0.5, 0.5, 0.5)); + renderObject(ro, T); + + glm::mat4 T2 = glm::mat4(1.0f); + T2 = glm::translate(T2, glm::vec3(-0.5, 0.4, 0.0)); + T2 = glm::rotate(T2, rotang, glm::vec3(0.707, 0.707, 0.0)); + T2 = glm::scale(T2, glm::vec3(0.5, 0.5, 0.2)); + renderObject(ro, T2); // glfw glfwSwapBuffers(win); diff --git a/src/shaders.cpp b/src/shaders.cpp index 870006c..bb4997e 100644 --- a/src/shaders.cpp +++ b/src/shaders.cpp @@ -7,6 +7,8 @@ #include "../headers/shaders.hpp" +#include + namespace Shaders { std::string loadSourceFromFile(const char* fp) { std::string src; @@ -88,14 +90,23 @@ namespace Shaders { void Shader::use() { glUseProgram(id); } void Shader::setInt(const std::string &name, int val) const { - glUniform1i(glGetUniformLocation(id, name.c_str()), val); + glUniform1i(uniLocation(name), val); } void Shader::setFloat(const std::string &name, float val) const { - glUniform1f(glGetUniformLocation(id, name.c_str()), val); + glUniform1f(uniLocation(name), val); } void Shader::setBool(const std::string &name, bool val) const { - glUniform1i(glGetUniformLocation(id, name.c_str()), (int)val) ; + glUniform1i(uniLocation(name), (int)val); + } + + void Shader::setMat4(const std::string &name, glm::mat4 val) const { + glUniformMatrix4fv(uniLocation(name), 1, GL_FALSE, glm::value_ptr(val)); + }; + + // Private stuff + unsigned int Shader::uniLocation(const std::string &name) const { + return glGetUniformLocation(id, name.c_str()); } } diff --git a/src/textures.cpp b/src/textures.cpp index 2e7ff0b..a75cc71 100644 --- a/src/textures.cpp +++ b/src/textures.cpp @@ -40,7 +40,7 @@ namespace Textures { stbi_image_free(data); } - void Texture2D::use() { + void Texture2D::bind() { glBindTexture(GL_TEXTURE_2D, id); } }