Skip to content

Commit

Permalink
graphics: Generate index data if missing.
Browse files Browse the repository at this point in the history
  • Loading branch information
fubark committed Jun 15, 2022
1 parent 91ef349 commit 50ae319
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions graphics/src/graphics.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1485,13 +1485,6 @@ pub const GLTFnode = struct {
const mesh = @ptrCast(*cgltf.cgltf_mesh, node.mesh);
if (mesh.primitives_count > 0) {
const primitive = mesh.primitives[0];
const indices = @ptrCast(*cgltf.cgltf_accessor, primitive.indices);
var ii: u32 = 0;

const indexes = alloc.alloc(u16, indices.count) catch fatal();
while (ii < indices.count) : (ii += 1) {
indexes[ii] = @intCast(u16, cgltf.cgltf_accessor_read_index(indices, ii));
}

// Determine number of verts by looking at the first attribute.
var verts: []gpu.TexShaderVertex = undefined;
Expand Down Expand Up @@ -1575,6 +1568,23 @@ pub const GLTFnode = struct {
}
}

var indexes: []u16 = undefined;
if (primitive.indices != null) {
const indices = @ptrCast(*cgltf.cgltf_accessor, primitive.indices);
var i: u32 = 0;
indexes = alloc.alloc(u16, indices.count) catch fatal();
while (i < indices.count) : (i += 1) {
indexes[i] = @intCast(u16, cgltf.cgltf_accessor_read_index(indices, i));
}
} else {
// No index data. Generate them from verts.
indexes = alloc.alloc(u16, verts.len) catch fatal();
var i: u32 = 0;
while (i < indexes.len) : (i += 1) {
indexes[i] = @intCast(u16, i);
}
}

if (node.skin != null) {
const skin = @ptrCast(*cgltf.cgltf_skin, node.skin);
if (skin.joints_count > 0) {
Expand Down

0 comments on commit 50ae319

Please sign in to comment.