Skip to content

Commit

Permalink
gltfpack: Refactor .obj parsing a little bit
Browse files Browse the repository at this point in the history
This change splits nodes and materials parsing and keeps mesh group
parsing loop so that the code looks more similar to what we used to have
when we just had one group. That will also make it easier to ensure the
relationships between objects and nodes stay 1-1.
  • Loading branch information
zeux committed Aug 22, 2023
1 parent 288d3a6 commit 7f33cc7
Showing 1 changed file with 40 additions and 40 deletions.
80 changes: 40 additions & 40 deletions gltf/parseobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@ static int textureIndex(const std::vector<std::string>& textures, const char* na
return -1;
}

static cgltf_data* parseSceneObj(fastObjMesh* obj)
static void parseMaterialsObj(fastObjMesh* obj, cgltf_data* data)
{
cgltf_data* data = (cgltf_data*)calloc(1, sizeof(cgltf_data));
data->memory.free_func = defaultFree;

std::vector<std::string> textures;

for (unsigned int mi = 0; mi < obj->material_count; ++mi)
Expand Down Expand Up @@ -97,7 +94,10 @@ static cgltf_data* parseSceneObj(fastObjMesh* obj)
gm.alpha_mode = cgltf_alpha_mode_blend;
}
}
}

static void parseNodesObj(fastObjMesh* obj, cgltf_data* data)
{
data->nodes = (cgltf_node*)calloc(obj->object_count, sizeof(cgltf_node));
data->nodes_count = obj->object_count;

Expand All @@ -122,8 +122,6 @@ static cgltf_data* parseSceneObj(fastObjMesh* obj)

data->scenes->nodes[oi] = node;
}

return data;
}

static void parseMeshObj(fastObjMesh* obj, unsigned int face_offset, unsigned int face_vertex_offset, unsigned int face_count, unsigned int face_vertex_count, unsigned int index_count, Mesh& mesh)
Expand Down Expand Up @@ -214,49 +212,44 @@ static void parseMeshObj(fastObjMesh* obj, unsigned int face_offset, unsigned in
assert(index_offset == index_count);
}

static void parseMeshesObj(fastObjMesh* obj, cgltf_data* data, std::vector<Mesh>& meshes)
static void parseMeshGroupObj(fastObjMesh* obj, const fastObjGroup& og, cgltf_data* data, cgltf_node* node, std::vector<Mesh>& meshes)
{
for (unsigned int oi = 0; oi < obj->object_count; ++oi)
unsigned int face_vertex_offset = og.index_offset;
unsigned int face_end_offset = og.face_offset + og.face_count;

for (unsigned int face_offset = og.face_offset; face_offset < face_end_offset; )
{
const fastObjGroup& og = obj->objects[oi];
unsigned int mi = obj->face_materials[face_offset];

unsigned int face_vertex_offset = og.index_offset;
unsigned int face_end_offset = og.face_offset + og.face_count;
unsigned int face_count = 0;
unsigned int face_vertex_count = 0;
unsigned int index_count = 0;

for (unsigned int face_offset = og.face_offset; face_offset < face_end_offset; )
for (unsigned int fj = face_offset; fj < face_end_offset && obj->face_materials[fj] == mi; ++fj)
{
unsigned int mi = obj->face_materials[face_offset];

unsigned int face_count = 0;
unsigned int face_vertex_count = 0;
unsigned int index_count = 0;

for (unsigned int fj = face_offset; fj < face_end_offset && obj->face_materials[fj] == mi; ++fj)
{
face_count += 1;
face_vertex_count += obj->face_vertices[fj];
index_count += (obj->face_vertices[fj] - 2) * 3;
}
face_count += 1;
face_vertex_count += obj->face_vertices[fj];
index_count += (obj->face_vertices[fj] - 2) * 3;
}

meshes.push_back(Mesh());
Mesh& mesh = meshes.back();
meshes.push_back(Mesh());
Mesh& mesh = meshes.back();

if (data->materials_count)
{
assert(mi < data->materials_count);
mesh.material = &data->materials[mi];
}
if (data->materials_count)
{
assert(mi < data->materials_count);
mesh.material = &data->materials[mi];
}

mesh.type = cgltf_primitive_type_triangles;
mesh.targets = 0;
mesh.type = cgltf_primitive_type_triangles;
mesh.targets = 0;

mesh.nodes.push_back(&data->nodes[oi]);
mesh.nodes.push_back(node);

parseMeshObj(obj, face_offset, face_vertex_offset, face_count, face_vertex_count, index_count, mesh);
parseMeshObj(obj, face_offset, face_vertex_offset, face_count, face_vertex_count, index_count, mesh);

face_offset += face_count;
face_vertex_offset += face_vertex_count;
}
face_offset += face_count;
face_vertex_offset += face_vertex_count;
}
}

Expand All @@ -270,9 +263,16 @@ cgltf_data* parseObj(const char* path, std::vector<Mesh>& meshes, const char** e
return 0;
}

cgltf_data* data = parseSceneObj(obj);
cgltf_data* data = (cgltf_data*)calloc(1, sizeof(cgltf_data));
data->memory.free_func = defaultFree;

parseMaterialsObj(obj, data);

parseMeshesObj(obj, data, meshes);
parseNodesObj(obj, data);
assert(data->nodes_count == obj->object_count);

for (unsigned int oi = 0; oi < obj->object_count; ++oi)
parseMeshGroupObj(obj, obj->objects[oi], data, &data->nodes[oi], meshes);

fast_obj_destroy(obj);

Expand Down

0 comments on commit 7f33cc7

Please sign in to comment.