forked from zeux/meshoptimizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseobj.cpp
217 lines (162 loc) · 5.78 KB
/
parseobj.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
// This file is part of gltfpack; see gltfpack.h for version/license details
#include "gltfpack.h"
#include "../extern/fast_obj.h"
#include <algorithm>
#include <stdlib.h>
#include <string.h>
static void defaultFree(void*, void* p)
{
free(p);
}
static int textureIndex(const std::vector<std::string>& textures, const char* name)
{
for (size_t i = 0; i < textures.size(); ++i)
if (textures[i] == name)
return int(i);
return -1;
}
static cgltf_data* parseSceneObj(fastObjMesh* obj)
{
cgltf_data* data = (cgltf_data*)calloc(1, sizeof(cgltf_data));
data->memory.free = defaultFree;
std::vector<std::string> textures;
for (unsigned int mi = 0; mi < obj->material_count; ++mi)
{
fastObjMaterial& om = obj->materials[mi];
if (om.map_Kd.name && textureIndex(textures, om.map_Kd.name) < 0)
textures.push_back(om.map_Kd.name);
}
data->images = (cgltf_image*)calloc(textures.size(), sizeof(cgltf_image));
data->images_count = textures.size();
for (size_t i = 0; i < textures.size(); ++i)
{
data->images[i].uri = (char*)malloc(textures[i].size() + 1);
strcpy(data->images[i].uri, textures[i].c_str());
}
data->textures = (cgltf_texture*)calloc(textures.size(), sizeof(cgltf_texture));
data->textures_count = textures.size();
for (size_t i = 0; i < textures.size(); ++i)
{
data->textures[i].image = &data->images[i];
}
data->materials = (cgltf_material*)calloc(obj->material_count, sizeof(cgltf_material));
data->materials_count = obj->material_count;
for (unsigned int mi = 0; mi < obj->material_count; ++mi)
{
cgltf_material& gm = data->materials[mi];
fastObjMaterial& om = obj->materials[mi];
gm.has_pbr_metallic_roughness = true;
gm.pbr_metallic_roughness.base_color_factor[0] = 1.0f;
gm.pbr_metallic_roughness.base_color_factor[1] = 1.0f;
gm.pbr_metallic_roughness.base_color_factor[2] = 1.0f;
gm.pbr_metallic_roughness.base_color_factor[3] = 1.0f;
gm.pbr_metallic_roughness.metallic_factor = 0.0f;
gm.pbr_metallic_roughness.roughness_factor = 1.0f;
gm.alpha_cutoff = 0.5f;
if (om.map_Kd.name)
{
gm.pbr_metallic_roughness.base_color_texture.texture = &data->textures[textureIndex(textures, om.map_Kd.name)];
gm.pbr_metallic_roughness.base_color_texture.scale = 1.0f;
gm.alpha_mode = (om.illum == 4 || om.illum == 6 || om.illum == 7 || om.illum == 9) ? cgltf_alpha_mode_mask : cgltf_alpha_mode_opaque;
}
if (om.map_d.name)
{
gm.alpha_mode = cgltf_alpha_mode_blend;
}
}
return data;
}
static void parseMeshesObj(fastObjMesh* obj, cgltf_data* data, std::vector<Mesh>& meshes)
{
unsigned int material_count = std::max(obj->material_count, 1u);
std::vector<size_t> vertex_count(material_count);
std::vector<size_t> index_count(material_count);
for (unsigned int fi = 0; fi < obj->face_count; ++fi)
{
unsigned int mi = obj->face_materials[fi];
vertex_count[mi] += obj->face_vertices[fi];
index_count[mi] += (obj->face_vertices[fi] - 2) * 3;
}
std::vector<size_t> mesh_index(material_count);
for (unsigned int mi = 0; mi < material_count; ++mi)
{
if (index_count[mi] == 0)
continue;
mesh_index[mi] = meshes.size();
meshes.push_back(Mesh());
Mesh& mesh = meshes.back();
if (data->materials_count)
{
assert(mi < data->materials_count);
mesh.material = &data->materials[mi];
}
mesh.type = cgltf_primitive_type_triangles;
mesh.streams.resize(3);
mesh.streams[0].type = cgltf_attribute_type_position;
mesh.streams[0].data.resize(vertex_count[mi]);
mesh.streams[1].type = cgltf_attribute_type_normal;
mesh.streams[1].data.resize(vertex_count[mi]);
mesh.streams[2].type = cgltf_attribute_type_texcoord;
mesh.streams[2].data.resize(vertex_count[mi]);
mesh.indices.resize(index_count[mi]);
mesh.targets = 0;
}
std::vector<char> mesh_normals(meshes.size());
std::vector<char> mesh_texcoords(meshes.size());
std::vector<size_t> vertex_offset(material_count);
std::vector<size_t> index_offset(material_count);
size_t group_offset = 0;
for (unsigned int fi = 0; fi < obj->face_count; ++fi)
{
unsigned int mi = obj->face_materials[fi];
Mesh& mesh = meshes[mesh_index[mi]];
size_t vo = vertex_offset[mi];
size_t io = index_offset[mi];
for (unsigned int vi = 0; vi < obj->face_vertices[fi]; ++vi)
{
fastObjIndex ii = obj->indices[group_offset + vi];
Attr p = {{obj->positions[ii.p * 3 + 0], obj->positions[ii.p * 3 + 1], obj->positions[ii.p * 3 + 2]}};
Attr n = {{obj->normals[ii.n * 3 + 0], obj->normals[ii.n * 3 + 1], obj->normals[ii.n * 3 + 2]}};
Attr t = {{obj->texcoords[ii.t * 2 + 0], 1.f - obj->texcoords[ii.t * 2 + 1]}};
mesh.streams[0].data[vo + vi] = p;
mesh.streams[1].data[vo + vi] = n;
mesh.streams[2].data[vo + vi] = t;
mesh_normals[mesh_index[mi]] |= ii.n > 0;
mesh_texcoords[mesh_index[mi]] |= ii.t > 0;
}
for (unsigned int vi = 2; vi < obj->face_vertices[fi]; ++vi)
{
size_t to = io + (vi - 2) * 3;
mesh.indices[to + 0] = unsigned(vo);
mesh.indices[to + 1] = unsigned(vo + vi - 1);
mesh.indices[to + 2] = unsigned(vo + vi);
}
vertex_offset[mi] += obj->face_vertices[fi];
index_offset[mi] += (obj->face_vertices[fi] - 2) * 3;
group_offset += obj->face_vertices[fi];
}
for (size_t i = 0; i < meshes.size(); ++i)
{
Mesh& mesh = meshes[i];
assert(mesh.streams.size() == 3);
assert(mesh.streams[1].type == cgltf_attribute_type_normal);
assert(mesh.streams[2].type == cgltf_attribute_type_texcoord);
if (!mesh_texcoords[i])
mesh.streams.erase(mesh.streams.begin() + 2);
if (!mesh_normals[i])
mesh.streams.erase(mesh.streams.begin() + 1);
}
}
cgltf_data* parseObj(const char* path, std::vector<Mesh>& meshes, const char** error)
{
fastObjMesh* obj = fast_obj_read(path);
if (!obj)
{
*error = "file not found";
return 0;
}
cgltf_data* data = parseSceneObj(obj);
parseMeshesObj(obj, data, meshes);
fast_obj_destroy(obj);
return data;
}