diff options
| author | 3gg <3gg@shellblade.net> | 2025-11-02 15:57:16 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-11-02 15:57:16 -0800 |
| commit | d7220ee51c59cd3e51927f2f5e0388c8573f1792 (patch) | |
| tree | d949b88768f09e06f9456a8028ae89ef53e8a091 /src/asset | |
| parent | d3bfccdc4f90aabfa3493b0db0e6fe357a527485 (diff) | |
Fix view/accessor offset and invariants; fix loading of Flight Helmet
Diffstat (limited to 'src/asset')
| -rw-r--r-- | src/asset/model.c | 34 |
1 files changed, 19 insertions, 15 deletions
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( | |||
| 1030 | const cgltf_attribute* attrib = &prim->attributes[a]; | 1030 | const cgltf_attribute* attrib = &prim->attributes[a]; |
| 1031 | const cgltf_accessor* accessor = attrib->data; | 1031 | const cgltf_accessor* accessor = attrib->data; |
| 1032 | const cgltf_buffer_view* view = accessor->buffer_view; | 1032 | const cgltf_buffer_view* view = accessor->buffer_view; |
| 1033 | const cgltf_size offset = accessor->offset + view->offset; | ||
| 1034 | const cgltf_size buffer_index = view->buffer - data->buffers; | 1033 | const cgltf_size buffer_index = view->buffer - data->buffers; |
| 1035 | 1034 | ||
| 1036 | assert(buffer_index < data->buffers_count); | 1035 | assert(buffer_index < data->buffers_count); |
| @@ -1120,12 +1119,20 @@ static bool load_meshes( | |||
| 1120 | break; | 1119 | break; |
| 1121 | } | 1120 | } |
| 1122 | 1121 | ||
| 1123 | #define CONFIGURE_BUFFER(buf) \ | 1122 | // See comments here for accessor/view/buffer invariants: |
| 1124 | if (buf) { \ | 1123 | // https://github.com/KhronosGroup/glTF-Sample-Assets/issues/242 |
| 1125 | buf->buffer = buffer; \ | 1124 | // Gfx only has Buffer and BufferView, not accessors. We must combine |
| 1126 | buf->offset_bytes = offset; \ | 1125 | // the glTF's accessor and view offsets correctly. |
| 1127 | buf->size_bytes = view->size; \ | 1126 | const cgltf_size offset = accessor->offset + view->offset; |
| 1128 | buf->stride_bytes = view->stride; \ | 1127 | const cgltf_size size_bytes = view->size - accessor->offset; |
| 1128 | |||
| 1129 | #define CONFIGURE_BUFFER(buf) \ | ||
| 1130 | if (buf) { \ | ||
| 1131 | buf->buffer = buffer; \ | ||
| 1132 | buf->offset_bytes = offset; \ | ||
| 1133 | buf->size_bytes = size_bytes; \ | ||
| 1134 | buf->stride_bytes = view->stride; \ | ||
| 1135 | buf->count = accessor->count; \ | ||
| 1129 | } | 1136 | } |
| 1130 | CONFIGURE_BUFFER(buffer_view_2d); | 1137 | CONFIGURE_BUFFER(buffer_view_2d); |
| 1131 | CONFIGURE_BUFFER(buffer_view_3d); | 1138 | CONFIGURE_BUFFER(buffer_view_3d); |
| @@ -1160,14 +1167,11 @@ static bool load_meshes( | |||
| 1160 | // either 2d or 3d positions but not both, here we can perform addition | 1167 | // either 2d or 3d positions but not both, here we can perform addition |
| 1161 | // to compute the total number of vertices. | 1168 | // to compute the total number of vertices. |
| 1162 | geometry_desc.num_verts = | 1169 | geometry_desc.num_verts = |
| 1163 | (geometry_desc.positions2d.size_bytes / sizeof(vec2)) + | 1170 | geometry_desc.positions2d.count + geometry_desc.positions3d.count; |
| 1164 | (geometry_desc.positions3d.size_bytes / sizeof(vec3)); | 1171 | |
| 1165 | 1172 | #define CHECK_COUNT(buffer_view, type, num_components) \ | |
| 1166 | #define CHECK_COUNT(buffer_view, type, num_components) \ | 1173 | if (geometry_desc.buffer_view.buffer) { \ |
| 1167 | if (geometry_desc.buffer_view.buffer) { \ | 1174 | assert(geometry_desc.buffer_view.count == geometry_desc.num_verts); \ |
| 1168 | assert( \ | ||
| 1169 | (geometry_desc.buffer_view.size_bytes / \ | ||
| 1170 | (num_components * sizeof(type))) == geometry_desc.num_verts); \ | ||
| 1171 | } | 1175 | } |
| 1172 | 1176 | ||
| 1173 | // Check that the number of vertices is consistent across all vertex | 1177 | // Check that the number of vertices is consistent across all vertex |
