From e6036680231a36e0793794f33ef7ef70e36a1a27 Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Thu, 6 Feb 2025 11:28:14 +0200 Subject: [PATCH 1/4] STL exporting using assimp --- openVulkanoCpp/Scene/Export/MeshWriter.cpp | 88 ++++++++++++++++++++++ openVulkanoCpp/Scene/Export/MeshWriter.hpp | 1 + 2 files changed, 89 insertions(+) diff --git a/openVulkanoCpp/Scene/Export/MeshWriter.cpp b/openVulkanoCpp/Scene/Export/MeshWriter.cpp index c748015..399c1ca 100644 --- a/openVulkanoCpp/Scene/Export/MeshWriter.cpp +++ b/openVulkanoCpp/Scene/Export/MeshWriter.cpp @@ -183,6 +183,94 @@ namespace OpenVulkano::Scene } #else throw std::runtime_error("Unable to convert the scene to FBX: Assimp is not available!"); +#endif + } + + void MeshWriter::WriteAsSTL(Geometry* geometry, const std::string& filePath, bool binary) + { +#if __has_include("assimp/Exporter.hpp") + aiNode rootNode; + + aiScene scene; + scene.mRootNode = &rootNode; + + aiMesh mesh; + mesh.mNumVertices = geometry->vertexCount; + mesh.mMaterialIndex = 0; + mesh.mPrimitiveTypes = aiPrimitiveType_TRIANGLE; + + std::unique_ptr vertices = std::make_unique(geometry->vertexCount); + mesh.mVertices = vertices.get(); + + std::unique_ptr normals = std::make_unique(geometry->vertexCount); + mesh.mNormals = normals.get(); + + for (int i = 0; i < geometry->vertexCount; ++i) + { + const Vertex& vertex = geometry->vertices[i]; + mesh.mVertices[i] = aiVector3D(vertex.position.x, vertex.position.y, vertex.position.z); + mesh.mNormals[i] = aiVector3D(vertex.normal.x, vertex.normal.y, vertex.normal.z); + } + + mesh.mNumFaces = geometry->indexCount / 3; + std::unique_ptr faces = std::make_unique(mesh.mNumFaces); + mesh.mFaces = faces.get(); + + std::unique_ptr indices = std::make_unique(geometry->indexCount); + size_t lastUsedIndex = 0; + + for (int i = 0; i < mesh.mNumFaces; ++i) + { + aiFace& face = mesh.mFaces[i]; + face.mNumIndices = 3; + face.mIndices = &indices[lastUsedIndex]; + + face.mIndices[0] = geometry->GetIndex(i * 3 + 0); + face.mIndices[1] = geometry->GetIndex(i * 3 + 1); + face.mIndices[2] = geometry->GetIndex(i * 3 + 2); + + lastUsedIndex += face.mNumIndices; + } + + aiMesh* meshes[1] = { &mesh }; + scene.mMeshes = meshes; + scene.mNumMeshes = 1; + + unsigned int meshIndices[1]; + scene.mRootNode->mMeshes = meshIndices; + scene.mRootNode->mMeshes[0] = 0; + scene.mRootNode->mNumMeshes = 1; + + // STL doesn't use materials, but Assimp requires at least one + aiMaterial material; + aiMaterial* materials[1] = { &material }; + scene.mMaterials = materials; + scene.mNumMaterials = 1; + + Assimp::Exporter exporter; + const char* formatId = binary ? "stlb" : "stl"; + aiReturn result = exporter.Export(&scene, formatId, filePath); + + mesh.mVertices = nullptr; + mesh.mNormals = nullptr; + for (int i = 0; i < mesh.mNumFaces; ++i) + { + aiFace& face = mesh.mFaces[i]; + face.mIndices = nullptr; + } + mesh.mFaces = nullptr; + + rootNode.mMeshes = nullptr; + scene.mRootNode = nullptr; + scene.mMeshes = nullptr; + scene.mMaterials = nullptr; + + if (result != aiReturn_SUCCESS) + { + throw std::runtime_error("Unable to write STL file to " + filePath + ": " + exporter.GetErrorString()); + } +#else + throw std::runtime_error("Unable to export to STL: Assimp is not available!"); #endif } } \ No newline at end of file diff --git a/openVulkanoCpp/Scene/Export/MeshWriter.hpp b/openVulkanoCpp/Scene/Export/MeshWriter.hpp index ba24bce..3b9255b 100644 --- a/openVulkanoCpp/Scene/Export/MeshWriter.hpp +++ b/openVulkanoCpp/Scene/Export/MeshWriter.hpp @@ -19,5 +19,6 @@ namespace OpenVulkano::Scene static void WriteObjAsZip(Geometry* geometry, const std::string& texturePath, const std::string& zipPath); static void WriteAsUSDZ(Geometry* geometry, const std::string& texturePath, const std::string& usdzPath); static void WriteAsFBX(Geometry* geometry, const std::string& texturePath, const std::string& fbxPath); + static void WriteAsSTL(Geometry* geometry, const std::string& filePath, bool binary); }; } \ No newline at end of file From dc460cb1067195fabe4d356c2d6fccf5ff3d76e1 Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Thu, 6 Feb 2025 20:34:43 +0200 Subject: [PATCH 2/4] WriteAsFBX and WriteAsSTL refactoring --- openVulkanoCpp/Scene/Export/MeshWriter.cpp | 221 ++++++++------------- openVulkanoCpp/Scene/Export/MeshWriter.hpp | 9 + 2 files changed, 90 insertions(+), 140 deletions(-) diff --git a/openVulkanoCpp/Scene/Export/MeshWriter.cpp b/openVulkanoCpp/Scene/Export/MeshWriter.cpp index 399c1ca..47da6f5 100644 --- a/openVulkanoCpp/Scene/Export/MeshWriter.cpp +++ b/openVulkanoCpp/Scene/Export/MeshWriter.cpp @@ -90,180 +90,90 @@ namespace OpenVulkano::Scene } } - void MeshWriter::WriteAsFBX(Geometry* geometry, const std::string& texturePath, const std::string& fbxPath) - { #if __has_include("assimp/Exporter.hpp") - aiNode rootNode; + void MeshWriter::SetupAssimpScene(Geometry* geometry, aiScene& scene, aiNode& rootNode, aiMesh& mesh, + bool withTexCoords, float scaling) + { + mesh.mVertices = nullptr; + mesh.mNormals = nullptr; + mesh.mTangents = nullptr; + mesh.mBitangents = nullptr; + mesh.mFaces = nullptr; + for(unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) { + mesh.mTextureCoords[i] = nullptr; + mesh.mNumUVComponents[i] = 0; + } + for(unsigned int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i) { + mesh.mColors[i] = nullptr; + } - aiScene scene; + rootNode.mName = aiString("RootNode"); + rootNode.mChildren = nullptr; + rootNode.mNumChildren = 0; scene.mRootNode = &rootNode; - aiMesh mesh; + mesh.mName = aiString("Mesh"); mesh.mNumVertices = geometry->vertexCount; mesh.mMaterialIndex = 0; mesh.mPrimitiveTypes = aiPrimitiveType_TRIANGLE; - mesh.mNumUVComponents[0] = 2; - - std::unique_ptr vertices = std::make_unique(geometry->vertexCount); - mesh.mVertices = vertices.get(); - std::unique_ptr normals = std::make_unique(geometry->vertexCount); - mesh.mNormals = normals.get(); + mesh.mVertices = new aiVector3D[geometry->vertexCount]; + mesh.mNormals = new aiVector3D[geometry->vertexCount]; - std::unique_ptr texCoords = std::make_unique(geometry->vertexCount); - mesh.mTextureCoords[0] = texCoords.get(); - - float scaling = 100; // fbx units are centimeters... - for (uint32_t i = 0; i < geometry->vertexCount; ++i) + if (withTexCoords) + { + mesh.mNumUVComponents[0] = 2; + mesh.mTextureCoords[0] = new aiVector3D[geometry->vertexCount]; + } + + for (int i = 0; i < geometry->vertexCount; ++i) { const Vertex& vertex = geometry->vertices[i]; mesh.mVertices[i] = aiVector3D(vertex.position.x, vertex.position.y, vertex.position.z) * scaling; mesh.mNormals[i] = aiVector3D(vertex.normal.x, vertex.normal.y, vertex.normal.z); - mesh.mTextureCoords[0][i] = aiVector3D(vertex.textureCoordinates.x, vertex.textureCoordinates.y, 0.0f); + if (withTexCoords) + { + mesh.mTextureCoords[0][i] = aiVector3D(vertex.textureCoordinates.x, vertex.textureCoordinates.y, 0.0f); + } } mesh.mNumFaces = geometry->indexCount / 3; - std::unique_ptr faces = std::make_unique(mesh.mNumFaces); - mesh.mFaces = faces.get(); + mesh.mFaces = new aiFace[mesh.mNumFaces]; - std::unique_ptr indices = std::make_unique(geometry->indexCount); - size_t lastUsedIndex = 0; - - for (uint32_t i = 0; i < mesh.mNumFaces; ++i) + for (unsigned int i = 0; i < mesh.mNumFaces; ++i) { - aiFace& face = mesh.mFaces[i]; - face.mNumIndices = 3; - face.mIndices = &indices[lastUsedIndex]; - - face.mIndices[0] = geometry->GetIndex(i * 3 + 0); - face.mIndices[1] = geometry->GetIndex(i * 3 + 1); - face.mIndices[2] = geometry->GetIndex(i * 3 + 2); - - lastUsedIndex += face.mNumIndices; + mesh.mFaces[i].mNumIndices = 3; + mesh.mFaces[i].mIndices = new unsigned int[3]; + mesh.mFaces[i].mIndices[0] = geometry->GetIndex(i * 3 + 0); + mesh.mFaces[i].mIndices[1] = geometry->GetIndex(i * 3 + 1); + mesh.mFaces[i].mIndices[2] = geometry->GetIndex(i * 3 + 2); } - aiMesh* meshes[1] = { &mesh }; - scene.mMeshes = meshes; scene.mNumMeshes = 1; + scene.mMeshes = new aiMesh*[1] { &mesh }; - unsigned int meshIndices[1]; - scene.mRootNode->mMeshes = meshIndices; - scene.mRootNode->mMeshes[0] = 0; - scene.mRootNode->mNumMeshes = 1; - - aiMaterial material; - aiMaterial* materials[1] = { &material }; - scene.mMaterials = materials; scene.mNumMaterials = 1; + scene.mMaterials = new aiMaterial*[1] { new aiMaterial() }; - aiString externalPath(texturePath); - scene.mMaterials[0]->AddProperty(&externalPath, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0)); - - Assimp::Exporter exporter; - aiReturn result = exporter.Export(&scene, "fbx", fbxPath); - - mesh.mVertices = nullptr; - mesh.mNormals = nullptr; - mesh.mTextureCoords[0] = nullptr; - for (uint32_t i = 0; i < mesh.mNumFaces; ++i) - { - aiFace& face = mesh.mFaces[i]; - face.mIndices = nullptr; - } - mesh.mFaces = nullptr; - - rootNode.mMeshes = nullptr; - scene.mRootNode = nullptr; - scene.mMeshes = nullptr; - scene.mMaterials = nullptr; - - if (result != aiReturn_SUCCESS) - { - throw std::runtime_error("Unable to write a fbx file to " + fbxPath + ": " + exporter.GetErrorString()); - } -#else - throw std::runtime_error("Unable to convert the scene to FBX: Assimp is not available!"); -#endif + rootNode.mNumMeshes = 1; + rootNode.mMeshes = new unsigned int[1] { 0 }; } void MeshWriter::WriteAsSTL(Geometry* geometry, const std::string& filePath, bool binary) { #if __has_include("assimp/Exporter.hpp") - aiNode rootNode; + std::unique_ptr scene(new aiScene()); + std::unique_ptr rootNode(new aiNode()); + std::unique_ptr mesh(new aiMesh()); - aiScene scene; - scene.mRootNode = &rootNode; - - aiMesh mesh; - mesh.mNumVertices = geometry->vertexCount; - mesh.mMaterialIndex = 0; - mesh.mPrimitiveTypes = aiPrimitiveType_TRIANGLE; - - std::unique_ptr vertices = std::make_unique(geometry->vertexCount); - mesh.mVertices = vertices.get(); - - std::unique_ptr normals = std::make_unique(geometry->vertexCount); - mesh.mNormals = normals.get(); - - for (int i = 0; i < geometry->vertexCount; ++i) - { - const Vertex& vertex = geometry->vertices[i]; - mesh.mVertices[i] = aiVector3D(vertex.position.x, vertex.position.y, vertex.position.z); - mesh.mNormals[i] = aiVector3D(vertex.normal.x, vertex.normal.y, vertex.normal.z); - } - - mesh.mNumFaces = geometry->indexCount / 3; - std::unique_ptr faces = std::make_unique(mesh.mNumFaces); - mesh.mFaces = faces.get(); - - std::unique_ptr indices = std::make_unique(geometry->indexCount); - size_t lastUsedIndex = 0; - - for (int i = 0; i < mesh.mNumFaces; ++i) - { - aiFace& face = mesh.mFaces[i]; - face.mNumIndices = 3; - face.mIndices = &indices[lastUsedIndex]; - - face.mIndices[0] = geometry->GetIndex(i * 3 + 0); - face.mIndices[1] = geometry->GetIndex(i * 3 + 1); - face.mIndices[2] = geometry->GetIndex(i * 3 + 2); - - lastUsedIndex += face.mNumIndices; - } - - aiMesh* meshes[1] = { &mesh }; - scene.mMeshes = meshes; - scene.mNumMeshes = 1; - - unsigned int meshIndices[1]; - scene.mRootNode->mMeshes = meshIndices; - scene.mRootNode->mMeshes[0] = 0; - scene.mRootNode->mNumMeshes = 1; - - // STL doesn't use materials, but Assimp requires at least one - aiMaterial material; - aiMaterial* materials[1] = { &material }; - scene.mMaterials = materials; - scene.mNumMaterials = 1; + SetupAssimpScene(geometry, *scene, *rootNode, *mesh, false, 1.0f); Assimp::Exporter exporter; const char* formatId = binary ? "stlb" : "stl"; - aiReturn result = exporter.Export(&scene, formatId, filePath); + aiReturn result = exporter.Export(scene.get(), formatId, filePath); - mesh.mVertices = nullptr; - mesh.mNormals = nullptr; - for (int i = 0; i < mesh.mNumFaces; ++i) - { - aiFace& face = mesh.mFaces[i]; - face.mIndices = nullptr; - } - mesh.mFaces = nullptr; - - rootNode.mMeshes = nullptr; - scene.mRootNode = nullptr; - scene.mMeshes = nullptr; - scene.mMaterials = nullptr; + scene->mRootNode = nullptr; + scene->mMeshes = nullptr; if (result != aiReturn_SUCCESS) { @@ -273,4 +183,35 @@ namespace OpenVulkano::Scene throw std::runtime_error("Unable to export to STL: Assimp is not available!"); #endif } + + void MeshWriter::WriteAsFBX(Geometry* geometry, const std::string& texturePath, const std::string& fbxPath) + { +#if __has_include("assimp/Exporter.hpp") + std::unique_ptr scene(new aiScene()); + std::unique_ptr rootNode(new aiNode()); + std::unique_ptr mesh(new aiMesh()); + + SetupAssimpScene(geometry, *scene, *rootNode, *mesh, true, 100.0f); + + if (!texturePath.empty()) + { + aiString externalPath(texturePath); + scene->mMaterials[0]->AddProperty(&externalPath, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0)); + } + + Assimp::Exporter exporter; + aiReturn result = exporter.Export(scene.get(), "fbx", fbxPath); + + scene->mRootNode = nullptr; + scene->mMeshes = nullptr; + + if (result != aiReturn_SUCCESS) + { + throw std::runtime_error("Unable to write a fbx file to " + fbxPath + ": " + exporter.GetErrorString()); + } +#else + throw std::runtime_error("Unable to convert the scene to FBX: Assimp is not available!"); +#endif + } +#endif } \ No newline at end of file diff --git a/openVulkanoCpp/Scene/Export/MeshWriter.hpp b/openVulkanoCpp/Scene/Export/MeshWriter.hpp index 3b9255b..0439dff 100644 --- a/openVulkanoCpp/Scene/Export/MeshWriter.hpp +++ b/openVulkanoCpp/Scene/Export/MeshWriter.hpp @@ -8,6 +8,10 @@ #include +#if __has_include("assimp/Exporter.hpp") +#include +#endif + namespace OpenVulkano::Scene { class Geometry; @@ -20,5 +24,10 @@ namespace OpenVulkano::Scene static void WriteAsUSDZ(Geometry* geometry, const std::string& texturePath, const std::string& usdzPath); static void WriteAsFBX(Geometry* geometry, const std::string& texturePath, const std::string& fbxPath); static void WriteAsSTL(Geometry* geometry, const std::string& filePath, bool binary); + private: +#if __has_include("assimp/Exporter.hpp") + static void SetupAssimpScene(Geometry* geometry, aiScene& scene, aiNode& rootNode, aiMesh& mesh, + bool withTexCoords, float scaling); +#endif }; } \ No newline at end of file From 480b667e5c78eff96309ed752b57b862af9a18ae Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Fri, 7 Feb 2025 16:50:08 +0200 Subject: [PATCH 3/4] Removed SetupAssimpScene from header, using indices array per mesh --- openVulkanoCpp/Scene/Export/MeshWriter.cpp | 164 ++++++++++++--------- openVulkanoCpp/Scene/Export/MeshWriter.hpp | 9 -- 2 files changed, 92 insertions(+), 81 deletions(-) diff --git a/openVulkanoCpp/Scene/Export/MeshWriter.cpp b/openVulkanoCpp/Scene/Export/MeshWriter.cpp index 47da6f5..cc74c18 100644 --- a/openVulkanoCpp/Scene/Export/MeshWriter.cpp +++ b/openVulkanoCpp/Scene/Export/MeshWriter.cpp @@ -24,6 +24,82 @@ #include #endif +namespace +{ +#if __has_include("assimp/Exporter.hpp") + void SetupAssimpScene(OpenVulkano::Scene::Geometry* geometry, aiScene& scene, aiNode& rootNode, aiMesh& mesh, unsigned int*& indices, + bool withTexCoords, float scaling) + { + mesh.mVertices = nullptr; + mesh.mNormals = nullptr; + mesh.mTangents = nullptr; + mesh.mBitangents = nullptr; + mesh.mFaces = nullptr; + for(unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) { + mesh.mTextureCoords[i] = nullptr; + mesh.mNumUVComponents[i] = 0; + } + for(unsigned int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i) { + mesh.mColors[i] = nullptr; + } + + rootNode.mName = aiString("RootNode"); + rootNode.mChildren = nullptr; + rootNode.mNumChildren = 0; + scene.mRootNode = &rootNode; + + mesh.mName = aiString("Mesh"); + mesh.mNumVertices = geometry->vertexCount; + mesh.mMaterialIndex = 0; + mesh.mPrimitiveTypes = aiPrimitiveType_TRIANGLE; + + mesh.mVertices = new aiVector3D[geometry->vertexCount]; + mesh.mNormals = new aiVector3D[geometry->vertexCount]; + + if (withTexCoords) + { + mesh.mNumUVComponents[0] = 2; + mesh.mTextureCoords[0] = new aiVector3D[geometry->vertexCount]; + } + + for (int i = 0; i < geometry->vertexCount; ++i) + { + const OpenVulkano::Vertex& vertex = geometry->vertices[i]; + mesh.mVertices[i] = aiVector3D(vertex.position.x, vertex.position.y, vertex.position.z) * scaling; + mesh.mNormals[i] = aiVector3D(vertex.normal.x, vertex.normal.y, vertex.normal.z); + if (withTexCoords) + { + mesh.mTextureCoords[0][i] = aiVector3D(vertex.textureCoordinates.x, vertex.textureCoordinates.y, 0.0f); + } + } + + mesh.mNumFaces = geometry->indexCount / 3; + mesh.mFaces = new aiFace[mesh.mNumFaces]; + indices = new unsigned int[geometry->indexCount]; + + for (unsigned int i = 0; i < geometry->indexCount; ++i) + { + indices[i] = geometry->GetIndex(i); + } + + for (unsigned int i = 0; i < mesh.mNumFaces; ++i) + { + mesh.mFaces[i].mNumIndices = 3; + mesh.mFaces[i].mIndices = &indices[i * 3]; + } + + scene.mNumMeshes = 1; + scene.mMeshes = new aiMesh*[1] { &mesh }; + + scene.mNumMaterials = 1; + scene.mMaterials = new aiMaterial*[1] { new aiMaterial() }; + + rootNode.mNumMeshes = 1; + rootNode.mMeshes = new unsigned int[1] { 0 }; + } +#endif +} + namespace OpenVulkano::Scene { void MeshWriter::WriteAsOBJ(Geometry* geometry, const std::string& filePath) @@ -90,83 +166,15 @@ namespace OpenVulkano::Scene } } -#if __has_include("assimp/Exporter.hpp") - void MeshWriter::SetupAssimpScene(Geometry* geometry, aiScene& scene, aiNode& rootNode, aiMesh& mesh, - bool withTexCoords, float scaling) - { - mesh.mVertices = nullptr; - mesh.mNormals = nullptr; - mesh.mTangents = nullptr; - mesh.mBitangents = nullptr; - mesh.mFaces = nullptr; - for(unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) { - mesh.mTextureCoords[i] = nullptr; - mesh.mNumUVComponents[i] = 0; - } - for(unsigned int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i) { - mesh.mColors[i] = nullptr; - } - - rootNode.mName = aiString("RootNode"); - rootNode.mChildren = nullptr; - rootNode.mNumChildren = 0; - scene.mRootNode = &rootNode; - - mesh.mName = aiString("Mesh"); - mesh.mNumVertices = geometry->vertexCount; - mesh.mMaterialIndex = 0; - mesh.mPrimitiveTypes = aiPrimitiveType_TRIANGLE; - - mesh.mVertices = new aiVector3D[geometry->vertexCount]; - mesh.mNormals = new aiVector3D[geometry->vertexCount]; - - if (withTexCoords) - { - mesh.mNumUVComponents[0] = 2; - mesh.mTextureCoords[0] = new aiVector3D[geometry->vertexCount]; - } - - for (int i = 0; i < geometry->vertexCount; ++i) - { - const Vertex& vertex = geometry->vertices[i]; - mesh.mVertices[i] = aiVector3D(vertex.position.x, vertex.position.y, vertex.position.z) * scaling; - mesh.mNormals[i] = aiVector3D(vertex.normal.x, vertex.normal.y, vertex.normal.z); - if (withTexCoords) - { - mesh.mTextureCoords[0][i] = aiVector3D(vertex.textureCoordinates.x, vertex.textureCoordinates.y, 0.0f); - } - } - - mesh.mNumFaces = geometry->indexCount / 3; - mesh.mFaces = new aiFace[mesh.mNumFaces]; - - for (unsigned int i = 0; i < mesh.mNumFaces; ++i) - { - mesh.mFaces[i].mNumIndices = 3; - mesh.mFaces[i].mIndices = new unsigned int[3]; - mesh.mFaces[i].mIndices[0] = geometry->GetIndex(i * 3 + 0); - mesh.mFaces[i].mIndices[1] = geometry->GetIndex(i * 3 + 1); - mesh.mFaces[i].mIndices[2] = geometry->GetIndex(i * 3 + 2); - } - - scene.mNumMeshes = 1; - scene.mMeshes = new aiMesh*[1] { &mesh }; - - scene.mNumMaterials = 1; - scene.mMaterials = new aiMaterial*[1] { new aiMaterial() }; - - rootNode.mNumMeshes = 1; - rootNode.mMeshes = new unsigned int[1] { 0 }; - } - void MeshWriter::WriteAsSTL(Geometry* geometry, const std::string& filePath, bool binary) { #if __has_include("assimp/Exporter.hpp") std::unique_ptr scene(new aiScene()); std::unique_ptr rootNode(new aiNode()); std::unique_ptr mesh(new aiMesh()); + unsigned int* indices = nullptr; - SetupAssimpScene(geometry, *scene, *rootNode, *mesh, false, 1.0f); + SetupAssimpScene(geometry, *scene, *rootNode, *mesh, indices, false, 1.0f); Assimp::Exporter exporter; const char* formatId = binary ? "stlb" : "stl"; @@ -174,6 +182,12 @@ namespace OpenVulkano::Scene scene->mRootNode = nullptr; scene->mMeshes = nullptr; + for (uint32_t i = 0; i < mesh->mNumFaces; ++i) + { + aiFace& face = mesh->mFaces[i]; + face.mIndices = nullptr; + } + delete[] indices; if (result != aiReturn_SUCCESS) { @@ -190,8 +204,9 @@ namespace OpenVulkano::Scene std::unique_ptr scene(new aiScene()); std::unique_ptr rootNode(new aiNode()); std::unique_ptr mesh(new aiMesh()); + unsigned int* indices = nullptr; - SetupAssimpScene(geometry, *scene, *rootNode, *mesh, true, 100.0f); + SetupAssimpScene(geometry, *scene, *rootNode, *mesh, indices, true, 100.0f); if (!texturePath.empty()) { @@ -204,6 +219,12 @@ namespace OpenVulkano::Scene scene->mRootNode = nullptr; scene->mMeshes = nullptr; + for (uint32_t i = 0; i < mesh->mNumFaces; ++i) + { + aiFace& face = mesh->mFaces[i]; + face.mIndices = nullptr; + } + delete[] indices; if (result != aiReturn_SUCCESS) { @@ -213,5 +234,4 @@ namespace OpenVulkano::Scene throw std::runtime_error("Unable to convert the scene to FBX: Assimp is not available!"); #endif } -#endif } \ No newline at end of file diff --git a/openVulkanoCpp/Scene/Export/MeshWriter.hpp b/openVulkanoCpp/Scene/Export/MeshWriter.hpp index 0439dff..3b9255b 100644 --- a/openVulkanoCpp/Scene/Export/MeshWriter.hpp +++ b/openVulkanoCpp/Scene/Export/MeshWriter.hpp @@ -8,10 +8,6 @@ #include -#if __has_include("assimp/Exporter.hpp") -#include -#endif - namespace OpenVulkano::Scene { class Geometry; @@ -24,10 +20,5 @@ namespace OpenVulkano::Scene static void WriteAsUSDZ(Geometry* geometry, const std::string& texturePath, const std::string& usdzPath); static void WriteAsFBX(Geometry* geometry, const std::string& texturePath, const std::string& fbxPath); static void WriteAsSTL(Geometry* geometry, const std::string& filePath, bool binary); - private: -#if __has_include("assimp/Exporter.hpp") - static void SetupAssimpScene(Geometry* geometry, aiScene& scene, aiNode& rootNode, aiMesh& mesh, - bool withTexCoords, float scaling); -#endif }; } \ No newline at end of file From 3d1be5dfede6db975d599ce4f3951d74c33faf4c Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Mon, 10 Feb 2025 15:42:41 +0200 Subject: [PATCH 4/4] Using unique ptr for indices --- openVulkanoCpp/Scene/Export/MeshWriter.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/openVulkanoCpp/Scene/Export/MeshWriter.cpp b/openVulkanoCpp/Scene/Export/MeshWriter.cpp index cc74c18..17054ce 100644 --- a/openVulkanoCpp/Scene/Export/MeshWriter.cpp +++ b/openVulkanoCpp/Scene/Export/MeshWriter.cpp @@ -27,8 +27,8 @@ namespace { #if __has_include("assimp/Exporter.hpp") - void SetupAssimpScene(OpenVulkano::Scene::Geometry* geometry, aiScene& scene, aiNode& rootNode, aiMesh& mesh, unsigned int*& indices, - bool withTexCoords, float scaling) + void SetupAssimpScene(OpenVulkano::Scene::Geometry* geometry, aiScene& scene, aiNode& rootNode, aiMesh& mesh, + std::unique_ptr& indices, bool withTexCoords, float scaling) { mesh.mVertices = nullptr; mesh.mNormals = nullptr; @@ -75,7 +75,7 @@ namespace mesh.mNumFaces = geometry->indexCount / 3; mesh.mFaces = new aiFace[mesh.mNumFaces]; - indices = new unsigned int[geometry->indexCount]; + indices = std::make_unique(geometry->indexCount); for (unsigned int i = 0; i < geometry->indexCount; ++i) { @@ -172,7 +172,7 @@ namespace OpenVulkano::Scene std::unique_ptr scene(new aiScene()); std::unique_ptr rootNode(new aiNode()); std::unique_ptr mesh(new aiMesh()); - unsigned int* indices = nullptr; + std::unique_ptr indices; SetupAssimpScene(geometry, *scene, *rootNode, *mesh, indices, false, 1.0f); @@ -187,7 +187,6 @@ namespace OpenVulkano::Scene aiFace& face = mesh->mFaces[i]; face.mIndices = nullptr; } - delete[] indices; if (result != aiReturn_SUCCESS) { @@ -204,7 +203,7 @@ namespace OpenVulkano::Scene std::unique_ptr scene(new aiScene()); std::unique_ptr rootNode(new aiNode()); std::unique_ptr mesh(new aiMesh()); - unsigned int* indices = nullptr; + std::unique_ptr indices; SetupAssimpScene(geometry, *scene, *rootNode, *mesh, indices, true, 100.0f); @@ -224,7 +223,6 @@ namespace OpenVulkano::Scene aiFace& face = mesh->mFaces[i]; face.mIndices = nullptr; } - delete[] indices; if (result != aiReturn_SUCCESS) {