-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMeshBuilder.cpp
332 lines (276 loc) · 8.16 KB
/
MeshBuilder.cpp
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include "MeshBuilder.h"
#include "assimp/Importer.hpp"
#include "assimp/scene.h"
#include "assimp/postprocess.h"
#include <regex>
#include <functional>
#include <Windows.h>
#undef min
#undef max
#include "tiny_obj_loader.h"
MeshBuilder::Data MeshBuilder::build(const std::string & filename)
{
//std::regex obj("^.+\\.obj$");
//if (std::regex_match(filename, obj))
// return buildByTinyobj(filename);
//else
return buildByAssimp(filename);
}
MeshBuilder::Data MeshBuilder::buildByAssimp(const std::string & filename)
{
Data ret;
ret.aabb = { FLT_MAX,FLT_MAX,FLT_MAX,FLT_MIN, FLT_MIN,FLT_MIN };
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(filename,
aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_GenNormals |
//aiProcess_GenSmoothNormals|
aiProcess_CalcTangentSpace |
aiProcess_ConvertToLeftHanded);
if (!scene)
{
::MessageBoxA(NULL, importer.GetErrorString(), NULL, NULL);
abort();
return ret;
}
std::regex reg("^(.+)[/\\\\].+\\..+$");
std::smatch match;
std::string totalpath;
if (std::regex_match(filename, match, reg))
{
totalpath = std::string(match[1]) + "/";
}
if (scene->HasMaterials())
{
for (int i = 0; i < scene->mNumMaterials; ++i)
{
auto m = scene->mMaterials[i];
Data::Material mat;
auto getTex = [&](auto type) {
if (m->GetTextureCount(type))
{
aiString path;
aiTextureMapping mapping;
UINT index;
m->GetTexture(type, 0, &path, &mapping, &index);
if (path.length == 0)
return std::string();
std::string realpath = totalpath + path.C_Str();
return realpath;
}
return std::string();
};
mat.albedo = getTex(aiTextureType_DIFFUSE);
mat.normal = getTex(aiTextureType_NORMALS);
mat.ambient = getTex(aiTextureType_AMBIENT);
mat.height = getTex(aiTextureType_HEIGHT);
mat.shininess = getTex(aiTextureType_SHININESS);
aiColor4D d;
aiGetMaterialColor(m, AI_MATKEY_COLOR_DIFFUSE, &d);
mat.diffuse = { d.r, d.g, d.b};
//if (m->GetTextureCount(aiTextureType_DIFFUSE) > 0)
//{
// aiString path;
// aiTextureMapping mapping;
// UINT index;
// m->GetTexture(aiTextureType_DIFFUSE, 0, &path, &mapping, &index);
// std::string realpath = totalpath + path.C_Str();
// mat.albedo = realpath;
//}
//if (m->GetTextureCount(aiTextureType_NORMALS) > 0)
//{
// aiString path;
// aiTextureMapping mapping;
// UINT index;
// m->GetTexture(aiTextureType_NORMALS, 0, &path, &mapping, &index);
// std::string realpath = totalpath + path.C_Str();
// mat.normal = realpath;
//}
ret.materials.push_back(mat);
}
}
std::function<void(const aiNode*, const aiMatrix4x4*, std::vector<Data::Mesh>&)> process;
process = [&process, scene, &ret](const aiNode* node, const aiMatrix4x4* pm, std::vector<Data::Mesh>& rets)
{
aiMatrix4x4 trans = node->mTransformation;
if (pm)
{
trans = *pm * trans;
}
for (int i = 0; i < node->mNumChildren; ++i)
{
process(node->mChildren[i], &trans, rets);
}
auto meshs = node->mMeshes;
for (int i = 0; i < node->mNumMeshes; ++i)
{
Data::Mesh mesh;
mesh.layout.push_back(POSITION);
auto m = scene->mMeshes[meshs[i]];
mesh.materialIndex = m->mMaterialIndex;
bool hasMaterial = ret.materials.size() > mesh.materialIndex;
mesh.numVertex = m->mNumVertices;
memcpy(&mesh.tranfromation, &trans, sizeof(mesh.tranfromation));
size_t size = 4 * 3;
if (m->HasNormals())
{
mesh.layout.push_back(NORMAL);
size += 4 * 3;
}
if (m->HasTextureCoords(0))
{
mesh.layout.push_back(TEXCOORD0);
size += 4 * 2;
}
if (m->HasTangentsAndBitangents())
{
mesh.layout.push_back(TANGENT);
mesh.layout.push_back(BITANGENT);
size += 4 * 3;
size += 4 * 3;
}
mesh.vertices.resize(mesh.numVertex * size);
char* begin = mesh.vertices.data();
auto copy = [](char*& buffer, const void* cont, size_t size) {
memcpy(buffer, cont, size);
buffer += size;
};
for (int j = 0; j < m->mNumVertices; ++j)
{
aiVector3D pos = *(m->mVertices + j);
pos = trans * pos;
copy(begin, &pos, sizeof(aiVector3D));
ret.aabb[0] = std::min(ret.aabb[0], pos.x);
ret.aabb[1] = std::min(ret.aabb[1], pos.y);
ret.aabb[2] = std::min(ret.aabb[2], pos.z);
ret.aabb[3] = std::max(ret.aabb[3], pos.x);
ret.aabb[4] = std::max(ret.aabb[4], pos.y);
ret.aabb[5] = std::max(ret.aabb[5], pos.z);
if (m->HasNormals())
{
copy(begin, m->mNormals + j, sizeof(aiVector3D));
}
if (m->HasTextureCoords(0))
{
copy(begin, m->mTextureCoords[0] + j, 4 * 2);
}
if (m->HasTangentsAndBitangents())
{
copy(begin, m->mTangents + j, sizeof(aiVector3D));
copy(begin, m->mBitangents + j, sizeof(aiVector3D));
}
}
if (m->HasFaces())
{
for (int j = 0; j < m->mNumFaces; ++j)
{
for (int k = 0; k < 3; ++k)
mesh.indices.push_back(m->mFaces[j].mIndices[k]);
}
}
rets.push_back(std::move(mesh));
}
};
process(scene->mRootNode, nullptr, ret.meshs);
return ret;
}
MeshBuilder::Data MeshBuilder::buildByTinyobj(const std::string & filename)
{
std::regex reg("^(.+)[/\\\\].+\\..+$");
std::smatch match;
std::string totalpath;
if (std::regex_match(filename, match, reg))
{
totalpath = std::string(match[1]) + "/";
}
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err;
if (!tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, filename.c_str(), totalpath.c_str(),true))
{
MessageBoxA(NULL, err.c_str(), NULL,NULL);
abort();
}
Data ret;
ret.aabb = { FLT_MAX,FLT_MAX,FLT_MAX,FLT_MIN, FLT_MIN,FLT_MIN };
const auto* vertices = attrib.vertices.data();
const auto* normals = attrib.normals.data();
const auto* texcoords = attrib.texcoords.data();
for (auto& i : materials)
{
Data::Material m;
if (i.diffuse_texname.size() > 0)
m.albedo = totalpath + i.diffuse_texname;
if (!i.normal_texname.empty())
m.normal = totalpath + i.normal_texname;
memcpy(m.diffuse.data(), i.diffuse, 4 * m.diffuse.size());
ret.materials.push_back(m);
}
// Loop over shapes
for (size_t s = 0; s < shapes.size(); s++) {
// Loop over faces(polygon)
size_t index_offset = 0;
Data::Mesh mesh;
memset(mesh.tranfromation, 0, sizeof(mesh.tranfromation));
mesh.tranfromation[0] = 1;
mesh.tranfromation[5] = 1;
mesh.tranfromation[10] = 1;
mesh.tranfromation[15] = 1;
auto copy = [](char*& buffer, const void* src, int size) {
memcpy(buffer, src, size);
buffer += size;
};
mesh.layout = { POSITION };
int stride = 12;
if (shapes[s].mesh.indices[0].normal_index != 0xffffffff)
{
stride += 12;
mesh.layout.push_back(NORMAL);
}
if (shapes[s].mesh.indices[0].texcoord_index != 0xffffffff)
{
stride += 8;
mesh.layout.push_back(TEXCOORD0);
}
mesh.numVertex = shapes[s].mesh.num_face_vertices.size() * 3;
mesh.vertices.resize(mesh.numVertex * stride);
mesh.materialIndex = shapes[s].mesh.material_ids[0];
char* data = mesh.vertices.data();
for (unsigned int f = 0; f < shapes[s].mesh.num_face_vertices.size(); f++) {
int fv = shapes[s].mesh.num_face_vertices[f];
if (fv != 3)
abort();
// Loop over vertices in the face.
for (unsigned int v = 0; v < fv; v++) {
// access to vertex
tinyobj::index_t idx = shapes[s].mesh.indices[index_offset + v];
mesh.indices.push_back(f * 3 + v);
const float* pos = vertices + (3 * idx.vertex_index);
ret.aabb[0] = std::min(ret.aabb[0], pos[0]);
ret.aabb[1] = std::min(ret.aabb[1], pos[1]);
ret.aabb[2] = std::min(ret.aabb[2], pos[2]);
ret.aabb[3] = std::max(ret.aabb[3], pos[0]);
ret.aabb[4] = std::max(ret.aabb[4], pos[1]);
ret.aabb[5] = std::max(ret.aabb[5], pos[2]);
copy(data, pos, 12);
if (idx.normal_index != 0xffffffff)
copy(data, normals + (3 * idx.normal_index), 12);
if (idx.texcoord_index != 0xffffffff)
{
float coord[2];
memcpy(coord, texcoords + (2 * idx.texcoord_index), 8);
coord[1] = 1 - coord[1];
copy(data, coord, 8);
}
}
index_offset += fv;
// per-face material
shapes[s].mesh.material_ids[f];
}
ret.meshs.push_back((mesh));
}
return ret;
}