mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-08-02 14:57:31 +03:00
opengl: buffers
This commit is contained in:
18
src/main.zig
18
src/main.zig
@ -52,17 +52,11 @@ pub fn main() !void {
|
|||||||
};
|
};
|
||||||
const vao = try gl.VertexArray.create();
|
const vao = try gl.VertexArray.create();
|
||||||
defer vao.destroy();
|
defer vao.destroy();
|
||||||
var vbo: c_uint = undefined;
|
const vbo = try gl.Buffer.create();
|
||||||
c.glGenBuffers(1, &vbo);
|
defer vbo.destroy();
|
||||||
try vao.bind();
|
try vao.bind();
|
||||||
|
try vbo.bind(c.GL_ARRAY_BUFFER);
|
||||||
c.glBindBuffer(c.GL_ARRAY_BUFFER, vbo);
|
try vbo.setData(c.GL_ARRAY_BUFFER, vertices[0..], c.GL_STATIC_DRAW);
|
||||||
c.glBufferData(
|
|
||||||
c.GL_ARRAY_BUFFER,
|
|
||||||
@as(isize, @sizeOf(@TypeOf(vertices))),
|
|
||||||
&vertices,
|
|
||||||
c.GL_STATIC_DRAW,
|
|
||||||
);
|
|
||||||
|
|
||||||
c.glVertexAttribPointer(
|
c.glVertexAttribPointer(
|
||||||
0,
|
0,
|
||||||
@ -74,8 +68,8 @@ pub fn main() !void {
|
|||||||
);
|
);
|
||||||
c.glEnableVertexAttribArray(0);
|
c.glEnableVertexAttribArray(0);
|
||||||
|
|
||||||
c.glBindBuffer(c.GL_ARRAY_BUFFER, 0);
|
try gl.Buffer.unbind(c.GL_ARRAY_BUFFER);
|
||||||
c.glBindVertexArray(0);
|
try gl.VertexArray.unbind();
|
||||||
|
|
||||||
// Wait for the user to close the window.
|
// Wait for the user to close the window.
|
||||||
while (!window.shouldClose()) {
|
while (!window.shouldClose()) {
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
//! API is exposed via the `c` constant.
|
//! API is exposed via the `c` constant.
|
||||||
|
|
||||||
pub const c = @import("opengl/c.zig");
|
pub const c = @import("opengl/c.zig");
|
||||||
|
pub const Buffer = @import("opengl/Buffer.zig");
|
||||||
pub const Program = @import("opengl/Program.zig");
|
pub const Program = @import("opengl/Program.zig");
|
||||||
pub const Shader = @import("opengl/Shader.zig");
|
pub const Shader = @import("opengl/Shader.zig");
|
||||||
pub const VertexArray = @import("opengl/VertexArray.zig");
|
pub const VertexArray = @import("opengl/VertexArray.zig");
|
||||||
|
56
src/opengl/Buffer.zig
Normal file
56
src/opengl/Buffer.zig
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
const Buffer = @This();
|
||||||
|
|
||||||
|
const c = @import("c.zig");
|
||||||
|
const errors = @import("errors.zig");
|
||||||
|
|
||||||
|
id: c.GLuint,
|
||||||
|
|
||||||
|
/// Create a single buffer.
|
||||||
|
pub inline fn create() !Buffer {
|
||||||
|
var vbo: c.GLuint = undefined;
|
||||||
|
c.glGenBuffers(1, &vbo);
|
||||||
|
return Buffer{ .id = vbo };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unbind any active vertex array.
|
||||||
|
pub inline fn unbind(target: c.GLenum) !void {
|
||||||
|
c.glBindBuffer(target, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// glBindBuffer
|
||||||
|
pub inline fn bind(v: Buffer, target: c.GLenum) !void {
|
||||||
|
c.glBindBuffer(target, v.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub inline fn setData(
|
||||||
|
v: Buffer,
|
||||||
|
target: c.GLenum,
|
||||||
|
data: []const f32,
|
||||||
|
usage: c.GLenum,
|
||||||
|
) !void {
|
||||||
|
// Maybe one day in debug mode we can validate that this buffer
|
||||||
|
// is currently bound.
|
||||||
|
_ = v;
|
||||||
|
|
||||||
|
// Determine the per-element size. This is all comptime-computed.
|
||||||
|
const dataInfo = @typeInfo(@TypeOf(data));
|
||||||
|
const size: usize = switch (dataInfo) {
|
||||||
|
.Pointer => |ptr| switch (ptr.size) {
|
||||||
|
.Slice => @sizeOf(ptr.child),
|
||||||
|
else => unreachable,
|
||||||
|
},
|
||||||
|
else => unreachable,
|
||||||
|
};
|
||||||
|
|
||||||
|
c.glBufferData(
|
||||||
|
target,
|
||||||
|
@intCast(isize, size * data.len),
|
||||||
|
data.ptr,
|
||||||
|
usage,
|
||||||
|
);
|
||||||
|
try errors.getError();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub inline fn destroy(v: Buffer) void {
|
||||||
|
c.glDeleteBuffers(1, &v.id);
|
||||||
|
}
|
@ -11,6 +11,11 @@ pub inline fn create() !VertexArray {
|
|||||||
return VertexArray{ .id = vao };
|
return VertexArray{ .id = vao };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Unbind any active vertex array.
|
||||||
|
pub inline fn unbind() !void {
|
||||||
|
c.glBindVertexArray(0);
|
||||||
|
}
|
||||||
|
|
||||||
/// glBindVertexArray
|
/// glBindVertexArray
|
||||||
pub inline fn bind(v: VertexArray) !void {
|
pub inline fn bind(v: VertexArray) !void {
|
||||||
c.glBindVertexArray(v.id);
|
c.glBindVertexArray(v.id);
|
||||||
|
Reference in New Issue
Block a user