/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ #include "MeshWriter.hpp" #include "IO/MemMappedFile.hpp" #include "Scene/Geometry.hpp" #include "Scene/Vertex.hpp" #include "Scene/UsdEncoder.hpp" #include "Scene/ObjEncoder.hpp" #include "IO/Archive/ArchiveWriter.hpp" #include "IO/Archive/ZipWriter.hpp" #include #include namespace OpenVulkano::Scene { void MeshWriter::WriteAsOBJ(Geometry* geometry, const std::string& filePath) { std::ofstream file(filePath); if (!file.is_open()) [[unlikely]] throw std::runtime_error("Failed to open file '" + filePath + "' for writing!"); WriteObjContents(geometry, "", file); file.close(); } void MeshWriter::WriteAsUSD(Geometry* geometry, const std::string& filePath) { std::ofstream file(filePath); if (!file.is_open()) [[unlikely]] throw std::runtime_error("Failed to open file '" + filePath + "' for writing!"); WriteUsdContents(file, geometry); file.close(); } void MeshWriter::WriteObjAsZip(Geometry* geometry, const std::string& texturePath, const std::string& zipPath) { OpenVulkano::ArchiveWriter zipWriter(zipPath.c_str()); { std::stringstream objContents; WriteObjContents(geometry, DEFAULT_OBJ_MATERIAL_NAME, objContents); std::string objContentsStr = objContents.str(); FileDescription objDesc = FileDescription::MakeDescriptionForFile("model.obj", objContentsStr.size()); zipWriter.AddFile(objDesc, objContentsStr.data()); } { FileDescription mtlDesc = FileDescription::MakeDescriptionForFile("material.mtl", DEFAULT_OBJ_MATERIAL_CONTENTS.size()); zipWriter.AddFile(mtlDesc, DEFAULT_OBJ_MATERIAL_CONTENTS.data()); } if (!texturePath.empty() && std::filesystem::exists(texturePath)) { MemMappedFile textureFile(texturePath); FileDescription texDesc = FileDescription::MakeDescriptionForFile("texture.png", textureFile.Size()); zipWriter.AddFile(texDesc, textureFile.Data()); } } void MeshWriter::WriteAsUSDZ(Geometry* geometry, const std::string& texturePath, const std::string& usdzPath) { OpenVulkano::ZipWriter zipWriter(usdzPath, true); { std::stringstream usdFile; WriteUsdContents(usdFile, geometry); std::string usdFileStr = usdFile.str(); FileDescription usdDesc = FileDescription::MakeDescriptionForFile("geometry.usda", usdFileStr.size()); zipWriter.AddFile(usdDesc, usdFileStr.data()); } if (!texturePath.empty() && std::filesystem::exists(texturePath)) { MemMappedFile textureFile(texturePath); FileDescription texDesc = FileDescription::MakeDescriptionForFile("texture.png", textureFile.Size()); zipWriter.AddFile(texDesc, textureFile.Data()); } } }