Minimal Zig parser for .obj
and .mtl
files.
The following features are implemented:
OBJ files:
- Vertices
- Texture coordinates
- Normals
- Objects
MTL files:
- Bump map
- Diffuse map
- Specular map
- Ambient color
- Diffuse color
- Specular color
- Specular highlight
- Emissive coefficient
- Optical density
- Dissolve
- Illumination
If something is missing or not working properly, feel free to open an issue/pull request and I’ll take a look.
Add module to your projects build.zig.zon
file (replacing <COMMIT>
with the
commit you want to use):
.{
.name = "my-project",
.version = "1.0.0",
.dependencies = .{
.obj = .{
.url = "https://github.com/chip2n/zig-obj/archive/<COMMIT>.tar.gz",
},
},
}
Run zig build
once, get the hash and add it to build.zig.zon
:
.{
.name = "my-project",
.version = "1.0.0",
.dependencies = .{
.obj = .{
.url = "https://github.com/chip2n/zig-obj/archive/<COMMIT>.tar.gz",
.hash = "<HASH>",
},
},
}
Add the dependency to your executable in build.zig
:
pub fn build(b: *std.build.Builder) void {
...
const obj_mod = b.dependency("obj", .{ .target = target, .optimize = optimize }).module("obj");
exe.addModule("obj", obj_mod);
}
Build a static library by running:
zig build
const obj = @import("obj");
var model = try obj.parseObj(allocator, @embedFile("cube.obj"));
defer model.deinit(allocator);
var material = try obj.parseMtl(allocator, @embedFile("cube.mtl"));
defer material.deinit(allocator);
Tests are being ran automatically each day using the nightly Zig build.
Run the test suite manually with:
zig build test