Backlog/engine/rend/src/meshes/meshes.zig

98 lines
2.6 KiB
Zig

pub const MeshVertex = extern struct {
position: core.Vectorf = .{},
normal: core.Vectorf = .{},
color: core.colors.Color = .{},
uv: core.Vector2f = .{},
bones: [4]u8 = .{ 0, 0, 0, 0 },
weights: [4]u8 = .{ 0, 0, 0, 0 },
};
pub const MeshUpdate = union(enum(u8)) {
new: struct {
vertices: []MeshVertex,
indices: []u32,
jointNames: []JointNameEntry,
name: core.Name,
skeletonName: ?core.Name,
},
free: struct {
vertices: core.Span,
indices: core.Span,
name: core.Name,
},
pub fn deinit(self: @This(), allocator: std.mem.Allocator) void {
switch (self) {
.new => |new| {
allocator.free(new.vertices);
allocator.free(new.indices);
for (new.jointNames) |entry| {
entry.deinit(allocator);
}
allocator.free(new.jointNames);
},
.free => {},
}
}
};
pub const JointNameEntry = struct {
name: []u8 = undefined,
index: u32 = 0,
pub fn deinit(self: @This(), allocator: std.mem.Allocator) void {
allocator.free(self.name);
}
};
pub const IndexedMesh = struct {
vertex: core.Span,
index: core.Span,
name: core.Name,
jointRemap: ?[]u8, // this is NOT a string, they're joint indices.. which happen to be u8s
};
pub const MeshSourceType = enum { obj, gltf };
pub const LoadMeshSettings = struct {
path: []const u8,
sourceType: ?MeshSourceType = null,
skeletonName: ?core.Name,
};
pub fn loadIndexedMeshForPooling(meshName: core.Name, opt: LoadMeshSettings) !void {
var sourceType = MeshSourceType.gltf;
if (opt.sourceType) |st| {
sourceType = st;
}
// todo.. check if we have a cooked version of that file, if so just load that instead.
// otherwise, load the file
var update: MeshUpdate = undefined;
switch (sourceType) {
.obj => {
update = try loadIndexedMeshForPoolingObj(rend.getAllocator(), meshName, opt.path);
},
.gltf => {
update = try loadIndexedMeshForPoolingGltf(rend.getAllocator(), meshName, opt.skeletonName, opt.path);
},
}
try renderer.pushMeshUpdate(update);
}
const loadIndexedMeshForPoolingGltf = gltfLoader.loadIndexedMeshForPoolingGltf;
const loadIndexedMeshForPoolingObj = objLoader.loadIndexedMeshForPoolingObj;
const gltfLoader = @import("gltfLoader.zig");
const objLoader = @import("objLoader.zig");
const core = @import("core");
const rend = @import("../rend.zig");
const renderer = rend.renderer;
const std = @import("std");
const zgltf = @import("zgltf");