summaryrefslogtreecommitdiff
path: root/pkg/opengl/VertexArray.zig
blob: 44bf3162187baa2ff62c7467436f39615fc25a63 (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
const VertexArray = @This();

const c = @import("c.zig").c;
const glad = @import("glad.zig");
const errors = @import("errors.zig");

id: c.GLuint,

/// Create a single vertex array object.
pub fn create() !VertexArray {
    var vao: c.GLuint = undefined;
    glad.context.GenVertexArrays.?(1, &vao);
    return VertexArray{ .id = vao };
}

/// glBindVertexArray
pub fn bind(v: VertexArray) !Binding {
    glad.context.BindVertexArray.?(v.id);
    try errors.getError();
    return .{};
}

pub fn destroy(v: VertexArray) void {
    glad.context.DeleteVertexArrays.?(1, &v.id);
}

pub const Binding = struct {
    pub fn unbind(self: Binding) void {
        _ = self;
        glad.context.BindVertexArray.?(0);
    }

    pub fn enableAttribArray(_: Binding, idx: c.GLuint) !void {
        glad.context.EnableVertexAttribArray.?(idx);
        try errors.getError();
    }

    pub fn bindingDivisor(_: Binding, idx: c.GLuint, divisor: c.GLuint) !void {
        glad.context.VertexBindingDivisor.?(idx, divisor);
        try errors.getError();
    }

    pub fn attributeBinding(
        _: Binding,
        attrib_idx: c.GLuint,
        binding_idx: c.GLuint,
    ) !void {
        glad.context.VertexAttribBinding.?(attrib_idx, binding_idx);
        try errors.getError();
    }

    pub fn attributeFormat(
        _: Binding,
        idx: c.GLuint,
        size: c.GLint,
        typ: c.GLenum,
        normalized: bool,
        offset: c.GLuint,
    ) !void {
        glad.context.VertexAttribFormat.?(
            idx,
            size,
            typ,
            @intCast(@intFromBool(normalized)),
            offset,
        );
        try errors.getError();
    }

    pub fn attributeIFormat(
        _: Binding,
        idx: c.GLuint,
        size: c.GLint,
        typ: c.GLenum,
        offset: c.GLuint,
    ) !void {
        glad.context.VertexAttribIFormat.?(
            idx,
            size,
            typ,
            offset,
        );
        try errors.getError();
    }

    pub fn attributeLFormat(
        _: Binding,
        idx: c.GLuint,
        size: c.GLint,
        offset: c.GLuint,
    ) !void {
        glad.context.VertexAttribLFormat.?(
            idx,
            size,
            c.GL_DOUBLE,
            offset,
        );
        try errors.getError();
    }

    pub fn bindVertexBuffer(
        _: Binding,
        idx: c.GLuint,
        buffer: c.GLuint,
        offset: c.GLintptr,
        stride: c.GLsizei,
    ) !void {
        glad.context.BindVertexBuffer.?(
            idx,
            buffer,
            offset,
            stride,
        );
        try errors.getError();
    }
};