OpenGLAD is a cross-platform Swift Package that wraps GLAD 2, the OpenGL function loader, providing C/Swift interoperability through a Clang module map.
It is designed to be used in conjunction with an external OpenGL context manager such as OpenGLFW.
OpenGLfunction loader generated viaGLAD 2C/Swiftinteroperability via module map- Cross-platform support (
Windows,Linux,macOS) - Generated from GLAD 2.0.8
- Designed for external context management
| Tool | Version |
|---|---|
| Swift | 6.0+ |
| OS | macOS 13+, Ubuntu 22.04+, Windows 10+ |
Add the package to your Package.swift:
dependencies: [
.package(url: "https://github.com/helbertgs/OpenGLAD.git", from: "1.0.0")
],
targets: [
.target(
name: "YourTarget",
dependencies: ["OpenGLAD"]
)
]- Initialize the loader
Initialize GLAD after an OpenGL context has been created.
import OpenGLAD
gladLoaderLoadGL()The exact loader function is provided by the generated GLAD 2 sources.
- Use OpenGL functions
All gl* functions and GL_* constants become available after initialization.
glad_glClearColor(0.2, 0.3, 0.3, 1.0)
glad_glClear(GLenum(GL_COLOR_BUFFER_BIT))
glad_glDrawArrays(GLenum(GL_TRIANGLES), 0, 3)- Unload (optional)
Release function pointers before destroying the context.
gladLoaderUnloadGL()OpenGL requires that all gl* calls are executed on the same thread that owns the active context.
In Swift 6 environments, a common pattern is to confine rendering to @MainActor when the context is bound to the main thread:
@MainActor
final class Renderer {
func setup() {
gladLoaderLoadGL()
}
func render() {
glClear(GLenum(GL_COLOR_BUFFER_BIT))
}
deinit {
gladLoaderUnloadGL()
}
}OpenGLAD
└── Sources
└── OpenGLAD
├── include
│ ├── module.modulemap
│ ├── glad
│ │ ├── gl.h
│ └── khr
│ └── khrplatform.h
│
└── src
└── khrplatform.h
This package is licensed under the MIT License. The bundled GLAD sources are generated code from GLAD 2 and follow the licensing terms defined by the generator configuration.