diff --git a/openVulkanoCpp/Scene/MeshWriter.cpp b/openVulkanoCpp/Scene/MeshWriter.cpp index 64ad450..b1fe6ea 100644 --- a/openVulkanoCpp/Scene/MeshWriter.cpp +++ b/openVulkanoCpp/Scene/MeshWriter.cpp @@ -15,13 +15,11 @@ #include "IO/Archive/ZipWriter.hpp" #include -#include #include #include #include #include #include -#include namespace OpenVulkano::Scene { @@ -91,51 +89,58 @@ namespace OpenVulkano::Scene void MeshWriter::WriteAsFBX(Geometry* geometry, const std::string& texturePath, const std::string& fbxPath) { + aiNode rootNode; + aiScene scene; - scene.mRootNode = new aiNode(); - - aiMesh* mesh = new aiMesh(); - mesh->mNumVertices = geometry->vertexCount; - mesh->mVertices = new aiVector3D[geometry->vertexCount]; - mesh->mNormals = new aiVector3D[geometry->vertexCount]; - mesh->mTextureCoords[0] = new aiVector3D[geometry->vertexCount]; - mesh->mMaterialIndex = 0; - mesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE; - mesh->mNumUVComponents[0] = 2; + scene.mRootNode = &rootNode; + aiMesh mesh; + mesh.mNumVertices = geometry->vertexCount; + mesh.mMaterialIndex = 0; + mesh.mPrimitiveTypes = aiPrimitiveType_TRIANGLE; + mesh.mNumUVComponents[0] = 2; + + mesh.mVertices = new aiVector3D[geometry->vertexCount]; + mesh.mNormals = new aiVector3D[geometry->vertexCount]; + mesh.mTextureCoords[0] = new aiVector3D[geometry->vertexCount]; + float scaling = 100; // fbx units are centimeters... for (uint32_t 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); + 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); } - mesh->mNumFaces = geometry->indexCount / 3; - mesh->mFaces = new aiFace[mesh->mNumFaces]; + mesh.mNumFaces = geometry->indexCount / 3; + mesh.mFaces = new aiFace[mesh.mNumFaces]; - for (uint32_t i = 0; i < mesh->mNumFaces; ++i) + for (uint32_t i = 0; i < mesh.mNumFaces; ++i) { - aiFace& face = mesh->mFaces[i]; + aiFace& face = mesh.mFaces[i]; face.mNumIndices = 3; - face.mIndices = new unsigned int[3]; + face.mIndices = new unsigned int[face.mNumIndices]; face.mIndices[0] = geometry->GetIndex(i * 3 + 0); face.mIndices[1] = geometry->GetIndex(i * 3 + 1); face.mIndices[2] = geometry->GetIndex(i * 3 + 2); } - scene.mMeshes = new aiMesh*[1]; - scene.mMeshes[0] = mesh; + aiMesh* meshes[1]; + scene.mMeshes = meshes; + scene.mMeshes[0] = &mesh; scene.mNumMeshes = 1; - scene.mRootNode->mMeshes = new unsigned int[1]; + + unsigned int meshIndices[1]; + scene.mRootNode->mMeshes = meshIndices; scene.mRootNode->mMeshes[0] = 0; scene.mRootNode->mNumMeshes = 1; - aiMaterial* material = new aiMaterial(); - scene.mMaterials = new aiMaterial*[1]; - scene.mMaterials[0] = material; + aiMaterial material; + aiMaterial* materials[1]; + scene.mMaterials = materials; + scene.mMaterials[0] = &material; scene.mNumMaterials = 1; aiString externalPath(texturePath); @@ -144,37 +149,10 @@ namespace OpenVulkano::Scene Assimp::Exporter exporter; aiReturn result = exporter.Export(&scene, "fbx", fbxPath); -#define SAFE_DELETE_ARRAY(arr) \ - do \ - { \ - if (arr) \ - delete[] arr; \ - arr = nullptr; \ - } while (0) - - SAFE_DELETE_ARRAY(mesh->mVertices); - SAFE_DELETE_ARRAY(mesh->mNormals); - SAFE_DELETE_ARRAY(mesh->mTextureCoords[0]); - - for (uint32_t i = 0; i < mesh->mNumFaces; ++i) - { - SAFE_DELETE_ARRAY(mesh->mFaces[i].mIndices); - } - - SAFE_DELETE_ARRAY(mesh->mFaces); - - delete mesh; - mesh = nullptr; - - delete material; - material = nullptr; - - SAFE_DELETE_ARRAY(scene.mMeshes); - SAFE_DELETE_ARRAY(scene.mRootNode->mMeshes); - SAFE_DELETE_ARRAY(scene.mMaterials); - delete scene.mRootNode; scene.mRootNode = nullptr; -#undef SAFE_DELETE_ARRAY + rootNode.mMeshes = nullptr; + scene.mMeshes = nullptr; + scene.mMaterials = nullptr; if (result != aiReturn_SUCCESS) {