liveBook · Manning

OpenGL context

As explained in Systems Programming with Zig

An OpenGL context is the state container associated with an OpenGL connection to a display system and its rendering resources.

Before an application can draw with OpenGL, it needs an OpenGL context. The context stores the state associated with an instance of OpenGL; think of it as the configuration for the rendering pipeline. Change that state through OpenGL functions, then issue a draw call to write pixels to the current framebuffer. section 7.4.1 section 7.6

An OpenGL object holds state of its own. When the object is bound to the context, its state is mapped into the context’s state. Objects can refer to stored data and state such as shaders, images, and geometry. section 7.4.1 section 7.6

GLFW handles platform-specific work and lets an application request OpenGL features and acquire a context. To call OpenGL functions consistently across platforms, glad supplies an OpenGL 3.3 Core header and loads the system’s OpenGL implementation. This avoids relying on operating-system headers that may be missing or out of sync with supported versions. section 7.1.3

Listing 7.7 Aren’t you glad we’re loading GL?
pub fn main() !void {
    // ... from init to window ...

    const glad_res = c.gladLoadGL(@ptrCast(&c.glfwGetProcAddress)); #1
    if (glad_res == 0) return error.GladLoadGlFailed; #1

    // ... main loop ...
}

After window setup, this Zig listing loads OpenGL through GLFW’s procedure-address function and checks for failure. section 7.4.1

How Systems Programming with Zig covers it

How deep it goes
Explains context state and object binding through a pipeline metaphor, while acknowledging that the abstractions resist a short explanation.
Examples
Loads OpenGL through GLFW in a Zig main function and checks glad’s return value for failure.
What you can do after
Operate/configure an OpenGL context using GLFW and load OpenGL functions with glad.
How it connects
Connects contexts to OpenGL objects, the rasterization pipeline, draw calls, and framebuffers.

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.