From d7220ee51c59cd3e51927f2f5e0388c8573f1792 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sun, 2 Nov 2025 15:57:16 -0800 Subject: Fix view/accessor offset and invariants; fix loading of Flight Helmet --- src/asset/model.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) (limited to 'src/asset') diff --git a/src/asset/model.c b/src/asset/model.c index a97d20e..b5c6b0d 100644 --- a/src/asset/model.c +++ b/src/asset/model.c @@ -1030,7 +1030,6 @@ static bool load_meshes( const cgltf_attribute* attrib = &prim->attributes[a]; const cgltf_accessor* accessor = attrib->data; const cgltf_buffer_view* view = accessor->buffer_view; - const cgltf_size offset = accessor->offset + view->offset; const cgltf_size buffer_index = view->buffer - data->buffers; assert(buffer_index < data->buffers_count); @@ -1120,12 +1119,20 @@ static bool load_meshes( break; } -#define CONFIGURE_BUFFER(buf) \ - if (buf) { \ - buf->buffer = buffer; \ - buf->offset_bytes = offset; \ - buf->size_bytes = view->size; \ - buf->stride_bytes = view->stride; \ + // See comments here for accessor/view/buffer invariants: + // https://github.com/KhronosGroup/glTF-Sample-Assets/issues/242 + // Gfx only has Buffer and BufferView, not accessors. We must combine + // the glTF's accessor and view offsets correctly. + const cgltf_size offset = accessor->offset + view->offset; + const cgltf_size size_bytes = view->size - accessor->offset; + +#define CONFIGURE_BUFFER(buf) \ + if (buf) { \ + buf->buffer = buffer; \ + buf->offset_bytes = offset; \ + buf->size_bytes = size_bytes; \ + buf->stride_bytes = view->stride; \ + buf->count = accessor->count; \ } CONFIGURE_BUFFER(buffer_view_2d); CONFIGURE_BUFFER(buffer_view_3d); @@ -1160,14 +1167,11 @@ static bool load_meshes( // either 2d or 3d positions but not both, here we can perform addition // to compute the total number of vertices. geometry_desc.num_verts = - (geometry_desc.positions2d.size_bytes / sizeof(vec2)) + - (geometry_desc.positions3d.size_bytes / sizeof(vec3)); - -#define CHECK_COUNT(buffer_view, type, num_components) \ - if (geometry_desc.buffer_view.buffer) { \ - assert( \ - (geometry_desc.buffer_view.size_bytes / \ - (num_components * sizeof(type))) == geometry_desc.num_verts); \ + geometry_desc.positions2d.count + geometry_desc.positions3d.count; + +#define CHECK_COUNT(buffer_view, type, num_components) \ + if (geometry_desc.buffer_view.buffer) { \ + assert(geometry_desc.buffer_view.count == geometry_desc.num_verts); \ } // Check that the number of vertices is consistent across all vertex -- cgit v1.2.3