@@ -51,10 +51,10 @@ pub const Mesh = struct {
5151 zmesh_data : ? * zmesh.io.zcgltf.Data = null ,
5252
5353 pub fn initFromData (allocator : std.mem.Allocator , data : * zmesh.io.zcgltf.Data , mesh_index : usize , cfg : MeshConfig ) ? Mesh {
54- var bindings_list = std .ArrayList (graphics .Bindings ). init ( allocator ) ;
54+ var bindings_list : std .ArrayList (graphics .Bindings ) = .empty ;
5555
56- var vertices = std .ArrayList (PackedVertex ). init ( allocator ) ;
57- defer vertices .deinit ();
56+ var vertices : std .ArrayList (PackedVertex ) = .empty ;
57+ defer vertices .deinit (allocator );
5858
5959 var any_submesh_had_joints : bool = false ;
6060
@@ -63,24 +63,27 @@ pub const Mesh = struct {
6363
6464 const mesh = data .meshes .? [mesh_index ];
6565 for (0.. mesh .primitives_count ) | primitive_index | {
66- var mesh_indices = std .ArrayList (u32 ).init (allocator );
67- var mesh_positions = std .ArrayList ([3 ]f32 ).init (allocator );
68- var mesh_normals = std .ArrayList ([3 ]f32 ).init (allocator );
69- var mesh_texcoords = std .ArrayList ([2 ]f32 ).init (allocator );
70- var mesh_tangents = std .ArrayList ([4 ]f32 ).init (allocator );
71-
72- var mesh_joints = std .ArrayList ([4 ]f32 ).init (allocator );
73- var mesh_weights = std .ArrayList ([4 ]f32 ).init (allocator );
74-
75- defer mesh_indices .deinit ();
76- defer mesh_positions .deinit ();
77- defer mesh_normals .deinit ();
78- defer mesh_texcoords .deinit ();
79- defer mesh_tangents .deinit ();
80- defer mesh_joints .deinit ();
81- defer mesh_weights .deinit ();
66+ var mesh_indices : std .ArrayList (u32 ) = .empty ;
67+ var mesh_positions : std .ArrayList ([3 ]f32 ) = .empty ;
68+ var mesh_normals : std .ArrayList ([3 ]f32 ) = .empty ;
69+ var mesh_texcoords : std .ArrayList ([2 ]f32 ) = .empty ;
70+ var mesh_tangents : std .ArrayList ([4 ]f32 ) = .empty ;
71+
72+ // TODO appendMeshPrimitive does not accept weights and joints anymore
73+ // maybe remove?
74+ var mesh_joints : std .ArrayList ([4 ]f32 ) = .empty ;
75+ var mesh_weights : std .ArrayList ([4 ]f32 ) = .empty ;
76+
77+ defer mesh_indices .deinit (allocator );
78+ defer mesh_positions .deinit (allocator );
79+ defer mesh_normals .deinit (allocator );
80+ defer mesh_texcoords .deinit (allocator );
81+ defer mesh_tangents .deinit (allocator );
82+ defer mesh_joints .deinit (allocator );
83+ defer mesh_weights .deinit (allocator );
8284
8385 zmesh .io .appendMeshPrimitive (
86+ allocator ,
8487 data , // *zmesh.io.cgltf.Data
8588 @intCast (mesh_index ), // mesh index
8689 @intCast (primitive_index ), // gltf primitive index (submesh index)
@@ -89,8 +92,6 @@ pub const Mesh = struct {
8992 & mesh_normals , // normals (optional)
9093 & mesh_texcoords , // texcoords (optional)
9194 & mesh_tangents , // tangents (optional)
92- & mesh_joints , // joints (optional)
93- & mesh_weights , // weights (optional)
9495 ) catch {
9596 debug .log ("Could not process mesh file!" , .{});
9697 return null ;
@@ -107,7 +108,7 @@ pub const Mesh = struct {
107108 v_textcoord = mesh_texcoords .items [i ][1 ];
108109 }
109110
110- vertices .append (.{ .x = vert [0 ], .y = vert [1 ], .z = vert [2 ], .u = u_textcoord , .v = v_textcoord , .color = white_color }) catch {
111+ vertices .append (allocator , .{ .x = vert [0 ], .y = vert [1 ], .z = vert [2 ], .u = u_textcoord , .v = v_textcoord , .color = white_color }) catch {
111112 debug .log ("Could not process mesh file!" , .{});
112113 return null ;
113114 };
@@ -117,7 +118,7 @@ pub const Mesh = struct {
117118 if (mesh_normals .items .len == 0 ) {
118119 for (0.. mesh_positions .items .len ) | _ | {
119120 const empty = [3 ]f32 { 0.0 , 0.0 , 0.0 };
120- mesh_normals .append (empty ) catch {
121+ mesh_normals .append (allocator , empty ) catch {
121122 return null ;
122123 };
123124 }
@@ -127,7 +128,7 @@ pub const Mesh = struct {
127128 if (mesh_tangents .items .len == 0 ) {
128129 for (0.. mesh_positions .items .len ) | _ | {
129130 const empty = [4 ]f32 { 0.0 , 0.0 , 0.0 , 0.0 };
130- mesh_tangents .append (empty ) catch {
131+ mesh_tangents .append (allocator , empty ) catch {
131132 return null ;
132133 };
133134 }
@@ -159,7 +160,7 @@ pub const Mesh = struct {
159160 bindings .set (vertices .items [vertices_start .. ], mesh_indices .items , mesh_normals .items , mesh_tangents .items , mesh_indices .items .len );
160161 }
161162
162- bindings_list .append (bindings ) catch {
163+ bindings_list .append (allocator , bindings ) catch {
163164 return null ;
164165 };
165166
@@ -173,11 +174,12 @@ pub const Mesh = struct {
173174 return createMesh (vertices .items , bindings_list , cfg .materials );
174175 }
175176
176- pub fn deinit (self : * Mesh ) void {
177+ // TODO store a copy of allocator in mesh
178+ pub fn deinit (self : * Mesh , allocator : std.mem.Allocator ) void {
177179 for (self .bindings_list .items ) | * b | {
178180 b .destroy ();
179181 }
180- self .bindings_list .deinit ();
182+ self .bindings_list .deinit (allocator );
181183 }
182184
183185 /// Draw this mesh
@@ -279,18 +281,14 @@ pub fn getSkinnedShaderAttributes() []const graphics.ShaderAttribute {
279281
280282/// MeshBuilder is a helper for making runtime meshes
281283pub const MeshBuilder = struct {
282- vertices : std .ArrayList (PackedVertex ) = undefined ,
283- indices : std .ArrayList (u32 ) = undefined ,
284- normals : std .ArrayList ([3 ]f32 ) = undefined ,
285- tangents : std .ArrayList ([4 ]f32 ) = undefined ,
284+ vertices : std .ArrayList (PackedVertex ). empty ,
285+ indices : std .ArrayList (u32 ). empty ,
286+ normals : std .ArrayList ([3 ]f32 ). empty ,
287+ tangents : std .ArrayList ([4 ]f32 ). empty ,
286288 allocator : std.mem.Allocator = undefined ,
287289
288290 pub fn init (allocator : std.mem.Allocator ) MeshBuilder {
289291 return MeshBuilder {
290- .vertices = std .ArrayList (PackedVertex ).init (allocator ),
291- .indices = std .ArrayList (u32 ).init (allocator ),
292- .normals = std .ArrayList ([3 ]f32 ).init (allocator ),
293- .tangents = std .ArrayList ([4 ]f32 ).init (allocator ),
294292 .allocator = allocator ,
295293 };
296294 }
0 commit comments