Code style changes, minor reorderings

This commit is contained in:
Vladyslav Baranovskyi
2024-09-09 21:52:17 +03:00
parent fa3a7a7fb7
commit 83c3a775ee
2 changed files with 37 additions and 36 deletions

View File

@@ -7,16 +7,16 @@
#include "MeshWriter.hpp" #include "MeshWriter.hpp"
#include "Scene/Geometry.hpp" #include "Scene/Geometry.hpp"
#include "Scene/Vertex.hpp" #include "Scene/Vertex.hpp"
#include <fmt/core.h>
#include <fstream> #include <fstream>
#include "tinyusdz.hh" #include <fmt/core.h>
#include "pprinter.hh" #include <tinyusdz.hh>
#include "prim-pprint.hh" #include <pprinter.hh>
#include "value-pprint.hh" #include <prim-pprint.hh>
#include <value-pprint.hh>
namespace namespace
{ {
uint32_t GetIndexFromGeometry(OpenVulkano::Scene::Geometry *geometry, int index) uint32_t GetIndexFromGeometry(OpenVulkano::Scene::Geometry* geometry, int index)
{ {
uint32_t result = 0; uint32_t result = 0;
@@ -41,47 +41,54 @@ namespace
namespace OpenVulkano::Scene namespace OpenVulkano::Scene
{ {
void MeshWriter::WriteAsOBJ(Geometry *geometry, const std::string &filePath) void MeshWriter::WriteAsOBJ(Geometry* geometry, const std::string& filePath)
{ {
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!");
// Vertices
for (int i = 0; i < geometry->vertexCount; ++i) for (int i = 0; i < geometry->vertexCount; ++i)
{ {
OpenVulkano::Vertex v = geometry->vertices[i]; const OpenVulkano::Vertex& v = geometry->vertices[i];
std::string line; std::string line;
line = fmt::format("v {} {} {}\n", v.position.x, v.position.y, v.position.z); line = fmt::format("v {} {} {}\n", v.position.x, v.position.y, v.position.z);
file << line; file << line;
line = fmt::format("vn {} {} {}\n", v.normal.x, v.normal.y, v.normal.z);
file << line;
line = fmt::format("vt {} {}\n", v.textureCoordinates.x, v.textureCoordinates.y);
file << line;
// NOTE(vb): There aren't specd way to encode colors...
} }
// Normals
for (int i = 0; i < geometry->vertexCount; ++i)
{
const OpenVulkano::Vertex& v = geometry->vertices[i];
std::string line;
line = fmt::format("vn {} {} {}\n", v.normal.x, v.normal.y, v.normal.z);
file << line;
}
// TexCoords
for (int i = 0; i < geometry->vertexCount; ++i)
{
const OpenVulkano::Vertex& v = geometry->vertices[i];
std::string line;
line = fmt::format("vt {} {}\n", v.textureCoordinates.x, v.textureCoordinates.y);
file << line;
}
// Indices
for (int i = 0; i < geometry->indexCount; i += 3) for (int i = 0; i < geometry->indexCount; i += 3)
{ {
uint32_t i0 = GetIndexFromGeometry(geometry, i + 0) + 1; uint32_t i0 = GetIndexFromGeometry(geometry, i + 0) + 1;
uint32_t i1 = GetIndexFromGeometry(geometry, i + 1) + 1; uint32_t i1 = GetIndexFromGeometry(geometry, i + 1) + 1;
uint32_t i2 = GetIndexFromGeometry(geometry, i + 2) + 1; uint32_t i2 = GetIndexFromGeometry(geometry, i + 2) + 1;
std::string line = fmt::format("f {}/{}/{} {}/{}/{} {}/{}/{}\n", std::string line = fmt::format("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}\n", i0, i1, i2);
i0, i0, i0,
i1, i1, i1,
i2, i2, i2
);
file << line; file << line;
} }
file.close(); file.close();
} }
void MeshWriter::WriteAsUSD(Geometry *geometry, const std::string &filePath) void MeshWriter::WriteAsUSD(Geometry* geometry, const std::string& filePath)
{ {
tinyusdz::Stage stage; tinyusdz::Stage stage;
tinyusdz::Xform xform; tinyusdz::Xform xform;
@@ -95,23 +102,17 @@ namespace OpenVulkano::Scene
for (int i = 0; i < geometry->vertexCount; ++i) for (int i = 0; i < geometry->vertexCount; ++i)
{ {
Vertex v = geometry->vertices[i]; const Vertex& v = geometry->vertices[i];
tinyusdz::value::point3f p; pts[i].x = v.position.x;
p.x = v.position.x; pts[i].y = v.position.y;
p.y = v.position.y; pts[i].z = v.position.z;
p.z = v.position.z;
pts[i] = p;
uvs[i] = { v.textureCoordinates.x, v.textureCoordinates.y }; uvs[i] = { v.textureCoordinates.x, v.textureCoordinates.y };
} }
mesh.points.set_value(pts); mesh.points.set_value(pts);
uvAttr.set_value(uvs); uvAttr.set_value(uvs);
std::vector<int> counts; std::vector<int> counts(geometry->indexCount / 3, 3); // NOTE(vb): The value 3 is kind of arbitrary, but this array must be in the mesh!
for (int i = 0; i < geometry->indexCount / 3; ++i)
{
counts.push_back(3); // NOTE(vb): This value is kind of arbitrary, but the array must be in the mesh!
}
mesh.faceVertexCounts.set_value(counts); mesh.faceVertexCounts.set_value(counts);
for (int i = 0; i < geometry->indexCount; ++i) for (int i = 0; i < geometry->indexCount; ++i)

View File

@@ -14,7 +14,7 @@ namespace OpenVulkano::Scene
class MeshWriter class MeshWriter
{ {
public: public:
static void WriteAsOBJ(Geometry *geometry, const std::string &filePath); static void WriteAsOBJ(Geometry* geometry, const std::string& filePath);
static void WriteAsUSD(Geometry *geometry, const std::string &filePath); static void WriteAsUSD(Geometry* geometry, const std::string& filePath);
}; };
} }