From 9165e30d0e3383778d513dda4b6f8e331918cbce Mon Sep 17 00:00:00 2001 From: Nigel Barink Date: Wed, 4 May 2022 23:25:18 +0200 Subject: [PATCH] Incorrectly loading a model, Adding a VertexArray abstraction * Using import library assimp to incorrectly load a cube.obj * Using a temporary Renderable class as a placeholder for all data needed to render the mesh. * Vertex Array abstraction added --- .gitignore | 2 + .gitmodules | 3 + MyGraphicsEngine/ModelImporter.cpp | 70 +++++++++- MyGraphicsEngine/Shader.cpp | 2 +- MyGraphicsEngine/VertexArray.cpp | 25 ++++ .../include/AssetManager/ModelImporter.h | 9 +- .../include/Editor/EditorWindow.h | 12 ++ .../include/MyGraphicsEngine/Mesh.h | 14 +- .../include/MyGraphicsEngine/VertexArray.h | 18 +++ MyGraphicsEngine/shaders/fragment.shader | 2 +- SandboxApplication/Cube.mtl | 12 ++ SandboxApplication/Cube.obj | 40 ++++++ SandboxApplication/Renderable.cpp | 50 +++++++ SandboxApplication/Renderable.h | 24 ++++ SandboxApplication/Sandbox.cpp | 128 ++++++------------ TODO.md | 2 +- imgui.ini | 10 -- libs/assimp | 1 + premake5.lua | 10 +- 19 files changed, 322 insertions(+), 112 deletions(-) create mode 100644 MyGraphicsEngine/VertexArray.cpp create mode 100644 MyGraphicsEngine/include/Editor/EditorWindow.h create mode 100644 MyGraphicsEngine/include/MyGraphicsEngine/VertexArray.h create mode 100644 SandboxApplication/Cube.mtl create mode 100644 SandboxApplication/Cube.obj create mode 100644 SandboxApplication/Renderable.cpp create mode 100644 SandboxApplication/Renderable.h delete mode 100644 imgui.ini create mode 160000 libs/assimp diff --git a/.gitignore b/.gitignore index dc2c197..83d36f4 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,5 @@ x64/ *.gltf !sponza.gltf + +imgui.ini \ No newline at end of file diff --git a/.gitmodules b/.gitmodules index 8ac84ea..c3b7a0a 100644 --- a/.gitmodules +++ b/.gitmodules @@ -16,3 +16,6 @@ [submodule "ImGui"] path = libs/ImGui url = https://github.com/ocornut/imgui.git +[submodule "assimp"] + path = libs/assimp + url = https://github.com/assimp/assimp.git diff --git a/MyGraphicsEngine/ModelImporter.cpp b/MyGraphicsEngine/ModelImporter.cpp index 937d156..cce44a3 100644 --- a/MyGraphicsEngine/ModelImporter.cpp +++ b/MyGraphicsEngine/ModelImporter.cpp @@ -1,5 +1,6 @@ #include "include/AssetManager/ModelImporter.h" + void ModelImporter::ImportFBX(std::string path) { spdlog::warn("ImportFBX not implemented!"); @@ -25,7 +26,10 @@ void ModelImporter::Import(std::string path) spdlog::warn("Import not implemented!"); } -void ModelImporter::Test() { +std::vector ModelImporter::Test() { + + /* + spdlog::info("====== Tiny GLTF ======"); tinygltf::Model loadedModel; tinygltf::TinyGLTF loader; std::string error; @@ -44,4 +48,68 @@ void ModelImporter::Test() { spdlog::info("Meshes in model: {}", loadedModel.meshes.size()); spdlog::info("Primitives in mesh: {}", loadedModel.meshes[0].primitives.size()); + */ + + + spdlog::info("======= Assimp ======"); + + Assimp::Importer importer; + const aiScene* scene = importer.ReadFile("build/SandboxApplication/Debug/Cube.obj", aiProcess_Triangulate | aiProcess_FlipUVs); + + aiNode* currentNode = scene->mRootNode; + + return processNode(currentNode, scene); + + +} + +std::vector ModelImporter::processNode(aiNode* node, const aiScene* scene) { + std::vector meshes; + for (unsigned int i = 0; i < node->mNumMeshes; i++) { + aiMesh* mesh = scene->mMeshes[node->mMeshes[i]]; + meshes.push_back(processMesh(mesh, scene)); + } + + for (unsigned int i = 0; i < node->mNumChildren; i++) { + auto m2 = processNode(node->mChildren[i], scene); + + for(auto m : m2) { + meshes.push_back(m); + } + } + + return meshes; +} + +BarinkEngine::Mesh ModelImporter::processMesh(aiMesh* mesh, const aiScene* scene) { + std::vector vertices ; + std::vector indices; + + // Process vertices + for (unsigned int i = 0; i < mesh->mNumVertices; i++) { + glm::vec3 vector; + vector.x = mesh->mVertices[i].x; + vector.y = mesh->mVertices[i].y; + vector.z = mesh->mVertices[i].z; + vertices.push_back(vector); + } + + // Process Indices + for (unsigned int i = 0; i < mesh->mNumFaces; i++) { + aiFace face = mesh->mFaces[i]; + if (face.mNumIndices < 3) + continue; + for (unsigned int j = 0; j < face.mNumIndices; j++) { + indices.push_back(face.mIndices[j]); + } + } + + BarinkEngine::Mesh result; + + + result.vertices = vertices; + result.elements = indices; + + return result; + } \ No newline at end of file diff --git a/MyGraphicsEngine/Shader.cpp b/MyGraphicsEngine/Shader.cpp index fb4f530..9f07d45 100644 --- a/MyGraphicsEngine/Shader.cpp +++ b/MyGraphicsEngine/Shader.cpp @@ -58,7 +58,7 @@ Shader::Shader(const std::string vertexShaderPath, const std::string fragmentSha } char* Shader::readFile (const char* filePath){ - spdlog::info("try Open file {}", filePath); + std::ifstream file ; file.open(filePath); diff --git a/MyGraphicsEngine/VertexArray.cpp b/MyGraphicsEngine/VertexArray.cpp new file mode 100644 index 0000000..18fd7bc --- /dev/null +++ b/MyGraphicsEngine/VertexArray.cpp @@ -0,0 +1,25 @@ +#include +#include + +void VertexArray::Create(){ + glGenVertexArrays(1, &id); +} + +void VertexArray::Bind(){ + glBindVertexArray(id); +} + +void VertexArray::Unbind(){ + glBindVertexArray(0); +} + +void VertexArray::Delete() { + glDeleteVertexArrays(1, &id); +} + + +void VertexArray::AttachAttribute(unsigned int index , int size, int stride ){ + glVertexAttribPointer(index, size, GL_FLOAT, GL_FALSE, stride, 0); + glEnableVertexAttribArray(0); +} + diff --git a/MyGraphicsEngine/include/AssetManager/ModelImporter.h b/MyGraphicsEngine/include/AssetManager/ModelImporter.h index 6630fe6..9a6ab3f 100644 --- a/MyGraphicsEngine/include/AssetManager/ModelImporter.h +++ b/MyGraphicsEngine/include/AssetManager/ModelImporter.h @@ -4,7 +4,10 @@ #define STB_IMAGE_WRITE_IMPLEMENTATION #define TINYGLTF_NO_EXTERNAL_IMAGE -#include +#include +#include +#include +#include #include #include @@ -14,9 +17,11 @@ private: void ImportBlend(std::string path); void ImportGLTF(std::string path); void ImportOBJ(std::string path); + static BarinkEngine::Mesh ModelImporter::processMesh(aiMesh* mesh, const aiScene* scene); + static std::vector ModelImporter::processNode(aiNode* node, const aiScene* scene); public: void Import(std::string path); - static void Test(); + static std::vector Test(); }; \ No newline at end of file diff --git a/MyGraphicsEngine/include/Editor/EditorWindow.h b/MyGraphicsEngine/include/Editor/EditorWindow.h new file mode 100644 index 0000000..57fe9f5 --- /dev/null +++ b/MyGraphicsEngine/include/Editor/EditorWindow.h @@ -0,0 +1,12 @@ +#pragma once +#include + +class EditorWindow { +protected: + std::string WindowTitle; + +public: + + virtual void Show() = 0; + +}; \ No newline at end of file diff --git a/MyGraphicsEngine/include/MyGraphicsEngine/Mesh.h b/MyGraphicsEngine/include/MyGraphicsEngine/Mesh.h index 57c35a7..3eeae11 100644 --- a/MyGraphicsEngine/include/MyGraphicsEngine/Mesh.h +++ b/MyGraphicsEngine/include/MyGraphicsEngine/Mesh.h @@ -1,10 +1,12 @@ #pragma once #include #include +namespace BarinkEngine{ -class Mesh { -public: - std::vector vertices; - std::vector elements; - std::vector uv; -}; \ No newline at end of file + class Mesh { + public: + std::vector vertices; + std::vector elements; + std::vector uv; + }; +} \ No newline at end of file diff --git a/MyGraphicsEngine/include/MyGraphicsEngine/VertexArray.h b/MyGraphicsEngine/include/MyGraphicsEngine/VertexArray.h new file mode 100644 index 0000000..383da61 --- /dev/null +++ b/MyGraphicsEngine/include/MyGraphicsEngine/VertexArray.h @@ -0,0 +1,18 @@ +#pragma once + +class VertexArray{ +private: +unsigned int id; + + +public: +void Create(); +void Bind(); +void Unbind(); + +void Delete(); + +void AttachAttribute(unsigned int index, int size, int stride); + + +}; \ No newline at end of file diff --git a/MyGraphicsEngine/shaders/fragment.shader b/MyGraphicsEngine/shaders/fragment.shader index 3148a0f..c0129ea 100644 --- a/MyGraphicsEngine/shaders/fragment.shader +++ b/MyGraphicsEngine/shaders/fragment.shader @@ -3,5 +3,5 @@ out vec4 FragColor; void main(){ - FragColor = vec4(0.0f, 1.0f, 0.0f , 1.0f); + FragColor = vec4(0.5f, 0.5f, 0.0f , 1.0f); } \ No newline at end of file diff --git a/SandboxApplication/Cube.mtl b/SandboxApplication/Cube.mtl new file mode 100644 index 0000000..32c0596 --- /dev/null +++ b/SandboxApplication/Cube.mtl @@ -0,0 +1,12 @@ +# Blender 3.1.2 MTL File: 'None' +# www.blender.org + +newmtl Material +Ns 360.000000 +Ka 1.000000 1.000000 1.000000 +Kd 0.800000 0.800000 0.800000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 +illum 2 diff --git a/SandboxApplication/Cube.obj b/SandboxApplication/Cube.obj new file mode 100644 index 0000000..fcec802 --- /dev/null +++ b/SandboxApplication/Cube.obj @@ -0,0 +1,40 @@ +# Blender 3.1.2 +# www.blender.org +mtllib Cube.mtl +o Cube +v 1.000000 1.000000 -1.000000 +v 1.000000 -1.000000 -1.000000 +v 1.000000 1.000000 1.000000 +v 1.000000 -1.000000 1.000000 +v -1.000000 1.000000 -1.000000 +v -1.000000 -1.000000 -1.000000 +v -1.000000 1.000000 1.000000 +v -1.000000 -1.000000 1.000000 +vn -0.0000 1.0000 -0.0000 +vn -0.0000 -0.0000 1.0000 +vn -1.0000 -0.0000 -0.0000 +vn -0.0000 -1.0000 -0.0000 +vn 1.0000 -0.0000 -0.0000 +vn -0.0000 -0.0000 -1.0000 +vt 0.625000 0.500000 +vt 0.375000 0.500000 +vt 0.625000 0.750000 +vt 0.375000 0.750000 +vt 0.875000 0.500000 +vt 0.625000 0.250000 +vt 0.125000 0.500000 +vt 0.375000 0.250000 +vt 0.875000 0.750000 +vt 0.625000 1.000000 +vt 0.625000 0.000000 +vt 0.375000 1.000000 +vt 0.375000 0.000000 +vt 0.125000 0.750000 +s 0 +usemtl Material +f 1/1/1 5/5/1 7/9/1 3/3/1 +f 4/4/2 3/3/2 7/10/2 8/12/2 +f 8/13/3 7/11/3 5/6/3 6/8/3 +f 6/7/4 2/2/4 4/4/4 8/14/4 +f 2/2/5 1/1/5 3/3/5 4/4/5 +f 6/8/6 5/6/6 1/1/6 2/2/6 diff --git a/SandboxApplication/Renderable.cpp b/SandboxApplication/Renderable.cpp new file mode 100644 index 0000000..4567016 --- /dev/null +++ b/SandboxApplication/Renderable.cpp @@ -0,0 +1,50 @@ +#include "Renderable.h" + +Renderable Renderable::Load() +{ + return Renderable(); +} + +Renderable::Renderable() +{ + + meshes = ModelImporter::Test(); + + transform.Scale = glm::vec3(1.0f); + transform.Rotation = glm::vec3(0.0f, 90.0f, 0.0f); + transform.Position = glm::vec3(0.0f, 0.0f, -10.0f); + + + VAO.Create(); + VAO.Bind(); + + + vertexBuffer.createBuffer(); + vertexBuffer.Bind(false); + vertexBuffer.setBufferData(&meshes[0].vertices[0], meshes[0].vertices.size() * sizeof(glm::vec3), false); + + + elementBuffer.createBuffer(); + elementBuffer.Bind(true); + elementBuffer.setBufferData(&meshes[0].elements[0], meshes[0].elements.size() * sizeof(unsigned int), true); + + VAO.AttachAttribute(0, 3, 0); + + vertexBuffer.Unbind(false); + VAO.Unbind(); + + +} + +Renderable::~Renderable() +{ +} + +void Renderable::Draw() +{ + VAO.Bind(); + elementBuffer.Bind(true); + glDrawElements(GL_TRIANGLES, static_cast(meshes[0].elements.size()), GL_UNSIGNED_SHORT, NULL); + VAO.Unbind(); + +} diff --git a/SandboxApplication/Renderable.h b/SandboxApplication/Renderable.h new file mode 100644 index 0000000..778ab5f --- /dev/null +++ b/SandboxApplication/Renderable.h @@ -0,0 +1,24 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include +class Renderable { +private: + std::vector meshes; + Renderable(); + +public: + Buffer vertexBuffer; + Buffer elementBuffer; + VertexArray VAO; + Transform transform; + ~Renderable(); + + static Renderable Load(); + void Draw(); + +}; \ No newline at end of file diff --git a/SandboxApplication/Sandbox.cpp b/SandboxApplication/Sandbox.cpp index ff9842d..7787248 100644 --- a/SandboxApplication/Sandbox.cpp +++ b/SandboxApplication/Sandbox.cpp @@ -1,60 +1,37 @@ +#include + #include #include #include -#include -#include -#include -#include +#include #include "imgui.h" #include "backends/imgui_impl_glfw.h" #include "backends/imgui_impl_opengl3.h" +#include "Renderable.h" +#include -/* -* extern "C" + + +extern "C" { #include "lauxlib.h" #include "lua.h" #include "lualib.h" } -#include - +/* #include #include */ int main(int argc, char* argv[]) { - spdlog::info("Working directory: {}", argv[0]); - - //ModelImporter::Test(); - - - Transform t; - t.Scale.x = 1.0f; - t.Scale.y = 1.0f; - t.Rotation.y = 90.0f; - - Mesh mesh; - - mesh.vertices = { - glm::vec3( 0.5f, 0.5f, 0.0f), // top, right - glm::vec3( 0.5f, -0.5f, 0.0f), // bottom right - glm::vec3(-0.5f, -0.5f, 0.0f), // bottom left - glm::vec3(-0.5f, 0.5f, 0.0f) // top left - }; - - - mesh.elements = { - 0,1,3, - 1,2,3 - }; - - - Camera cam(glm::vec3(2.0f, 0.0f, 0.0f), glm::vec3(0.0f,0.0f,0.0f), 90.0f); - + char cwd[256]; + getcwd(cwd, 256); + spdlog::info("Working directory: {}", cwd); + BarinkWindow GameWindow(800, 600); @@ -66,45 +43,22 @@ int main(int argc, char* argv[]) { ImGui::StyleColorsDark(); ImGui_ImplGlfw_InitForOpenGL(GameWindow.windowptr(), true); ImGui_ImplOpenGL3_Init("#version 440"); - + + Camera cam(glm::vec3(0.0f, 1.5f, -10.0f), glm::vec3(0.0f, 0.0f, 0.0f), 90.0f); + Renderable Cube = Renderable::Load(); + + spdlog::info("==== Load Shader(s) ===="); std::string vertexShaderSource = "build/SandboxApplication/Debug/test.vs"; std::string fragmentShaderSource = "build/SandboxApplication/Debug/test.fs"; Shader shader (vertexShaderSource, fragmentShaderSource); - /* - * lua_State* L = luaL_newstate(); + lua_State* L = luaL_newstate(); luaL_openlibs(L); luaL_dostring(L, "print('BarinkEngine')"); - luaL_dofile(L,"./script.lua"); - */ - - spdlog::info("Vertices: {}, {} bytes", mesh.vertices.size(), sizeof(glm::vec3)); - spdlog::info("Elements: {}, {} bytes", mesh.elements.size(), sizeof(GLushort)); - - - Buffer VertexBuffer; - Buffer ElementBuffer; - - unsigned int VAO; - - glGenVertexArrays(1, &VAO); - glBindVertexArray(VAO); - - VertexBuffer.createBuffer(); - VertexBuffer.Bind(false); - VertexBuffer.setBufferData(&mesh.vertices[0], mesh.vertices.size() * sizeof(glm::vec3),false); - - ElementBuffer.createBuffer(); - ElementBuffer.Bind(true); - ElementBuffer.setBufferData(&mesh.elements[0], mesh.elements.size() * sizeof(GLushort), true); - - - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); - glEnableVertexAttribArray(0); - - VertexBuffer.Unbind(false); - glBindVertexArray(0); + spdlog::info("==== Run script ===="); + luaL_dofile(L,"build/SandboxApplication/Debug/script.lua"); + /* * gau_Manager* mgr; @@ -122,17 +76,15 @@ int main(int argc, char* argv[]) { mixer = gau_manager_mixer(mgr); */ - - - + while (!GameWindow.WindowShouldClose()) { - glm::mat4 tran = glm::translate(glm::mat4(), t.Position); - glm::mat4 scale = glm::scale(glm::mat4(), t.Scale); + glm::mat4 tran = glm::translate(glm::mat4(), Cube.transform.Position); + glm::mat4 scale = glm::scale(glm::mat4(), Cube.transform.Scale); glm::mat4 rot = - glm::rotate(glm::mat4(), glm::radians(t.Rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)) * - glm::rotate(glm::mat4(), glm::radians(t.Rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)) * - glm::rotate(glm::mat4(), glm::radians(t.Rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); + glm::rotate(glm::mat4(), glm::radians(Cube.transform.Rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)) * + glm::rotate(glm::mat4(), glm::radians(Cube.transform.Rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)) * + glm::rotate(glm::mat4(), glm::radians(Cube.transform.Rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); glm::mat4 model = tran * rot * scale; @@ -148,23 +100,23 @@ int main(int argc, char* argv[]) { shader.setUniformMat4("P", projection); shader.setUniformMat4("M", model); shader.setUniformMat4("V", cam.GetViewMatrix()); - glBindVertexArray(VAO); - ElementBuffer.Bind(true); - glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, NULL); - glBindVertexArray(0); - + + Cube.Draw(); ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); - ImGui::Begin("Transform Test Tool"); + ImGui::Begin("Test"); - ImGui::SliderFloat("Scale Y:", (float*)&t.Scale.y, 1, 4); - ImGui::SliderFloat("Scale X:", (float*)&t.Scale.x, 1, 4); - - ImGui::SliderFloat("Position X:", (float*)&t.Position.z, -5, 5); + ImGui::SliderFloat("Scale Y:", (float*)&Cube.transform.Scale.y, 1, 4); + ImGui::SliderFloat("Scale X:", (float*)&Cube.transform.Scale.x, 1, 4); + ImGui::SliderFloat("Camera Zoom:", &cam.Zoom, 40, 190); + ImGui::SliderFloat("Position X:", (float*)&Cube.transform.Position.x, -9, -60); + + ImGui::SliderFloat("Rotate Y:", (float*)&Cube.transform.Rotation.y, 0, 180); + ImGui::SliderFloat("Rotate Z:", (float*)&Cube.transform.Rotation.z, 0, 180); ImGui::End(); @@ -182,8 +134,8 @@ int main(int argc, char* argv[]) { ImGui_ImplGlfw_Shutdown(); ImGui::DestroyContext(); - glDeleteVertexArrays(1, &VAO); - ElementBuffer.Delete(); + Cube.VAO.Delete(); + Cube.elementBuffer.Delete(); } diff --git a/TODO.md b/TODO.md index b699487..1ff52d4 100644 --- a/TODO.md +++ b/TODO.md @@ -9,7 +9,7 @@ Basic Triangle rendering \ Basic IMGui \ Basic Textures \ - Link GLEW \ + Link GLEW or GLAD \ Work on basic logging \ Input handling \ More shader work \ diff --git a/imgui.ini b/imgui.ini deleted file mode 100644 index f4374b5..0000000 --- a/imgui.ini +++ /dev/null @@ -1,10 +0,0 @@ -[Window][Debug##Default] -Pos=60,60 -Size=400,400 -Collapsed=0 - -[Window][Transform Test Tool] -Pos=443,154 -Size=396,490 -Collapsed=0 - diff --git a/libs/assimp b/libs/assimp new file mode 160000 index 0000000..19f2a62 --- /dev/null +++ b/libs/assimp @@ -0,0 +1 @@ +Subproject commit 19f2a624a9d69aa8d1680685766b76bd8983ffe8 diff --git a/premake5.lua b/premake5.lua index f3447be..7203669 100644 --- a/premake5.lua +++ b/premake5.lua @@ -12,6 +12,7 @@ workspace "BarinkEngine" buildmessage "Building BarinkEngineSandbox ..." includedirs { + "./libs/assimp/include", "./libs/glad/include", "./MyGraphicsEngine", "./MyGraphicsEngine/include", @@ -20,7 +21,9 @@ workspace "BarinkEngine" "./libs/GorillaAudio/include", "./libs/lua/include", "./libs/glfw/include", - "./libs/ImGui" + "./libs/ImGui", + "./libs/lua/include" + } libdirs{ @@ -31,7 +34,7 @@ workspace "BarinkEngine" } links{ - "liblua", + "lua54", "spdlog", "glfw3", "MyGraphicsEngine" @@ -62,6 +65,7 @@ workspace "BarinkEngine" buildmessage "Building MyGraphicsEngine ..." includedirs { + "./libs/assimp/include", "./libs/glad/include", "./libs/glfw/include", "./libs/tinygltf", @@ -74,11 +78,13 @@ workspace "BarinkEngine" libdirs{ "./libs/spdlog/build/Release", + "./libs/assimp/lib/Debug", "./libs/glfw/build/src/Debug" } links { "spdlog", + "assimp-vc143-mtd", "glfw3" }