Skip to content

Commit

Permalink
Add cgltf_find_accessor helper
Browse files Browse the repository at this point in the history
When using cgltf something that comes up often is the need to find the
accessor corresponding to a given attribute for a primitive (e.g.
positions or second UV channel). The code for this is simple, but it is
needed so frequently that it would be nice to include a helper for this
in cgltf proper.
  • Loading branch information
zeux committed Nov 2, 2024
1 parent fa3b80f commit 9e3675d
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cgltf.h
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,8 @@ void cgltf_node_transform_world(const cgltf_node* node, cgltf_float* out_matrix)

const uint8_t* cgltf_buffer_view_data(const cgltf_buffer_view* view);

const cgltf_accessor* cgltf_find_accessor(const cgltf_primitive* prim, cgltf_attribute_type type, cgltf_int index);

cgltf_bool cgltf_accessor_read_float(const cgltf_accessor* accessor, cgltf_size index, cgltf_float* out, cgltf_size element_size);
cgltf_bool cgltf_accessor_read_uint(const cgltf_accessor* accessor, cgltf_size index, cgltf_uint* out, cgltf_size element_size);
cgltf_size cgltf_accessor_read_index(const cgltf_accessor* accessor, cgltf_size index);
Expand Down Expand Up @@ -2312,6 +2314,18 @@ const uint8_t* cgltf_buffer_view_data(const cgltf_buffer_view* view)
return result;
}

const cgltf_accessor* cgltf_find_accessor(const cgltf_primitive* prim, cgltf_attribute_type type, cgltf_int index)
{
for (cgltf_size i = 0; i < prim->attributes_count; ++i)
{
const cgltf_attribute* attr = &prim->attributes[i];
if (attr->type == type && attr->index == index)
return attr->data;
}

return NULL;
}

cgltf_bool cgltf_accessor_read_float(const cgltf_accessor* accessor, cgltf_size index, cgltf_float* out, cgltf_size element_size)
{
if (accessor->is_sparse)
Expand Down

0 comments on commit 9e3675d

Please sign in to comment.