liveBook · Manning

Glad

As explained in Systems Programming with Zig

glad is a generator for libraries that load graphics API functions for specified API versions and extensions.

When a graphics project needs a consistent OpenGL interface across platforms, system headers can be a headache: they may be missing or out of sync with the OpenGL version supported by the system. glad generates an OpenGL loader library for selected versions and extensions, including a consistent header for the chosen API. section 7.1.3 section 7.2.2

GLFW requests OpenGL features and acquires a context; glad supplies the loader interface. For a project targeting OpenGL 3.3, generate the core profile without extensions at gen.glad.sh. The core profile is enough when the project does not need OpenGL 1.x or 2.x functionality. section 7.1.3 section 7.2.2

Vendor the generated files in a glad/ directory alongside src/ and build.zig. Keeping the code in the project avoids depending on its continued availability, lets updates happen in lock-step with project changes, and leaves room for local fixes. section 7.2.2

To expose glad to Zig through src/c.h, include its header before GLFW’s header. GLFW can then detect that glad is in use. section 7.2.2

#include <glad/glad.h>
#include <GLFW/glfw3.h>

The include order puts glad before GLFW. section 7.2.2

The C translator also needs the generated header’s include path. Unlike the system-installed GLFW header in this setup, it cannot find glad’s header on its own. section 7.2.2

translator.addIncludePath(b.path("glad/include"));

Add glad/include to the translator’s search paths. section 7.2.2

Finally, compile the generated glad.c as part of the build. Zig’s compiler can compile the C source directly. section 7.2.2

c.addCSourceFile(.{ .file = b.path("glad/src/glad.c") });

Add the generated C source to the build. section 7.2.2

How Systems Programming with Zig covers it

How deep it goes
Walks through generation, vendoring, header ordering, and compilation without detailing runtime initialization.
Examples
Uses a generated OpenGL 3.3 core loader with no extensions in a Zig project.
What you can do after
Operate/configure it by generating a loader and integrating its headers and C source into a Zig build.
How it connects
Connects OpenGL loading with GLFW, OpenGL contexts, and C interoperability.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Systems Programming with Zig. The section links point to the parts of the book it draws on.