Files
OpenVulkano/openVulkanoCpp/Scene/MeshWriter.cpp
Vladyslav Baranovskyi ac843c0fe3 Summary:
- Removed ReadEntireFile
- ZipWriter now writes directly to a file when possible
- Added GetIndex to geometry
- Moved Usd and Obj generators to different files
- Removed unused procedures
- Deduplicated obj generators
- Updated tests for ZipWriter
2024-11-22 15:45:57 +02:00

88 lines
2.9 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>
#include <tinyusdz.hh>
#include <pprinter.hh>
#include <prim-pprint.hh>
#include <value-pprint.hh>
#include <tiny-format.hh>
#include <tiny-variant.hh>
#include <usda-reader.hh>
#include <usda-writer.hh>
#include <usdc-reader.hh>
#include <usdc-writer.hh>
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& zipPath, const std::string& texturePath)
{
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& usdzPath, const std::string& texturePath)
{
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();
}
}