-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp3dt_objloader.h
232 lines (203 loc) · 6.28 KB
/
p3dt_objloader.h
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// ==-----------------------------------------------------------------==
// COPYRIGHT
// ==-----------------------------------------------------------------==
// Created by Omar Sherif Fathy on 11/14/19.
// Copyright © 2019 Omar Sherif Fathy. All rights reserved.
// ==-----------------------------------------------------------------==
// ==-----------------------------------------------------------------==
// SHORT DESCRIPTION
// ==-----------------------------------------------------------------==
// This is a simple .obj file loader
// It doesn't load materials from .mtl files
// It only loads vertices, normals and texture coordinates
//
// NOTE :
// --------
// It expects a triangulated mesh only !
// Tick the "triangulate faces" option in blender while exporting
// ==-----------------------------------------------------------------==
#pragma once
#include "p3dt_maths.h"
// STL
#include <array>
#include <fstream>
#include <sstream>
#include <vector>
#if defined(_WIN32)
#pragma warning(disable:4996)
#endif
namespace p3dt {
struct vertex_t
{
maths::vector3_t<> position;
maths::vector3_t<> normal;
maths::vector2_t<> texcoord;
};
struct triangle_t
{
vertex_t a;
vertex_t b;
vertex_t c;
};
struct trimesh_t
{
std::vector<triangle_t> triangles;
};
namespace objloader {
struct element_t
{
const uint32_t vertex_index;
const uint32_t normal_index;
const uint32_t texcoord_index;
element_t(const uint32_t& v, const uint32_t vn, const uint32_t vt)
: vertex_index(v)
, normal_index(vn)
, texcoord_index(vt)
{}
};
struct texcoord_t
{
float m_u;
float m_v;
texcoord_t(float u, float v)
{
m_u = u;
m_v = v;
}
};
inline void
fill(trimesh_t& mesh,
const std::vector<maths::vector3_t<>>& vertices,
const std::vector<maths::vector3_t<>>& normals,
const std::vector<maths::vector2_t<>>& texcoords)
{
for (size_t i = 0; i < vertices.size(); i += 3) {
vertex_t a = { { vertices[i].x, vertices[i].y, vertices[i].z },
{ normals[i].x, normals[i].y, normals[i].z },
{ texcoords[i].x, texcoords[i].y } };
vertex_t b = { { vertices[i + 1].x, vertices[i + 1].y, vertices[i + 1].z },
{ normals[i + 1].x, normals[i + 1].y, normals[i + 1].z },
{ texcoords[i + 1].x, texcoords[i + 1].y } };
vertex_t c = { { vertices[i + 2].x, vertices[i + 2].y, vertices[i + 2].z },
{ normals[i + 2].x, normals[i + 2].y, normals[i + 2].z },
{ texcoords[i + 2].x, texcoords[i + 2].y } };
mesh.triangles.push_back({ a, b, c });
}
}
inline void
load(const std::vector<std::string>& lines, std::vector<trimesh_t>& meshes)
{
std::vector<element_t> _elements;
std::vector<maths::vector3_t<>> shared_vertices;
std::vector<maths::vector3_t<>> shared_normals;
std::vector<maths::vector2_t<>> shared_texcoords;
bool isTriangulated = true;
for (size_t l = 0; l < lines.size(); ++l) {
if (lines[l][0] == '#') {
continue;
}
std::stringstream chunks(lines[l]);
std::string flag;
chunks >> flag;
if (flag == "o") {
if (!meshes.empty()) {
std::vector<maths::vector3_t<>> vertices;
std::vector<maths::vector3_t<>> normals;
std::vector<maths::vector2_t<>> texcoords;
for (size_t i = 0; i < _elements.size(); ++i) {
vertices.push_back(shared_vertices[_elements[i].vertex_index]);
normals.push_back(shared_normals[_elements[i].normal_index]);
texcoords.push_back(shared_texcoords[_elements[i].texcoord_index]);
}
fill(meshes.back(), vertices, normals, texcoords);
_elements.clear();
vertices.clear();
normals.clear();
texcoords.clear();
}
meshes.push_back(trimesh_t{});
continue;
}
else if (flag == "v") {
maths::vector3_t<> values = {};
chunks >> values.x >> values.y >> values.z;
shared_vertices.push_back(values);
}
else if (flag == "vn") {
maths::vector3_t<> values = {};
chunks >> values.x >> values.y >> values.z;
shared_normals.push_back(values);
}
else if (flag == "vt") {
maths::vector2_t<> values = {};
chunks >> values.x >> values.y;
values.y = 1.0f - values.y;
shared_texcoords.push_back(values);
}
else if (flag == "f") {
uint32_t v0, v1, v2;
uint32_t vn0, vn1, vn2;
uint32_t vt0, vt1, vt2;
std::string chunk0, chunk1, chunk2;
chunks >> chunk0 >> chunk1 >> chunk2;
isTriangulated &= chunk0.find("/") != std::string::npos;
isTriangulated &= chunk1.find("/") != std::string::npos;
isTriangulated &= chunk2.find("/") != std::string::npos;
if (isTriangulated) {
sscanf(chunk0.c_str(), "%i/%i/%i", &v0, &vt0, &vn0);
sscanf(chunk1.c_str(), "%i/%i/%i", &v1, &vt1, &vn1);
sscanf(chunk2.c_str(), "%i/%i/%i", &v2, &vt2, &vn2);
v0--;
vt0--;
vn0--;
v1--;
vt1--;
vn1--;
v2--;
vt2--;
vn2--;
_elements.push_back(element_t(v0, vn0, vt0));
_elements.push_back(element_t(v1, vn1, vt1));
_elements.push_back(element_t(v2, vn2, vt2));
}
else {
printf("WARNING: MESH ISN'T TRIANGULATED !!\n");
}
}
}
std::vector<maths::vector3_t<>> vertices;
std::vector<maths::vector3_t<>> normals;
std::vector<maths::vector2_t<>> texcoords;
for (size_t i = 0; i < _elements.size(); ++i) {
vertices.push_back(shared_vertices[_elements[i].vertex_index]);
normals.push_back(shared_normals[_elements[i].normal_index]);
texcoords.push_back(shared_texcoords[_elements[i].texcoord_index]);
}
fill(meshes.back(), vertices, normals, texcoords);
_elements.clear();
vertices.clear();
normals.clear();
texcoords.clear();
}
static inline void
load_from_text(const std::string& text, std::vector<trimesh_t>& meshes)
{
std::vector<std::string> lines;
std::istringstream iss(text);
for (std::string line; std::getline(iss, line);) {
lines.push_back(line);
}
return load(lines, meshes);
}
static inline void
load_from_file(const std::string& filepath, std::vector<trimesh_t>& meshes)
{
std::vector<std::string> lines;
std::fstream fs(filepath);
for (std::string line; std::getline(fs, line);) {
lines.push_back(line);
}
return load(lines, meshes);
}
} // namespace objloader
} // namespace p3dt