From 179117b575400d041684e1118945ff26ccd428aa Mon Sep 17 00:00:00 2001 From: Thomas Date: Sat, 21 Dec 2024 19:29:38 +0100 Subject: [PATCH 01/22] basic stuff --- ICE/Graphics/CMakeLists.txt | 3 +- ICE/Graphics/include/Model.h | 18 +++++++++ ICE/Graphics/src/Model.cpp | 9 +++++ ICE/IO/CMakeLists.txt | 2 +- ICE/IO/include/MeshLoader.h | 19 --------- ICE/IO/include/ModelLoader.h | 19 +++++++++ ICE/IO/src/MeshLoader.cpp | 72 ----------------------------------- ICE/IO/src/ModelLoader.cpp | 74 ++++++++++++++++++++++++++++++++++++ 8 files changed, 123 insertions(+), 93 deletions(-) create mode 100644 ICE/Graphics/include/Model.h create mode 100644 ICE/Graphics/src/Model.cpp delete mode 100644 ICE/IO/include/MeshLoader.h create mode 100644 ICE/IO/include/ModelLoader.h delete mode 100644 ICE/IO/src/MeshLoader.cpp create mode 100644 ICE/IO/src/ModelLoader.cpp diff --git a/ICE/Graphics/CMakeLists.txt b/ICE/Graphics/CMakeLists.txt index dc528bba..d1e42010 100644 --- a/ICE/Graphics/CMakeLists.txt +++ b/ICE/Graphics/CMakeLists.txt @@ -11,7 +11,8 @@ target_sources(${PROJECT_NAME} PRIVATE src/ForwardRenderer.cpp src/Material.cpp src/Mesh.cpp - src/GeometryPass.cpp "include/RenderData.h") + src/Model.cpp + src/GeometryPass.cpp) target_link_libraries(${PROJECT_NAME} PUBLIC diff --git a/ICE/Graphics/include/Model.h b/ICE/Graphics/include/Model.h new file mode 100644 index 00000000..abd6148a --- /dev/null +++ b/ICE/Graphics/include/Model.h @@ -0,0 +1,18 @@ +#pragma once + +#include "Asset.h" +#include "Mesh.h" + +namespace ICE { +class Model : public Asset { + public: + Model(const std::vector> &meshes, const std::vector &materials); + + std::vector> getMeshes() const { return m_meshes; } + std::vector getMaterialsIDs() const { return m_materials; } + + private: + std::vector> m_meshes; + std::vector m_materials; +}; +} // namespace ICE \ No newline at end of file diff --git a/ICE/Graphics/src/Model.cpp b/ICE/Graphics/src/Model.cpp new file mode 100644 index 00000000..13782d23 --- /dev/null +++ b/ICE/Graphics/src/Model.cpp @@ -0,0 +1,9 @@ +#include "Model.h" + +namespace ICE { +Model::Model(const std::vector> &meshes, const std::vector &materials) + : m_meshes(meshes), + m_materials(materials) { +} + +} // namespace ICE \ No newline at end of file diff --git a/ICE/IO/CMakeLists.txt b/ICE/IO/CMakeLists.txt index e59e2794..81eece53 100644 --- a/ICE/IO/CMakeLists.txt +++ b/ICE/IO/CMakeLists.txt @@ -10,7 +10,7 @@ target_sources(${PROJECT_NAME} PRIVATE src/Project.cpp src/MaterialExporter.cpp src/TextureLoader.cpp - src/MeshLoader.cpp + "src/ModelLoader.cpp" src/ShaderLoader.cpp src/MaterialLoader.cpp ) diff --git a/ICE/IO/include/MeshLoader.h b/ICE/IO/include/MeshLoader.h deleted file mode 100644 index 4983b1ac..00000000 --- a/ICE/IO/include/MeshLoader.h +++ /dev/null @@ -1,19 +0,0 @@ -// -// Created by Thomas Ibanez on 31.07.21. -// - -#pragma once - -#include - -#include "Asset.h" -#include "IAssetLoader.h" -#include "Mesh.h" - -namespace ICE { -class MeshLoader : public IAssetLoader { - public: - MeshLoader(const std::shared_ptr &factory) : IAssetLoader(factory) {} - std::shared_ptr load(const std::vector &file) override; -}; -} // namespace ICE diff --git a/ICE/IO/include/ModelLoader.h b/ICE/IO/include/ModelLoader.h new file mode 100644 index 00000000..dbeeb1bc --- /dev/null +++ b/ICE/IO/include/ModelLoader.h @@ -0,0 +1,19 @@ +// +// Created by Thomas Ibanez on 31.07.21. +// + +#pragma once + +#include + +#include "Asset.h" +#include "IAssetLoader.h" +#include "Model.h" + +namespace ICE { +class ModelLoader : public IAssetLoader { + public: + ModelLoader(const std::shared_ptr &factory) : IAssetLoader(factory) {} + std::shared_ptr load(const std::vector &file) override; +}; +} // namespace ICE diff --git a/ICE/IO/src/MeshLoader.cpp b/ICE/IO/src/MeshLoader.cpp deleted file mode 100644 index bbcb0cf5..00000000 --- a/ICE/IO/src/MeshLoader.cpp +++ /dev/null @@ -1,72 +0,0 @@ -// -// Created by Thomas Ibanez on 31.07.21. -// - -#include "MeshLoader.h" - -#include -#include -#include - -#include -#include - -namespace ICE { -std::shared_ptr MeshLoader::load(const std::vector &file) { - Assimp::Importer importer; - - const aiScene *scene = importer.ReadFile( - file[0].string(), - aiProcess_CalcTangentSpace | aiProcess_Triangulate | aiProcess_JoinIdenticalVertices); - - auto vertices = std::vector(); - auto normals = std::vector(); - auto uvs = std::vector(); - auto indices = std::vector(); - // Loop over faces(polygon) - auto mesh0 = scene->mMeshes[0]; - for (int i = 0; i < mesh0->mNumVertices; i++) { - auto v = mesh0->mVertices[i]; - auto n = mesh0->mNormals[i]; - Eigen::Vector2f uv(0, 0); - if (mesh0->mTextureCoords[0] != nullptr) { - auto uv_file = mesh0->mTextureCoords[0][i]; - uv.x() = uv_file.x; - uv.y() = uv_file.y; - } - vertices.emplace_back(v.x, v.y, v.z); - normals.emplace_back(n.x, n.y, n.z); - uvs.push_back(uv); - } - for (int i = 0; i < mesh0->mNumFaces; i++) { - auto f = mesh0->mFaces[i]; - assert(f.mNumIndices == 3); - indices.emplace_back(f.mIndices[0], f.mIndices[1], f.mIndices[2]); - } - - auto mesh = std::make_shared(vertices, normals, uvs, indices); - - mesh->setSources(file); - - auto vertexArray = graphics_factory->createVertexArray(); - - auto vertexBuffer = graphics_factory->createVertexBuffer(); - auto normalsBuffer = graphics_factory->createVertexBuffer(); - auto uvBuffer = graphics_factory->createVertexBuffer(); - auto indexBuffer = graphics_factory->createIndexBuffer(); - - vertexBuffer->putData(BufferUtils::CreateFloatBuffer(mesh->getVertices()), 3 * mesh->getVertices().size() * sizeof(float)); - normalsBuffer->putData(BufferUtils::CreateFloatBuffer(mesh->getNormals()), 3 * mesh->getNormals().size() * sizeof(float)); - uvBuffer->putData(BufferUtils::CreateFloatBuffer(mesh->getUVCoords()), 2 * mesh->getUVCoords().size() * sizeof(float)); - indexBuffer->putData(BufferUtils::CreateIntBuffer(mesh->getIndices()), 3 * mesh->getIndices().size() * sizeof(int)); - - vertexArray->pushVertexBuffer(vertexBuffer, 3); - vertexArray->pushVertexBuffer(normalsBuffer, 3); - vertexArray->pushVertexBuffer(uvBuffer, 2); - vertexArray->setIndexBuffer(indexBuffer); - - mesh->setVertexArray(vertexArray); - - return mesh; -} -} // namespace ICE diff --git a/ICE/IO/src/ModelLoader.cpp b/ICE/IO/src/ModelLoader.cpp new file mode 100644 index 00000000..00bbe610 --- /dev/null +++ b/ICE/IO/src/ModelLoader.cpp @@ -0,0 +1,74 @@ +// +// Created by Thomas Ibanez on 31.07.21. +// + +#include "ModelLoader.h" + +#include +#include +#include + +#include +#include + +namespace ICE { +std::shared_ptr ModelLoader::load(const std::vector &file) { + Assimp::Importer importer; + + const aiScene *scene = importer.ReadFile(file[0].string(), aiProcess_CalcTangentSpace | aiProcess_Triangulate | aiProcess_JoinIdenticalVertices); + + std::vector> meshes; + std::vector materials; + + for (int m = 0; m < scene->mNumMeshes; m++) { + auto vertices = std::vector(); + auto normals = std::vector(); + auto uvs = std::vector(); + auto indices = std::vector(); + // Loop over faces(polygon) + auto mesh0 = scene->mMeshes[m]; + for (int i = 0; i < mesh0->mNumVertices; i++) { + auto v = mesh0->mVertices[i]; + auto n = mesh0->mNormals[i]; + Eigen::Vector2f uv(0, 0); + if (mesh0->mTextureCoords[0] != nullptr) { + auto uv_file = mesh0->mTextureCoords[0][i]; + uv.x() = uv_file.x; + uv.y() = uv_file.y; + } + vertices.emplace_back(v.x, v.y, v.z); + normals.emplace_back(n.x, n.y, n.z); + uvs.push_back(uv); + } + for (int i = 0; i < mesh0->mNumFaces; i++) { + auto f = mesh0->mFaces[i]; + assert(f.mNumIndices == 3); + indices.emplace_back(f.mIndices[0], f.mIndices[1], f.mIndices[2]); + } + + auto mesh = std::make_shared(vertices, normals, uvs, indices); + + mesh->setSources(file); + + auto vertexArray = graphics_factory->createVertexArray(); + + auto vertexBuffer = graphics_factory->createVertexBuffer(); + auto normalsBuffer = graphics_factory->createVertexBuffer(); + auto uvBuffer = graphics_factory->createVertexBuffer(); + auto indexBuffer = graphics_factory->createIndexBuffer(); + + vertexBuffer->putData(BufferUtils::CreateFloatBuffer(mesh->getVertices()), 3 * mesh->getVertices().size() * sizeof(float)); + normalsBuffer->putData(BufferUtils::CreateFloatBuffer(mesh->getNormals()), 3 * mesh->getNormals().size() * sizeof(float)); + uvBuffer->putData(BufferUtils::CreateFloatBuffer(mesh->getUVCoords()), 2 * mesh->getUVCoords().size() * sizeof(float)); + indexBuffer->putData(BufferUtils::CreateIntBuffer(mesh->getIndices()), 3 * mesh->getIndices().size() * sizeof(int)); + + vertexArray->pushVertexBuffer(vertexBuffer, 3); + vertexArray->pushVertexBuffer(normalsBuffer, 3); + vertexArray->pushVertexBuffer(uvBuffer, 2); + vertexArray->setIndexBuffer(indexBuffer); + + mesh->setVertexArray(vertexArray); + } + return mesh; +} +} // namespace ICE From 61133414b41f9565cd3aaf1758e64374eb2a7621 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 22 Dec 2024 17:31:50 +0100 Subject: [PATCH 02/22] wip on multi-meshes --- ICE/Assets/include/Asset.h | 4 +- ICE/Assets/include/AssetBank.h | 2 +- ICE/Assets/include/AssetLoader.h | 8 ++-- ICE/Assets/src/AssetBank.cpp | 6 +-- ICE/Graphics/include/Material.h | 2 - ICE/Graphics/include/Mesh.h | 2 - ICE/Graphics/include/Model.h | 3 ++ ICE/Graphics/include/Texture.h | 4 -- ICE/Graphics/src/GeometryPass.cpp | 1 + ICE/Graphics/src/Material.cpp | 4 -- ICE/Graphics/src/Mesh.cpp | 4 -- ICE/GraphicsAPI/OpenGL/include/OpenGLShader.h | 4 -- .../OpenGL/include/OpenGLTexture.h | 5 +-- ICE/IO/include/ModelLoader.h | 4 +- ICE/IO/src/ModelLoader.cpp | 42 ++++++++++++------- ICE/IO/src/Project.cpp | 8 ++-- ICEBERG/src/Editor.cpp | 2 +- 17 files changed, 49 insertions(+), 56 deletions(-) diff --git a/ICE/Assets/include/Asset.h b/ICE/Assets/include/Asset.h index b47de8d8..1dae6038 100644 --- a/ICE/Assets/include/Asset.h +++ b/ICE/Assets/include/Asset.h @@ -14,14 +14,12 @@ namespace ICE { typedef unsigned long long AssetUID; -enum class AssetType { ETex2D, ETexCube, EShader, EMesh, EMaterial }; +enum class AssetType { ETex2D, ETexCube, EShader, EMesh, EModel, EMaterial }; class Asset : public Resource { public: Asset() : Resource({}) {} virtual AssetType getType() const = 0; virtual std::string getTypeName() const = 0; - virtual void load() = 0; - virtual void unload() = 0; }; } // namespace ICE diff --git a/ICE/Assets/include/AssetBank.h b/ICE/Assets/include/AssetBank.h index 5ad30f0f..795e3d4e 100644 --- a/ICE/Assets/include/AssetBank.h +++ b/ICE/Assets/include/AssetBank.h @@ -108,7 +108,7 @@ class AssetBank { if (nameMapping.find(name) != nameMapping.end()) { AssetUID id = getUID(name); nameMapping.erase(name); - resources[id].asset->unload(); + //TODO: Check resources[id].asset->unload(); resources.erase(id); return true; } diff --git a/ICE/Assets/include/AssetLoader.h b/ICE/Assets/include/AssetLoader.h index 4fafc6f6..75d25cc2 100644 --- a/ICE/Assets/include/AssetLoader.h +++ b/ICE/Assets/include/AssetLoader.h @@ -12,6 +12,7 @@ #include "Asset.h" #include "IAssetLoader.h" +#include "Model.h" namespace ICE { class AssetLoader { @@ -32,9 +33,10 @@ class AssetLoader { } private: - std::unordered_map>, std::shared_ptr>, std::shared_ptr>, - std::shared_ptr>, std::shared_ptr>>> + std::unordered_map< + std::type_index, + std::variant>, std::shared_ptr>, std::shared_ptr>, + std::shared_ptr>, std::shared_ptr>>> loaders; }; } // namespace ICE diff --git a/ICE/Assets/src/AssetBank.cpp b/ICE/Assets/src/AssetBank.cpp index 669e54b1..95f3635b 100644 --- a/ICE/Assets/src/AssetBank.cpp +++ b/ICE/Assets/src/AssetBank.cpp @@ -5,10 +5,10 @@ #include "AssetBank.h" #include -#include +#include #include "MaterialLoader.h" -#include "MeshLoader.h" +#include "ModelLoader.h" #include "ShaderLoader.h" #include "TextureLoader.h" @@ -17,7 +17,7 @@ namespace ICE { AssetBank::AssetBank(const std::shared_ptr &factory) : graphics_factory(factory) { loader.AddLoader(std::make_shared(factory)); loader.AddLoader(std::make_shared(factory)); - loader.AddLoader(std::make_shared(factory)); + loader.AddLoader(std::make_shared(factory)); loader.AddLoader(std::make_shared(factory)); loader.AddLoader(std::make_shared()); } diff --git a/ICE/Graphics/include/Material.h b/ICE/Graphics/include/Material.h index e3675501..e4ade123 100644 --- a/ICE/Graphics/include/Material.h +++ b/ICE/Graphics/include/Material.h @@ -44,8 +44,6 @@ class Material : public Asset { //Asset interface std::string getTypeName() const override; AssetType getType() const override; - void load() override; - void unload() override; private: AssetUID m_shader = NO_ASSET_ID; diff --git a/ICE/Graphics/include/Mesh.h b/ICE/Graphics/include/Mesh.h index 68fbea86..b1881ca3 100644 --- a/ICE/Graphics/include/Mesh.h +++ b/ICE/Graphics/include/Mesh.h @@ -35,8 +35,6 @@ class Mesh : public Asset { std::string getTypeName() const override; AssetType getType() const override; - void load() override; - void unload() override; private: std::vector vertices, normals; diff --git a/ICE/Graphics/include/Model.h b/ICE/Graphics/include/Model.h index abd6148a..3973e8ac 100644 --- a/ICE/Graphics/include/Model.h +++ b/ICE/Graphics/include/Model.h @@ -11,6 +11,9 @@ class Model : public Asset { std::vector> getMeshes() const { return m_meshes; } std::vector getMaterialsIDs() const { return m_materials; } + AssetType getType() const override { return AssetType::EModel; } + std::string getTypeName() const override { return "Model"; } + private: std::vector> m_meshes; std::vector m_materials; diff --git a/ICE/Graphics/include/Texture.h b/ICE/Graphics/include/Texture.h index 682ba6df..2950cf1c 100644 --- a/ICE/Graphics/include/Texture.h +++ b/ICE/Graphics/include/Texture.h @@ -50,8 +50,6 @@ class Texture2D : public Texture { virtual AssetType getType() const override { return AssetType::ETex2D; } virtual std::string getTypeName() const override { return "Texture2D"; } - virtual void load() override = 0; - virtual void unload() override = 0; }; class TextureCube : public Texture { @@ -61,8 +59,6 @@ class TextureCube : public Texture { virtual AssetType getType() const override { return AssetType::ETexCube; } virtual std::string getTypeName() const override { return "TextureCube"; } - virtual void load() override = 0; - virtual void unload() override = 0; }; } // namespace ICE diff --git a/ICE/Graphics/src/GeometryPass.cpp b/ICE/Graphics/src/GeometryPass.cpp index 2bb8b771..60a1ccf4 100644 --- a/ICE/Graphics/src/GeometryPass.cpp +++ b/ICE/Graphics/src/GeometryPass.cpp @@ -13,6 +13,7 @@ void GeometryPass::execute() { std::shared_ptr current_shader; std::shared_ptr current_material; std::shared_ptr current_mesh; + return; //TODO for (const auto& command : *m_render_queue) { auto& shader = command.shader; auto& material = command.material; diff --git a/ICE/Graphics/src/Material.cpp b/ICE/Graphics/src/Material.cpp index 8415e16d..19a5cc84 100644 --- a/ICE/Graphics/src/Material.cpp +++ b/ICE/Graphics/src/Material.cpp @@ -32,8 +32,4 @@ std::string Material::getTypeName() const { AssetType Material::getType() const { return AssetType::EMaterial; } -void Material::load() { -} -void Material::unload() { -} } // namespace ICE \ No newline at end of file diff --git a/ICE/Graphics/src/Mesh.cpp b/ICE/Graphics/src/Mesh.cpp index 921a4c79..ae1b2265 100644 --- a/ICE/Graphics/src/Mesh.cpp +++ b/ICE/Graphics/src/Mesh.cpp @@ -60,8 +60,4 @@ std::string Mesh::getTypeName() const { AssetType Mesh::getType() const { return AssetType::EMesh; } -void Mesh::load() { -} -void Mesh::unload() { -} } // namespace ICE \ No newline at end of file diff --git a/ICE/GraphicsAPI/OpenGL/include/OpenGLShader.h b/ICE/GraphicsAPI/OpenGL/include/OpenGLShader.h index dbde0dd2..476605d0 100644 --- a/ICE/GraphicsAPI/OpenGL/include/OpenGLShader.h +++ b/ICE/GraphicsAPI/OpenGL/include/OpenGLShader.h @@ -36,10 +36,6 @@ namespace ICE { OpenGLShader(const std::string &vertexFile, const std::string &fragmentFile); - void load() override {} - - void unload() override {} - private: GLint getLocation(const std::string &name); diff --git a/ICE/GraphicsAPI/OpenGL/include/OpenGLTexture.h b/ICE/GraphicsAPI/OpenGL/include/OpenGLTexture.h index 64142d3c..945148d1 100644 --- a/ICE/GraphicsAPI/OpenGL/include/OpenGLTexture.h +++ b/ICE/GraphicsAPI/OpenGL/include/OpenGLTexture.h @@ -29,8 +29,6 @@ namespace ICE { void *getTexture() const override; TextureType getTextureType() const override; - void load() override {} - void unload() override {} private: std::string file; @@ -60,8 +58,7 @@ namespace ICE { void *getTexture() const override; TextureType getTextureType() const override; - void load() override {} - void unload() override {} + private: std::string file; uint32_t id; diff --git a/ICE/IO/include/ModelLoader.h b/ICE/IO/include/ModelLoader.h index dbeeb1bc..470e96ef 100644 --- a/ICE/IO/include/ModelLoader.h +++ b/ICE/IO/include/ModelLoader.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include "Asset.h" @@ -13,7 +14,8 @@ namespace ICE { class ModelLoader : public IAssetLoader { public: - ModelLoader(const std::shared_ptr &factory) : IAssetLoader(factory) {} + ModelLoader(const std::shared_ptr &factory) : IAssetLoader(factory) {} std::shared_ptr load(const std::vector &file) override; + AssetUID extractMaterial(const aiMaterial *material); }; } // namespace ICE diff --git a/ICE/IO/src/ModelLoader.cpp b/ICE/IO/src/ModelLoader.cpp index 00bbe610..1a5f17dd 100644 --- a/ICE/IO/src/ModelLoader.cpp +++ b/ICE/IO/src/ModelLoader.cpp @@ -5,6 +5,7 @@ #include "ModelLoader.h" #include +#include #include #include @@ -26,13 +27,17 @@ std::shared_ptr ModelLoader::load(const std::vector(); auto indices = std::vector(); // Loop over faces(polygon) - auto mesh0 = scene->mMeshes[m]; - for (int i = 0; i < mesh0->mNumVertices; i++) { - auto v = mesh0->mVertices[i]; - auto n = mesh0->mNormals[i]; + auto mesh = scene->mMeshes[m]; + auto material = scene->mMaterials[mesh->mMaterialIndex]; + + materials.push_back(extractMaterial(material)); + + for (int i = 0; i < mesh->mNumVertices; i++) { + auto v = mesh->mVertices[i]; + auto n = mesh->mNormals[i]; Eigen::Vector2f uv(0, 0); - if (mesh0->mTextureCoords[0] != nullptr) { - auto uv_file = mesh0->mTextureCoords[0][i]; + if (mesh->mTextureCoords[0] != nullptr) { + auto uv_file = mesh->mTextureCoords[0][i]; uv.x() = uv_file.x; uv.y() = uv_file.y; } @@ -40,15 +45,15 @@ std::shared_ptr ModelLoader::load(const std::vectormNumFaces; i++) { - auto f = mesh0->mFaces[i]; + for (int i = 0; i < mesh->mNumFaces; i++) { + auto f = mesh->mFaces[i]; assert(f.mNumIndices == 3); indices.emplace_back(f.mIndices[0], f.mIndices[1], f.mIndices[2]); } - auto mesh = std::make_shared(vertices, normals, uvs, indices); + meshes.push_back(std::make_shared(vertices, normals, uvs, indices)); - mesh->setSources(file); + meshes.back()->setSources(file); auto vertexArray = graphics_factory->createVertexArray(); @@ -57,18 +62,23 @@ std::shared_ptr ModelLoader::load(const std::vectorcreateVertexBuffer(); auto indexBuffer = graphics_factory->createIndexBuffer(); - vertexBuffer->putData(BufferUtils::CreateFloatBuffer(mesh->getVertices()), 3 * mesh->getVertices().size() * sizeof(float)); - normalsBuffer->putData(BufferUtils::CreateFloatBuffer(mesh->getNormals()), 3 * mesh->getNormals().size() * sizeof(float)); - uvBuffer->putData(BufferUtils::CreateFloatBuffer(mesh->getUVCoords()), 2 * mesh->getUVCoords().size() * sizeof(float)); - indexBuffer->putData(BufferUtils::CreateIntBuffer(mesh->getIndices()), 3 * mesh->getIndices().size() * sizeof(int)); + vertexBuffer->putData(BufferUtils::CreateFloatBuffer(meshes.back()->getVertices()), 3 * meshes.back()->getVertices().size() * sizeof(float)); + normalsBuffer->putData(BufferUtils::CreateFloatBuffer(meshes.back()->getNormals()), 3 * meshes.back()->getNormals().size() * sizeof(float)); + uvBuffer->putData(BufferUtils::CreateFloatBuffer(meshes.back()->getUVCoords()), 2 * meshes.back()->getUVCoords().size() * sizeof(float)); + indexBuffer->putData(BufferUtils::CreateIntBuffer(meshes.back()->getIndices()), 3 * meshes.back()->getIndices().size() * sizeof(int)); vertexArray->pushVertexBuffer(vertexBuffer, 3); vertexArray->pushVertexBuffer(normalsBuffer, 3); vertexArray->pushVertexBuffer(uvBuffer, 2); vertexArray->setIndexBuffer(indexBuffer); - mesh->setVertexArray(vertexArray); + meshes.back()->setVertexArray(vertexArray); } - return mesh; + return std::make_shared(meshes, materials); +} + +AssetUID extractMaterial(const aiMaterial *material) { + auto mtl = std::make_shared(); + mtl->setShader() } } // namespace ICE diff --git a/ICE/IO/src/Project.cpp b/ICE/IO/src/Project.cpp index 71c9c2a3..b641d386 100644 --- a/ICE/IO/src/Project.cpp +++ b/ICE/IO/src/Project.cpp @@ -56,8 +56,8 @@ bool Project::CreateDirectories() { copyAssetFile("Cubemaps", "skybox", "Assets/Textures/skybox.png"); copyAssetFile("Materials", "base_mat", "Assets/Materials/base_mat.icm"); - assetBank->addAsset("cube", {m_meshes_directory / "cube.obj"}); - assetBank->addAsset("sphere", {m_meshes_directory / "sphere.obj"}); + assetBank->addAsset("cube", {m_meshes_directory / "cube.obj"}); + assetBank->addAsset("sphere", {m_meshes_directory / "sphere.obj"}); assetBank->addAsset("solid", {m_shaders_directory / "solid.vs", m_shaders_directory / "solid.fs"}); assetBank->addAsset("phong", {m_shaders_directory / "phong.vs", m_shaders_directory / "phong.fs"}); assetBank->addAsset("normal", {m_shaders_directory / "normal.vs", m_shaders_directory / "normal.fs"}); @@ -94,7 +94,7 @@ void Project::writeToFile(const std::shared_ptr &editorCamera) { j["scenes"] = vec; vec.clear(); - for (const auto &[asset_id, mesh] : assetBank->getAll()) { + for (const auto &[asset_id, mesh] : assetBank->getAll()) { vec.push_back(dumpAsset(asset_id, mesh)); } j["meshes"] = vec; @@ -207,7 +207,7 @@ void Project::loadFromFile() { loadAssetsOfType(texture); loadAssetsOfType(cubeMap); - loadAssetsOfType(meshes); + loadAssetsOfType(meshes); loadAssetsOfType(material); loadAssetsOfType(shader); diff --git a/ICEBERG/src/Editor.cpp b/ICEBERG/src/Editor.cpp index 5564b082..4b6d3d69 100644 --- a/ICEBERG/src/Editor.cpp +++ b/ICEBERG/src/Editor.cpp @@ -41,7 +41,7 @@ Editor::Editor(const std::shared_ptr& engine, const std::shared_ } while (m_engine->getAssetBank()->nameInUse(ICE::AssetPath::WithTypePrefix(import_name + std::to_string(i)))); import_name = import_name + std::to_string(i); m_engine->getProject()->copyAssetFile("Meshes", import_name, file); - m_engine->getAssetBank()->addAsset( + m_engine->getAssetBank()->addAsset( import_name, {m_engine->getProject()->getBaseDirectory() / "Assets" / "Meshes" / (import_name + file.extension().string())}); m_assets->rebuildViewer(); } From b6f81be46dbd0204c3b1966a3d9ad85166e5f7b1 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 22 Dec 2024 19:16:17 +0100 Subject: [PATCH 03/22] extract material test --- Assets/Shaders/phong.fs | 6 +++--- ICE/Assets/src/AssetBank.cpp | 2 +- ICE/IO/include/ModelLoader.h | 12 +++++++++-- ICE/IO/src/ModelLoader.cpp | 39 +++++++++++++++++++++++++++++++++--- 4 files changed, 50 insertions(+), 9 deletions(-) diff --git a/Assets/Shaders/phong.fs b/Assets/Shaders/phong.fs index 6468e838..e457ea32 100644 --- a/Assets/Shaders/phong.fs +++ b/Assets/Shaders/phong.fs @@ -10,9 +10,9 @@ struct Light { }; struct Material { - vec3 albedo; - vec3 specular; - vec3 ambient; + vec4 albedo; + vec4 specular; + vec4 ambient; float alpha; bool use_diffuse_map; sampler2D diffuse_map; diff --git a/ICE/Assets/src/AssetBank.cpp b/ICE/Assets/src/AssetBank.cpp index 95f3635b..3ac81f18 100644 --- a/ICE/Assets/src/AssetBank.cpp +++ b/ICE/Assets/src/AssetBank.cpp @@ -17,7 +17,7 @@ namespace ICE { AssetBank::AssetBank(const std::shared_ptr &factory) : graphics_factory(factory) { loader.AddLoader(std::make_shared(factory)); loader.AddLoader(std::make_shared(factory)); - loader.AddLoader(std::make_shared(factory)); + loader.AddLoader(std::make_shared(factory, *this)); loader.AddLoader(std::make_shared(factory)); loader.AddLoader(std::make_shared()); } diff --git a/ICE/IO/include/ModelLoader.h b/ICE/IO/include/ModelLoader.h index 470e96ef..5789b2f4 100644 --- a/ICE/IO/include/ModelLoader.h +++ b/ICE/IO/include/ModelLoader.h @@ -5,6 +5,7 @@ #pragma once #include + #include #include "Asset.h" @@ -12,10 +13,17 @@ #include "Model.h" namespace ICE { +class AssetBank; + class ModelLoader : public IAssetLoader { public: - ModelLoader(const std::shared_ptr &factory) : IAssetLoader(factory) {} + ModelLoader(const std::shared_ptr &factory, AssetBank &bank) : ref_bank(bank), IAssetLoader(factory) {} std::shared_ptr load(const std::vector &file) override; - AssetUID extractMaterial(const aiMaterial *material); + AssetUID extractMaterial(const aiMaterial *material, const std::string &model_name); + + private: + Eigen::Vector4f colorToVec(aiColor4D *color); + + AssetBank &ref_bank; }; } // namespace ICE diff --git a/ICE/IO/src/ModelLoader.cpp b/ICE/IO/src/ModelLoader.cpp index 1a5f17dd..1f8e6bc8 100644 --- a/ICE/IO/src/ModelLoader.cpp +++ b/ICE/IO/src/ModelLoader.cpp @@ -4,6 +4,7 @@ #include "ModelLoader.h" +#include #include #include #include @@ -30,7 +31,7 @@ std::shared_ptr ModelLoader::load(const std::vectormMeshes[m]; auto material = scene->mMaterials[mesh->mMaterialIndex]; - materials.push_back(extractMaterial(material)); + materials.push_back(extractMaterial(material, file[0].filename().stem().string())); for (int i = 0; i < mesh->mNumVertices; i++) { auto v = mesh->mVertices[i]; @@ -77,8 +78,40 @@ std::shared_ptr ModelLoader::load(const std::vector(meshes, materials); } -AssetUID extractMaterial(const aiMaterial *material) { +AssetUID ModelLoader::extractMaterial(const aiMaterial *material, const std::string &model_name) { auto mtl = std::make_shared(); - mtl->setShader() + mtl->setShader(ref_bank.getUID(AssetPath::WithTypePrefix("phong"))); + + aiColor4D diffuse; + aiColor4D specular; + aiColor4D ambient; + ai_real alpha = 1.0; + + if (aiGetMaterialColor(material, AI_MATKEY_COLOR_DIFFUSE, &diffuse) == aiReturn_SUCCESS) + mtl->setUniform("material.albedo", colorToVec(&diffuse)); + if (aiGetMaterialColor(material, AI_MATKEY_COLOR_SPECULAR, &specular) == aiReturn_SUCCESS) + mtl->setUniform("material.specular", colorToVec(&specular)); + if (aiGetMaterialColor(material, AI_MATKEY_COLOR_AMBIENT, &ambient) == aiReturn_SUCCESS) + mtl->setUniform("material.ambient", colorToVec(&ambient)); + if (aiGetMaterialFloat(material, AI_MATKEY_SHININESS, &alpha) == aiReturn_SUCCESS) + mtl->setUniform("material.alpha", alpha); + mtl->setUniform("material.use_diffuse_map", false); + mtl->setUniform("material.use_ambient_map", false); + mtl->setUniform("material.use_specular_map", false); + mtl->setUniform("material.use_normal_map", false); + + auto bank_name = model_name + "-" + material->GetName().C_Str(); + ref_bank.addAsset(bank_name, mtl); + return ref_bank.getUID(AssetPath::WithTypePrefix(bank_name)); } + +Eigen::Vector4f ModelLoader::colorToVec(aiColor4D *color) { + Eigen::Vector4f v; + v.x() = color->r; + v.y() = color->g; + v.z() = color->b; + v.w() = color->a; + return v; +} + } // namespace ICE From 6b8afcffcaf822e34a6f4cc3028945a7ffcba97e Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 24 Dec 2024 01:11:09 +0100 Subject: [PATCH 04/22] iterate through model for rendering --- Assets/Shaders/phong.fs | 16 ++-- ICE/Components/include/RenderComponent.h | 5 +- ICE/Graphics/src/ForwardRenderer.cpp | 104 ++++++++++++----------- ICE/Graphics/src/GeometryPass.cpp | 2 +- ICEBERG/UI/AddComponentPopup.h | 2 +- 5 files changed, 66 insertions(+), 63 deletions(-) diff --git a/Assets/Shaders/phong.fs b/Assets/Shaders/phong.fs index e457ea32..0b9f2d01 100644 --- a/Assets/Shaders/phong.fs +++ b/Assets/Shaders/phong.fs @@ -37,28 +37,28 @@ in vec2 ftex_coords; vec3 normal; vec3 pointLight(Light light) { - vec3 rcolor = vec3(0.0); + vec4 rcolor = vec3(0.0); //diffuse vec3 n = normalize(normal); vec3 light_direction = normalize(light.position - fposition); float diff = max(dot(n, light_direction), 0.0); - vec3 diffuse_color = material.albedo; + vec4 diffuse_color = material.albedo; if(material.use_diffuse_map) { - diffuse_color *= texture(material.diffuse_map, ftex_coords).xyz; + diffuse_color *= texture(material.diffuse_map, ftex_coords); } - rcolor += light.color * (diff * diffuse_color); + rcolor += vec4(light.color, 1.0) * (diff * diffuse_color); if(diff > 0) { //specular vec3 view_direction = normalize(fview - fposition); vec3 reflection_direction = reflect(-light_direction, n); float spec = pow(max(dot(view_direction, reflection_direction), 0.0), material.alpha); - vec3 specular_color = material.specular; + vec4 specular_color = material.specular; if(material.use_specular_map) { - specular_color *= texture(material.specular_map, ftex_coords).xyz; + specular_color *= texture(material.specular_map, ftex_coords); } - rcolor += light.color * (spec * specular_color); + rcolor += vec4(light.color, 1.0) * (spec * specular_color); } return rcolor; } @@ -76,7 +76,7 @@ void main() { normal = fnormal; } //ambient - vec3 color_accumulator = vec3(material.ambient*ambient_light); + vec4 color_accumulator = material.ambient * vec4(ambient_light, 1.0); if(material.use_ambient_map) { color_accumulator *= texture(material.ambient_map, ftex_coords).xyz; } diff --git a/ICE/Components/include/RenderComponent.h b/ICE/Components/include/RenderComponent.h index e5e44d36..f44bb4b9 100644 --- a/ICE/Components/include/RenderComponent.h +++ b/ICE/Components/include/RenderComponent.h @@ -11,9 +11,8 @@ namespace ICE { struct RenderComponent : public Component { - RenderComponent(AssetUID mesh_id, AssetUID material_id) : mesh(mesh_id), material(material_id) {} - AssetUID mesh; - AssetUID material; + RenderComponent(AssetUID model_id) : model(model_id) {} + AssetUID model; }; } // namespace ICE diff --git a/ICE/Graphics/src/ForwardRenderer.cpp b/ICE/Graphics/src/ForwardRenderer.cpp index 11746728..d487cf12 100644 --- a/ICE/Graphics/src/ForwardRenderer.cpp +++ b/ICE/Graphics/src/ForwardRenderer.cpp @@ -67,10 +67,10 @@ void ForwardRenderer::remove(Entity e) { void ForwardRenderer::prepareFrame(Camera& camera) { //TODO: Sort entities, make shader list, batch, make instances, set uniforms, etc.. std::sort(m_render_queue.begin(), m_render_queue.end(), [this](Entity a, Entity b) { - auto rc_a = m_registry->getComponent(a); - auto material_a = m_asset_bank->getAsset(rc_a->material); - auto rc_b = m_registry->getComponent(b); - auto material_b = m_asset_bank->getAsset(rc_b->material); + auto model_a = m_asset_bank->getAsset(m_registry->getComponent(a)->model); + auto material_a = m_asset_bank->getAsset(model_a->getMaterialsIDs().front()); + auto model_b = m_asset_bank->getAsset(m_registry->getComponent(b)->model); + auto material_b = m_asset_bank->getAsset(model_b->getMaterialsIDs().front()); bool a_transparent = material_a ? material_a->isTransparent() : false; bool b_transparent = material_b ? material_b->isTransparent() : false; @@ -109,59 +109,63 @@ void ForwardRenderer::prepareFrame(Camera& camera) { for (const auto& e : m_render_queue) { auto rc = m_registry->getComponent(e); auto tc = m_registry->getComponent(e); - auto material = m_asset_bank->getAsset(rc->material); - if (!material) { - continue; - } - auto shader = m_asset_bank->getAsset(material->getShader()); - if (!shader) { - continue; - } + auto model = m_asset_bank->getAsset(m_registry->getComponent(e)->model); + for (int i = 0; i < model->getMeshes().size(); i++) { + auto mtl_id = model->getMaterialsIDs().at(i); + auto& mesh = model->getMeshes().at(i); + auto material = m_asset_bank->getAsset(mtl_id); + if (!material) { + continue; + } + auto shader = m_asset_bank->getAsset(material->getShader()); + if (!shader) { + continue; + } - if (!prepared_shaders.contains(material->getShader())) { - shader->bind(); - - shader->loadMat4("projection", camera.getProjection()); - shader->loadMat4("view", view_mat); - - shader->loadFloat3("ambient_light", Eigen::Vector3f(0.1f, 0.1f, 0.1f)); - int i = 0; - for (const auto& e : m_lights) { - auto light = m_registry->getComponent(e); - auto transform = m_registry->getComponent(e); - std::string light_name = (std::string("lights[") + std::to_string(i) + std::string("].")); - shader->loadFloat3((light_name + std::string("position")).c_str(), transform->getPosition()); - shader->loadFloat3((light_name + std::string("rotation")).c_str(), transform->getRotation()); - shader->loadFloat3((light_name + std::string("color")).c_str(), light->color); - shader->loadInt((light_name + std::string("type")).c_str(), static_cast(light->type)); - i++; + if (!prepared_shaders.contains(material->getShader())) { + shader->bind(); + + shader->loadMat4("projection", camera.getProjection()); + shader->loadMat4("view", view_mat); + + shader->loadFloat3("ambient_light", Eigen::Vector3f(0.1f, 0.1f, 0.1f)); + int i = 0; + for (const auto& e : m_lights) { + auto light = m_registry->getComponent(e); + auto transform = m_registry->getComponent(e); + std::string light_name = (std::string("lights[") + std::to_string(i) + std::string("].")); + shader->loadFloat3((light_name + std::string("position")).c_str(), transform->getPosition()); + shader->loadFloat3((light_name + std::string("rotation")).c_str(), transform->getRotation()); + shader->loadFloat3((light_name + std::string("color")).c_str(), light->color); + shader->loadInt((light_name + std::string("type")).c_str(), static_cast(light->type)); + i++; + } + shader->loadInt("light_count", i); + prepared_shaders.emplace(material->getShader()); } - shader->loadInt("light_count", i); - prepared_shaders.emplace(material->getShader()); - } - auto mesh = m_asset_bank->getAsset(rc->mesh); - if (!mesh) { - return; - } + if (!mesh) { + return; + } - std::unordered_map> texs; - for (const auto& [name, value] : material->getAllUniforms()) { - if (std::holds_alternative(value)) { - auto v = std::get(value); - if (auto tex = m_asset_bank->getAsset(v); tex) { - texs.try_emplace(v, tex); + std::unordered_map> texs; + for (const auto& [name, value] : material->getAllUniforms()) { + if (std::holds_alternative(value)) { + auto v = std::get(value); + if (auto tex = m_asset_bank->getAsset(v); tex) { + texs.try_emplace(v, tex); + } } } - } - m_render_commands.push_back(RenderCommand{.mesh = mesh, - .material = material, - .shader = shader, - .textures = texs, - .model_matrix = tc->getModelMatrix(), - .faceCulling = true, - .depthTest = true}); + m_render_commands.push_back(RenderCommand{.mesh = mesh, + .material = material, + .shader = shader, + .textures = texs, + .model_matrix = tc->getModelMatrix(), + .faceCulling = true, + .depthTest = true}); + } } m_geometry_pass.submit(&m_render_commands); diff --git a/ICE/Graphics/src/GeometryPass.cpp b/ICE/Graphics/src/GeometryPass.cpp index 60a1ccf4..36ad1f6e 100644 --- a/ICE/Graphics/src/GeometryPass.cpp +++ b/ICE/Graphics/src/GeometryPass.cpp @@ -13,7 +13,7 @@ void GeometryPass::execute() { std::shared_ptr current_shader; std::shared_ptr current_material; std::shared_ptr current_mesh; - return; //TODO + for (const auto& command : *m_render_queue) { auto& shader = command.shader; auto& material = command.material; diff --git a/ICEBERG/UI/AddComponentPopup.h b/ICEBERG/UI/AddComponentPopup.h index 48e55742..92b85627 100644 --- a/ICEBERG/UI/AddComponentPopup.h +++ b/ICEBERG/UI/AddComponentPopup.h @@ -35,7 +35,7 @@ class AddComponentPopup { m_components_combo.render(); if (ImGui::Button("Add")) { if(m_components_combo.getSelectedItem() == "Render Component") - m_registry->addComponent(m_entity, ICE::RenderComponent(0, 0)); + m_registry->addComponent(m_entity, ICE::RenderComponent(0)); if(m_components_combo.getSelectedItem() == "Light Component") m_registry->addComponent(m_entity, ICE::LightComponent(ICE::PointLight, Eigen::Vector3f(1, 1, 1))); From d9e6f1506f8073fd84d877677658528f1ccc4f8d Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 24 Dec 2024 16:16:24 +0100 Subject: [PATCH 05/22] First working test for multi-mesh models --- Assets/{Meshes => Models}/cube.obj | 0 Assets/{Meshes => Models}/sphere.obj | 0 Assets/Shaders/phong.fs | 10 ++-- ICE/Assets/src/AssetPath.cpp | 78 ++++++++++++++-------------- ICE/Graphics/src/ForwardRenderer.cpp | 6 ++- ICE/GraphicsAPI/None/NoneGraphics.h | 8 --- ICE/IO/src/Project.cpp | 19 +++---- ICE/IO/test/MeshLoaderTest.cpp | 12 +++-- ICEBERG/UI/InspectorWidget.h | 12 ++--- ICEBERG/UI/NewMaterialWidget.h | 4 +- ICEBERG/src/Assets.cpp | 8 +-- ICEBERG/src/Hierarchy.cpp | 5 +- ICEBERG/src/Inspector.cpp | 16 ++---- ICEBERG/src/Viewport.cpp | 10 ++-- ICEFIELD/icefield.cpp | 6 +-- 15 files changed, 90 insertions(+), 104 deletions(-) rename Assets/{Meshes => Models}/cube.obj (100%) rename Assets/{Meshes => Models}/sphere.obj (100%) diff --git a/Assets/Meshes/cube.obj b/Assets/Models/cube.obj similarity index 100% rename from Assets/Meshes/cube.obj rename to Assets/Models/cube.obj diff --git a/Assets/Meshes/sphere.obj b/Assets/Models/sphere.obj similarity index 100% rename from Assets/Meshes/sphere.obj rename to Assets/Models/sphere.obj diff --git a/Assets/Shaders/phong.fs b/Assets/Shaders/phong.fs index 0b9f2d01..8d651cbc 100644 --- a/Assets/Shaders/phong.fs +++ b/Assets/Shaders/phong.fs @@ -36,8 +36,8 @@ in vec2 ftex_coords; vec3 normal; -vec3 pointLight(Light light) { - vec4 rcolor = vec3(0.0); +vec4 pointLight(Light light) { + vec4 rcolor = vec4(0.0); //diffuse vec3 n = normalize(normal); @@ -78,17 +78,17 @@ void main() { //ambient vec4 color_accumulator = material.ambient * vec4(ambient_light, 1.0); if(material.use_ambient_map) { - color_accumulator *= texture(material.ambient_map, ftex_coords).xyz; + color_accumulator *= texture(material.ambient_map, ftex_coords); } for(int i = 0; i < light_count; i++) { color_accumulator += pointLight(lights[i]); } if(light_count == 0) { - frag_color = vec4(material.albedo, 1.0); + frag_color = material.albedo; if(material.use_diffuse_map) { frag_color *= texture(material.diffuse_map, ftex_coords); } } else { - frag_color = vec4(color_accumulator, 1.0); + frag_color = color_accumulator; } } \ No newline at end of file diff --git a/ICE/Assets/src/AssetPath.cpp b/ICE/Assets/src/AssetPath.cpp index 070d4952..2460372e 100644 --- a/ICE/Assets/src/AssetPath.cpp +++ b/ICE/Assets/src/AssetPath.cpp @@ -2,56 +2,56 @@ // Created by Thomas Ibanez on 03.08.21. // -#include -#include +#include "AssetPath.h" + #include +#include #include -#include "AssetPath.h" +#include namespace ICE { - std::unordered_map AssetPath::typenames = { - {typeid(Texture2D), "Textures"}, - {typeid(TextureCube), "CubeMaps"}, - {typeid(Mesh), "Meshes"}, - {typeid(Material), "Materials"}, - {typeid(Shader), "Shaders"} - }; - - AssetPath::AssetPath(std::string path) { - size_t last = 0; - for(size_t i = 0; i < path.length(); i++) { - if(path[i] == ASSET_PATH_SEPARATOR) { - this->path.push_back(path.substr(last, i-last)); - last = i + 1; - } +std::unordered_map AssetPath::typenames = {{typeid(Texture2D), "Textures"}, + {typeid(TextureCube), "CubeMaps"}, + {typeid(Model), "Models"}, + {typeid(Material), "Materials"}, + {typeid(Shader), "Shaders"}}; + +AssetPath::AssetPath(std::string path) { + size_t last = 0; + for (size_t i = 0; i < path.length(); i++) { + if (path[i] == ASSET_PATH_SEPARATOR) { + this->path.push_back(path.substr(last, i - last)); + last = i + 1; } - name = path.substr(last, path.length()-last); } + name = path.substr(last, path.length() - last); +} - std::string AssetPath::toString() const { - return (prefix() + name); - } +std::string AssetPath::toString() const { + return (prefix() + name); +} - std::vector AssetPath::getPath() const { - return path; - } +std::vector AssetPath::getPath() const { + return path; +} - std::string AssetPath::getName() const { - return name; - } +std::string AssetPath::getName() const { + return name; +} - void AssetPath::setName(const std::string &name) { - AssetPath::name = name; - } +void AssetPath::setName(const std::string &name) { + AssetPath::name = name; +} - AssetPath::AssetPath(const AssetPath& cpy) : AssetPath(cpy.toString()) {} +AssetPath::AssetPath(const AssetPath &cpy) : AssetPath(cpy.toString()) { +} - std::string AssetPath::prefix() const { - auto str = std::string(); - for(auto &p : path) { - str += p+ASSET_PATH_SEPARATOR; - } - return str; +std::string AssetPath::prefix() const { + auto str = std::string(); + for (auto &p : path) { + str += p + ASSET_PATH_SEPARATOR; } -} \ No newline at end of file + return str; +} +} // namespace ICE \ No newline at end of file diff --git a/ICE/Graphics/src/ForwardRenderer.cpp b/ICE/Graphics/src/ForwardRenderer.cpp index d487cf12..40b7c864 100644 --- a/ICE/Graphics/src/ForwardRenderer.cpp +++ b/ICE/Graphics/src/ForwardRenderer.cpp @@ -109,10 +109,12 @@ void ForwardRenderer::prepareFrame(Camera& camera) { for (const auto& e : m_render_queue) { auto rc = m_registry->getComponent(e); auto tc = m_registry->getComponent(e); - auto model = m_asset_bank->getAsset(m_registry->getComponent(e)->model); + auto model = m_asset_bank->getAsset(rc->model); + if (!model) + continue; for (int i = 0; i < model->getMeshes().size(); i++) { auto mtl_id = model->getMaterialsIDs().at(i); - auto& mesh = model->getMeshes().at(i); + auto mesh = model->getMeshes().at(i); auto material = m_asset_bank->getAsset(mtl_id); if (!material) { continue; diff --git a/ICE/GraphicsAPI/None/NoneGraphics.h b/ICE/GraphicsAPI/None/NoneGraphics.h index d9ff1e7e..685c2ec7 100644 --- a/ICE/GraphicsAPI/None/NoneGraphics.h +++ b/ICE/GraphicsAPI/None/NoneGraphics.h @@ -75,8 +75,6 @@ class NoneShader : public Shader { void loadMat4(const std::string &name, Eigen::Matrix4f mat) override {} virtual AssetType getType() const override { return AssetType::EShader; } virtual std::string getTypeName() const override { return "Shader"; } - virtual void load() override {} - virtual void unload() override {} }; class NoneTexture2D : public Texture2D { @@ -89,9 +87,6 @@ class NoneTexture2D : public Texture2D { void setData(void *data, uint32_t size) override {} void *getTexture() const override { return nullptr; } TextureType getTextureType() const override { return TextureType::Tex2D; } - - virtual void load() override {} - virtual void unload() override {} }; class NoneTextureCube : public TextureCube { @@ -104,9 +99,6 @@ class NoneTextureCube : public TextureCube { void setData(void *data, uint32_t size) override {} void *getTexture() const override { return nullptr; } TextureType getTextureType() const override { return TextureType::CubeMap; } - - virtual void load() override {} - virtual void unload() override {} }; class NoneVertexArray : public VertexArray { diff --git a/ICE/IO/src/Project.cpp b/ICE/IO/src/Project.cpp index b641d386..00fd9694 100644 --- a/ICE/IO/src/Project.cpp +++ b/ICE/IO/src/Project.cpp @@ -29,7 +29,7 @@ Project::Project(const fs::path &base_directory, const std::string &name) m_shaders_directory = m_base_directory / assets_folder / "Shaders"; m_textures_directory = m_base_directory / assets_folder / "Textures"; m_cubemaps_directory = m_base_directory / assets_folder / "Cubemaps"; - m_meshes_directory = m_base_directory / assets_folder / "Meshes"; + m_meshes_directory = m_base_directory / assets_folder / "Models"; m_scenes_directory = m_base_directory / "Scenes"; } @@ -41,8 +41,8 @@ bool Project::CreateDirectories() { fs::create_directories(m_meshes_directory); fs::create_directories(m_scenes_directory); - copyAssetFile("Meshes", "cube", "Assets/Meshes/cube.obj"); - copyAssetFile("Meshes", "sphere", "Assets/Meshes/sphere.obj"); + copyAssetFile("Models", "cube", "Assets/Models/cube.obj"); + copyAssetFile("Models", "sphere", "Assets/Models/sphere.obj"); copyAssetFile("Shaders", "phong", "Assets/Shaders/phong.vs"); copyAssetFile("Shaders", "phong", "Assets/Shaders/phong.fs"); copyAssetFile("Shaders", "solid", "Assets/Shaders/solid.vs"); @@ -56,13 +56,15 @@ bool Project::CreateDirectories() { copyAssetFile("Cubemaps", "skybox", "Assets/Textures/skybox.png"); copyAssetFile("Materials", "base_mat", "Assets/Materials/base_mat.icm"); - assetBank->addAsset("cube", {m_meshes_directory / "cube.obj"}); - assetBank->addAsset("sphere", {m_meshes_directory / "sphere.obj"}); assetBank->addAsset("solid", {m_shaders_directory / "solid.vs", m_shaders_directory / "solid.fs"}); assetBank->addAsset("phong", {m_shaders_directory / "phong.vs", m_shaders_directory / "phong.fs"}); assetBank->addAsset("normal", {m_shaders_directory / "normal.vs", m_shaders_directory / "normal.fs"}); assetBank->addAsset("lastpass", {m_shaders_directory / "lastpass.vs", m_shaders_directory / "lastpass.fs"}); assetBank->addAsset("__ice__picking_shader", {m_shaders_directory / "picking.vs", m_shaders_directory / "picking.fs"}); + + assetBank->addAsset("cube", {m_meshes_directory / "cube.obj"}); + assetBank->addAsset("sphere", {m_meshes_directory / "sphere.obj"}); + assetBank->addAsset("skybox", {m_cubemaps_directory / "skybox.png"}); assetBank->addAsset("base_mat", {m_materials_directory / "base_mat.icm"}); @@ -147,8 +149,7 @@ void Project::writeToFile(const std::shared_ptr &editorCamera) { if (s->getRegistry()->entityHasComponent(e)) { RenderComponent rc = *s->getRegistry()->getComponent(e); json renderjson; - renderjson["mesh"] = rc.mesh; - renderjson["material"] = rc.material; + renderjson["model"] = rc.model; entity["renderComponent"] = renderjson; } if (s->getRegistry()->entityHasComponent(e)) { @@ -234,12 +235,12 @@ void Project::loadFromFile() { } if (!jentity["renderComponent"].is_null()) { json rj = jentity["renderComponent"]; - RenderComponent rc(rj["mesh"], rj["material"]); + RenderComponent rc(rj["model"]); scene.getRegistry()->addComponent(e, rc); } if (!jentity["lightComponent"].is_null()) { json lj = jentity["lightComponent"]; - LightComponent lc(static_cast((int)lj["type"]), JsonParser::parseVec3(lj["color"])); + LightComponent lc(static_cast((int) lj["type"]), JsonParser::parseVec3(lj["color"])); scene.getRegistry()->addComponent(e, lc); } } diff --git a/ICE/IO/test/MeshLoaderTest.cpp b/ICE/IO/test/MeshLoaderTest.cpp index 5fe47926..c834bc1d 100644 --- a/ICE/IO/test/MeshLoaderTest.cpp +++ b/ICE/IO/test/MeshLoaderTest.cpp @@ -1,13 +1,15 @@ +#include #include #include -#include "MeshLoader.h" +#include "ModelLoader.h" using namespace ICE; -TEST(MeshLoaderTest, LoadFromObj) { +TEST(ModelLoaderTest, LoadFromObj) { auto gr_f = std::make_shared(); - auto mesh = MeshLoader(gr_f).load({"cube.obj"}); - EXPECT_EQ(mesh->getVertices().size(), 24); - EXPECT_EQ(mesh->getIndices().size(), 12); + AssetBank bank(gr_f); + auto mesh = ModelLoader(gr_f, bank).load({"cube.obj"}); + EXPECT_EQ(mesh->getMeshes().at(0)->getVertices().size(), 24); + EXPECT_EQ(mesh->getMeshes().at(0)->getIndices().size(), 12); } \ No newline at end of file diff --git a/ICEBERG/UI/InspectorWidget.h b/ICEBERG/UI/InspectorWidget.h index 24379213..902d27ae 100644 --- a/ICEBERG/UI/InspectorWidget.h +++ b/ICEBERG/UI/InspectorWidget.h @@ -85,21 +85,15 @@ class InspectorWidget : public Widget { } } - void setRenderComponent(ICE::RenderComponent* rc, const std::vector& meshes_paths, const std::vector& meshes_ids, - const std::vector& materials_paths, const std::vector& materials_ids) { + void setRenderComponent(ICE::RenderComponent* rc, const std::vector& meshes_paths, const std::vector& meshes_ids) { m_rc = rc; m_rc_inputs.clear(); if (rc) { m_rc_inputs.reserve(2); - UniformInputs in_mesh("Mesh", rc->mesh); - in_mesh.onValueChanged([this](const ICE::UniformValue& v) { m_rc->mesh = std::get(v); }); + UniformInputs in_mesh("Mesh", rc->model); + in_mesh.onValueChanged([this](const ICE::UniformValue& v) { m_rc->model = std::get(v); }); in_mesh.setAssetComboList(meshes_paths, meshes_ids); m_rc_inputs.push_back(in_mesh); - - UniformInputs in_mat("Material", rc->material); - in_mat.onValueChanged([this](const ICE::UniformValue& v) { m_rc->material = std::get(v); }); - in_mat.setAssetComboList(materials_paths, materials_ids); - m_rc_inputs.push_back(in_mat); } } diff --git a/ICEBERG/UI/NewMaterialWidget.h b/ICEBERG/UI/NewMaterialWidget.h index a04b930c..d852e8c1 100644 --- a/ICEBERG/UI/NewMaterialWidget.h +++ b/ICEBERG/UI/NewMaterialWidget.h @@ -185,9 +185,9 @@ class NewMaterialWidget : public Widget { s.getRegistry()->addSystem(render_system); auto entity = s.createEntity(); - auto mesh_uid = m_engine->getAssetBank()->getUID(ICE::AssetPath("Meshes/sphere")); + auto model_uid = m_engine->getAssetBank()->getUID(ICE::AssetPath::WithTypePrefix("sphere")); - s.getRegistry()->addComponent(entity, ICE::RenderComponent(mesh_uid, m_id)); + s.getRegistry()->addComponent(entity, ICE::RenderComponent(model_uid)); s.getRegistry()->addComponent(entity, ICE::TransformComponent({0, 0, 0}, {0, 45, 0}, {1, 1, 1})); render_system->setTarget(preview_framebuffer); render_system->update(1.0f); diff --git a/ICEBERG/src/Assets.cpp b/ICEBERG/src/Assets.cpp index cf302c80..2d7fd0a9 100644 --- a/ICEBERG/src/Assets.cpp +++ b/ICEBERG/src/Assets.cpp @@ -42,12 +42,12 @@ void* Assets::createThumbnail(const ICE::AssetBankEntry& entry) { s.getRegistry()->addSystem(render_system); auto entity = s.createEntity(); - auto mesh_uid = m_engine->getAssetBank()->getUID(ICE::AssetPath("Meshes/sphere")); + auto model_uid = m_engine->getAssetBank()->getUID(ICE::AssetPath::WithTypePrefix("sphere")); auto material_uid = m_engine->getAssetBank()->getUID(ICE::AssetPath("Materials/base_mat")); auto self_uid = m_engine->getAssetBank()->getUID(entry.path); - if (auto m = std::dynamic_pointer_cast(asset); m) { - mesh_uid = self_uid; + if (auto m = std::dynamic_pointer_cast(asset); m) { + model_uid = self_uid; } else if (auto m = std::dynamic_pointer_cast(asset); m) { material_uid = self_uid; } else { @@ -55,7 +55,7 @@ void* Assets::createThumbnail(const ICE::AssetBankEntry& entry) { return nullptr; } - s.getRegistry()->addComponent(entity, ICE::RenderComponent(mesh_uid, material_uid)); + s.getRegistry()->addComponent(entity, ICE::RenderComponent(model_uid)); s.getRegistry()->addComponent(entity, ICE::TransformComponent({0, 0, 0}, {0, 45, 0}, {1, 1, 1})); render_system->setTarget(preview_framebuffer); render_system->update(1.0f); diff --git a/ICEBERG/src/Hierarchy.cpp b/ICEBERG/src/Hierarchy.cpp index c33594b4..a1ff313d 100644 --- a/ICEBERG/src/Hierarchy.cpp +++ b/ICEBERG/src/Hierarchy.cpp @@ -14,9 +14,8 @@ Hierarchy::Hierarchy(const std::shared_ptr &engine) : m_engine(e scene->getRegistry()->addComponent( entity, ICE::TransformComponent(Eigen::Vector3f::Zero(), Eigen::Vector3f::Zero(), Eigen::Vector3f(1, 1, 1))); - auto cube_id = m_engine->getAssetBank()->getUID(ICE::AssetPath::WithTypePrefix("cube")); - auto mat_id = m_engine->getAssetBank()->getUID(ICE::AssetPath::WithTypePrefix("base_mat")); - scene->getRegistry()->addComponent(entity, ICE::RenderComponent(cube_id, mat_id)); + auto cube_id = m_engine->getAssetBank()->getUID(ICE::AssetPath::WithTypePrefix("cube")); + scene->getRegistry()->addComponent(entity, ICE::RenderComponent(cube_id)); scene->getGraph()->setParent(entity, parent, false); m_need_rebuild_tree = true; diff --git a/ICEBERG/src/Inspector.cpp b/ICEBERG/src/Inspector.cpp index c3b94a3c..71e8e8f2 100644 --- a/ICEBERG/src/Inspector.cpp +++ b/ICEBERG/src/Inspector.cpp @@ -46,7 +46,7 @@ void Inspector::setSelectedEntity(ICE::Entity e, bool force_refesh) { auto registry = m_engine->getProject()->getCurrentScene()->getRegistry(); ui.setEntityName(m_engine->getProject()->getCurrentScene()->getAlias(e)); ui.setLightComponent(nullptr); - ui.setRenderComponent(nullptr, {}, {}, {}, {}); + ui.setRenderComponent(nullptr, {}, {}); if (registry->entityHasComponent(e)) { auto tc = registry->getComponent(e); @@ -55,23 +55,17 @@ void Inspector::setSelectedEntity(ICE::Entity e, bool force_refesh) { if (registry->entityHasComponent(e)) { auto rc = registry->getComponent(e); - auto meshes = m_engine->getAssetBank()->getAll(); - auto materials = m_engine->getAssetBank()->getAll(); + auto meshes = m_engine->getAssetBank()->getAll(); - std::vector meshes_paths, materials_paths; - std::vector meshes_ids, materials_ids; + std::vector meshes_paths; + std::vector meshes_ids; for (const auto& [id, m] : meshes) { meshes_ids.push_back(id); meshes_paths.push_back(m_engine->getAssetBank()->getName(id).toString()); } - for (const auto& [id, m] : materials) { - materials_ids.push_back(id); - materials_paths.push_back(m_engine->getAssetBank()->getName(id).toString()); - } - - ui.setRenderComponent(rc, meshes_paths, meshes_ids, materials_paths, materials_ids); + ui.setRenderComponent(rc, meshes_paths, meshes_ids); } if (registry->entityHasComponent(e)) { auto lc = registry->getComponent(e); diff --git a/ICEBERG/src/Viewport.cpp b/ICEBERG/src/Viewport.cpp index 1b40a01f..0830af5f 100644 --- a/ICEBERG/src/Viewport.cpp +++ b/ICEBERG/src/Viewport.cpp @@ -45,10 +45,12 @@ Viewport::Viewport(const std::shared_ptr &engine, const std::fun auto rc = registry->getComponent(e); m_engine->getAssetBank()->getAsset("__ice__picking_shader")->loadMat4("model", tc->getModelMatrix()); m_engine->getAssetBank()->getAsset("__ice__picking_shader")->loadInt("objectID", e); - auto mesh = m_engine->getAssetBank()->getAsset(rc->mesh); - mesh->getVertexArray()->bind(); - mesh->getVertexArray()->getIndexBuffer()->bind(); - m_engine->getApi()->renderVertexArray(mesh->getVertexArray()); + auto model = m_engine->getAssetBank()->getAsset(rc->model); + for (const auto &mesh : model->getMeshes()) { + mesh->getVertexArray()->bind(); + mesh->getVertexArray()->getIndexBuffer()->bind(); + m_engine->getApi()->renderVertexArray(mesh->getVertexArray()); + } } } auto color = m_picking_frambuffer->readPixel(x, y); diff --git a/ICEFIELD/icefield.cpp b/ICEFIELD/icefield.cpp index 5ad9c940..a2d3851b 100644 --- a/ICEFIELD/icefield.cpp +++ b/ICEFIELD/icefield.cpp @@ -23,7 +23,7 @@ int main(void) { engine.setProject(project); - engine.getAssetBank()->addAsset("cube", {"Assets/cube.obj"}); + engine.getAssetBank()->addAsset("cube", {"Assets/cube.obj"}); engine.getAssetBank()->addAsset("solid", {"Assets/solid.vs", "Assets/solid.fs"}); auto shader_id = engine.getAssetBank()->getUID(std::string("Shaders/solid")); @@ -33,12 +33,12 @@ int main(void) { mat->setUniform("uAlbedo", Eigen::Vector3f(0.2, 0.5, 1)); engine.getAssetBank()->addAsset("mat", mat); - auto mesh_id = engine.getAssetBank()->getUID(std::string("Meshes/cube")); + auto mesh_id = engine.getAssetBank()->getUID(std::string("Models/cube")); auto material_id = engine.getAssetBank()->getUID(std::string("Materials/mat")); auto entity = scene->createEntity(); scene->getRegistry()->addComponent(entity, TransformComponent(Eigen::Vector3f::Zero(), Eigen::Vector3f::Zero(), Eigen::Vector3f(1, 1, 1))); - scene->getRegistry()->addComponent(entity, RenderComponent(mesh_id, material_id)); + scene->getRegistry()->addComponent(entity, RenderComponent(mesh_id)); auto camera = std::make_shared(60.0, 16.0 / 9.0, 0.01, 10000.0); camera->backward(2); From 4d9e44ec859477d25bdffc582e4dce85a358bc2a Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 24 Dec 2024 16:34:41 +0100 Subject: [PATCH 06/22] fix save/load of project with models --- ICE/IO/src/ModelLoader.cpp | 9 +++++++-- ICE/IO/src/Project.cpp | 15 ++++++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/ICE/IO/src/ModelLoader.cpp b/ICE/IO/src/ModelLoader.cpp index 1f8e6bc8..385f8730 100644 --- a/ICE/IO/src/ModelLoader.cpp +++ b/ICE/IO/src/ModelLoader.cpp @@ -75,10 +75,16 @@ std::shared_ptr ModelLoader::load(const std::vectorsetVertexArray(vertexArray); } - return std::make_shared(meshes, materials); + auto model = std::make_shared(meshes, materials); + model->setSources(file); + return model; } AssetUID ModelLoader::extractMaterial(const aiMaterial *material, const std::string &model_name) { + auto bank_name = model_name + "-" + material->GetName().C_Str(); + if (ref_bank.getUID(AssetPath::WithTypePrefix(bank_name)) != 0) { + return ref_bank.getUID(AssetPath::WithTypePrefix(bank_name)); + } auto mtl = std::make_shared(); mtl->setShader(ref_bank.getUID(AssetPath::WithTypePrefix("phong"))); @@ -100,7 +106,6 @@ AssetUID ModelLoader::extractMaterial(const aiMaterial *material, const std::str mtl->setUniform("material.use_specular_map", false); mtl->setUniform("material.use_normal_map", false); - auto bank_name = model_name + "-" + material->GetName().C_Str(); ref_bank.addAsset(bank_name, mtl); return ref_bank.getUID(AssetPath::WithTypePrefix(bank_name)); } diff --git a/ICE/IO/src/Project.cpp b/ICE/IO/src/Project.cpp index 00fd9694..58f6b054 100644 --- a/ICE/IO/src/Project.cpp +++ b/ICE/IO/src/Project.cpp @@ -62,12 +62,13 @@ bool Project::CreateDirectories() { assetBank->addAsset("lastpass", {m_shaders_directory / "lastpass.vs", m_shaders_directory / "lastpass.fs"}); assetBank->addAsset("__ice__picking_shader", {m_shaders_directory / "picking.vs", m_shaders_directory / "picking.fs"}); - assetBank->addAsset("cube", {m_meshes_directory / "cube.obj"}); - assetBank->addAsset("sphere", {m_meshes_directory / "sphere.obj"}); - assetBank->addAsset("skybox", {m_cubemaps_directory / "skybox.png"}); + assetBank->addAsset("base_mat", {m_materials_directory / "base_mat.icm"}); + assetBank->addAsset("cube", {m_meshes_directory / "cube.obj"}); + assetBank->addAsset("sphere", {m_meshes_directory / "sphere.obj"}); + scenes.push_back(std::make_shared("MainScene")); setCurrentScene(getScenes()[0]); return true; @@ -99,7 +100,7 @@ void Project::writeToFile(const std::shared_ptr &editorCamera) { for (const auto &[asset_id, mesh] : assetBank->getAll()) { vec.push_back(dumpAsset(asset_id, mesh)); } - j["meshes"] = vec; + j["models"] = vec; vec.clear(); for (const auto &[asset_id, material] : assetBank->getAll()) { @@ -197,7 +198,7 @@ void Project::loadFromFile() { infile.close(); std::vector sceneNames = j["scenes"]; - json meshes = j["meshes"]; + json meshes = j["models"]; json material = j["materials"]; json shader = j["shaders"]; json texture = j["textures2D"]; @@ -206,11 +207,11 @@ void Project::loadFromFile() { cameraPosition = JsonParser::parseVec3(j["camera_position"]); cameraRotation = JsonParser::parseVec3(j["camera_rotation"]); + loadAssetsOfType(shader); loadAssetsOfType(texture); loadAssetsOfType(cubeMap); - loadAssetsOfType(meshes); loadAssetsOfType(material); - loadAssetsOfType(shader); + loadAssetsOfType(meshes); for (const auto &s : sceneNames) { infile = std::ifstream(m_scenes_directory / (s + ".ics")); From fe9e649574a725427185be8d35ec61fcc9928593 Mon Sep 17 00:00:00 2001 From: Thomas Date: Wed, 25 Dec 2024 11:16:37 +0100 Subject: [PATCH 07/22] place materials from models into a subfolder --- Assets/Shaders/phong.fs | 19 +++++++++++++++---- ICE/IO/src/ModelLoader.cpp | 2 +- ICE/IO/src/Project.cpp | 4 +++- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Assets/Shaders/phong.fs b/Assets/Shaders/phong.fs index 8d651cbc..dfb6fc00 100644 --- a/Assets/Shaders/phong.fs +++ b/Assets/Shaders/phong.fs @@ -1,5 +1,5 @@ #version 330 core -#define ICE_MAX_LIGHTS (256) +#define ICE_MAX_LIGHTS (16) out vec4 frag_color; @@ -7,6 +7,7 @@ struct Light { vec3 position; vec3 rotation; vec3 color; + int type; //0 = Point, 1 = Directional, 2 = Spot }; struct Material { @@ -36,12 +37,11 @@ in vec2 ftex_coords; vec3 normal; -vec4 pointLight(Light light) { +vec4 computeLightEffect(Light light, vec3 light_direction) { vec4 rcolor = vec4(0.0); //diffuse vec3 n = normalize(normal); - vec3 light_direction = normalize(light.position - fposition); float diff = max(dot(n, light_direction), 0.0); vec4 diffuse_color = material.albedo; if(material.use_diffuse_map) { @@ -63,6 +63,17 @@ vec4 pointLight(Light light) { return rcolor; } +vec4 applyLight(Light light) { + if(light.type == 0) { + vec3 light_direction = normalize(light.position - fposition); + float distance = length(light.position - fposition); + return computeLightEffect(light, light_direction); + } else if(light.type == 1) { + return computeLightEffect(light, -normalize(light.rotation)); + } else if(light.type == 2) { + //TODO: Spot lights + } +} vec3 colorToNormal(vec3 color) { return normalize(vec3(color.x * 2 - 1, color.y * 2 - 1, color.z * 2 - 1)); @@ -81,7 +92,7 @@ void main() { color_accumulator *= texture(material.ambient_map, ftex_coords); } for(int i = 0; i < light_count; i++) { - color_accumulator += pointLight(lights[i]); + color_accumulator += applyLight(lights[i]); } if(light_count == 0) { frag_color = material.albedo; diff --git a/ICE/IO/src/ModelLoader.cpp b/ICE/IO/src/ModelLoader.cpp index 385f8730..b8007644 100644 --- a/ICE/IO/src/ModelLoader.cpp +++ b/ICE/IO/src/ModelLoader.cpp @@ -81,7 +81,7 @@ std::shared_ptr ModelLoader::load(const std::vectorGetName().C_Str(); + auto bank_name = model_name + "/" + material->GetName().C_Str(); if (ref_bank.getUID(AssetPath::WithTypePrefix(bank_name)) != 0) { return ref_bank.getUID(AssetPath::WithTypePrefix(bank_name)); } diff --git a/ICE/IO/src/Project.cpp b/ICE/IO/src/Project.cpp index 58f6b054..397fb3dc 100644 --- a/ICE/IO/src/Project.cpp +++ b/ICE/IO/src/Project.cpp @@ -105,7 +105,9 @@ void Project::writeToFile(const std::shared_ptr &editorCamera) { for (const auto &[asset_id, material] : assetBank->getAll()) { auto mtlName = assetBank->getName(asset_id).getName(); - fs::path path = m_materials_directory / (mtlName + ".icm"); + + fs::path path = m_materials_directory.parent_path() / (assetBank->getName(asset_id).prefix() + mtlName + ".icm"); + fs::create_directories(path.parent_path()); MaterialExporter().writeToJson(path, *material); material->setSources({path}); From a1d914eab7c821149956006b2b321caac1cc9e3a Mon Sep 17 00:00:00 2001 From: ProtectedVariable Date: Mon, 30 Dec 2024 17:59:39 +0100 Subject: [PATCH 08/22] fix edit of materials inside a subfolder --- ICEBERG/UI/AssetsWidget.h | 5 ++++- ICEBERG/UI/NewMaterialWidget.h | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/ICEBERG/UI/AssetsWidget.h b/ICEBERG/UI/AssetsWidget.h index 9474c3f3..50ef68ec 100644 --- a/ICEBERG/UI/AssetsWidget.h +++ b/ICEBERG/UI/AssetsWidget.h @@ -37,6 +37,7 @@ class AssetsWidget : public Widget { if (ImGui::Selectable(asset->folder_name.c_str(), i == m_selected_index)) { m_selected_index = i; m_current_view = m_assets[i]; + m_prefix = ""; } } ImGui::EndTable(); @@ -51,6 +52,7 @@ class AssetsWidget : public Widget { ImGui::Text(folder->folder_name.c_str()); if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(0)) { m_current_view = folder; + m_prefix += folder->folder_name+"/"; } } @@ -64,7 +66,7 @@ class AssetsWidget : public Widget { ImGui::EndGroup(); if (m_assets[m_selected_index]->folder_name == "Materials") { if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(0)) { - callback("material_edit", name); + callback("material_edit", m_prefix+name); } } if (ImGui::BeginPopupContextItem((name + "_material_context").c_str())) { @@ -112,4 +114,5 @@ class AssetsWidget : public Widget { std::vector> m_assets; int m_selected_index = 0; std::shared_ptr m_current_view = nullptr; + std::string m_prefix; }; diff --git a/ICEBERG/UI/NewMaterialWidget.h b/ICEBERG/UI/NewMaterialWidget.h index d852e8c1..52586ad8 100644 --- a/ICEBERG/UI/NewMaterialWidget.h +++ b/ICEBERG/UI/NewMaterialWidget.h @@ -62,8 +62,9 @@ class NewMaterialWidget : public Widget { } ImGui::SameLine(); if (ImGui::Button("Apply")) { + auto new_name = m_engine->getAssetBank()->getName(m_id).prefix() + m_name; auto rename_ok = m_engine->getAssetBank()->renameAsset(m_engine->getAssetBank()->getName(m_id), - ICE::AssetPath::WithTypePrefix(m_name)); + new_name); if (rename_ok) { m_accepted = true; ImGui::CloseCurrentPopup(); From 910752c8c94f7f79c1ba9f68f21ee65c5f6398f9 Mon Sep 17 00:00:00 2001 From: Thomas Ibanez Date: Fri, 3 Jan 2025 11:12:43 +0100 Subject: [PATCH 09/22] rewrite asset preview to work with multi-mesh-models --- ICE/Graphics/include/Shader.h | 42 ++++++++++++++++------------------- ICEBERG/src/Assets.cpp | 39 ++++++++++++++++---------------- 2 files changed, 39 insertions(+), 42 deletions(-) diff --git a/ICE/Graphics/include/Shader.h b/ICE/Graphics/include/Shader.h index 8e3c4e80..bc839067 100644 --- a/ICE/Graphics/include/Shader.h +++ b/ICE/Graphics/include/Shader.h @@ -5,35 +5,31 @@ #ifndef ICE_SHADER_H #define ICE_SHADER_H -#include -#include #include -namespace ICE { - class Shader : public Asset { - public: - virtual void bind() const = 0; - virtual void unbind() const = 0; +#include +#include - virtual void loadInt(const std::string &name, int v) = 0; - virtual void loadInts(const std::string &name, int* array, uint32_t size) = 0; +namespace ICE { +class Shader : public Asset { + public: + virtual void bind() const = 0; + virtual void unbind() const = 0; - virtual void loadFloat(const std::string &name, float v) = 0; - virtual void loadFloat2(const std::string &name, Eigen::Vector2f vec) = 0; - virtual void loadFloat3(const std::string &name, Eigen::Vector3f vec) = 0; - virtual void loadFloat4(const std::string &name, Eigen::Vector4f vec) = 0; + virtual void loadInt(const std::string &name, int v) = 0; + virtual void loadInts(const std::string &name, int *array, uint32_t size) = 0; - virtual void loadMat4(const std::string &name, Eigen::Matrix4f mat) = 0; + virtual void loadFloat(const std::string &name, float v) = 0; + virtual void loadFloat2(const std::string &name, Eigen::Vector2f vec) = 0; + virtual void loadFloat3(const std::string &name, Eigen::Vector3f vec) = 0; + virtual void loadFloat4(const std::string &name, Eigen::Vector4f vec) = 0; - std::string getTypeName() const override { - return "Shader"; - }; + virtual void loadMat4(const std::string &name, Eigen::Matrix4f mat) = 0; - AssetType getType() const override { - return AssetType::EShader; - }; + std::string getTypeName() const override { return "Shader"; }; - }; -} + AssetType getType() const override { return AssetType::EShader; }; +}; +} // namespace ICE -#endif //ICE_SHADER_H +#endif //ICE_SHADER_H diff --git a/ICEBERG/src/Assets.cpp b/ICEBERG/src/Assets.cpp index 2d7fd0a9..c4244ab0 100644 --- a/ICEBERG/src/Assets.cpp +++ b/ICEBERG/src/Assets.cpp @@ -27,21 +27,7 @@ void* Assets::createThumbnail(const ICE::AssetBankEntry& entry) { if (auto m = std::dynamic_pointer_cast(asset); m) { return m->getTexture(); } - auto preview_framebuffer = m_g_factory->createFramebuffer({256, 256, 1}); - ICE::Scene s("preview_scene"); - auto render_system = std::make_shared(); - render_system->setRenderer(std::make_shared(m_engine->getApi(), m_g_factory, s.getRegistry(), m_engine->getAssetBank())); - - auto camera = std::make_shared(60.0, 1.0, 0.01, 10000.0); - camera->backward(2); - camera->up(1); - camera->pitch(-30); - render_system->setCamera(camera); - render_system->setViewport(0, 0, 256, 256); - s.getRegistry()->addSystem(render_system); - - auto entity = s.createEntity(); auto model_uid = m_engine->getAssetBank()->getUID(ICE::AssetPath::WithTypePrefix("sphere")); auto material_uid = m_engine->getAssetBank()->getUID(ICE::AssetPath("Materials/base_mat")); @@ -54,13 +40,28 @@ void* Assets::createThumbnail(const ICE::AssetBankEntry& entry) { //TODO return default icons return nullptr; } + auto model = m_engine->getAssetBank()->getAsset(model_uid); + auto material = m_engine->getAssetBank()->getAsset(material_uid); + auto shader = m_engine->getAssetBank()->getAsset(material->getShader()); - s.getRegistry()->addComponent(entity, ICE::RenderComponent(model_uid)); - s.getRegistry()->addComponent(entity, ICE::TransformComponent({0, 0, 0}, {0, 45, 0}, {1, 1, 1})); - render_system->setTarget(preview_framebuffer); - render_system->update(1.0f); + auto camera = std::make_shared(60.0, 1.0, 0.01, 10000.0); + camera->backward(2); + camera->up(1); + camera->pitch(-30); + + shader->bind(); + shader->loadMat4("projection", camera->getProjection()); + shader->loadMat4("view", camera->lookThrough()); + + ICE::GeometryPass pass(m_engine->getApi(), m_engine->getGraphicsFactory(), {256, 256, 1}); + std::vector cmds; + for (const auto& mesh : model->getMeshes()) { + cmds.push_back(ICE::RenderCommand{.mesh = mesh, .material = material, .shader = shader, .model_matrix = Eigen::Matrix4f::Identity()}); + } + pass.submit(&cmds); + pass.execute(); - return static_cast(0) + preview_framebuffer->getTexture(); + return static_cast(0) + pass.getResult()->getTexture(); } void Assets::rebuildViewer() { From 88966ba35030ecf3e4c78883ec7a929490cf1bd1 Mon Sep 17 00:00:00 2001 From: Thomas Date: Fri, 3 Jan 2025 14:08:47 +0100 Subject: [PATCH 10/22] fix material view --- ICEBERG/UI/NewMaterialWidget.h | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/ICEBERG/UI/NewMaterialWidget.h b/ICEBERG/UI/NewMaterialWidget.h index 52586ad8..82a75ce5 100644 --- a/ICEBERG/UI/NewMaterialWidget.h +++ b/ICEBERG/UI/NewMaterialWidget.h @@ -169,31 +169,29 @@ class NewMaterialWidget : public Widget { } void* renderPreview() { - auto preview_framebuffer = m_engine->getGraphicsFactory()->createFramebuffer({256, 256, 1}); - ICE::Scene s("preview_scene"); + auto model_uid = m_engine->getAssetBank()->getUID(ICE::AssetPath::WithTypePrefix("sphere")); - auto render_system = std::make_shared(); - render_system->setRenderer( - std::make_shared(m_engine->getApi(), m_engine->getGraphicsFactory(), s.getRegistry(), m_engine->getAssetBank())); + auto model = m_engine->getAssetBank()->getAsset(model_uid); + auto shader = m_engine->getAssetBank()->getAsset(m_material->getShader()); auto camera = std::make_shared(60.0, 1.0, 0.01, 10000.0); camera->backward(2); camera->up(1); camera->pitch(-30); - render_system->setCamera(camera); - render_system->setViewport(0, 0, 256, 256); - - s.getRegistry()->addSystem(render_system); - auto entity = s.createEntity(); - auto model_uid = m_engine->getAssetBank()->getUID(ICE::AssetPath::WithTypePrefix("sphere")); + shader->bind(); + shader->loadMat4("projection", camera->getProjection()); + shader->loadMat4("view", camera->lookThrough()); - s.getRegistry()->addComponent(entity, ICE::RenderComponent(model_uid)); - s.getRegistry()->addComponent(entity, ICE::TransformComponent({0, 0, 0}, {0, 45, 0}, {1, 1, 1})); - render_system->setTarget(preview_framebuffer); - render_system->update(1.0f); + ICE::GeometryPass pass(m_engine->getApi(), m_engine->getGraphicsFactory(), {256, 256, 1}); + std::vector cmds; + for (const auto& mesh : model->getMeshes()) { + cmds.push_back(ICE::RenderCommand{.mesh = mesh, .material = m_material, .shader = shader, .model_matrix = Eigen::Matrix4f::Identity()}); + } + pass.submit(&cmds); + pass.execute(); - return static_cast(0) + preview_framebuffer->getTexture(); + return static_cast(0) + pass.getResult()->getTexture(); } bool accepted() { From 265703132c5bc788f7e795e41ad57419618ddd0c Mon Sep 17 00:00:00 2001 From: Thomas Date: Fri, 3 Jan 2025 14:29:09 +0100 Subject: [PATCH 11/22] fix assetpath test --- ICE/Assets/test/AssetPathTest.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ICE/Assets/test/AssetPathTest.cpp b/ICE/Assets/test/AssetPathTest.cpp index 2a090ed1..e560eff5 100644 --- a/ICE/Assets/test/AssetPathTest.cpp +++ b/ICE/Assets/test/AssetPathTest.cpp @@ -2,7 +2,7 @@ // Created by Thomas Ibanez on 24.02.21. // #include -#include +#include #include #include #include @@ -32,7 +32,7 @@ TEST(AssetPathTest, setNameOk) { TEST(AssetPathTest, TypePrefixOk) { EXPECT_EQ(AssetPath::WithTypePrefix("a").toString(), "Materials/a"); - EXPECT_EQ(AssetPath::WithTypePrefix("a").toString(), "Meshes/a"); + EXPECT_EQ(AssetPath::WithTypePrefix("a").toString(), "Models/a"); EXPECT_EQ(AssetPath::WithTypePrefix("a").toString(), "Shaders/a"); EXPECT_EQ(AssetPath::WithTypePrefix("a").toString(), "Textures/a"); EXPECT_EQ(AssetPath::WithTypePrefix("a").toString(), "CubeMaps/a"); From ac2dc0a387c7617ebdb61aef46840251662cd3eb Mon Sep 17 00:00:00 2001 From: Thomas Date: Fri, 3 Jan 2025 14:33:48 +0100 Subject: [PATCH 12/22] fix asset prefix not reset --- ICEBERG/UI/AssetsWidget.h | 1 + 1 file changed, 1 insertion(+) diff --git a/ICEBERG/UI/AssetsWidget.h b/ICEBERG/UI/AssetsWidget.h index 50ef68ec..ab500c93 100644 --- a/ICEBERG/UI/AssetsWidget.h +++ b/ICEBERG/UI/AssetsWidget.h @@ -108,6 +108,7 @@ class AssetsWidget : public Widget { m_assets.clear(); m_current_view = nullptr; m_selected_index = 0; + m_prefix = ""; } private: From 18ff6e8ca596c0d86ccd3690b2ab6b1a3b02a531 Mon Sep 17 00:00:00 2001 From: Thomas Ibanez Date: Mon, 6 Jan 2025 16:37:45 +0100 Subject: [PATCH 13/22] starting work on textures from models --- ICE/Core/src/ICEEngine.cpp | 1 + ICE/Graphics/include/GraphicsFactory.h | 1 + ICE/Graphics/include/Texture.h | 2 +- ICE/GraphicsAPI/None/NoneGraphicsFactory.h | 3 + .../OpenGL/include/OpenGLFactory.h | 1 + .../OpenGL/include/OpenGLTexture.h | 90 ++++++------- .../OpenGL/src/OpenGLTexture2D.cpp | 121 +++++++++--------- .../OpenGL/src/OpenGLTextureCube.cpp | 95 +++++++------- ICE/IO/include/ModelLoader.h | 9 +- ICE/IO/src/ModelLoader.cpp | 15 ++- 10 files changed, 182 insertions(+), 156 deletions(-) diff --git a/ICE/Core/src/ICEEngine.cpp b/ICE/Core/src/ICEEngine.cpp index dc1464ad..2d901755 100644 --- a/ICE/Core/src/ICEEngine.cpp +++ b/ICE/Core/src/ICEEngine.cpp @@ -1,4 +1,5 @@ #define STB_IMAGE_IMPLEMENTATION + #include "ICEEngine.h" #include diff --git a/ICE/Graphics/include/GraphicsFactory.h b/ICE/Graphics/include/GraphicsFactory.h index ef11a962..329684c9 100644 --- a/ICE/Graphics/include/GraphicsFactory.h +++ b/ICE/Graphics/include/GraphicsFactory.h @@ -33,6 +33,7 @@ class GraphicsFactory { const std::string& fragmentFile) const = 0; virtual std::shared_ptr createTexture2D(const std::string& file) const = 0; + virtual std::shared_ptr createTexture2D(const void* data, size_t w, size_t h, TextureFormat fmt) const = 0; virtual std::shared_ptr createTextureCube(const std::string& file) const = 0; }; diff --git a/ICE/Graphics/include/Texture.h b/ICE/Graphics/include/Texture.h index 2950cf1c..458a88fd 100644 --- a/ICE/Graphics/include/Texture.h +++ b/ICE/Graphics/include/Texture.h @@ -4,10 +4,10 @@ #ifndef ICE_TEXTURE_H #define ICE_TEXTURE_H +#include #include #include -#include #include #include diff --git a/ICE/GraphicsAPI/None/NoneGraphicsFactory.h b/ICE/GraphicsAPI/None/NoneGraphicsFactory.h index 2f940027..2baeac67 100644 --- a/ICE/GraphicsAPI/None/NoneGraphicsFactory.h +++ b/ICE/GraphicsAPI/None/NoneGraphicsFactory.h @@ -36,6 +36,9 @@ class NoneGraphicsFactory : public GraphicsFactory { } std::shared_ptr createTexture2D(const std::string& file) const override { return std::make_shared(); } + std::shared_ptr createTexture2D(const void* data, size_t w, size_t h, TextureFormat fmt) const override { + return std::make_shared(); + } std::shared_ptr createTextureCube(const std::string& file) const override { return std::make_shared(); } }; diff --git a/ICE/GraphicsAPI/OpenGL/include/OpenGLFactory.h b/ICE/GraphicsAPI/OpenGL/include/OpenGLFactory.h index ba4198e7..a7861794 100644 --- a/ICE/GraphicsAPI/OpenGL/include/OpenGLFactory.h +++ b/ICE/GraphicsAPI/OpenGL/include/OpenGLFactory.h @@ -44,6 +44,7 @@ class OpenGLFactory : public GraphicsFactory { } std::shared_ptr createTexture2D(const std::string& file) const override { return std::make_shared(file); } + std::shared_ptr createTexture2D(const void* data, size_t w, size_t h, TextureFormat fmt) const override { return std::make_shared(data, w, h, fmt); } std::shared_ptr createTextureCube(const std::string& file) const override { return std::make_shared(file); } }; diff --git a/ICE/GraphicsAPI/OpenGL/include/OpenGLTexture.h b/ICE/GraphicsAPI/OpenGL/include/OpenGLTexture.h index 945148d1..fcf285bd 100644 --- a/ICE/GraphicsAPI/OpenGL/include/OpenGLTexture.h +++ b/ICE/GraphicsAPI/OpenGL/include/OpenGLTexture.h @@ -5,70 +5,70 @@ #ifndef ICE_OPENGLTEXTURE_H #define ICE_OPENGLTEXTURE_H - +#include #include + #include -#include namespace ICE { - class OpenGLTexture2D : public Texture2D { - public: - OpenGLTexture2D(const std::string &file); - - void bind(uint32_t slot) const override; +class OpenGLTexture2D : public Texture2D { + public: + OpenGLTexture2D(const std::string &file); + OpenGLTexture2D(const void *data, size_t w, size_t h, TextureFormat fmt); - TextureFormat getFormat() const override; + void bind(uint32_t slot) const override; - uint32_t getWidth() const override; - uint32_t getHeight() const override; + TextureFormat getFormat() const override; - TextureWrap getWrap() const override; + uint32_t getWidth() const override; + uint32_t getHeight() const override; - void setData(void *data, uint32_t size) override; + TextureWrap getWrap() const override; - void *getTexture() const override; + void setData(void *data, uint32_t size) override; - TextureType getTextureType() const override; + void *getTexture() const override; - private: - std::string file; - uint32_t id; - uint32_t width, height; - TextureFormat format; - TextureWrap wrap; - GLenum storageFormat; - GLenum dataFormat; - }; + TextureType getTextureType() const override; - class OpenGLTextureCube : public TextureCube { - public: - OpenGLTextureCube(const std::string &file); + private: + std::string file; + uint32_t id; + uint32_t width, height; + TextureFormat format; + TextureWrap wrap; + GLenum storageFormat; + GLenum dataFormat; +}; - void bind(uint32_t slot) const override; +class OpenGLTextureCube : public TextureCube { + public: + OpenGLTextureCube(const std::string &file); - TextureFormat getFormat() const override; + void bind(uint32_t slot) const override; - uint32_t getWidth() const override; - uint32_t getHeight() const override; + TextureFormat getFormat() const override; - TextureWrap getWrap() const override; + uint32_t getWidth() const override; + uint32_t getHeight() const override; - void setData(void *data, uint32_t size) override; + TextureWrap getWrap() const override; - void *getTexture() const override; + void setData(void *data, uint32_t size) override; - TextureType getTextureType() const override; + void *getTexture() const override; - private: - std::string file; - uint32_t id; - uint32_t width, height; - TextureFormat format; - TextureWrap wrap; - GLenum storageFormat; - GLenum dataFormat; - }; -} + TextureType getTextureType() const override; + private: + std::string file; + uint32_t id; + uint32_t width, height; + TextureFormat format; + TextureWrap wrap; + GLenum storageFormat; + GLenum dataFormat; +}; +} // namespace ICE -#endif //ICE_OPENGLTEXTURE_H +#endif //ICE_OPENGLTEXTURE_H diff --git a/ICE/GraphicsAPI/OpenGL/src/OpenGLTexture2D.cpp b/ICE/GraphicsAPI/OpenGL/src/OpenGLTexture2D.cpp index dc6a091e..a2e0de10 100644 --- a/ICE/GraphicsAPI/OpenGL/src/OpenGLTexture2D.cpp +++ b/ICE/GraphicsAPI/OpenGL/src/OpenGLTexture2D.cpp @@ -2,75 +2,80 @@ // Created by Thomas Ibanez on 22.12.20. // -#include "OpenGLTexture.h" #include #include +#include "OpenGLTexture.h" + namespace ICE { - OpenGLTexture2D::OpenGLTexture2D(const std::string &file) : file(file) { - int channels, w, h; - void* data = Texture::getDataFromFile(file, &w, &h, &channels); - width = w; - height = h; - storageFormat = GL_RGBA; - dataFormat = GL_RGBA; - if(channels == 4) { - format = TextureFormat::RGBA; - } else if(channels == 3) { - storageFormat = dataFormat = GL_RGB; - format = TextureFormat::RGB; - } - - glGenTextures(1, &id); - glBindTexture(GL_TEXTURE_2D, id); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - - wrap = TextureWrap::Repeat; - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - - glTexImage2D(GL_TEXTURE_2D, 0, storageFormat, width, height, 0, dataFormat, GL_UNSIGNED_BYTE, data); - - stbi_image_free(data); +OpenGLTexture2D::OpenGLTexture2D(const std::string &file) : file(file) { + int channels, w, h; + void *data = Texture::getDataFromFile(file, &w, &h, &channels); + width = w; + height = h; + storageFormat = GL_RGBA; + dataFormat = GL_RGBA; + if (channels == 4) { + format = TextureFormat::RGBA; + } else if (channels == 3) { + storageFormat = dataFormat = GL_RGB; + format = TextureFormat::RGB; } - void OpenGLTexture2D::setData(void *data, uint32_t size) { - uint32_t bpp = (format == TextureFormat::RGBA) ? 4 : 3; - if(size != bpp * width * height) { - throw ICEException("Texture size corrupted"); - } - glTextureSubImage2D(id, 0,0,0, width, height, dataFormat, GL_UNSIGNED_BYTE, data); - } + glGenTextures(1, &id); + glBindTexture(GL_TEXTURE_2D, id); - void OpenGLTexture2D::bind(uint32_t slot) const { - glActiveTexture(GL_TEXTURE0 + slot); - glBindTexture(GL_TEXTURE_2D, id); - } + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - TextureWrap OpenGLTexture2D::getWrap() const { - return wrap; - } + wrap = TextureWrap::Repeat; + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - TextureFormat OpenGLTexture2D::getFormat() const { - return format; - } + glTexImage2D(GL_TEXTURE_2D, 0, storageFormat, width, height, 0, dataFormat, GL_UNSIGNED_BYTE, data); - uint32_t OpenGLTexture2D::getWidth() const { - return width; - } + stbi_image_free(data); +} - uint32_t OpenGLTexture2D::getHeight() const { - return height; - } - - void *OpenGLTexture2D::getTexture() const { - return static_cast(0)+id; - } +OpenGLTexture2D::OpenGLTexture2D(const void *data, size_t w, size_t h, TextureFormat fmt) { + //TODO :) +} - TextureType OpenGLTexture2D::getTextureType() const { - return TextureType::Tex2D; +void OpenGLTexture2D::setData(void *data, uint32_t size) { + uint32_t bpp = (format == TextureFormat::RGBA) ? 4 : 3; + if (size != bpp * width * height) { + throw ICEException("Texture size corrupted"); } -} \ No newline at end of file + glTextureSubImage2D(id, 0, 0, 0, width, height, dataFormat, GL_UNSIGNED_BYTE, data); +} + +void OpenGLTexture2D::bind(uint32_t slot) const { + glActiveTexture(GL_TEXTURE0 + slot); + glBindTexture(GL_TEXTURE_2D, id); +} + +TextureWrap OpenGLTexture2D::getWrap() const { + return wrap; +} + +TextureFormat OpenGLTexture2D::getFormat() const { + return format; +} + +uint32_t OpenGLTexture2D::getWidth() const { + return width; +} + +uint32_t OpenGLTexture2D::getHeight() const { + return height; +} + +void *OpenGLTexture2D::getTexture() const { + return static_cast(0) + id; +} + +TextureType OpenGLTexture2D::getTextureType() const { + return TextureType::Tex2D; +} +} // namespace ICE \ No newline at end of file diff --git a/ICE/GraphicsAPI/OpenGL/src/OpenGLTextureCube.cpp b/ICE/GraphicsAPI/OpenGL/src/OpenGLTextureCube.cpp index a9ed4f62..faeba6c4 100644 --- a/ICE/GraphicsAPI/OpenGL/src/OpenGLTextureCube.cpp +++ b/ICE/GraphicsAPI/OpenGL/src/OpenGLTextureCube.cpp @@ -1,66 +1,67 @@ // // Created by Thomas Ibanez on 29.12.20. // -#include -#include "OpenGLTexture.h" -#include #include -namespace ICE { +#include - OpenGLTextureCube::OpenGLTextureCube(const std::string &file) { - int channels, w, h; - void* data = Texture::getDataFromFile(file, &w, &h, &channels, STBI_rgb); - width = w; - height = h; +#include "OpenGLTexture.h" - glGenTextures(1, &id); - glBindTexture(GL_TEXTURE_CUBE_MAP, id); +namespace ICE { - auto faces = equirectangularToCubemap((uint8_t*)data, width, height); - for(int i = 0; i < 6; i++) { - glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width/4, width/4, 0, GL_RGB, GL_UNSIGNED_BYTE, faces[i]); - } +OpenGLTextureCube::OpenGLTextureCube(const std::string &file) { + int channels, w, h; + void *data = Texture::getDataFromFile(file, &w, &h, &channels, STBI_rgb); + width = w; + height = h; - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); + glGenTextures(1, &id); + glBindTexture(GL_TEXTURE_CUBE_MAP, id); - stbi_image_free(data); + auto faces = equirectangularToCubemap((uint8_t *) data, width, height); + for (int i = 0; i < 6; i++) { + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width / 4, width / 4, 0, GL_RGB, GL_UNSIGNED_BYTE, faces[i]); } - void OpenGLTextureCube::bind(uint32_t slot) const { - glActiveTexture(GL_TEXTURE0 + slot); - glBindTexture(GL_TEXTURE_CUBE_MAP, id); - } + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); - TextureFormat OpenGLTextureCube::getFormat() const { - return TextureFormat::RGB; - } + stbi_image_free(data); +} - uint32_t OpenGLTextureCube::getWidth() const { - return width; - } +void OpenGLTextureCube::bind(uint32_t slot) const { + glActiveTexture(GL_TEXTURE0 + slot); + glBindTexture(GL_TEXTURE_CUBE_MAP, id); +} - uint32_t OpenGLTextureCube::getHeight() const { - return height; - } +TextureFormat OpenGLTextureCube::getFormat() const { + return TextureFormat::RGB; +} - TextureWrap OpenGLTextureCube::getWrap() const { - return TextureWrap::Clamp; - } +uint32_t OpenGLTextureCube::getWidth() const { + return width; +} - void OpenGLTextureCube::setData(void *data, uint32_t size) { - //TODO - } +uint32_t OpenGLTextureCube::getHeight() const { + return height; +} - void *OpenGLTextureCube::getTexture() const { - return static_cast(0)+id; - } +TextureWrap OpenGLTextureCube::getWrap() const { + return TextureWrap::Clamp; +} - TextureType OpenGLTextureCube::getTextureType() const { - return TextureType::CubeMap; - } -} \ No newline at end of file +void OpenGLTextureCube::setData(void *data, uint32_t size) { + //TODO +} + +void *OpenGLTextureCube::getTexture() const { + return static_cast(0) + id; +} + +TextureType OpenGLTextureCube::getTextureType() const { + return TextureType::CubeMap; +} +} // namespace ICE \ No newline at end of file diff --git a/ICE/IO/include/ModelLoader.h b/ICE/IO/include/ModelLoader.h index 5789b2f4..5f7e66b9 100644 --- a/ICE/IO/include/ModelLoader.h +++ b/ICE/IO/include/ModelLoader.h @@ -5,6 +5,7 @@ #pragma once #include +#include #include @@ -17,13 +18,17 @@ class AssetBank; class ModelLoader : public IAssetLoader { public: - ModelLoader(const std::shared_ptr &factory, AssetBank &bank) : ref_bank(bank), IAssetLoader(factory) {} + ModelLoader(const std::shared_ptr &factory, AssetBank &bank) + : ref_bank(bank), + m_graphics_factory(factory), + IAssetLoader(factory) {} std::shared_ptr load(const std::vector &file) override; - AssetUID extractMaterial(const aiMaterial *material, const std::string &model_name); + AssetUID extractMaterial(const aiMaterial *material, const std::string &model_name, const aiScene *scene); private: Eigen::Vector4f colorToVec(aiColor4D *color); AssetBank &ref_bank; + std::shared_ptr m_graphics_factory; }; } // namespace ICE diff --git a/ICE/IO/src/ModelLoader.cpp b/ICE/IO/src/ModelLoader.cpp index b8007644..9c20e854 100644 --- a/ICE/IO/src/ModelLoader.cpp +++ b/ICE/IO/src/ModelLoader.cpp @@ -31,11 +31,11 @@ std::shared_ptr ModelLoader::load(const std::vectormMeshes[m]; auto material = scene->mMaterials[mesh->mMaterialIndex]; - materials.push_back(extractMaterial(material, file[0].filename().stem().string())); + materials.push_back(extractMaterial(material, file[0].filename().stem().string(), scene)); for (int i = 0; i < mesh->mNumVertices; i++) { auto v = mesh->mVertices[i]; - auto n = mesh->mNormals[i]; + auto n = mesh->mNormals ? mesh->mNormals[i] : aiVector3D{0, 0, 0}; Eigen::Vector2f uv(0, 0); if (mesh->mTextureCoords[0] != nullptr) { auto uv_file = mesh->mTextureCoords[0][i]; @@ -80,7 +80,7 @@ std::shared_ptr ModelLoader::load(const std::vectorGetName().C_Str(); if (ref_bank.getUID(AssetPath::WithTypePrefix(bank_name)) != 0) { return ref_bank.getUID(AssetPath::WithTypePrefix(bank_name)); @@ -101,6 +101,15 @@ AssetUID ModelLoader::extractMaterial(const aiMaterial *material, const std::str mtl->setUniform("material.ambient", colorToVec(&ambient)); if (aiGetMaterialFloat(material, AI_MATKEY_SHININESS, &alpha) == aiReturn_SUCCESS) mtl->setUniform("material.alpha", alpha); + + aiString texture_file; + material->Get(AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0), texture_file); + if (auto texture = scene->GetEmbeddedTexture(texture_file.C_Str())) { + uint32_t *data = reinterpret_cast(texture->pcData); + auto texture_ice = m_graphics_factory->createTexture2D(data, texture->mWidth, texture->mHeight, TextureFormat::RGBA); + } else { + //regular file, check if it exists and read it + } mtl->setUniform("material.use_diffuse_map", false); mtl->setUniform("material.use_ambient_map", false); mtl->setUniform("material.use_specular_map", false); From 734ed6f043e65f745cc186ed4b4b738c92e776b3 Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 7 Jan 2025 16:41:46 +0100 Subject: [PATCH 14/22] refactored texture extraction --- .../OpenGL/include/OpenGLTexture.h | 2 + .../OpenGL/src/OpenGLTexture2D.cpp | 19 +++--- ICE/IO/include/ModelLoader.h | 1 + ICE/IO/src/ModelLoader.cpp | 64 +++++++++++++++---- 4 files changed, 67 insertions(+), 19 deletions(-) diff --git a/ICE/GraphicsAPI/OpenGL/include/OpenGLTexture.h b/ICE/GraphicsAPI/OpenGL/include/OpenGLTexture.h index fcf285bd..8753aef4 100644 --- a/ICE/GraphicsAPI/OpenGL/include/OpenGLTexture.h +++ b/ICE/GraphicsAPI/OpenGL/include/OpenGLTexture.h @@ -32,6 +32,8 @@ class OpenGLTexture2D : public Texture2D { TextureType getTextureType() const override; private: + void loadData(const void *data, size_t w, size_t h, TextureFormat fmt); + std::string file; uint32_t id; uint32_t width, height; diff --git a/ICE/GraphicsAPI/OpenGL/src/OpenGLTexture2D.cpp b/ICE/GraphicsAPI/OpenGL/src/OpenGLTexture2D.cpp index a2e0de10..f121663d 100644 --- a/ICE/GraphicsAPI/OpenGL/src/OpenGLTexture2D.cpp +++ b/ICE/GraphicsAPI/OpenGL/src/OpenGLTexture2D.cpp @@ -12,13 +12,22 @@ namespace ICE { OpenGLTexture2D::OpenGLTexture2D(const std::string &file) : file(file) { int channels, w, h; void *data = Texture::getDataFromFile(file, &w, &h, &channels); + loadData(data, w, h, channels == 4 ? TextureFormat::RGBA : TextureFormat::RGB); + stbi_image_free(data); +} + +OpenGLTexture2D::OpenGLTexture2D(const void *data, size_t w, size_t h, TextureFormat fmt) { + loadData(data, w, h, fmt); +} + +void OpenGLTexture2D::loadData(const void *data, size_t w, size_t h, TextureFormat fmt) { width = w; height = h; storageFormat = GL_RGBA; dataFormat = GL_RGBA; - if (channels == 4) { + if (fmt == TextureFormat::RGBA) { format = TextureFormat::RGBA; - } else if (channels == 3) { + } else if (fmt == TextureFormat::RGB) { storageFormat = dataFormat = GL_RGB; format = TextureFormat::RGB; } @@ -34,12 +43,6 @@ OpenGLTexture2D::OpenGLTexture2D(const std::string &file) : file(file) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexImage2D(GL_TEXTURE_2D, 0, storageFormat, width, height, 0, dataFormat, GL_UNSIGNED_BYTE, data); - - stbi_image_free(data); -} - -OpenGLTexture2D::OpenGLTexture2D(const void *data, size_t w, size_t h, TextureFormat fmt) { - //TODO :) } void OpenGLTexture2D::setData(void *data, uint32_t size) { diff --git a/ICE/IO/include/ModelLoader.h b/ICE/IO/include/ModelLoader.h index 5f7e66b9..5dfd1af9 100644 --- a/ICE/IO/include/ModelLoader.h +++ b/ICE/IO/include/ModelLoader.h @@ -24,6 +24,7 @@ class ModelLoader : public IAssetLoader { IAssetLoader(factory) {} std::shared_ptr load(const std::vector &file) override; AssetUID extractMaterial(const aiMaterial *material, const std::string &model_name, const aiScene *scene); + AssetUID extractTexture(const aiMaterial *material, const std::string &tex_path, const aiScene *scene, aiTextureType type); private: Eigen::Vector4f colorToVec(aiColor4D *color); diff --git a/ICE/IO/src/ModelLoader.cpp b/ICE/IO/src/ModelLoader.cpp index 9c20e854..f357ec45 100644 --- a/ICE/IO/src/ModelLoader.cpp +++ b/ICE/IO/src/ModelLoader.cpp @@ -17,7 +17,9 @@ namespace ICE { std::shared_ptr ModelLoader::load(const std::vector &file) { Assimp::Importer importer; - const aiScene *scene = importer.ReadFile(file[0].string(), aiProcess_CalcTangentSpace | aiProcess_Triangulate | aiProcess_JoinIdenticalVertices); + const aiScene *scene = importer.ReadFile( + file[0].string(), + aiProcess_ValidateDataStructure | aiProcess_SortByPType | aiProcess_GenSmoothNormals | aiProcess_CalcTangentSpace | aiProcess_Triangulate); std::vector> meshes; std::vector materials; @@ -81,7 +83,11 @@ std::shared_ptr ModelLoader::load(const std::vectorGetName().C_Str(); + auto mtl_name = material->GetName().C_Str(); + if (strlen(mtl_name) == 0) { + mtl_name = "DefaultMat"; + } + auto bank_name = model_name + "/" + mtl_name; if (ref_bank.getUID(AssetPath::WithTypePrefix(bank_name)) != 0) { return ref_bank.getUID(AssetPath::WithTypePrefix(bank_name)); } @@ -100,25 +106,61 @@ AssetUID ModelLoader::extractMaterial(const aiMaterial *material, const std::str if (aiGetMaterialColor(material, AI_MATKEY_COLOR_AMBIENT, &ambient) == aiReturn_SUCCESS) mtl->setUniform("material.ambient", colorToVec(&ambient)); if (aiGetMaterialFloat(material, AI_MATKEY_SHININESS, &alpha) == aiReturn_SUCCESS) - mtl->setUniform("material.alpha", alpha); + mtl->setUniform("material.alpha", max(alpha, 1.0f)); - aiString texture_file; - material->Get(AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0), texture_file); - if (auto texture = scene->GetEmbeddedTexture(texture_file.C_Str())) { - uint32_t *data = reinterpret_cast(texture->pcData); - auto texture_ice = m_graphics_factory->createTexture2D(data, texture->mWidth, texture->mHeight, TextureFormat::RGBA); - } else { - //regular file, check if it exists and read it - } mtl->setUniform("material.use_diffuse_map", false); mtl->setUniform("material.use_ambient_map", false); mtl->setUniform("material.use_specular_map", false); mtl->setUniform("material.use_normal_map", false); + if (auto ambient_map = extractTexture(material, bank_name + "/ambient_map", scene, aiTextureType_AMBIENT); ambient_map != 0) { + mtl->setUniform("material.ambient_map", ambient_map); + mtl->setUniform("material.use_ambient_map", true); + } + if (auto diffuse_tex = extractTexture(material, bank_name + "/diffuse_map", scene, aiTextureType_DIFFUSE); diffuse_tex != 0) { + mtl->setUniform("material.diffuse_map", diffuse_tex); + mtl->setUniform("material.use_diffuse_map", true); + } + if (auto specular_tex = extractTexture(material, bank_name + "/specular_map", scene, aiTextureType_SPECULAR); specular_tex != 0) { + mtl->setUniform("material.specular_map", specular_tex); + mtl->setUniform("material.use_specular_map", true); + } + if (auto normal_tex = extractTexture(material, bank_name + "/normal_map", scene, aiTextureType_NORMALS); normal_tex != 0) { + mtl->setUniform("material.normal_map", normal_tex); + mtl->setUniform("material.use_normal_map", true); + } + ref_bank.addAsset(bank_name, mtl); return ref_bank.getUID(AssetPath::WithTypePrefix(bank_name)); } +AssetUID ModelLoader::extractTexture(const aiMaterial *material, const std::string &tex_path, const aiScene *scene, aiTextureType type) { + AssetUID tex_id = 0; + aiString texture_file; + if (material->Get(AI_MATKEY_TEXTURE(type, 0), texture_file) == aiReturn_SUCCESS) { + if (auto texture = scene->GetEmbeddedTexture(texture_file.C_Str())) { + unsigned char *data = reinterpret_cast(texture->pcData); + void *data2 = nullptr; + int width = texture->mWidth; + int height = texture->mHeight; + int channels = 0; + if (height == 0) { + //Compressed memory, use stbi to load + data2 = stbi_load_from_memory(data, texture->mWidth, &width, &height, &channels, 4); + } else { + data2 = data; + } + auto texture_ice = m_graphics_factory->createTexture2D(data2, width, height, TextureFormat::RGBA); + ref_bank.addAsset(tex_path, texture_ice); + tex_id = ref_bank.getUID(AssetPath::WithTypePrefix(tex_path)); + } else { + //regular file, check if it exists and read it + //TODO :) + } + } + return tex_id; +} + Eigen::Vector4f ModelLoader::colorToVec(aiColor4D *color) { Eigen::Vector4f v; v.x() = color->r; From 4b56fcac9e22c2dae7b1d72fb7f29e4f2e052297 Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 7 Jan 2025 17:09:44 +0100 Subject: [PATCH 15/22] allow source-less texture on project load, for embedded textures --- ICE/Assets/include/AssetBank.h | 9 ++++++ ICE/Graphics/include/Texture.h | 1 - ICE/IO/src/ModelLoader.cpp | 54 ++++++++++++++++++---------------- ICE/IO/src/TextureLoader.cpp | 5 +++- ICEBERG/src/Editor.cpp | 4 +-- 5 files changed, 44 insertions(+), 29 deletions(-) diff --git a/ICE/Assets/include/AssetBank.h b/ICE/Assets/include/AssetBank.h index 795e3d4e..69f34adb 100644 --- a/ICE/Assets/include/AssetBank.h +++ b/ICE/Assets/include/AssetBank.h @@ -86,6 +86,15 @@ class AssetBank { return false; } + bool addAssetWithSpecificUID(const AssetPath& name, const std::shared_ptr& asset, AssetUID id) { + if (resources.find(id) == resources.end() && nameMapping.find(name) == nameMapping.end()) { + resources.try_emplace(id, AssetBankEntry{name, asset}); + nameMapping.try_emplace(name, id); + return true; + } + return false; + } + bool renameAsset(const AssetPath& oldName, const AssetPath& newName) { if (oldName.toString() == newName.toString()) { return true; diff --git a/ICE/Graphics/include/Texture.h b/ICE/Graphics/include/Texture.h index 458a88fd..0ecbf37d 100644 --- a/ICE/Graphics/include/Texture.h +++ b/ICE/Graphics/include/Texture.h @@ -34,7 +34,6 @@ class Texture : public Asset { virtual TextureType getTextureType() const = 0; static void* getDataFromFile(const std::string file, int* width, int* height, int* channels, int force = STBI_default) { - stbi_set_flip_vertically_on_load(1); stbi_uc* data = stbi_load(file.c_str(), width, height, channels, force); if(data == nullptr) { Logger::Log(Logger::ERROR, "Graphics", "Texture %s could not load: %s", file.c_str(), stbi_failure_reason()); diff --git a/ICE/IO/src/ModelLoader.cpp b/ICE/IO/src/ModelLoader.cpp index f357ec45..051366a6 100644 --- a/ICE/IO/src/ModelLoader.cpp +++ b/ICE/IO/src/ModelLoader.cpp @@ -17,9 +17,9 @@ namespace ICE { std::shared_ptr ModelLoader::load(const std::vector &file) { Assimp::Importer importer; - const aiScene *scene = importer.ReadFile( - file[0].string(), - aiProcess_ValidateDataStructure | aiProcess_SortByPType | aiProcess_GenSmoothNormals | aiProcess_CalcTangentSpace | aiProcess_Triangulate); + const aiScene *scene = importer.ReadFile(file[0].string(), + aiProcess_FlipUVs | aiProcess_ValidateDataStructure | aiProcess_SortByPType | aiProcess_GenSmoothNormals + | aiProcess_CalcTangentSpace | aiProcess_Triangulate); std::vector> meshes; std::vector materials; @@ -88,31 +88,11 @@ AssetUID ModelLoader::extractMaterial(const aiMaterial *material, const std::str mtl_name = "DefaultMat"; } auto bank_name = model_name + "/" + mtl_name; - if (ref_bank.getUID(AssetPath::WithTypePrefix(bank_name)) != 0) { - return ref_bank.getUID(AssetPath::WithTypePrefix(bank_name)); - } auto mtl = std::make_shared(); - mtl->setShader(ref_bank.getUID(AssetPath::WithTypePrefix("phong"))); - - aiColor4D diffuse; - aiColor4D specular; - aiColor4D ambient; - ai_real alpha = 1.0; - - if (aiGetMaterialColor(material, AI_MATKEY_COLOR_DIFFUSE, &diffuse) == aiReturn_SUCCESS) - mtl->setUniform("material.albedo", colorToVec(&diffuse)); - if (aiGetMaterialColor(material, AI_MATKEY_COLOR_SPECULAR, &specular) == aiReturn_SUCCESS) - mtl->setUniform("material.specular", colorToVec(&specular)); - if (aiGetMaterialColor(material, AI_MATKEY_COLOR_AMBIENT, &ambient) == aiReturn_SUCCESS) - mtl->setUniform("material.ambient", colorToVec(&ambient)); - if (aiGetMaterialFloat(material, AI_MATKEY_SHININESS, &alpha) == aiReturn_SUCCESS) - mtl->setUniform("material.alpha", max(alpha, 1.0f)); - mtl->setUniform("material.use_diffuse_map", false); mtl->setUniform("material.use_ambient_map", false); mtl->setUniform("material.use_specular_map", false); mtl->setUniform("material.use_normal_map", false); - if (auto ambient_map = extractTexture(material, bank_name + "/ambient_map", scene, aiTextureType_AMBIENT); ambient_map != 0) { mtl->setUniform("material.ambient_map", ambient_map); mtl->setUniform("material.use_ambient_map", true); @@ -130,6 +110,25 @@ AssetUID ModelLoader::extractMaterial(const aiMaterial *material, const std::str mtl->setUniform("material.use_normal_map", true); } + if (ref_bank.getUID(AssetPath::WithTypePrefix(bank_name)) != 0) { + return ref_bank.getUID(AssetPath::WithTypePrefix(bank_name)); + } + mtl->setShader(ref_bank.getUID(AssetPath::WithTypePrefix("phong"))); + + aiColor4D diffuse; + aiColor4D specular; + aiColor4D ambient; + ai_real alpha = 1.0; + + if (aiGetMaterialColor(material, AI_MATKEY_COLOR_DIFFUSE, &diffuse) == aiReturn_SUCCESS) + mtl->setUniform("material.albedo", colorToVec(&diffuse)); + if (aiGetMaterialColor(material, AI_MATKEY_COLOR_SPECULAR, &specular) == aiReturn_SUCCESS) + mtl->setUniform("material.specular", colorToVec(&specular)); + if (aiGetMaterialColor(material, AI_MATKEY_COLOR_AMBIENT, &ambient) == aiReturn_SUCCESS) + mtl->setUniform("material.ambient", colorToVec(&ambient)); + if (aiGetMaterialFloat(material, AI_MATKEY_SHININESS, &alpha) == aiReturn_SUCCESS) + mtl->setUniform("material.alpha", max(alpha, 1.0f)); + ref_bank.addAsset(bank_name, mtl); return ref_bank.getUID(AssetPath::WithTypePrefix(bank_name)); } @@ -151,8 +150,13 @@ AssetUID ModelLoader::extractTexture(const aiMaterial *material, const std::stri data2 = data; } auto texture_ice = m_graphics_factory->createTexture2D(data2, width, height, TextureFormat::RGBA); - ref_bank.addAsset(tex_path, texture_ice); - tex_id = ref_bank.getUID(AssetPath::WithTypePrefix(tex_path)); + if (tex_id = ref_bank.getUID(AssetPath::WithTypePrefix(tex_path)); tex_id != 0) { + ref_bank.removeAsset(AssetPath::WithTypePrefix(tex_path)); + ref_bank.addAssetWithSpecificUID(AssetPath::WithTypePrefix(tex_path), texture_ice, tex_id); + } else { + ref_bank.addAsset(tex_path, texture_ice); + tex_id = ref_bank.getUID(AssetPath::WithTypePrefix(tex_path)); + } } else { //regular file, check if it exists and read it //TODO :) diff --git a/ICE/IO/src/TextureLoader.cpp b/ICE/IO/src/TextureLoader.cpp index 33ca33e5..c8296322 100644 --- a/ICE/IO/src/TextureLoader.cpp +++ b/ICE/IO/src/TextureLoader.cpp @@ -4,12 +4,15 @@ #include "TextureLoader.h" -#include #include +#include namespace ICE { std::shared_ptr Texture2DLoader::load(const std::vector &file) { Logger::Log(Logger::VERBOSE, "IO", "Loading texture..."); + if (file.empty()) { + return nullptr; + } auto texture = graphics_factory->createTexture2D(file[0].string()); texture->setSources(file); return texture; diff --git a/ICEBERG/src/Editor.cpp b/ICEBERG/src/Editor.cpp index 4b6d3d69..02266736 100644 --- a/ICEBERG/src/Editor.cpp +++ b/ICEBERG/src/Editor.cpp @@ -40,9 +40,9 @@ Editor::Editor(const std::shared_ptr& engine, const std::shared_ import_name + std::to_string(++i); } while (m_engine->getAssetBank()->nameInUse(ICE::AssetPath::WithTypePrefix(import_name + std::to_string(i)))); import_name = import_name + std::to_string(i); - m_engine->getProject()->copyAssetFile("Meshes", import_name, file); + m_engine->getProject()->copyAssetFile("Models", import_name, file); m_engine->getAssetBank()->addAsset( - import_name, {m_engine->getProject()->getBaseDirectory() / "Assets" / "Meshes" / (import_name + file.extension().string())}); + import_name, {m_engine->getProject()->getBaseDirectory() / "Assets" / "Models" / (import_name + file.extension().string())}); m_assets->rebuildViewer(); } }); From caf6a819a97d106ce88bd4465008556ab16f14f2 Mon Sep 17 00:00:00 2001 From: Thomas Ibanez Date: Wed, 8 Jan 2025 08:49:00 +0100 Subject: [PATCH 16/22] fix compilation issue --- ICE/Assets/include/AssetBank.h | 1 + ICE/Assets/test/AssetBankTest.cpp | 2 ++ ICE/CMakeLists.txt | 2 ++ ICE/IO/CMakeLists.txt | 2 +- ICE/IO/src/ModelLoader.cpp | 2 +- ICE/IO/test/CMakeLists.txt | 16 ++++++++-------- .../{MeshLoaderTest.cpp => ModelLoaderTest.cpp} | 4 +++- 7 files changed, 18 insertions(+), 11 deletions(-) rename ICE/IO/test/{MeshLoaderTest.cpp => ModelLoaderTest.cpp} (79%) diff --git a/ICE/Assets/include/AssetBank.h b/ICE/Assets/include/AssetBank.h index 69f34adb..54600e16 100644 --- a/ICE/Assets/include/AssetBank.h +++ b/ICE/Assets/include/AssetBank.h @@ -90,6 +90,7 @@ class AssetBank { if (resources.find(id) == resources.end() && nameMapping.find(name) == nameMapping.end()) { resources.try_emplace(id, AssetBankEntry{name, asset}); nameMapping.try_emplace(name, id); + nextUID = nextUID > id ? nextUID : id + 1; return true; } return false; diff --git a/ICE/Assets/test/AssetBankTest.cpp b/ICE/Assets/test/AssetBankTest.cpp index 1fafecfe..28c789e2 100644 --- a/ICE/Assets/test/AssetBankTest.cpp +++ b/ICE/Assets/test/AssetBankTest.cpp @@ -1,6 +1,7 @@ // // Created by Thomas Ibanez on 24.02.21. // +#define STB_IMAGE_IMPLEMENTATION #include #include "AssetBank.h" @@ -8,6 +9,7 @@ using namespace ICE; + TEST(AssetBankTest, AddedAssetsCanBeRetrieved) { NoneGraphicsFactory g_fac; AssetBank ab(std::make_shared(g_fac)); diff --git a/ICE/CMakeLists.txt b/ICE/CMakeLists.txt index ad50cd66..294fb2be 100644 --- a/ICE/CMakeLists.txt +++ b/ICE/CMakeLists.txt @@ -3,6 +3,8 @@ project(ICE) message(STATUS "Building ${PROJECT_NAME}") +add_compile_definitions(NOMINMAX) + add_subdirectory(Assets) add_subdirectory(Components) add_subdirectory(Core) diff --git a/ICE/IO/CMakeLists.txt b/ICE/IO/CMakeLists.txt index 81eece53..0c68baa4 100644 --- a/ICE/IO/CMakeLists.txt +++ b/ICE/IO/CMakeLists.txt @@ -10,7 +10,7 @@ target_sources(${PROJECT_NAME} PRIVATE src/Project.cpp src/MaterialExporter.cpp src/TextureLoader.cpp - "src/ModelLoader.cpp" + src/ModelLoader.cpp src/ShaderLoader.cpp src/MaterialLoader.cpp ) diff --git a/ICE/IO/src/ModelLoader.cpp b/ICE/IO/src/ModelLoader.cpp index 051366a6..8db3ea85 100644 --- a/ICE/IO/src/ModelLoader.cpp +++ b/ICE/IO/src/ModelLoader.cpp @@ -127,7 +127,7 @@ AssetUID ModelLoader::extractMaterial(const aiMaterial *material, const std::str if (aiGetMaterialColor(material, AI_MATKEY_COLOR_AMBIENT, &ambient) == aiReturn_SUCCESS) mtl->setUniform("material.ambient", colorToVec(&ambient)); if (aiGetMaterialFloat(material, AI_MATKEY_SHININESS, &alpha) == aiReturn_SUCCESS) - mtl->setUniform("material.alpha", max(alpha, 1.0f)); + mtl->setUniform("material.alpha", std::max(alpha, 1.0f)); ref_bank.addAsset(bank_name, mtl); return ref_bank.getUID(AssetPath::WithTypePrefix(bank_name)); diff --git a/ICE/IO/test/CMakeLists.txt b/ICE/IO/test/CMakeLists.txt index 7a439130..e7b0bb2e 100644 --- a/ICE/IO/test/CMakeLists.txt +++ b/ICE/IO/test/CMakeLists.txt @@ -18,23 +18,23 @@ target_link_libraries(MaterialsTestSuite gtest_main io) -add_executable(MeshLoaderTestSuite - MeshLoaderTest.cpp +add_executable(ModelLoaderTestSuite + ModelLoaderTest.cpp ) -add_test(NAME MeshLoaderTestSuite - COMMAND MeshLoaderTestSuite - WORKING_DIRECTORY $) +add_test(NAME ModelLoaderTestSuite + COMMAND ModelLoaderTestSuite + WORKING_DIRECTORY $) -target_link_libraries(MeshLoaderTestSuite +target_link_libraries(ModelLoaderTestSuite PRIVATE gtest_main io) add_custom_command( - TARGET MeshLoaderTestSuite POST_BUILD + TARGET ModelLoaderTestSuite POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/cube.obj" - $ + $ ) diff --git a/ICE/IO/test/MeshLoaderTest.cpp b/ICE/IO/test/ModelLoaderTest.cpp similarity index 79% rename from ICE/IO/test/MeshLoaderTest.cpp rename to ICE/IO/test/ModelLoaderTest.cpp index c834bc1d..6316aa42 100644 --- a/ICE/IO/test/MeshLoaderTest.cpp +++ b/ICE/IO/test/ModelLoaderTest.cpp @@ -1,3 +1,5 @@ +#define STB_IMAGE_IMPLEMENTATION + #include #include #include @@ -10,6 +12,6 @@ TEST(ModelLoaderTest, LoadFromObj) { auto gr_f = std::make_shared(); AssetBank bank(gr_f); auto mesh = ModelLoader(gr_f, bank).load({"cube.obj"}); - EXPECT_EQ(mesh->getMeshes().at(0)->getVertices().size(), 24); + EXPECT_EQ(mesh->getMeshes().at(0)->getVertices().size(), 36); EXPECT_EQ(mesh->getMeshes().at(0)->getIndices().size(), 12); } \ No newline at end of file From 4325cd6c67879fbc047755a1d479d149549654d7 Mon Sep 17 00:00:00 2001 From: Thomas Ibanez Date: Wed, 8 Jan 2025 10:14:30 +0100 Subject: [PATCH 17/22] add bounding box to model --- ICE/Graphics/include/Model.h | 2 ++ ICE/Graphics/src/Model.cpp | 3 +++ 2 files changed, 5 insertions(+) diff --git a/ICE/Graphics/include/Model.h b/ICE/Graphics/include/Model.h index 3973e8ac..653c7bfe 100644 --- a/ICE/Graphics/include/Model.h +++ b/ICE/Graphics/include/Model.h @@ -10,6 +10,7 @@ class Model : public Asset { std::vector> getMeshes() const { return m_meshes; } std::vector getMaterialsIDs() const { return m_materials; } + AABB getBoundingBox() const { return m_boundingbox; } AssetType getType() const override { return AssetType::EModel; } std::string getTypeName() const override { return "Model"; } @@ -17,5 +18,6 @@ class Model : public Asset { private: std::vector> m_meshes; std::vector m_materials; + AABB m_boundingbox{{0, 0, 0}, {0, 0, 0}}; }; } // namespace ICE \ No newline at end of file diff --git a/ICE/Graphics/src/Model.cpp b/ICE/Graphics/src/Model.cpp index 13782d23..0a9ad74f 100644 --- a/ICE/Graphics/src/Model.cpp +++ b/ICE/Graphics/src/Model.cpp @@ -4,6 +4,9 @@ namespace ICE { Model::Model(const std::vector> &meshes, const std::vector &materials) : m_meshes(meshes), m_materials(materials) { + for (const auto &mesh : meshes) { + m_boundingbox = m_boundingbox.unionWith(mesh->getBoundingBox()); + } } } // namespace ICE \ No newline at end of file From fbe86a100c52625e3ef4a8ad23007483eb000c91 Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 21 Jan 2025 11:56:45 +0100 Subject: [PATCH 18/22] pretransform vertices for now until we do animations --- ICE/IO/src/ModelLoader.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ICE/IO/src/ModelLoader.cpp b/ICE/IO/src/ModelLoader.cpp index 8db3ea85..0bb88259 100644 --- a/ICE/IO/src/ModelLoader.cpp +++ b/ICE/IO/src/ModelLoader.cpp @@ -18,7 +18,7 @@ std::shared_ptr ModelLoader::load(const std::vector> meshes; From e30f4b76700c512124ba33404ecc6b207b1251fd Mon Sep 17 00:00:00 2001 From: Thomas Date: Sat, 25 Jan 2025 12:29:32 +0100 Subject: [PATCH 19/22] Implement frustum culling --- ICE/Graphics/src/ForwardRenderer.cpp | 10 +++ ICE/Math/include/AABB.h | 2 + ICE/Math/include/ICEMath.h | 4 ++ ICE/Math/src/AABB.cpp | 93 +++++++++++++++------------- ICE/Math/src/ICEMath.cpp | 40 +++++++++++- 5 files changed, 104 insertions(+), 45 deletions(-) diff --git a/ICE/Graphics/src/ForwardRenderer.cpp b/ICE/Graphics/src/ForwardRenderer.cpp index 40b7c864..44d725a7 100644 --- a/ICE/Graphics/src/ForwardRenderer.cpp +++ b/ICE/Graphics/src/ForwardRenderer.cpp @@ -106,12 +106,22 @@ void ForwardRenderer::prepareFrame(Camera& camera) { std::unordered_set prepared_shaders; auto view_mat = camera.lookThrough(); + auto frustum = extractFrustumPlanes(camera.getProjection() * view_mat); for (const auto& e : m_render_queue) { auto rc = m_registry->getComponent(e); auto tc = m_registry->getComponent(e); auto model = m_asset_bank->getAsset(rc->model); if (!model) continue; + + auto aabb = model->getBoundingBox(); + Eigen::Vector3f min = (tc->getModelMatrix() * Eigen::Vector4f(aabb.getMin().x(), aabb.getMin().y(), aabb.getMin().z(), 1.0)).head<3>(); + Eigen::Vector3f max = (tc->getModelMatrix() * Eigen::Vector4f(aabb.getMax().x(), aabb.getMax().y(), aabb.getMax().z(), 1.0)).head<3>(); + aabb = AABB(std::vector{min, max}); + if (!isAABBInFrustum(frustum, aabb)) { + continue; + } + for (int i = 0; i < model->getMeshes().size(); i++) { auto mtl_id = model->getMaterialsIDs().at(i); auto mesh = model->getMeshes().at(i); diff --git a/ICE/Math/include/AABB.h b/ICE/Math/include/AABB.h index d7405e03..8f44ec29 100644 --- a/ICE/Math/include/AABB.h +++ b/ICE/Math/include/AABB.h @@ -14,6 +14,8 @@ namespace ICE { AABB(const Eigen::Vector3f& min, const Eigen::Vector3f& max); AABB(const std::vector& points); + AABB scaledBy(const Eigen::Vector3f& scale) const; + AABB translatedBy(const Eigen::Vector3f& tr) const; float getVolume() const; bool overlaps(const AABB& other) const; bool contains(const Eigen::Vector3f& point) const; diff --git a/ICE/Math/include/ICEMath.h b/ICE/Math/include/ICEMath.h index bf829c7a..aef5a91e 100644 --- a/ICE/Math/include/ICEMath.h +++ b/ICE/Math/include/ICEMath.h @@ -10,6 +10,8 @@ #include #include +#include "AABB.h" + #define DEG_TO_RAD(x) ((x) * M_PI / 180.0) #define RAD_TO_DEG(x) ((x) * 180.0 / M_PI) @@ -30,6 +32,8 @@ Eigen::Matrix4f transformationMatrix(const Eigen::Vector3f &translation, const E Eigen::Vector3f orientation(int face, float x, float y); int clamp(int x, int a, int b); std::array equirectangularToCubemap(uint8_t *inputPixels, int width, int height, float rotation = 180); +std::array extractFrustumPlanes(const Eigen::Matrix4f &PV); +bool isAABBInFrustum(const std::array &frustum, const AABB &aabb); } // namespace ICE #endif //ICE_ICEMATH_H diff --git a/ICE/Math/src/AABB.cpp b/ICE/Math/src/AABB.cpp index f486b8d2..c2c93f3e 100644 --- a/ICE/Math/src/AABB.cpp +++ b/ICE/Math/src/AABB.cpp @@ -6,47 +6,52 @@ namespace ICE { - AABB::AABB(const Eigen::Vector3f &min, const Eigen::Vector3f &max) : min(min), max(max) {} - AABB::AABB(const std::vector& points) : AABB(points[0], points[0]) { - for(auto v : points) { - min = min.cwiseMin(v); - max = max.cwiseMax(v); - } - } - - float AABB::getVolume() const { - return (max.x() - min.x()) * (max.y() - min.y()) * (max.z() - min.z()); - } - - bool AABB::overlaps(const AABB &other) const { - return (min.x() <= other.max.x() && max.x() >= other.min.x()) && - (min.y() <= other.max.y() && max.y() >= other.min.y()) && - (min.z() <= other.max.z() && max.z() >= other.min.z()); - } - - bool AABB::contains(const Eigen::Vector3f &point) const { - return (point.x() >= min.x() && point.x() <= max.x()) && - (point.y() >= min.y() && point.y() <= max.y()) && - (point.z() >= min.z() && point.z() <= max.z()); - } - - AABB AABB::operator+(const AABB &other) const { - return unionWith(other); - } - - AABB AABB::unionWith(const AABB &other) const { - return AABB(min.cwiseMin(other.min), max.cwiseMax(other.max)); - } - - Eigen::Vector3f AABB::getCenter() const { - return (min + max) / 2; - } - - const Eigen::Vector3f &AABB::getMin() const { - return min; - } - - const Eigen::Vector3f &AABB::getMax() const { - return max; - } -} \ No newline at end of file +AABB::AABB(const Eigen::Vector3f &min, const Eigen::Vector3f &max) : min(min), max(max) { +} +AABB::AABB(const std::vector &points) : AABB(points[0], points[0]) { + for (auto v : points) { + min = min.cwiseMin(v); + max = max.cwiseMax(v); + } +} + +AABB AABB::scaledBy(const Eigen::Vector3f &scale) const { + return AABB(min.cwiseProduct(scale), max.cwiseProduct(scale)); +} +AABB AABB::translatedBy(const Eigen::Vector3f &tr) const { + return AABB(min + tr, max + tr); +} +float AABB::getVolume() const { + return (max.x() - min.x()) * (max.y() - min.y()) * (max.z() - min.z()); +} + +bool AABB::overlaps(const AABB &other) const { + return (min.x() <= other.max.x() && max.x() >= other.min.x()) && (min.y() <= other.max.y() && max.y() >= other.min.y()) + && (min.z() <= other.max.z() && max.z() >= other.min.z()); +} + +bool AABB::contains(const Eigen::Vector3f &point) const { + return (point.x() >= min.x() && point.x() <= max.x()) && (point.y() >= min.y() && point.y() <= max.y()) + && (point.z() >= min.z() && point.z() <= max.z()); +} + +AABB AABB::operator+(const AABB &other) const { + return unionWith(other); +} + +AABB AABB::unionWith(const AABB &other) const { + return AABB(min.cwiseMin(other.min), max.cwiseMax(other.max)); +} + +Eigen::Vector3f AABB::getCenter() const { + return (min + max) / 2; +} + +const Eigen::Vector3f &AABB::getMin() const { + return min; +} + +const Eigen::Vector3f &AABB::getMax() const { + return max; +} +} // namespace ICE \ No newline at end of file diff --git a/ICE/Math/src/ICEMath.cpp b/ICE/Math/src/ICEMath.cpp index ae47648a..13fd4a1c 100644 --- a/ICE/Math/src/ICEMath.cpp +++ b/ICE/Math/src/ICEMath.cpp @@ -29,7 +29,7 @@ Eigen::Matrix4f rotationMatrix(Eigen::Vector3f angles, bool yaw_first) { mz(1, 0) = sinf(rz); mz(1, 1) = cosf(rz); - if(yaw_first) { + if (yaw_first) { return my * mx * mz; } else { return mx * my * mz; @@ -125,4 +125,42 @@ std::array equirectangularToCubemap(uint8_t *inputPixels, int widt } return outputPixels; } + +std::array extractFrustumPlanes(const Eigen::Matrix4f &PV) { + std::array planes; + // Left + planes[0] = {PV(3, 0) + PV(0, 0), PV(3, 1) + PV(0, 1), PV(3, 2) + PV(0, 2), PV(3, 3) + PV(0, 3)}; + // Right + planes[1] = {PV(3, 0) - PV(0, 0), PV(3, 1) - PV(0, 1), PV(3, 2) - PV(0, 2), PV(3, 3) - PV(0, 3)}; + // Bottom + planes[2] = {PV(3, 0) + PV(1, 0), PV(3, 1) + PV(1, 1), PV(3, 2) + PV(1, 2), PV(3, 3) + PV(1, 3)}; + // Top + planes[3] = {PV(3, 0) - PV(1, 0), PV(3, 1) - PV(1, 1), PV(3, 2) - PV(1, 2), PV(3, 3) - PV(1, 3)}; + // Near + planes[4] = {PV(3, 0) + PV(2, 0), PV(3, 1) + PV(2, 1), PV(3, 2) + PV(2, 2), PV(3, 3) + PV(2, 3)}; + // Far + planes[5] = {PV(3, 0) - PV(2, 0), PV(3, 1) - PV(2, 1), PV(3, 2) - PV(2, 2), PV(3, 3) - PV(2, 3)}; + // Normalize planes + for (int i = 0; i < 6; i++) { + float length = sqrt(planes[i](0) * planes[i](0) + planes[i](1) * planes[i](1) + planes[i](2) * planes[i](2)); + planes[i](0) /= length; + planes[i](1) /= length; + planes[i](2) /= length; + planes[i](3) /= length; + } + return planes; +} + +bool isAABBInFrustum(const std::array &frustum, const AABB &aabb) { + for (int i = 0; i < 6; i++) { + Eigen::Vector3f positive(frustum[i](0) >= 0 ? aabb.getMax().x() : aabb.getMin().x(), + frustum[i](1) >= 0 ? aabb.getMax().y() : aabb.getMin().y(), + frustum[i](2) >= 0 ? aabb.getMax().z() : aabb.getMin().z()); + + if (frustum[i](0) * positive.x() + frustum[i](1) * positive.y() + frustum[i](2) * positive.z() + frustum[i](3) < 0) { + return false; + } + } + return true; +} } // namespace ICE From a9799fd15e21833d4a67fa58352dd8164f0e04a8 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sat, 25 Jan 2025 13:58:47 +0100 Subject: [PATCH 20/22] slight optimize --- ICE/Graphics/src/ForwardRenderer.cpp | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/ICE/Graphics/src/ForwardRenderer.cpp b/ICE/Graphics/src/ForwardRenderer.cpp index 44d725a7..80eec167 100644 --- a/ICE/Graphics/src/ForwardRenderer.cpp +++ b/ICE/Graphics/src/ForwardRenderer.cpp @@ -66,21 +66,6 @@ void ForwardRenderer::remove(Entity e) { void ForwardRenderer::prepareFrame(Camera& camera) { //TODO: Sort entities, make shader list, batch, make instances, set uniforms, etc.. - std::sort(m_render_queue.begin(), m_render_queue.end(), [this](Entity a, Entity b) { - auto model_a = m_asset_bank->getAsset(m_registry->getComponent(a)->model); - auto material_a = m_asset_bank->getAsset(model_a->getMaterialsIDs().front()); - auto model_b = m_asset_bank->getAsset(m_registry->getComponent(b)->model); - auto material_b = m_asset_bank->getAsset(model_b->getMaterialsIDs().front()); - - bool a_transparent = material_a ? material_a->isTransparent() : false; - bool b_transparent = material_b ? material_b->isTransparent() : false; - - if (!a_transparent && b_transparent) { - return true; - } else { - return false; - } - }); if (m_skybox != NO_ASSET_ID) { auto shader = m_asset_bank->getAsset("__ice_skybox_shader"); @@ -180,6 +165,16 @@ void ForwardRenderer::prepareFrame(Camera& camera) { } } + std::sort(m_render_commands.begin(), m_render_commands.end(), [this](const RenderCommand &a, const RenderCommand &b) { + bool a_transparent = a.material->isTransparent(); + bool b_transparent = b.material->isTransparent(); + + if (!a_transparent && b_transparent) { + return true; + } else { + return false; + } + }); m_geometry_pass.submit(&m_render_commands); } From 80ac781143f87df7c04661899c6c63b4d3b908c1 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sat, 25 Jan 2025 16:10:07 +0100 Subject: [PATCH 21/22] fix previews --- ICEBERG/UI/NewMaterialWidget.h | 16 +++++++++++++--- ICEBERG/src/Assets.cpp | 13 ++++++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/ICEBERG/UI/NewMaterialWidget.h b/ICEBERG/UI/NewMaterialWidget.h index 82a75ce5..ece39eaa 100644 --- a/ICEBERG/UI/NewMaterialWidget.h +++ b/ICEBERG/UI/NewMaterialWidget.h @@ -63,8 +63,7 @@ class NewMaterialWidget : public Widget { ImGui::SameLine(); if (ImGui::Button("Apply")) { auto new_name = m_engine->getAssetBank()->getName(m_id).prefix() + m_name; - auto rename_ok = m_engine->getAssetBank()->renameAsset(m_engine->getAssetBank()->getName(m_id), - new_name); + auto rename_ok = m_engine->getAssetBank()->renameAsset(m_engine->getAssetBank()->getName(m_id), new_name); if (rename_ok) { m_accepted = true; ImGui::CloseCurrentPopup(); @@ -185,8 +184,19 @@ class NewMaterialWidget : public Widget { ICE::GeometryPass pass(m_engine->getApi(), m_engine->getGraphicsFactory(), {256, 256, 1}); std::vector cmds; + std::unordered_map> textures; + for (const auto& [k, v] : m_material->getAllUniforms()) { + if (std::holds_alternative(v)) { + auto id = std::get(v); + textures.try_emplace(id, m_engine->getAssetBank()->getAsset(id)); + } + } for (const auto& mesh : model->getMeshes()) { - cmds.push_back(ICE::RenderCommand{.mesh = mesh, .material = m_material, .shader = shader, .model_matrix = Eigen::Matrix4f::Identity()}); + cmds.push_back(ICE::RenderCommand{.mesh = mesh, + .material = m_material, + .shader = shader, + .textures = textures, + .model_matrix = Eigen::Matrix4f::Identity()}); } pass.submit(&cmds); pass.execute(); diff --git a/ICEBERG/src/Assets.cpp b/ICEBERG/src/Assets.cpp index c4244ab0..45a3653e 100644 --- a/ICEBERG/src/Assets.cpp +++ b/ICEBERG/src/Assets.cpp @@ -55,8 +55,19 @@ void* Assets::createThumbnail(const ICE::AssetBankEntry& entry) { ICE::GeometryPass pass(m_engine->getApi(), m_engine->getGraphicsFactory(), {256, 256, 1}); std::vector cmds; + std::unordered_map> textures; + for (const auto& [k, v] : material->getAllUniforms()) { + if (std::holds_alternative(v)) { + auto id = std::get(v); + textures.try_emplace(id, m_engine->getAssetBank()->getAsset(id)); + } + } for (const auto& mesh : model->getMeshes()) { - cmds.push_back(ICE::RenderCommand{.mesh = mesh, .material = material, .shader = shader, .model_matrix = Eigen::Matrix4f::Identity()}); + cmds.push_back(ICE::RenderCommand{.mesh = mesh, + .material = material, + .shader = shader, + .textures = textures, + .model_matrix = Eigen::Matrix4f::Identity()}); } pass.submit(&cmds); pass.execute(); From 164a1e41b44fe1a1cc8fe67a3bc5f3c9489a9044 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sat, 25 Jan 2025 16:16:56 +0100 Subject: [PATCH 22/22] replace strlen --- ICE/IO/src/ModelLoader.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ICE/IO/src/ModelLoader.cpp b/ICE/IO/src/ModelLoader.cpp index 0bb88259..b78dd350 100644 --- a/ICE/IO/src/ModelLoader.cpp +++ b/ICE/IO/src/ModelLoader.cpp @@ -83,11 +83,11 @@ std::shared_ptr ModelLoader::load(const std::vectorGetName().C_Str(); - if (strlen(mtl_name) == 0) { + auto mtl_name = material->GetName(); + if (mtl_name.length == 0) { mtl_name = "DefaultMat"; } - auto bank_name = model_name + "/" + mtl_name; + auto bank_name = model_name + "/" + mtl_name.C_Str(); auto mtl = std::make_shared(); mtl->setUniform("material.use_diffuse_map", false); mtl->setUniform("material.use_ambient_map", false);