From 28927d9a4e20a56f28cec6070c00519f8685fa1d Mon Sep 17 00:00:00 2001 From: Nigel Barink Date: Sat, 6 May 2023 21:06:49 +0200 Subject: [PATCH] Moved model file loading logic to the editor code base --- Editor/src/AssetManagement/Asset.h | 29 +---- Editor/src/AssetManagement/AssetFinder.h | 20 ++-- .../AssetLoaders/AssetLoader.h | 9 ++ .../AssetLoaders/ModelLoader.cpp | 102 ++++++++++++++++++ .../AssetLoaders/ModelLoader.h | 10 ++ Editor/src/AssetManagement/AssetManager.cpp | 51 ++++----- Editor/src/AssetManagement/AssetManager.h | 17 +-- Editor/src/AssetManagement/uuid.h | 63 +++++++++++ Editor/src/MainMenuBar.cpp | 4 +- Editor/src/app.cpp | 29 ++--- YoggieEngine/premake5.lua | 4 +- YoggieEngine/src/Application.cpp | 3 + .../src/AssetManager/ModelImporter.cpp | 102 ------------------ YoggieEngine/src/AssetManager/ModelImporter.h | 29 ----- .../src/Graphics/Primitives/Texture.cpp | 2 +- YoggieEngine/src/Graphics/Renderer.cpp | 11 -- .../src/Shaders/Deferred/lightPass.frag | 2 +- premake5.lua | 6 +- 18 files changed, 260 insertions(+), 233 deletions(-) create mode 100644 Editor/src/AssetManagement/AssetLoaders/AssetLoader.h create mode 100644 Editor/src/AssetManagement/AssetLoaders/ModelLoader.cpp create mode 100644 Editor/src/AssetManagement/AssetLoaders/ModelLoader.h create mode 100644 Editor/src/AssetManagement/uuid.h delete mode 100644 YoggieEngine/src/AssetManager/ModelImporter.cpp delete mode 100644 YoggieEngine/src/AssetManager/ModelImporter.h diff --git a/Editor/src/AssetManagement/Asset.h b/Editor/src/AssetManagement/Asset.h index 19881dd..53ede22 100644 --- a/Editor/src/AssetManagement/Asset.h +++ b/Editor/src/AssetManagement/Asset.h @@ -2,6 +2,7 @@ #include "../../YoggieEngine/src/YoggieEngine.h" #include #include +#include "uuid.h" enum class ASSET_TYPE { Unknown = -1, @@ -13,34 +14,12 @@ enum class ASSET_TYPE { class Asset { public: Asset(const char* name): name(name) {} + uuid::v4::UUID id = uuid::v4::UUID::New(); + virtual ASSET_TYPE GetType() { return ASSET_TYPE::Unknown; } const char* GetName() const { return name.c_str(); } bool isFolder = false ; protected: std::string name; - -}; - -class MeshAsset : Asset { -public: - MeshAsset(YoggieEngine::Mesh mesh) : Asset("New MeshAsset"), mesh(mesh) {} - ASSET_TYPE GetType() override { return ASSET_TYPE::Mesh; } -private: - YoggieEngine::Mesh& mesh; -}; - -class TextureAsset : Asset { -public: - TextureAsset (YoggieEngine::Texture texture): Asset("New TextureAsset"), texture(texture) {} - ASSET_TYPE GetType() override { return ASSET_TYPE::Texture; } -private: - YoggieEngine::Texture& texture; -}; - -class MaterialAsset : Asset { -public: - MaterialAsset () : Asset("New MaterialAsset"){} - ASSET_TYPE GetType() override { return ASSET_TYPE::Material; } -private: - + }; \ No newline at end of file diff --git a/Editor/src/AssetManagement/AssetFinder.h b/Editor/src/AssetManagement/AssetFinder.h index f6cdba4..7e4684a 100644 --- a/Editor/src/AssetManagement/AssetFinder.h +++ b/Editor/src/AssetManagement/AssetFinder.h @@ -5,24 +5,22 @@ class AssetFinder : EditorWindow { public: - AssetFinder() : EditorWindow("Assets") {} + AssetFinder() : EditorWindow("Assets"), + folderIcon ("rsc/FolderIcon.png"), + assetIcon ("rsc/AssetIcon.png") + {} void Draw() override { - - ImGui::DragInt("IconSize", &iconSize, 1, 30, 90); - - + ImGui::DragInt("Maximum Columns", &maxColumns, 1, 1, 6); if (ImGui::BeginTable("##resources", 3)) { ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.f, 0.f, 0.f, 0.f)); ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.f, 0.f, 0.f, 0.f)); ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(1.f, 1.f, 1.f, 0.2f)); - YoggieEngine::Texture folderIcon = YoggieEngine::Texture("rsc/folderIcon.png"); - YoggieEngine::Texture assetIcon = YoggieEngine::Texture("rsc/assetIcon.png"); - + /* int row = 0; int column = 0; for (auto& asset : AssetManager::assets) { @@ -52,6 +50,7 @@ public: column++; } + */ ImGui::PopStyleColor(3); @@ -64,5 +63,10 @@ public: private: int iconSize = 60; + int maxColumns = 3; + + YoggieEngine::Texture folderIcon; + YoggieEngine::Texture assetIcon; + }; diff --git a/Editor/src/AssetManagement/AssetLoaders/AssetLoader.h b/Editor/src/AssetManagement/AssetLoaders/AssetLoader.h new file mode 100644 index 0000000..ff38519 --- /dev/null +++ b/Editor/src/AssetManagement/AssetLoaders/AssetLoader.h @@ -0,0 +1,9 @@ +#pragma once +#include +#include "../Asset.h" +class AssetLoader { +public: + virtual Asset LoadAsset(std::filesystem::path& path) = 0; + // virtual void PackageAsset(Asset& asset ) = 0; + +}; \ No newline at end of file diff --git a/Editor/src/AssetManagement/AssetLoaders/ModelLoader.cpp b/Editor/src/AssetManagement/AssetLoaders/ModelLoader.cpp new file mode 100644 index 0000000..a4dad6e --- /dev/null +++ b/Editor/src/AssetManagement/AssetLoaders/ModelLoader.cpp @@ -0,0 +1,102 @@ +#include "ModelLoader.h" +#include +#include +#include + + + +void ProcessVertices(aiMesh* mesh, std::vector& out_vertices) { + for (unsigned int i = 0; i < mesh->mNumVertices; i++) { + YoggieEngine::Vertex v{}; + + glm::vec3 vector; + vector.x = mesh->mVertices[i].x; + vector.y = mesh->mVertices[i].y; + vector.z = mesh->mVertices[i].z; + + v.vertices = vector; + + if (mesh->mTextureCoords[0]) { + glm::vec2 texCoord; + + texCoord.x = mesh->mTextureCoords[0][i].x; + texCoord.y = mesh->mTextureCoords[0][i].y; + + v.uv = texCoord; + + } + + out_vertices.push_back(v); + } +} + + +void ProcessIndices(aiMesh* mesh, std::vector& out_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++) { + out_indices.push_back(face.mIndices[j]); + } + } +} + + + +YoggieEngine::Mesh processMesh(aiMesh* mesh, const aiScene* scene) { + std::vector indices; + std::vector vertices; + + ProcessVertices(mesh, vertices); + ProcessIndices(mesh, indices); + + YoggieEngine::Mesh result; + result.vertices = vertices; + result.elements = indices; + + return result; + +} + +std::vector processNode(aiNode* node, const aiScene* scene) { + + std::vector meshes = std::vector(); + + 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; + + +} + +Asset ModelLoader::LoadAsset(std::filesystem::path& path) { + + Assimp::Importer importer; + + const aiScene* scene = importer.ReadFile(path.string(), aiProcess_Triangulate | aiProcess_FlipUVs); + + aiNode* currentNode = scene->mRootNode; + + spdlog::debug("Loading meshes!"); + + auto meshes = processNode(currentNode, scene); + + return Asset("Mesh"); + + + +} \ No newline at end of file diff --git a/Editor/src/AssetManagement/AssetLoaders/ModelLoader.h b/Editor/src/AssetManagement/AssetLoaders/ModelLoader.h new file mode 100644 index 0000000..e9db61f --- /dev/null +++ b/Editor/src/AssetManagement/AssetLoaders/ModelLoader.h @@ -0,0 +1,10 @@ +#pragma once +#include "AssetLoader.h" + +class ModelLoader : AssetLoader { + + Asset LoadAsset(std::filesystem::path& path); + + + +}; \ No newline at end of file diff --git a/Editor/src/AssetManagement/AssetManager.cpp b/Editor/src/AssetManagement/AssetManager.cpp index dd96a0e..904d146 100644 --- a/Editor/src/AssetManagement/AssetManager.cpp +++ b/Editor/src/AssetManagement/AssetManager.cpp @@ -1,42 +1,34 @@ #include "AssetManager.h" -#include "../../YoggieEngine/src/AssetManager/ModelImporter.h" + #include #include #include - -std::vector AssetManager::assets; -std::filesystem::path AssetManager::currentPath; +#include "AssetLoaders/ModelLoader.h" +/* +* this is still a very naive approach to the asset manager +*/ void AssetManager::Init() -{ - assets = std::vector(); - currentPath = std::filesystem::path("."); -} - -void AssetManager::BuildAssetView() { - if (currentPath.empty()) { - return; - } - - for (auto& dir_entry : std::filesystem::directory_iterator(currentPath)) - { - auto asset = Asset(dir_entry.path().filename().string().c_str()); - - if (dir_entry.is_directory()) { - asset.isFolder = true; - } - - assets.push_back(asset); - } +// Assets = std::map(); + modelLoader = ModelLoader(); } -void AssetManager::setAssetPath(std::filesystem::path path) -{ - currentPath = path; +void AssetManager::RegisterAsset(Asset& asset) +{ +// Assets.try_emplace(asset.id, asset); } + +void AssetManager::UnregisterAsset(Asset& asset) { +// auto old = Assets[asset.id]; +// Assets[asset.id] = NULL; +} + + + + YoggieEngine::Mesh* AssetManager::LoadFromAssetFile(const std::filesystem::path assetPath) { YoggieEngine::Mesh* imported = nullptr; @@ -100,7 +92,8 @@ YoggieEngine::Mesh* AssetManager::LoadFromAssetFile(const std::filesystem::path YoggieEngine::Renderable* AssetManager::LoadFromSource(const std::filesystem::path srcPath, const std::filesystem::path assetFolder) { - auto model = (YoggieEngine::ModelImporter()).Import(srcPath.string()); + /* + * auto model = (YoggieEngine::ModelImporter()).Import(srcPath.string()); YoggieEngine::Mesh* exportMesh = model->renderable->mesh; std::filesystem::path MeshFileName = assetFolder / srcPath.filename().replace_extension(".mesh"); std::cout << "Save path: " << MeshFileName << std::endl; @@ -145,4 +138,6 @@ YoggieEngine::Renderable* AssetManager::LoadFromSource(const std::filesystem::pa return model->renderable; + */ + return nullptr; } diff --git a/Editor/src/AssetManagement/AssetManager.h b/Editor/src/AssetManagement/AssetManager.h index 08f93d1..0fda5d1 100644 --- a/Editor/src/AssetManagement/AssetManager.h +++ b/Editor/src/AssetManagement/AssetManager.h @@ -1,20 +1,23 @@ #pragma once #include #include "Asset.h" +#include "AssetLoaders/ModelLoader.h" +#include "uuid.h" class AssetManager { public: - static void Init(); - static void BuildAssetView(); - static void setAssetPath(std::filesystem::path path); + void Init(); + + void RegisterAsset(Asset& asset); + void UnregisterAsset(Asset& asset); + static YoggieEngine::Mesh* LoadFromAssetFile(const std::filesystem::path assetPath); static YoggieEngine::Renderable* LoadFromSource(const std::filesystem::path srcPath, const std::filesystem::path assetFolder); - static std::vector assets ; - -private: - static std::filesystem::path currentPath; +private: + std::map Assets; + ModelLoader modelLoader; }; \ No newline at end of file diff --git a/Editor/src/AssetManagement/uuid.h b/Editor/src/AssetManagement/uuid.h new file mode 100644 index 0000000..3fe0102 --- /dev/null +++ b/Editor/src/AssetManagement/uuid.h @@ -0,0 +1,63 @@ +/* +* +* SOURCE: https://github.com/rkg82/uuid-v4/blob/main/uuid/v4/uuid.h +* +*/ +#ifndef __UUID__ +#define __UUID__ + +#include +#include + +namespace uuid::v4 +{ + // Encaasulate the genaeration of a Version 4 UUID object + // A Version 4 UUID is a universally unique identifier that is generated using random numbers. + class UUID + { + public: + // Factory method for creating UUID object. + static UUID New() + { + UUID uuid; + std::random_device rd; + std::mt19937 engine{ rd() }; + std::uniform_int_distribution dist{ 0, 256 }; //Limits of the interval + + for (int index = 0; index < 16; ++index) + { + uuid._data[index] = (unsigned char)dist(engine); + } + + uuid._data[6] = ((uuid._data[6] & 0x0f) | 0x40); // Version 4 + uuid._data[8] = ((uuid._data[8] & 0x3f) | 0x80); // Variant is 10 + + return uuid; + } + + // Returns UUID as formatted string + std::string String() + { + // Formats to "0065e7d7-418c-4da4-b4d6-b54b6cf7466a" + char buffer[256] = { 0 }; + std::snprintf(buffer, 255, + "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", + _data[0], _data[1], _data[2], _data[3], + _data[4], _data[5], + _data[6], _data[7], + _data[8], _data[9], + _data[10], _data[11], _data[12], _data[13], _data[14], _data[15]); + + std::string uuid = buffer; + + return uuid; + } + + private: + UUID() {} + + unsigned char _data[16] = { 0 }; + }; +}; + +#endif // #ifndef __UUID__ \ No newline at end of file diff --git a/Editor/src/MainMenuBar.cpp b/Editor/src/MainMenuBar.cpp index f6a734a..cee558f 100644 --- a/Editor/src/MainMenuBar.cpp +++ b/Editor/src/MainMenuBar.cpp @@ -19,8 +19,8 @@ void MainMenuBar::ApplicationMenu(Project& project) { switch (result) { case(NFD_OKAY): Project::LoadProject(path, project); - AssetManager::setAssetPath(project.GetProjectDirectory()); - AssetManager::BuildAssetView(); + //AssetManager::setAssetPath(project.GetProjectDirectory()); + //AssetManager::BuildAssetView(); break; case(NFD_CANCEL): break; diff --git a/Editor/src/app.cpp b/Editor/src/app.cpp index d48a410..fdc6775 100644 --- a/Editor/src/app.cpp +++ b/Editor/src/app.cpp @@ -12,6 +12,7 @@ #include "PropertyPanels/Inspector.h" #include "Project/ProjectInfo.h" #include "Runtime/RuntimeControls.h" +#include "AssetManagement/uuid.h" #include "Project/Settings.h" #include "Console.h" @@ -26,21 +27,19 @@ public: std::string path = (std::filesystem::current_path()).string(); project.setProjectDirectory(path); - AssetManager::Init(); - AssetManager::setAssetPath(project.GetProjectDirectory()); - AssetManager::BuildAssetView(); - LoadLastOrEmptyProject(); - ProjectInfo projectInfo(project); - Viewport sceneview = Viewport(scene); + //ProjectInfo projectInfo(project); RuntimeControls rc = RuntimeControls(); + + Viewport sceneview = Viewport(scene); SceneExplorer explorer(Selected, scene); Inspector inspector = Inspector(Selected); - Settings settings = Settings(); - // AssetFinder assetsView = AssetFinder(); - Console console = Console(); + //Settings settings = Settings(); + AssetFinder assetsView = AssetFinder(); + //Console console = Console(); + Selected = YoggieEngine::Entity((entt::entity) -1, &scene); double previous = glfwGetTime(); @@ -77,16 +76,18 @@ public: } - projectInfo.Update(); + //projectInfo.Update(); sceneview.Update(); rc.Update(); explorer.Update(); - settings.Update(); + //settings.Update(); inspector.Update(); - console.Update(); + //console.Update(); - ImGui::ShowDemoWindow(); - ImGui::ShowMetricsWindow(); + assetsView.Draw(); + + //ImGui::ShowDemoWindow(); + //ImGui::ShowMetricsWindow(); GuiEnd(); diff --git a/YoggieEngine/premake5.lua b/YoggieEngine/premake5.lua index 6637e64..4dcf375 100644 --- a/YoggieEngine/premake5.lua +++ b/YoggieEngine/premake5.lua @@ -71,8 +71,8 @@ project "YoggieEngine" files { - "./src/**.cpp", - "./src/**.h" + "src/**.cpp", + "src/**.h" } prebuildcommands diff --git a/YoggieEngine/src/Application.cpp b/YoggieEngine/src/Application.cpp index 6aa7ebd..9edab16 100644 --- a/YoggieEngine/src/Application.cpp +++ b/YoggieEngine/src/Application.cpp @@ -59,9 +59,12 @@ namespace YoggieEngine { ImGui::NewFrame(); ImGuizmo::BeginFrame(); ImGui::DockSpaceOverViewport(ImGui::GetMainViewport()); + + } void Application::GuiEnd() { + ImGui::EndFrame(); diff --git a/YoggieEngine/src/AssetManager/ModelImporter.cpp b/YoggieEngine/src/AssetManager/ModelImporter.cpp deleted file mode 100644 index 88d2755..0000000 --- a/YoggieEngine/src/AssetManager/ModelImporter.cpp +++ /dev/null @@ -1,102 +0,0 @@ -#include -#include "ModelImporter.h" - -namespace YoggieEngine { - - SceneObject* ModelImporter::Import(const std::string path) - { - SceneObject* root = new SceneObject(std::string(path), nullptr); - - Assimp::Importer importer; - const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs); - - aiNode* currentNode = scene->mRootNode; - - std::vector meshes = processNode(currentNode, scene); - - std::cout << "[DEBUG]: Loaded " << meshes.size() << " meshes!" << std::endl; - - // create a renderable (per mesh ?? ) - root->renderable = new Renderable(); - root->renderable->mesh = new Mesh(meshes[0]); - - return root; - - } - - 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; - } - - Mesh ModelImporter::processMesh(aiMesh* mesh, const aiScene* scene) { - std::vector indices; - std::vector vertices; - - ProcessVertices(mesh, vertices); - - ProcessIndices(mesh, indices); - - Mesh result; - result.vertices = vertices; - result.elements = indices; - - - return result; - - } - - void ProcessVertices(aiMesh* mesh, std::vector& out_vertices) { - // Process vertices - for (unsigned int i = 0; i < mesh->mNumVertices; i++) { - Vertex v{}; - glm::vec3 vector; - vector.x = mesh->mVertices[i].x; - vector.y = mesh->mVertices[i].y; - vector.z = mesh->mVertices[i].z; - - v.vertices = vector; - - if (mesh->mTextureCoords[0]) { - - glm::vec2 texCoord; - - texCoord.x = mesh->mTextureCoords[0][i].x; - texCoord.y = mesh->mTextureCoords[0][i].y; - - v.uv = texCoord; - - } - - out_vertices.push_back(v); - } - } - - void ProcessIndices(aiMesh* mesh, std::vector& out_indices) { - // 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++) { - out_indices.push_back(face.mIndices[j]); - } - } - } -} \ No newline at end of file diff --git a/YoggieEngine/src/AssetManager/ModelImporter.h b/YoggieEngine/src/AssetManager/ModelImporter.h deleted file mode 100644 index 617a11a..0000000 --- a/YoggieEngine/src/AssetManager/ModelImporter.h +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - -#include "../Graphics/Primitives/Mesh.h" -#include -#include -#include -#include -#include "../Scene/TransformTree/SceneNodeTypes.h" - -namespace YoggieEngine { - void ProcessVertices(aiMesh* mesh, std::vector& out_vertices); - void ProcessIndices(aiMesh* mesh, std::vector& out_indices); - - class ModelImporter { - - public: - - SceneObject* Import(const std::string path); - - - private: - - static Mesh ModelImporter::processMesh(aiMesh* mesh, const aiScene* scene); - static std::vector ModelImporter::processNode(aiNode* node, const aiScene* scene); - - - - }; -} diff --git a/YoggieEngine/src/Graphics/Primitives/Texture.cpp b/YoggieEngine/src/Graphics/Primitives/Texture.cpp index 73dd4cf..8eccaba 100644 --- a/YoggieEngine/src/Graphics/Primitives/Texture.cpp +++ b/YoggieEngine/src/Graphics/Primitives/Texture.cpp @@ -27,7 +27,7 @@ namespace YoggieEngine { } else { - spdlog::error("Failed to load image (%s)", texturePath); + spdlog::error("Failed to load image ({0})", texturePath); } stbi_image_free(data); diff --git a/YoggieEngine/src/Graphics/Renderer.cpp b/YoggieEngine/src/Graphics/Renderer.cpp index 8c018ba..0404e36 100644 --- a/YoggieEngine/src/Graphics/Renderer.cpp +++ b/YoggieEngine/src/Graphics/Renderer.cpp @@ -435,17 +435,6 @@ void Renderer::Render(Scene& scene) BlendingPass(); //PostProcessing(); - - - // Lighting pass - /* - auto lights = scene.getReg().view(); - lights.each([&](auto entity, LightComponent& light) { - renderComponent.shader.setUniformVec3("lighting.color", light.Color); - renderComponent.shader.setUniformFloat("lighting.strength", light.Strength); - }); - - */ commands.clear(); glBindFramebuffer(GL_FRAMEBUFFER, 0); diff --git a/YoggieEngine/src/Shaders/Deferred/lightPass.frag b/YoggieEngine/src/Shaders/Deferred/lightPass.frag index 666616b..b568878 100644 --- a/YoggieEngine/src/Shaders/Deferred/lightPass.frag +++ b/YoggieEngine/src/Shaders/Deferred/lightPass.frag @@ -25,7 +25,7 @@ void main() float Specular = texture(gColorSpec, TexCoords).a; // calculate lighting as usual - vec3 lighting = Albedo * 0.1; // Hard-coded ambient light + vec3 lighting = Albedo * 0.0; // Hard-coded ambient light vec3 viewDir = normalize(viewPos - FragPos); for( int i = 0; i < NR_LIGHTS; ++i) { diff --git a/premake5.lua b/premake5.lua index e5a7d75..3ba729a 100644 --- a/premake5.lua +++ b/premake5.lua @@ -40,8 +40,8 @@ group("Other") group("Libraries") - include('../ImGui') - include("../ImGuizmo") - include("../yaml-cpp") + include('ImGui') + include("ImGuizmo") + include("yaml-cpp")