Updated tests, changed model encoders to output content to a stream

This commit is contained in:
Vladyslav Baranovskyi
2024-11-25 19:34:05 +02:00
parent 7ede43d1c2
commit ba8574f537
4 changed files with 113 additions and 110 deletions

View File

@@ -24,8 +24,8 @@ namespace OpenVulkano::Scene
if (!file.is_open()) if (!file.is_open())
throw std::runtime_error("Failed to open file '" + filePath + "' for writing!"); throw std::runtime_error("Failed to open file '" + filePath + "' for writing!");
auto [objContents, mtlContents] = GetObjContents(geometry, ""); std::stringstream dummy;
file << objContents; WriteObjContents(geometry, "", file, dummy);
file.close(); file.close();
} }
@@ -34,8 +34,7 @@ namespace OpenVulkano::Scene
std::ofstream file(filePath); std::ofstream file(filePath);
if (!file.is_open()) if (!file.is_open())
throw std::runtime_error("Failed to open file '" + filePath + "' for writing!"); throw std::runtime_error("Failed to open file '" + filePath + "' for writing!");
std::string scene = GetUsdContents(geometry); WriteUsdContents(file, geometry);
file << scene << "\n";
file.close(); file.close();
} }
@@ -43,13 +42,14 @@ namespace OpenVulkano::Scene
{ {
OpenVulkano::ArchiveWriter zipWriter(zipPath.c_str()); OpenVulkano::ArchiveWriter zipWriter(zipPath.c_str());
auto [objContents, mtlContents] = GetObjContents(geometry, texturePath); std::stringstream objContents, mtlContents;
WriteObjContents(geometry, texturePath, objContents, mtlContents);
auto objDesc = OpenVulkano::FileDescription::MakeDescriptionForFile("model.obj", objContents.size()); auto objDesc = OpenVulkano::FileDescription::MakeDescriptionForFile("model.obj", objContents.str().size());
zipWriter.AddFile(objDesc, objContents.data()); zipWriter.AddFile(objDesc, objContents.str().data());
auto mtlDesc = OpenVulkano::FileDescription::MakeDescriptionForFile("material.mtl", mtlContents.size()); auto mtlDesc = OpenVulkano::FileDescription::MakeDescriptionForFile("material.mtl", mtlContents.str().size());
zipWriter.AddFile(mtlDesc, mtlContents.data()); zipWriter.AddFile(mtlDesc, mtlContents.str().data());
if (!texturePath.empty() && std::filesystem::exists(texturePath)) if (!texturePath.empty() && std::filesystem::exists(texturePath))
{ {
@@ -63,9 +63,10 @@ namespace OpenVulkano::Scene
{ {
OpenVulkano::ZipWriter zipWriter(usdzPath); OpenVulkano::ZipWriter zipWriter(usdzPath);
std::string usd = GetUsdContents(geometry, texturePath); std::stringstream usdFile;
auto usdDesc = OpenVulkano::FileDescription::MakeDescriptionForFile("geometry.usda", usd.size()); WriteUsdContents(usdFile, geometry);
zipWriter.AddFile(usdDesc, usd.data()); auto usdDesc = OpenVulkano::FileDescription::MakeDescriptionForFile("geometry.usda", usdFile.str().size());
zipWriter.AddFile(usdDesc, usdFile.str().data());
if (!texturePath.empty() && std::filesystem::exists(texturePath)) if (!texturePath.empty() && std::filesystem::exists(texturePath))
{ {
@@ -73,7 +74,5 @@ namespace OpenVulkano::Scene
auto texDesc = OpenVulkano::FileDescription::MakeDescriptionForFile("texture.png", textureFile.Size()); auto texDesc = OpenVulkano::FileDescription::MakeDescriptionForFile("texture.png", textureFile.Size());
zipWriter.AddFile(texDesc, textureFile.Data()); zipWriter.AddFile(texDesc, textureFile.Data());
} }
zipWriter.Close();
} }
} }

View File

@@ -14,30 +14,22 @@
namespace OpenVulkano::Scene namespace OpenVulkano::Scene
{ {
// Returns [objContents, mtlContents] void WriteObjContents(Geometry* geometry, const std::string& texturePath, std::ostream& objContent, std::ostream& mtlContent)
std::pair<std::string, std::string> GetObjContents(Geometry* geometry, const std::string& texturePath)
{ {
std::pair<std::string, std::string> result;
bool useTexture = texturePath.size() != 0; bool useTexture = texturePath.size() != 0;
std::stringstream objContent;
objContent << "# OBJ file generated by OpenVulkanoCpp\n"; objContent << "# OBJ file generated by OpenVulkanoCpp\n";
if (useTexture) if (useTexture)
{ {
std::stringstream mtlContent; mtlContent << R"(newmtl Material0
std::string materialName = "Material0";
mtlContent << "newmtl " << materialName << R"(
Ka 1.000 1.000 1.000 Ka 1.000 1.000 1.000
Kd 1.000 1.000 1.000 Kd 1.000 1.000 1.000
Ks 0.000 0.000 0.000 Ks 0.000 0.000 0.000
map_Ka texture.png map_Ka texture.png
map_Kd texture.png map_Kd texture.png
)"; )";
objContent << "mtllib material.mtl\nusemtl " << materialName << "\n"; objContent << "mtllib material.mtl\nusemtl Material0\n";
result.second = mtlContent.str();
} }
for (int i = 0; i < geometry->vertexCount; ++i) for (int i = 0; i < geometry->vertexCount; ++i)
@@ -62,9 +54,5 @@ map_Kd texture.png
uint32_t i2 = geometry->GetIndex(i + 2) + 1; uint32_t i2 = geometry->GetIndex(i + 2) + 1;
objContent << fmt::format("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}\n", i0, i1, i2); objContent << fmt::format("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}\n", i0, i1, i2);
} }
result.first = objContent.str();
return result;
} }
} }

