summaryrefslogtreecommitdiff
path: root/src/rendering/gltf.zig
blob: 888191da50bbe8f5cb5c147a0518de388c40f493 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const std = @import("std");
const mesh = @import("mesh.zig");
const Allocator = std.mem.Allocator;

pub const Model = struct {
    const Asset = struct {
        version: []u8,
        generator: ?[]u8 = null,
        copyright: ?[]u8 = null,
    };
    const Buffer = struct {
        byteLength: usize,
        uri: ?[]u8 = null,
    };
    const BufferView = struct {
        buffer: usize,
        byteLength: usize,
        byteOffset: usize,
        byteStride: ?usize = null,
        target: ?usize = null,
    };
    const Node = struct {
        name: []u8,
        mesh: ?usize = null,
        weights: ?[]f64 = null,
        children: ?[]usize = null,
        rotation: ?[4]f64 = null,
        scale: ?[3]f64 = null,
        translation: ?[3]f64 = null,
        camera: ?usize = null,
        matrix: ?[16]usize = null,
    };
    const Accessor = struct {
        bufferView: usize,
        byteOffset: ?usize = null,
        componentType: usize,
        count: usize,
        type: []u8,
        max: ?[]f64 = null,
        min: ?[]f64 = null,
    };
    const Primitive = struct {
        const Attributes = struct {
            NORMAL: ?usize = null,
            POSITION: ?usize = null,
            TANGENT: ?usize = null,
            TEXCOORD_0: ?usize = null,
            TEXCOORD_1: ?usize = null,
            COLOR_0: ?usize = null,
            JOINTS_0: ?usize = null,
            WEIGHTS_0: ?usize = null,
        };

        attributes: ?Attributes = null,
        indices: ?usize = null,
        material: ?usize = null,
        mode: ?usize = null,
    };
    const Mesh = struct {
        name: ?[]u8 = null,
        primitives: ?[]Primitive = null,
        weights: ?[]f64 = null,
    };
    const Skin = struct {
        inverseBindMatrices: usize,
        joints: []usize,
        skeleton: usize,
    };
    const Texture = struct {
        sampler: usize,
        source: usize,
    };
    const Image = struct {
        uri: ?[]u8 = null,
        bufferView: ?usize = null,
        mimeType: ?[]u8 = null,
    };
    const Material = struct {
        const Pbr = struct {
            baseColorFactor: ?[4]f64 = null,
            baseColorTexture: ?struct {
                index: usize,
                texCoord: usize,
            } = null,
            metallicFactor: ?f64 = null,
            roughnessFactor: ?f64 = null,
        };
        name: ?[]u8 = null,
        pbrMetallicRoughness: Pbr,
        doubleSided: bool,
    };
    const Scene = struct {
        nodes: ?[]usize = null,
        name: ?[]u8 = null,
    };

    const Chunk = packed struct {
        const offset = Header.offset + 8;
        length: u32,
        type: u32,
    };

    const JsonChunk = struct {
        asset: Asset,
        scene: usize,
        scenes: ?[]Scene = null,
        nodes: ?[]Node = null,
        materials: ?[]Material = null,
        meshes: ?[]Mesh = null,
        accessors: ?[]Accessor = null,
        bufferViews: ?[]BufferView = null,
        buffers: ?[]Buffer = null,
    };

    const Header = packed struct {
        const offset = 12;
        magic: u32,
        version: u32,
        length: u32,
    };

    const Binary = struct {
        data: []u8,
        const Vec3 = [3]f32;

        pub fn readU16(self: Binary, allocator: Allocator, view: BufferView, count: usize) ![]u16 {
            const data = self.data[view.byteOffset .. view.byteOffset + view.byteLength];
            const scalars = try allocator.alloc(u16, count);

            var j: usize = 0;
            for (0..data.len / 2) |i| {
                scalars[i] = std.mem.bytesAsValue(u16, data[j .. j + 1]).*;
                j += 2;
            }

            return scalars;
        }

        pub fn readVec3(self: Binary, allocator: Allocator, view: BufferView, count: usize) ![]Vec3 {
            const data = self.data[view.byteOffset .. view.byteOffset + view.byteLength];
            const vectors = try allocator.alloc(Vec3, count);

            for (0..count) |i| {
                vectors[i] = std.mem.bytesAsValue(Vec3, data[(@sizeOf(Vec3) * i) .. (@sizeOf(Vec3) * i) + @sizeOf(Vec3)]).*;
            }

            return vectors;
        }
    };
};

pub fn parseFile(allocator: Allocator, name: []const u8) !struct { vertices: [][3]f32, indices: []u16 } {
    const file = try std.fs.cwd().openFile(name, .{});
    const all = try file.readToEndAlloc(allocator, 1_000_000);
    const json_chunk = std.mem.bytesAsValue(Model.Chunk, all[Model.Header.offset..]);

    const data = (try std.json.parseFromSlice(Model.JsonChunk, allocator, @constCast(all[Model.Chunk.offset .. Model.Chunk.offset + json_chunk.length]), .{ .ignore_unknown_fields = true })).value;
    const binary = Model.Binary{ .data = all[Model.Chunk.offset + json_chunk.length + 8 ..] };

    const vertices = try binary.readVec3(allocator, data.bufferViews.?[data.meshes.?[0].primitives.?[0].attributes.?.POSITION.?], 24);
    const indices = try binary.readU16(allocator, data.bufferViews.?[data.meshes.?[0].primitives.?[0].indices.?], 36);
    std.debug.print("vertices: {any}\n", .{vertices});
    std.debug.print("indices: {any}\n", .{indices});

    return .{ .vertices = vertices, .indices = indices };
}