Files
OpenVulkano/openVulkanoCpp/Scene/MeshWriter.cpp
Vladyslav Baranovskyi 34bfebbdd3 Summary:
- Changes in Cat functions
- Using gmtime_s and gmtime_r as a thread-safe functions
- ZFill() that is used for padding
- Option for ZipWriter to pad LocalFileHeaders(it is used to properly run tests)
- ZipWriter::IsOpen()
- Moved material creation away from WriteObjContents
- Using MemMappedFile instead of ReadFile
- Scoping files and adding them to archive
- UsdEncoder refactoring
2024-11-27 20:21:14 +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 "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 <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!");
WriteObjContents(geometry, "", file);
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!");
WriteUsdContents(file, geometry);
file.close();
}
void MeshWriter::WriteObjAsZip(Geometry* geometry, const std::string& texturePath, const std::string& zipPath)
{
OpenVulkano::ArchiveWriter zipWriter(zipPath.c_str());
const char* materialName = "Material0";
{
std::stringstream objContents;
WriteObjContents(geometry, materialName, objContents);
auto objContentsStr = objContents.str();
auto objDesc = OpenVulkano::FileDescription::MakeDescriptionForFile("model.obj", objContentsStr.size());
zipWriter.AddFile(objDesc, objContentsStr.data());
}
{
std::stringstream mtlContents;
mtlContents << "newmtl " << materialName << "\n" << ObjMaterialContents;
auto mtlContentsStr = mtlContents.str();
auto mtlDesc = OpenVulkano::FileDescription::MakeDescriptionForFile("material.mtl", mtlContentsStr.size());
zipWriter.AddFile(mtlDesc, mtlContentsStr.data());
}
if (!texturePath.empty() && std::filesystem::exists(texturePath))
{
auto textureFile = MemMappedFile(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);// NOCHECKIN, true);
{
std::stringstream usdFile;
WriteUsdContents(usdFile, geometry);
auto usdFileStr = usdFile.str();
auto usdDesc = OpenVulkano::FileDescription::MakeDescriptionForFile("geometry.usda", usdFileStr.size());
zipWriter.AddFile(usdDesc, usdFileStr.data());
}
if (!texturePath.empty() && std::filesystem::exists(texturePath))
{
auto textureFile = MemMappedFile(texturePath);
auto texDesc = OpenVulkano::FileDescription::MakeDescriptionForFile("texture.png", textureFile.Size());
zipWriter.AddFile(texDesc, textureFile.Data());
}
}
}