summaryrefslogtreecommitdiff
path: root/gfx/models/obj.impl.cpp
blob: d73d82efcd3e25f7f3c033c5cccb2ad4a914698b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "obj.h"
#include <algorithm>
#include <cassert>
#include <filesystem>
#include <fstream>
#include <gfx/models/mesh.h> // IWYU pragma: keep
#include <gfx/models/vertex.hpp>
#include <glm/glm.hpp>
#include <iterator>
#include <map>
#include <memory>
#include <utility>
#include <vector>

ObjParser::ObjParser(const std::filesystem::path & fileName) : ObjParser {std::make_unique<std::ifstream>(fileName)} { }

ObjParser::ObjParser(std::unique_ptr<std::istream> in) : yyFlexLexer(in.get())
{
	assert(in);
	ObjParser::yylex();
	assert(in->good());
	std::for_each(vertices.begin(), vertices.end(), [](auto & v) {
		constexpr const glm::mat4 obj2ilt {
				-1, 0, 0, 0, // x
				0, 0, 1, 0, // y
				0, 1, 0, 0, // z
				0, 0, 0, 0, // w
		};
		v = v * obj2ilt;
	});
}

ObjParser::NamedMeshes
ObjParser::createMeshes() const
{
	NamedMeshes out;
	const auto data {createMeshData()};
	std::transform(data.begin(), data.end(), std::inserter(out, out.end()), [](auto && obj) {
		return std::make_pair(obj.first, std::make_shared<Mesh>(obj.second.first, obj.second.second));
	});
	return out;
}

ObjParser::NamedMeshesData
ObjParser::createMeshData() const
{
	NamedMeshesData out;
	std::transform(objects.begin(), objects.end(), std::inserter(out, out.end()), [this](auto && obj) {
		std::vector<Vertex> overtices;
		std::vector<ObjParser::FaceElement> vertexOrder;
		std::vector<unsigned int> indices;
		for (const auto & face : obj.second) {
			for (auto idx = 2U; idx < face.size(); idx += 1) {
				auto f = [&](auto idx) {
					const auto & fe {face[idx]};
					if (const auto existing = std::find(vertexOrder.begin(), vertexOrder.end(), fe);
							existing != vertexOrder.end()) {
						indices.push_back(static_cast<unsigned int>(std::distance(vertexOrder.begin(), existing)));
					}
					else {
						indices.push_back(static_cast<unsigned int>(overtices.size()));
						overtices.emplace_back(vertices[fe.x - 1], texCoords[fe.y - 1], -normals[fe.z - 1]);
						vertexOrder.emplace_back(fe);
					}
				};
				f(0U);
				f(idx);
				f(idx - 1);
			}
		}
		return std::make_pair(obj.first, std::make_pair(overtices, indices));
	});
	return out;
}