src/mesh/io/obj.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "mesh/io/formats.h" | ||
| 4 | #include "util/text.h" | ||
| 5 | |||
| 6 | #define TINYOBJLOADER_IMPLEMENTATION | ||
| 7 | #include <tiny_obj_loader.h> | ||
| 8 | |||
| 9 | #include <set> | ||
| 10 | #include <stdexcept> | ||
| 11 | #include <string> | ||
| 12 | |||
| 13 | namespace terraingen::detail { | ||
| 14 | |||
| 15 | namespace { | ||
| 16 | |||
| 17 | /// `value` with 9 significant digits, negative zero written as 0. | ||
| 18 | 47913 | std::string Number(double value) { | |
| 19 | 49176 | return FormatNumber(value == 0.0 ? 0.0 : value, 9); | |
| 20 | } | ||
| 21 | |||
| 22 | /// An OBJ line of `tag` and a vector's components. | ||
| 23 | 15956 | std::string VectorLine(const char* tag, const Vec3d& v) { | |
| 24 | 79780 | return std::string(tag) + " " + Number(v.x) + " " + Number(v.y) + " " + Number(v.z) + "\n"; | |
| 25 | } | ||
| 26 | |||
| 27 | /// The MTL text of every distinct material of `meshes`, by name. | ||
| 28 | 15 | std::string Materials(const std::vector<SceneMesh>& meshes) { | |
| 29 | 15 | std::set<std::string> written; | |
| 30 | 15 | std::string mtl = "# TerrainGen\n"; | |
| 31 | 38 | for (const SceneMesh& scene : meshes) { | |
| 32 | 23 | const MeshMaterial& material = scene.material; | |
| 33 | 23 | if (!written.insert(material.name).second) { | |
| 34 | 8 | continue; | |
| 35 | } | ||
| 36 | 15 | const auto& c = material.color; | |
| 37 | 60 | mtl += "\nnewmtl " + material.name + "\nKa 0 0 0\nKd " + Number(c[0]) + " " + Number(c[1]) + | |
| 38 | 75 | " " + Number(c[2]) + "\nKs 0 0 0\nd 1\nillum 1\n"; | |
| 39 | } | ||
| 40 | 15 | return mtl; | |
| 41 | 15 | } | |
| 42 | |||
| 43 | /// The OBJ lines of one node: its object, material, vertices, normals and faces, numbered after | ||
| 44 | /// the `offset` vertices written before it. | ||
| 45 | 24 | std::string NodeObject(const SceneNode& node, std::size_t index, const SceneMesh& scene, | |
| 46 | std::size_t offset) { | ||
| 47 | 72 | std::string obj = "o node_" + std::to_string(index) + "\nusemtl " + scene.material.name + "\n"; | |
| 48 | 12595 | for (const Vec3d& p : scene.mesh.positions) { | |
| 49 | 25142 | obj += VectorLine("v", YUp(Placed(node, p))); | |
| 50 | } | ||
| 51 | 3409 | for (const Vec3d& n : scene.normals) { | |
| 52 | 6770 | obj += VectorLine("vn", YUp(PlacedNormal(node, n))); | |
| 53 | } | ||
| 54 | 24 | for (const auto& t : scene.mesh.triangles) { | |
| 55 | 18424 | obj += "f"; | |
| 56 | 73696 | for (const std::uint32_t v : t) { | |
| 57 | 55272 | const std::string i = std::to_string(offset + v + 1); | |
| 58 | 110856 | obj += " " + (scene.normals.empty() ? i : i + "//" + i); | |
| 59 | 55272 | } | |
| 60 | 36872 | obj += "\n"; | |
| 61 | } | ||
| 62 | 24 | return obj; | |
| 63 | } | ||
| 64 | |||
| 65 | } // namespace | ||
| 66 | |||
| 67 | 15 | void WriteObj(const std::filesystem::path& path, const std::vector<SceneMesh>& meshes, | |
| 68 | const std::vector<SceneNode>& nodes) { | ||
| 69 | 15 | std::filesystem::path mtl = path; | |
| 70 | 15 | mtl.replace_extension(".mtl"); | |
| 71 | 15 | WriteFile(mtl, Materials(meshes)); | |
| 72 | 45 | std::string obj = "# TerrainGen\nmtllib " + mtl.filename().string() + "\n"; | |
| 73 | 15 | std::size_t offset = 0; | |
| 74 | 39 | for (std::size_t n = 0; n < nodes.size(); ++n) { | |
| 75 | 24 | const SceneMesh& scene = meshes[nodes[n].mesh]; | |
| 76 | 48 | obj += NodeObject(nodes[n], n, scene, offset); | |
| 77 | 24 | offset += scene.mesh.positions.size(); | |
| 78 | } | ||
| 79 | 15 | WriteFile(path, obj); | |
| 80 | 30 | } | |
| 81 | |||
| 82 | 15 | SceneMesh ReadObj(const std::filesystem::path& path) { | |
| 83 | 15 | tinyobj::ObjReaderConfig config; | |
| 84 | 15 | config.triangulate = true; | |
| 85 | 15 | config.vertex_color = false; | |
| 86 | 32 | config.mtl_search_path = path.parent_path().string(); | |
| 87 | 15 | tinyobj::ObjReader reader; | |
| 88 | 15 | if (!reader.ParseFromFile(path.string(), config)) { | |
| 89 | 6 | throw std::runtime_error("Cannot read " + path.string() + ": " + reader.Error()); | |
| 90 | } | ||
| 91 | 13 | SceneMesh read; | |
| 92 | const std::vector<tinyobj::real_t>& vertices = reader.GetAttrib().vertices; | ||
| 93 | 9500 | for (std::size_t v = 0; v + 2 < vertices.size(); v += 3) { | |
| 94 | 9487 | read.mesh.positions.push_back( | |
| 95 | 9487 | ZUp({static_cast<double>(vertices[v]), static_cast<double>(vertices[v + 1]), | |
| 96 | 9487 | static_cast<double>(vertices[v + 2])})); | |
| 97 | } | ||
| 98 | 34 | for (const tinyobj::shape_t& shape : reader.GetShapes()) { | |
| 99 | const auto& indices = shape.mesh.indices; | ||
| 100 | 12287 | for (std::size_t i = 0; i + 2 < indices.size(); i += 3) { | |
| 101 | 12266 | read.mesh.triangles.push_back( | |
| 102 | 12266 | {static_cast<std::uint32_t>(indices[i].vertex_index), | |
| 103 | 12266 | static_cast<std::uint32_t>(indices[i + 1].vertex_index), | |
| 104 | 12266 | static_cast<std::uint32_t>(indices[i + 2].vertex_index)}); | |
| 105 | } | ||
| 106 | } | ||
| 107 | 13 | if (!reader.GetMaterials().empty()) { | |
| 108 | 11 | const tinyobj::material_t& material = reader.GetMaterials().front(); | |
| 109 | 11 | read.material = {material.name, | |
| 110 | 11 | {static_cast<double>(material.diffuse[0]), | |
| 111 | 11 | static_cast<double>(material.diffuse[1]), | |
| 112 | 11 | static_cast<double>(material.diffuse[2])}}; | |
| 113 | } | ||
| 114 | 26 | return read; | |
| 115 | 39 | } | |
| 116 | |||
| 117 | } // namespace terraingen::detail | ||
| 118 |