Skip to content

lawnjunk/zgl

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ZGL 4.1 Fork – Zig OpenGL Bindings

This library provides a thin, type-safe binding for OpenGL.

Upstream Repo

Installation

Add zgl to your build.zig.zon with one of the following commands (depending on the version you want):

zig fetch --save https://github.com/slugbyte/zgl-4.1/archive/zgl_1_0.tar.gz zig fetch --save https://github.com/slugbyte/zgl-4.1/archive/zgl_3_3.tar.gz zig fetch --save https://github.com/slugbyte/zgl-4.1/archive/zgl_4_1.tar.gz

Then add the following to your build.zig:

const zgl = b.dependency("zgl", .{
    .target = target,
    .optimize = optimize,
});
exe.root_module.addImport("zgl", zgl.module("zgl"));

Then import it with const gl = @import("zgl");, and build as normal with zig build.

Example with mach-glfw

const std = @import("std");
const gl = @import("zgl");
const glfw = @import("mach-glfw");

fn errorCallback(error_code: glfw.ErrorCode, description: [:0]const u8) void {
    std.log.err("glfw error code ({}): {s}\n", .{ error_code, description });
}

fn getProcAddress(_: type, symbolName: [:0]const u8) ?gl.binding.FunctionPointer {
    return glfw.getProcAddress(symbolName);
}

pub fn main() !void {
    glfw.setErrorCallback(errorCallback);

    if (!glfw.init(.{})) {
        std.log.err("failed to init glfw: {?s}", .{glfw.getErrorString()});
        std.process.exit(1);
    }
    defer glfw.terminate();

    const window_hints: glfw.Window.Hints = .{
        .context_version_major = 4,
        .context_version_minor = 1,
        .opengl_profile = .opengl_core_profile,
        .opengl_forward_compat = true,
        .context_debug = true,
    };
    const window = glfw.Window.create(640, 480, "OpenGL Window", null, null, window_hints) orelse {
        std.log.err("failed to create glfw window: {?s}", .{glfw.getErrorString()});
        std.process.exit(1);
    };
    defer window.destroy();

    glfw.makeContextCurrent(window);
    defer glfw.makeContextCurrent(null);

    gl.loadExtensions(void, getProcAddress) catch |err| {
        std.log.err("failed to load gl extenstion: {any}", .{err});
        std.process.exit(1);
    };

    while (!window.shouldClose()) {
        window.swapBuffers();

        gl.clearColor(1.0, 1.0, 1.0, 1.0);
        gl.clear(.{ .color = true });

        glfw.pollEvents();
    }
}

Development Philosophy

This library is developed incrementally. That means that functions and other things will be included on-demand and not just for the sake of completeness.

If you think a function is missing, fork the library, implement the missing function similar to the other functions and make a pull request. Issues that request implementation of missing functions will be closed immediatly.

Generated Bindings

This library includes OpenGL 4.1 bindings, generated by zig-opengl. Bindings for a different version may be substituted by replacing binding.zig.

Packages

No packages published

Languages

  • Zig 100.0%