|
1 | 1 | const std = @import("std"); |
2 | 2 | const assert = std.debug.assert; |
3 | 3 |
|
4 | | -const mem = @import("memory.zig"); |
5 | | - |
6 | 4 | /// Deprecated. Use `zmesh.io.zcgltf.parseAndLoadFile` instead. |
7 | 5 | pub const parseAndLoadFile = zcgltf.parseAndLoadFile; |
8 | 6 | /// Deprecated. Use `zmesh.io.zcgltf.freeData` instead. |
9 | 7 | pub const freeData = zcgltf.freeData; |
10 | 8 | /// Deprecated. Use `zmesh.io.zcgltf.appendMeshPrimitive` instead. |
11 | 9 | pub const appendMeshPrimitive = zcgltf.appendMeshPrimitive; |
12 | 10 |
|
13 | | -pub const zcgltf = struct { |
14 | | - const bindings = @import("zcgltf.zig"); |
15 | | - const Data = bindings.Data; |
16 | | - |
17 | | - pub usingnamespace bindings; |
18 | | - |
19 | | - pub fn parseAndLoadFile(pathname: [:0]const u8) bindings.Error!*Data { |
20 | | - const options = bindings.Options{ |
21 | | - .memory = .{ |
22 | | - .alloc_func = mem.zmeshAllocUser, |
23 | | - .free_func = mem.zmeshFreeUser, |
24 | | - }, |
25 | | - }; |
26 | | - |
27 | | - const data = try bindings.parseFile(options, pathname); |
28 | | - errdefer bindings.free(data); |
29 | | - |
30 | | - try bindings.loadBuffers(options, data, pathname); |
31 | | - |
32 | | - return data; |
33 | | - } |
34 | | - |
35 | | - pub fn freeData(data: *Data) void { |
36 | | - bindings.free(data); |
37 | | - } |
38 | | - |
39 | | - pub fn appendMeshPrimitive( |
40 | | - data: *Data, |
41 | | - mesh_index: u32, |
42 | | - prim_index: u32, |
43 | | - indices: *std.ArrayList(u32), |
44 | | - positions: *std.ArrayList([3]f32), |
45 | | - normals: ?*std.ArrayList([3]f32), |
46 | | - texcoords0: ?*std.ArrayList([2]f32), |
47 | | - tangents: ?*std.ArrayList([4]f32), |
48 | | - ) !void { |
49 | | - assert(mesh_index < data.meshes_count); |
50 | | - assert(prim_index < data.meshes.?[mesh_index].primitives_count); |
51 | | - |
52 | | - const mesh = &data.meshes.?[mesh_index]; |
53 | | - const prim = &mesh.primitives[prim_index]; |
54 | | - |
55 | | - const num_vertices: u32 = @as(u32, @intCast(prim.attributes[0].data.count)); |
56 | | - const num_indices: u32 = @as(u32, @intCast(prim.indices.?.count)); |
57 | | - |
58 | | - // Indices. |
59 | | - { |
60 | | - try indices.ensureTotalCapacity(indices.items.len + num_indices); |
61 | | - |
62 | | - const accessor = prim.indices.?; |
63 | | - const buffer_view = accessor.buffer_view.?; |
64 | | - |
65 | | - assert(accessor.stride == buffer_view.stride or buffer_view.stride == 0); |
66 | | - assert(buffer_view.buffer.data != null); |
67 | | - |
68 | | - const data_addr = @as([*]const u8, @ptrCast(buffer_view.buffer.data)) + |
69 | | - accessor.offset + buffer_view.offset; |
70 | | - |
71 | | - if (accessor.stride == 1) { |
72 | | - if (accessor.component_type != .r_8u) { |
73 | | - return error.InvalidIndicesAccessorComponentType; |
74 | | - } |
75 | | - const src = @as([*]const u8, @ptrCast(data_addr)); |
76 | | - var i: u32 = 0; |
77 | | - while (i < num_indices) : (i += 1) { |
78 | | - indices.appendAssumeCapacity(src[i]); |
79 | | - } |
80 | | - } else if (accessor.stride == 2) { |
81 | | - if (accessor.component_type != .r_16u) { |
82 | | - return error.InvalidIndicesAccessorComponentType; |
83 | | - } |
84 | | - const src = @as([*]const u16, @ptrCast(@alignCast(data_addr))); |
85 | | - var i: u32 = 0; |
86 | | - while (i < num_indices) : (i += 1) { |
87 | | - indices.appendAssumeCapacity(src[i]); |
88 | | - } |
89 | | - } else if (accessor.stride == 4) { |
90 | | - if (accessor.component_type != .r_32u) { |
91 | | - return error.InvalidIndicesAccessorComponentType; |
92 | | - } |
93 | | - const src = @as([*]const u32, @ptrCast(@alignCast(data_addr))); |
94 | | - var i: u32 = 0; |
95 | | - while (i < num_indices) : (i += 1) { |
96 | | - indices.appendAssumeCapacity(src[i]); |
97 | | - } |
98 | | - } else { |
99 | | - return error.InvalidIndicesAccessorStride; |
100 | | - } |
101 | | - } |
102 | | - |
103 | | - // Attributes. |
104 | | - { |
105 | | - const attributes = prim.attributes[0..prim.attributes_count]; |
106 | | - for (attributes) |attrib| { |
107 | | - const accessor = attrib.data; |
108 | | - assert(accessor.component_type == .r_32f); |
109 | | - |
110 | | - const buffer_view = accessor.buffer_view.?; |
111 | | - assert(buffer_view.buffer.data != null); |
112 | | - |
113 | | - assert(accessor.stride == buffer_view.stride or buffer_view.stride == 0); |
114 | | - assert(accessor.stride * accessor.count == buffer_view.size); |
115 | | - |
116 | | - const data_addr = @as([*]const u8, @ptrCast(buffer_view.buffer.data)) + |
117 | | - accessor.offset + buffer_view.offset; |
118 | | - |
119 | | - if (attrib.type == .position) { |
120 | | - assert(accessor.type == .vec3); |
121 | | - const slice = @as([*]const [3]f32, @ptrCast(@alignCast(data_addr)))[0..num_vertices]; |
122 | | - try positions.appendSlice(slice); |
123 | | - } else if (attrib.type == .normal) { |
124 | | - if (normals) |n| { |
125 | | - assert(accessor.type == .vec3); |
126 | | - const slice = @as([*]const [3]f32, @ptrCast(@alignCast(data_addr)))[0..num_vertices]; |
127 | | - try n.appendSlice(slice); |
128 | | - } |
129 | | - } else if (attrib.type == .texcoord) { |
130 | | - if (texcoords0) |tc| { |
131 | | - assert(accessor.type == .vec2); |
132 | | - const slice = @as([*]const [2]f32, @ptrCast(@alignCast(data_addr)))[0..num_vertices]; |
133 | | - try tc.appendSlice(slice); |
134 | | - } |
135 | | - } else if (attrib.type == .tangent) { |
136 | | - if (tangents) |tan| { |
137 | | - assert(accessor.type == .vec4); |
138 | | - const slice = @as([*]const [4]f32, @ptrCast(@alignCast(data_addr)))[0..num_vertices]; |
139 | | - try tan.appendSlice(slice); |
140 | | - } |
141 | | - } |
142 | | - } |
143 | | - } |
144 | | - } |
145 | | -}; |
| 11 | +pub const zcgltf = @import("zcgltf.zig"); |
0 commit comments