View File

@@ -11,56 +11,9 @@
namespace OpenVulkano::Scene namespace OpenVulkano::Scene
{ {
std::string GetUsdContents(OpenVulkano::Scene::Geometry* geometry, const std::string texturePath = "") void WriteUsdContents(std::ostream& output, OpenVulkano::Scene::Geometry* geometry)
{ {
std::ostringstream result; output << R"(#usda 1.0
std::ostringstream points, normals, indices, texCoords, faceCounts;
points << std::fixed << std::setprecision(6);
normals << std::fixed << std::setprecision(6);
for (size_t i = 0; i < geometry->vertexCount; ++i)
{
const auto& v = geometry->vertices[i];
points << "(" << v.position.x << ", " << v.position.y << ", " << v.position.z << ")";
normals << "(" << v.normal.x << ", " << v.normal.y << ", " << v.normal.z << ")";
if (i < geometry->vertexCount - 1)
{
points << ", ";
normals << ", ";
}
}
for (size_t i = 0; i < geometry->indexCount; ++i)
{
indices << geometry->GetIndex(i);
if (i < geometry->indexCount - 1)
{
indices << ", ";
}
if ((i + 1) % 3 == 0)
{
faceCounts << "3";
if (i < geometry->indexCount - 1)
{
faceCounts << ", ";
}
}
}
texCoords << std::fixed << std::setprecision(6);
for (size_t i = 0; i < geometry->indexCount; ++i)
{
const size_t vertexIndex = geometry->GetIndex(i);
const auto& v = geometry->vertices[vertexIndex];
texCoords << "(" << v.textureCoordinates.x << ", " << v.textureCoordinates.y << ")";
if (i < geometry->indexCount - 1)
{
texCoords << ", ";
}
}
result << R"(#usda 1.0
( (
defaultPrim = "root" defaultPrim = "root"
doc = "Exported from OpenVulkano" doc = "Exported from OpenVulkano"
@@ -91,19 +44,79 @@ def Xform "root" (
{ {
uniform bool doubleSided = 1 uniform bool doubleSided = 1
float3[] extent = [(-0.5, -0.5, 0), (0.5, 0.5, 0)] float3[] extent = [(-0.5, -0.5, 0), (0.5, 0.5, 0)]
int[] faceVertexCounts = [)" int[] faceVertexCounts = [)";
<< faceCounts.str() << R"(]
int[] faceVertexIndices = [)" for (size_t i = 0; i < geometry->indexCount; ++i)
<< indices.str() << R"(] {
if ((i + 1) % 3 == 0)
{
output << "3";
if (i < geometry->indexCount - 1)
{
output << ", ";
}
}
}
output << R"(]
int[] faceVertexIndices = [)";
for (size_t i = 0; i < geometry->indexCount; ++i)
{
output << geometry->GetIndex(i);
if (i < geometry->indexCount - 1)
{
output << ", ";
}
}
output << R"(]
rel material:binding = </root/_materials/Material0> rel material:binding = </root/_materials/Material0>
normal3f[] normals = [)" normal3f[] normals = [)";
<< normals.str() << R"(] (
output << std::fixed << std::setprecision(6);
for (size_t i = 0; i < geometry->vertexCount; ++i)
{
const auto& v = geometry->vertices[i];
output << "(" << v.normal.x << ", " << v.normal.y << ", " << v.normal.z << ")";
if (i < geometry->vertexCount - 1)
{
output << ", ";
}
}
output << R"(] (
interpolation = "faceVarying" interpolation = "faceVarying"
) )
point3f[] points = [)" point3f[] points = [)";
<< points.str() << R"(]
texCoord2f[] primvars:st = [)" for (size_t i = 0; i < geometry->vertexCount; ++i)
<< texCoords.str() << R"(] ( {
const auto& v = geometry->vertices[i];
output << "(" << v.position.x << ", " << v.position.y << ", " << v.position.z << ")";
if (i < geometry->vertexCount - 1)
{
output << ", ";
}
}
output << R"(]
texCoord2f[] primvars:st = [)";
output << std::fixed << std::setprecision(6);
for (size_t i = 0; i < geometry->indexCount; ++i)
{
const size_t vertexIndex = geometry->GetIndex(i);
const auto& v = geometry->vertices[vertexIndex];
output << "(" << v.textureCoordinates.x << ", " << v.textureCoordinates.y << ")";
if (i < geometry->indexCount - 1)
{
output << ", ";
}
}
output << R"(] (
interpolation = "faceVarying" interpolation = "faceVarying"
) )
uniform token subdivisionScheme = "none" uniform token subdivisionScheme = "none"
@@ -153,6 +166,5 @@ def Xform "root" (
} }
} }
)"; )";
return result.str();
} }
} }

