Skip to content

Commit affc5f4

Browse files
committed
gltfpack: Implement support for lines in OBJ files
Now that fast_obj parses line information, we can use it to split the meshes based on the primitive type. For now we assume that the input meshes have a reasonably small number of flips between polygons and lines and don't attempt to parse meshes out optimally; if multiple meshes are produced, the processing code will later merge them.
1 parent 9913e20 commit affc5f4

1 file changed

Lines changed: 27 additions & 10 deletions

File tree

gltf/parseobj.cpp

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -216,17 +216,33 @@ static void parseMeshObj(fastObjMesh* obj, unsigned int face_offset, unsigned in
216216
{
217217
unsigned int face_vertices = obj->face_vertices[face_offset + fi];
218218

219-
for (unsigned int vi = 2; vi < face_vertices; ++vi)
219+
if (mesh.type == cgltf_primitive_type_lines)
220220
{
221-
size_t to = index_offset + (vi - 2) * 3;
221+
for (unsigned int vi = 1; vi < face_vertices; ++vi)
222+
{
223+
size_t to = index_offset + (vi - 1) * 2;
222224

223-
mesh.indices[to + 0] = remap[vertex_offset];
224-
mesh.indices[to + 1] = remap[vertex_offset + vi - 1];
225-
mesh.indices[to + 2] = remap[vertex_offset + vi];
225+
mesh.indices[to + 0] = remap[vertex_offset + vi - 1];
226+
mesh.indices[to + 1] = remap[vertex_offset + vi];
227+
}
228+
229+
vertex_offset += face_vertices;
230+
index_offset += (face_vertices - 1) * 2;
226231
}
232+
else
233+
{
234+
for (unsigned int vi = 2; vi < face_vertices; ++vi)
235+
{
236+
size_t to = index_offset + (vi - 2) * 3;
227237

228-
vertex_offset += face_vertices;
229-
index_offset += (face_vertices - 2) * 3;
238+
mesh.indices[to + 0] = remap[vertex_offset];
239+
mesh.indices[to + 1] = remap[vertex_offset + vi - 1];
240+
mesh.indices[to + 2] = remap[vertex_offset + vi];
241+
}
242+
243+
vertex_offset += face_vertices;
244+
index_offset += (face_vertices - 2) * 3;
245+
}
230246
}
231247

232248
assert(vertex_offset == face_vertex_count);
@@ -241,16 +257,17 @@ static void parseMeshGroupObj(fastObjMesh* obj, const fastObjGroup& og, cgltf_da
241257
for (unsigned int face_offset = og.face_offset; face_offset < face_end_offset;)
242258
{
243259
unsigned int mi = obj->face_materials[face_offset];
260+
unsigned char isl = obj->face_lines ? obj->face_lines[face_offset] : 0;
244261

245262
unsigned int face_count = 0;
246263
unsigned int face_vertex_count = 0;
247264
unsigned int index_count = 0;
248265

249-
for (unsigned int fj = face_offset; fj < face_end_offset && obj->face_materials[fj] == mi; ++fj)
266+
for (unsigned int fj = face_offset; fj < face_end_offset && obj->face_materials[fj] == mi && (!obj->face_lines || obj->face_lines[fj] == isl); ++fj)
250267
{
251268
face_count += 1;
252269
face_vertex_count += obj->face_vertices[fj];
253-
index_count += (obj->face_vertices[fj] - 2) * 3;
270+
index_count += isl ? (obj->face_vertices[fj] - 1) * 2 : (obj->face_vertices[fj] - 2) * 3;
254271
}
255272

256273
meshes.push_back(Mesh());
@@ -262,7 +279,7 @@ static void parseMeshGroupObj(fastObjMesh* obj, const fastObjGroup& og, cgltf_da
262279
mesh.material = &data->materials[mi];
263280
}
264281

265-
mesh.type = cgltf_primitive_type_triangles;
282+
mesh.type = isl ? cgltf_primitive_type_lines : cgltf_primitive_type_triangles;
266283
mesh.targets = 0;
267284

268285
if (node)

0 commit comments

Comments
 (0)