bens / zig-obj

Minimal Zig parser for .obj and .mtl files

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

zig-obj

https://github.com/chip2n/zig-obj/workflows/CI/badge.svg

Minimal Zig parser for .obj and .mtl files.

Features

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.

Getting started

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);
}

Building a static library

Build a static library by running:

zig build

Usage

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);

Running tests

Tests are being ran automatically each day using the nightly Zig build.

Run the test suite manually with:

zig build test

About

Minimal Zig parser for .obj and .mtl files

License:MIT License


Languages

Language:Zig 100.0%