View File

@@ -19,8 +19,9 @@ using namespace OpenVulkano;
TEST_CASE("Empty zip file", "[ZipWriter]") TEST_CASE("Empty zip file", "[ZipWriter]")
{ {
const auto emptyZipPath = AppFolders::GetAppTempDir() / "empty.zip"; const auto emptyZipPath = AppFolders::GetAppTempDir() / "empty.zip";
ZipWriter writer(emptyZipPath); {
writer.Close(); ZipWriter writer(emptyZipPath);
}
auto mem = Utils::ReadFile(emptyZipPath); auto mem = Utils::ReadFile(emptyZipPath);
const int expectSize = 22; const int expectSize = 22;
Array<uint8_t> expect = {0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; Array<uint8_t> expect = {0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
@@ -34,15 +35,17 @@ TEST_CASE("Empty zip file", "[ZipWriter]")
TEST_CASE("Zip with one file(AAA.txt that has 'AAA')", "[ZipWriter]") TEST_CASE("Zip with one file(AAA.txt that has 'AAA')", "[ZipWriter]")
{ {
const auto oneFileZipPath = AppFolders::GetAppTempDir() / "one_file.zip"; const auto oneFileZipPath = AppFolders::GetAppTempDir() / "one_file.zip";
ZipWriter writer(oneFileZipPath);
{
ZipWriter writer(oneFileZipPath);
FileDescription desc = FileDescription::MakeDescriptionForFile("AAA.txt", 3); FileDescription desc = FileDescription::MakeDescriptionForFile("AAA.txt", 3);
desc.modTime = {}; desc.modTime = {};
desc.createTime = {}; desc.createTime = {};
char buffer[] = {'A', 'A', 'A'}; char buffer[] = {'A', 'A', 'A'};
writer.AddFile(desc, buffer); writer.AddFile(desc, buffer);
writer.Close(); }
auto mem = Utils::ReadFile(oneFileZipPath); auto mem = Utils::ReadFile(oneFileZipPath);
@@ -67,21 +70,22 @@ TEST_CASE("Zip with one file(AAA.txt that has 'AAA')", "[ZipWriter]")
TEST_CASE("Zip with two files(AAA.txt that has 'AAA', BBB.bin that has 'BBB')", "[ZipWriter]") TEST_CASE("Zip with two files(AAA.txt that has 'AAA', BBB.bin that has 'BBB')", "[ZipWriter]")
{ {
const auto twoFilesZipPath = AppFolders::GetAppTempDir() / "two_files.zip"; const auto twoFilesZipPath = AppFolders::GetAppTempDir() / "two_files.zip";
ZipWriter writer(twoFilesZipPath); {
ZipWriter writer(twoFilesZipPath);
FileDescription aaa = FileDescription::MakeDescriptionForFile("AAA.txt", 3); FileDescription aaa = FileDescription::MakeDescriptionForFile("AAA.txt", 3);
aaa.modTime = {}; aaa.modTime = {};
aaa.createTime = {}; aaa.createTime = {};
char aaaBuffer[] = {'A', 'A', 'A'}; char aaaBuffer[] = {'A', 'A', 'A'};
FileDescription bbb = FileDescription::MakeDescriptionForFile("BBB.bin", 3); FileDescription bbb = FileDescription::MakeDescriptionForFile("BBB.bin", 3);
bbb.modTime = {}; bbb.modTime = {};
bbb.createTime = {}; bbb.createTime = {};
char bbbBuffer[] = {'B', 'B', 'B'}; char bbbBuffer[] = {'B', 'B', 'B'};
writer.AddFile(aaa, aaaBuffer); writer.AddFile(aaa, aaaBuffer);
writer.AddFile(bbb, bbbBuffer); writer.AddFile(bbb, bbbBuffer);
writer.Close(); }
auto mem = Utils::ReadFile(twoFilesZipPath); auto mem = Utils::ReadFile(twoFilesZipPath);