Files
OpenVulkano/openVulkanoCpp/Scene/MeshWriter.cpp

79 lines
2.6 KiB
C++

/*
* 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 "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 <fstream>
#include <fmt/core.h>
namespace OpenVulkano::Scene
{
void MeshWriter::WriteAsOBJ(Geometry* geometry, const std::string& filePath)
{
std::ofstream file(filePath);
if (!file.is_open())
throw std::runtime_error("Failed to open file '" + filePath + "' for writing!");
auto [objContents, mtlContents] = GetObjContents(geometry, "");
file << objContents;
file.close();
}
void MeshWriter::WriteAsUSD(Geometry* geometry, const std::string& filePath)
{
std::ofstream file(filePath);
if (!file.is_open())
throw std::runtime_error("Failed to open file '" + filePath + "' for writing!");
std::string scene = GetUsdContents(geometry);
file << scene << "\n";
file.close();
}
void MeshWriter::WriteObjAsZip(Geometry* geometry, const std::string& texturePath, const std::string& zipPath)
{
OpenVulkano::ArchiveWriter zipWriter(zipPath.c_str());
auto [objContents, mtlContents] = GetObjContents(geometry, texturePath);
auto objDesc = OpenVulkano::FileDescription::MakeDescriptionForFile("model.obj", objContents.size());
zipWriter.AddFile(objDesc, objContents.data());
auto mtlDesc = OpenVulkano::FileDescription::MakeDescriptionForFile("material.mtl", mtlContents.size());
zipWriter.AddFile(mtlDesc, mtlContents.data());
if (!texturePath.empty() && std::filesystem::exists(texturePath))
{
auto textureFile = Utils::ReadFile(texturePath);
auto texDesc = OpenVulkano::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);
std::string usd = GetUsdContents(geometry, texturePath);
auto usdDesc = OpenVulkano::FileDescription::MakeDescriptionForFile("geometry.usda", usd.size());
zipWriter.AddFile(usdDesc, usd.data());
if (!texturePath.empty() && std::filesystem::exists(texturePath))
{
auto textureFile = Utils::ReadFile(texturePath);
auto texDesc = OpenVulkano::FileDescription::MakeDescriptionForFile("texture.png", textureFile.Size());
zipWriter.AddFile(texDesc, textureFile.Data());
}
zipWriter.Close();
}
}