Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,12 @@ pub fn main() !void {
const data = try zmesh.io.zcgltf.parseAndLoadFile(content_dir ++ "cube.gltf");
defer zmesh.io.zcgltf.freeData(data);

var mesh_indices = std.ArrayList(u32).init(allocator);
var mesh_positions = std.ArrayList([3]f32).init(allocator);
var mesh_normals = std.ArrayList([3]f32).init(allocator);
var mesh_indices = std.ArrayListUnmanaged(u32){};
var mesh_positions = std.ArrayListUnmanaged([3]f32){};
var mesh_normals = std.ArrayListUnmanaged([3]f32){};

zmesh.io.zcgltf.appendMeshPrimitive(
try zmesh.io.zcgltf.appendMeshPrimitive(
allocator,
data,
0, // mesh index
0, // gltf primitive index (submesh index)
Expand All @@ -98,8 +99,8 @@ pub fn main() !void {
normal: [3]f32,
};

var remap = std.ArrayList(u32).init(allocator);
remap.resize(src_indices.items.len) catch unreachable;
var remap = std.ArrayListUnmanaged(u32){};
try remap.resize(allocator, src_indices.items.len);

const num_unique_vertices = zmesh.opt.generateVertexRemap(
remap.items, // 'vertex remap' (destination)
Expand All @@ -108,8 +109,8 @@ pub fn main() !void {
src_vertices.items, // non-optimized vertices
);

var optimized_vertices = std.ArrayList(Vertex).init(allocator);
optimized_vertices.resize(num_unique_vertices) catch unreachable;
var optimized_vertices = std.ArrayListUnmanaged(Vertex){};
try optimized_vertices.resize(allocator, num_unique_vertices);

zmesh.opt.remapVertexBuffer(
Vertex, // Zig type describing your vertex
Expand Down
37 changes: 16 additions & 21 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,31 @@ pub fn build(b: *std.Build) void {
}

const options_module = options_step.createModule();

_ = b.addModule("root", .{
const zmesh_module = b.addModule("root", .{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "zmesh_options", .module = options_module },
},
});

const zmesh_lib = if (options.shared) blk: {
const lib = b.addSharedLibrary(.{
.name = "zmesh",
const zmesh_lib = b.addLibrary(.{
.name = "zmesh",
.linkage = if (options.shared) .dynamic else .static,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
});
}),
});

if (target.result.os.tag == .windows) {
lib.root_module.addCMacro("PAR_SHAPES_API", "__declspec(dllexport)");
lib.root_module.addCMacro("CGLTF_API", "__declspec(dllexport)");
lib.root_module.addCMacro("MESHOPTIMIZER_API", "__declspec(dllexport)");
lib.root_module.addCMacro("ZMESH_API", "__declspec(dllexport)");
}
if (options.shared and target.result.os.tag == .windows) {
zmesh_lib.root_module.addCMacro("PAR_SHAPES_API", "__declspec(dllexport)");
zmesh_lib.root_module.addCMacro("CGLTF_API", "__declspec(dllexport)");
zmesh_lib.root_module.addCMacro("MESHOPTIMIZER_API", "__declspec(dllexport)");
zmesh_lib.root_module.addCMacro("ZMESH_API", "__declspec(dllexport)");
}

break :blk lib;
} else b.addStaticLibrary(.{
.name = "zmesh",
.target = target,
.optimize = optimize,
});
b.installArtifact(zmesh_lib);

zmesh_lib.linkLibC();
Expand Down Expand Up @@ -93,9 +90,7 @@ pub fn build(b: *std.Build) void {

const tests = b.addTest(.{
.name = "zmesh-tests",
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
.root_module = zmesh_module,
});
b.installArtifact(tests);

Expand Down
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.name = .zmesh,
.fingerprint = 0xb56bea39e6c0eda0,
.version = "0.11.0-dev",
.minimum_zig_version = "0.14.0",
.minimum_zig_version = "0.15.1",
.paths = .{
"build.zig",
"build.zig.zon",
Expand Down
26 changes: 14 additions & 12 deletions src/Shape.zig
Original file line number Diff line number Diff line change
Expand Up @@ -458,20 +458,22 @@ test "zmesh.invert" {
test "zmesh.custom" {
const zmesh = @import("root.zig");

zmesh.init(std.testing.allocator);
const allocator = std.testing.allocator;

zmesh.init(allocator);
defer zmesh.deinit();

var positions = std.ArrayList([3]f32).init(std.testing.allocator);
defer positions.deinit();
try positions.append(.{ 0.0, 0.0, 0.0 });
try positions.append(.{ 1.0, 0.0, 0.0 });
try positions.append(.{ 1.0, 0.0, 1.0 });

var indices = std.ArrayList(IndexType).init(std.testing.allocator);
defer indices.deinit();
try indices.append(0);
try indices.append(1);
try indices.append(2);
var positions: std.ArrayList([3]f32) = .{};
defer positions.deinit(allocator);
try positions.append(allocator, .{ 0.0, 0.0, 0.0 });
try positions.append(allocator, .{ 1.0, 0.0, 0.0 });
try positions.append(allocator, .{ 1.0, 0.0, 1.0 });

var indices: std.ArrayList(IndexType) = .{};
defer indices.deinit(allocator);
try indices.append(allocator, 0);
try indices.append(allocator, 1);
try indices.append(allocator, 2);

var shape = Shape.init(indices, positions, null, null);
defer shape.deinit();
Expand Down
136 changes: 1 addition & 135 deletions src/io.zig
Original file line number Diff line number Diff line change
@@ -1,145 +1,11 @@
const std = @import("std");
const assert = std.debug.assert;

const mem = @import("memory.zig");

/// Deprecated. Use `zmesh.io.zcgltf.parseAndLoadFile` instead.
pub const parseAndLoadFile = zcgltf.parseAndLoadFile;
/// Deprecated. Use `zmesh.io.zcgltf.freeData` instead.
pub const freeData = zcgltf.freeData;
/// Deprecated. Use `zmesh.io.zcgltf.appendMeshPrimitive` instead.
pub const appendMeshPrimitive = zcgltf.appendMeshPrimitive;

pub const zcgltf = struct {
const bindings = @import("zcgltf.zig");
const Data = bindings.Data;

pub usingnamespace bindings;

pub fn parseAndLoadFile(pathname: [:0]const u8) bindings.Error!*Data {
const options = bindings.Options{
.memory = .{
.alloc_func = mem.zmeshAllocUser,
.free_func = mem.zmeshFreeUser,
},
};

const data = try bindings.parseFile(options, pathname);
errdefer bindings.free(data);

try bindings.loadBuffers(options, data, pathname);

return data;
}

pub fn freeData(data: *Data) void {
bindings.free(data);
}

pub fn appendMeshPrimitive(
data: *Data,
mesh_index: u32,
prim_index: u32,
indices: *std.ArrayList(u32),
positions: *std.ArrayList([3]f32),
normals: ?*std.ArrayList([3]f32),
texcoords0: ?*std.ArrayList([2]f32),
tangents: ?*std.ArrayList([4]f32),
) !void {
assert(mesh_index < data.meshes_count);
assert(prim_index < data.meshes.?[mesh_index].primitives_count);

const mesh = &data.meshes.?[mesh_index];
const prim = &mesh.primitives[prim_index];

const num_vertices: u32 = @as(u32, @intCast(prim.attributes[0].data.count));
const num_indices: u32 = @as(u32, @intCast(prim.indices.?.count));

// Indices.
{
try indices.ensureTotalCapacity(indices.items.len + num_indices);

const accessor = prim.indices.?;
const buffer_view = accessor.buffer_view.?;

assert(accessor.stride == buffer_view.stride or buffer_view.stride == 0);
assert(buffer_view.buffer.data != null);

const data_addr = @as([*]const u8, @ptrCast(buffer_view.buffer.data)) +
accessor.offset + buffer_view.offset;

if (accessor.stride == 1) {
if (accessor.component_type != .r_8u) {
return error.InvalidIndicesAccessorComponentType;
}
const src = @as([*]const u8, @ptrCast(data_addr));
var i: u32 = 0;
while (i < num_indices) : (i += 1) {
indices.appendAssumeCapacity(src[i]);
}
} else if (accessor.stride == 2) {
if (accessor.component_type != .r_16u) {
return error.InvalidIndicesAccessorComponentType;
}
const src = @as([*]const u16, @ptrCast(@alignCast(data_addr)));
var i: u32 = 0;
while (i < num_indices) : (i += 1) {
indices.appendAssumeCapacity(src[i]);
}
} else if (accessor.stride == 4) {
if (accessor.component_type != .r_32u) {
return error.InvalidIndicesAccessorComponentType;
}
const src = @as([*]const u32, @ptrCast(@alignCast(data_addr)));
var i: u32 = 0;
while (i < num_indices) : (i += 1) {
indices.appendAssumeCapacity(src[i]);
}
} else {
return error.InvalidIndicesAccessorStride;
}
}

// Attributes.
{
const attributes = prim.attributes[0..prim.attributes_count];
for (attributes) |attrib| {
const accessor = attrib.data;
assert(accessor.component_type == .r_32f);

const buffer_view = accessor.buffer_view.?;
assert(buffer_view.buffer.data != null);

assert(accessor.stride == buffer_view.stride or buffer_view.stride == 0);
assert(accessor.stride * accessor.count == buffer_view.size);

const data_addr = @as([*]const u8, @ptrCast(buffer_view.buffer.data)) +
accessor.offset + buffer_view.offset;

if (attrib.type == .position) {
assert(accessor.type == .vec3);
const slice = @as([*]const [3]f32, @ptrCast(@alignCast(data_addr)))[0..num_vertices];
try positions.appendSlice(slice);
} else if (attrib.type == .normal) {
if (normals) |n| {
assert(accessor.type == .vec3);
const slice = @as([*]const [3]f32, @ptrCast(@alignCast(data_addr)))[0..num_vertices];
try n.appendSlice(slice);
}
} else if (attrib.type == .texcoord) {
if (texcoords0) |tc| {
assert(accessor.type == .vec2);
const slice = @as([*]const [2]f32, @ptrCast(@alignCast(data_addr)))[0..num_vertices];
try tc.appendSlice(slice);
}
} else if (attrib.type == .tangent) {
if (tangents) |tan| {
assert(accessor.type == .vec4);
const slice = @as([*]const [4]f32, @ptrCast(@alignCast(data_addr)))[0..num_vertices];
try tan.appendSlice(slice);
}
}
}
}
}
};
pub const zcgltf = @import("zcgltf.zig");
Loading
Loading