Skip to content

Commit c3e96a6

Browse files
committed
files from monorepo @ 14e6258901c6fb47ae598705682837ef6c298e60
0 parents  commit c3e96a6

33 files changed

+21856
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Ignore some special directories
2+
.zig-cache
3+
zig-out
4+
5+
# Ignore some special OS files
6+
*.DS_Store

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Michal Ziulek
4+
Copyright (c) 2024 zig-gamedev contributors
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.

README.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
![image](logo.jpg)
2+
3+
# [zmesh](https://github.com/zig-gamedev/zmesh)
4+
5+
Zig library for loading, generating, processing and optimising triangle meshes.
6+
7+
Under the hood this library uses below C/C++ libraries:
8+
9+
* [par shapes](https://github.com/prideout/par/blob/master/par_shapes.h)
10+
* [meshoptimizer](https://github.com/zeux/meshoptimizer)
11+
* [cgltf](https://github.com/jkuhlmann/cgltf)
12+
13+
All memory allocations go through user-supplied, Zig allocator.
14+
15+
As an example program please see [procedural mesh (wgpu)](https://github.com/michal-z/zig-gamedev/tree/main/samples/procedural_mesh_wgpu).
16+
17+
## Getting started
18+
19+
Example `build.zig`:
20+
```zig
21+
pub fn build(b: *std.Build) void {
22+
const exe = b.addExecutable(.{ ... });
23+
24+
const zmesh = b.dependency("zmesh", .{});
25+
exe.root_module.addImport("zmesh", zmesh.module("root"));
26+
exe.linkLibrary(zmesh.artifact("zmesh"));
27+
}
28+
```
29+
30+
Now in your code you may import and use `zmesh`:
31+
32+
```zig
33+
const zmesh = @import("zmesh");
34+
35+
pub fn main() !void {
36+
...
37+
zmesh.init(allocator);
38+
defer zmesh.deinit();
39+
40+
var custom = zmesh.Shape.init(indices, positions, normals, texcoords);
41+
defer custom.deinit();
42+
43+
var disk = zmesh.Shape.initParametricDisk(10, 2);
44+
defer disk.deinit();
45+
disk.invert(0, 0);
46+
47+
var cylinder = zmesh.Shape.initCylinder(10, 4);
48+
defer cylinder.deinit();
49+
50+
cylinder.merge(disk);
51+
cylinder.translate(0, 0, -1);
52+
disk.invert(0, 0);
53+
cylinder.merge(disk);
54+
55+
cylinder.scale(0.5, 0.5, 2);
56+
cylinder.rotate(math.pi * 0.5, 1.0, 0.0, 0.0);
57+
58+
cylinder.unweld();
59+
cylinder.computeNormals();
60+
...
61+
}
62+
```
63+
64+
```zig
65+
const zmesh = @import("zmesh");
66+
67+
pub fn main() !void {
68+
zmesh.init(allocator);
69+
defer zmesh.deinit();
70+
71+
//
72+
// Load mesh
73+
//
74+
const data = try zmesh.io.zcgltf.parseAndLoadFile(content_dir ++ "cube.gltf");
75+
defer zmesh.io.zcgltf.freeData(data);
76+
77+
var mesh_indices = std.ArrayList(u32).init(allocator);
78+
var mesh_positions = std.ArrayList([3]f32).init(allocator);
79+
var mesh_normals = std.ArrayList([3]f32).init(allocator);
80+
81+
zmesh.io.zcgltf.appendMeshPrimitive(
82+
data,
83+
0, // mesh index
84+
0, // gltf primitive index (submesh index)
85+
&mesh_indices,
86+
&mesh_positions,
87+
&mesh_normals, // normals (optional)
88+
null, // texcoords (optional)
89+
null, // tangents (optional)
90+
);
91+
...
92+
93+
//
94+
// Optimize mesh
95+
//
96+
const Vertex = struct {
97+
position: [3]f32,
98+
normal: [3]f32,
99+
};
100+
101+
var remap = std.ArrayList(u32).init(allocator);
102+
remap.resize(src_indices.items.len) catch unreachable;
103+
104+
const num_unique_vertices = zmesh.opt.generateVertexRemap(
105+
remap.items, // 'vertex remap' (destination)
106+
src_indices.items, // non-optimized indices
107+
Vertex, // Zig type describing your vertex
108+
src_vertices.items, // non-optimized vertices
109+
);
110+
111+
var optimized_vertices = std.ArrayList(Vertex).init(allocator);
112+
optimized_vertices.resize(num_unique_vertices) catch unreachable;
113+
114+
zmesh.opt.remapVertexBuffer(
115+
Vertex, // Zig type describing your vertex
116+
optimized_vertices.items, // optimized vertices (destination)
117+
src_vertices.items, // non-optimized vertices (source)
118+
remap.items, // 'vertex remap' generated by generateVertexRemap()
119+
);
120+
121+
// More optimization steps are available - see `zmeshoptimizer.zig` file.
122+
}
123+
```

build.zig

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) void {
4+
const optimize = b.standardOptimizeOption(.{});
5+
const target = b.standardTargetOptions(.{});
6+
7+
const options = .{
8+
.shape_use_32bit_indices = b.option(
9+
bool,
10+
"shape_use_32bit_indices",
11+
"Enable par shapes 32-bit indices",
12+
) orelse true,
13+
.shared = b.option(
14+
bool,
15+
"shared",
16+
"Build as shared library",
17+
) orelse false,
18+
};
19+
20+
const options_step = b.addOptions();
21+
inline for (std.meta.fields(@TypeOf(options))) |field| {
22+
options_step.addOption(field.type, field.name, @field(options, field.name));
23+
}
24+
25+
const options_module = options_step.createModule();
26+
27+
_ = b.addModule("root", .{
28+
.root_source_file = b.path("src/root.zig"),
29+
.imports = &.{
30+
.{ .name = "zmesh_options", .module = options_module },
31+
},
32+
});
33+
34+
const zmesh_lib = if (options.shared) blk: {
35+
const lib = b.addSharedLibrary(.{
36+
.name = "zmesh",
37+
.target = target,
38+
.optimize = optimize,
39+
});
40+
41+
if (target.result.os.tag == .windows) {
42+
lib.defineCMacro("PAR_SHAPES_API", "__declspec(dllexport)");
43+
lib.defineCMacro("CGLTF_API", "__declspec(dllexport)");
44+
lib.defineCMacro("MESHOPTIMIZER_API", "__declspec(dllexport)");
45+
lib.defineCMacro("ZMESH_API", "__declspec(dllexport)");
46+
}
47+
48+
break :blk lib;
49+
} else b.addStaticLibrary(.{
50+
.name = "zmesh",
51+
.target = target,
52+
.optimize = optimize,
53+
});
54+
b.installArtifact(zmesh_lib);
55+
56+
zmesh_lib.linkLibC();
57+
if (target.result.abi != .msvc)
58+
zmesh_lib.linkLibCpp();
59+
60+
const par_shapes_t = if (options.shape_use_32bit_indices)
61+
"-DPAR_SHAPES_T=uint32_t"
62+
else
63+
"-DPAR_SHAPES_T=uint16_t";
64+
65+
zmesh_lib.addIncludePath(b.path("libs/par_shapes"));
66+
zmesh_lib.addCSourceFile(.{
67+
.file = b.path("libs/par_shapes/par_shapes.c"),
68+
.flags = &.{ "-std=c99", "-fno-sanitize=undefined", par_shapes_t },
69+
});
70+
71+
zmesh_lib.addCSourceFiles(.{
72+
.files = &.{
73+
"libs/meshoptimizer/clusterizer.cpp",
74+
"libs/meshoptimizer/indexgenerator.cpp",
75+
"libs/meshoptimizer/vcacheoptimizer.cpp",
76+
"libs/meshoptimizer/vcacheanalyzer.cpp",
77+
"libs/meshoptimizer/vfetchoptimizer.cpp",
78+
"libs/meshoptimizer/vfetchanalyzer.cpp",
79+
"libs/meshoptimizer/overdrawoptimizer.cpp",
80+
"libs/meshoptimizer/overdrawanalyzer.cpp",
81+
"libs/meshoptimizer/simplifier.cpp",
82+
"libs/meshoptimizer/allocator.cpp",
83+
},
84+
.flags = &.{""},
85+
});
86+
zmesh_lib.addIncludePath(b.path("libs/cgltf"));
87+
zmesh_lib.addCSourceFile(.{
88+
.file = b.path("libs/cgltf/cgltf.c"),
89+
.flags = &.{"-std=c99"},
90+
});
91+
92+
const test_step = b.step("test", "Run zmesh tests");
93+
94+
const tests = b.addTest(.{
95+
.name = "zmesh-tests",
96+
.root_source_file = b.path("src/root.zig"),
97+
.target = target,
98+
.optimize = optimize,
99+
});
100+
b.installArtifact(tests);
101+
102+
tests.linkLibrary(zmesh_lib);
103+
tests.addIncludePath(b.path("libs/cgltf"));
104+
105+
test_step.dependOn(&b.addRunArtifact(tests).step);
106+
}

build.zig.zon

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.{
2+
.name = "zmesh",
3+
.version = "0.11.0-dev",
4+
.paths = .{
5+
"build.zig",
6+
"build.zig.zon",
7+
"libs",
8+
"src",
9+
"README.md",
10+
"LICENSE",
11+
},
12+
}

libs/cgltf/cgltf.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#define CGLTF_IMPLEMENTATION
2+
#define CGLTF_WRITE_IMPLEMENTATION
3+
#include "cgltf_write.h"

0 commit comments

Comments
 (0)