glbinding

repository·master·Indexed 21 days ago

https://github.com/cginternals/glbinding

A modern, type-safe C++11 binding for the OpenGL API. It provides a macro-free interface featuring lazy function resolution, multi-context and multi-thread support, and strong typing for parameters. The library includes per-feature API headers for version compatibility and an auxiliary library (glbinding-aux) for logging, debugging, and querying OpenGL runtime meta-information.

Tokens
9.8K
Snippets
39
Records
43
Agent score
74%

What's inside glbinding

  1. Overview of glbinding

    master

    glbinding is a cross-platform C++ binding for the OpenGL API. Unlike traditional C bindings (like GLEW) that rely heavily on macros, glbinding leverages C++11 features such as enum classes, lambdas, and variadic templates.

    Key features include:

    • All OpenGL symbols are real functions and variables (no macros).
    • Type-safe parameters.
    • Per-feature API headers.
    • Lazy function resolution.
    • Multi-context and multi-thread support.
    • Global and local function callbacks.
    • Meta information about the generated binding and the OpenGL runtime.

    It is designed to be highly compatible with existing OpenGL code. To migrate from a C binding, you can typically replace the includes, replace the initialization code, and use the appropriate API namespace (e.g., gl for full availability).

    #include <glbinding/gl/gl.h>
    using namespace gl;
    
    // ...
    auto shader = glCreateShader(GL_COMPUTE_SHADER);
    // ...
  2. Use type-safe OpenGL parameters in glbinding

    master

    Unlike the standard OpenGL API, glbinding uses strong typing for parameters such as booleans, bitfields, enums, and special values. This allows the compiler and IDE to detect invalid usage, such as passing a GLenum where a specific bitfield group is expected.

    Note on Enums: While bitfields are strictly validated, some enums (named values) may still result in runtime errors if the wrong constant is used (e.g., passing GL_COLOR instead of GL_VERTEX_SHADER to glCreateShader), as the underlying groups for enums are not always fully maintained in the specification.

    // Valid bitfield usage
    glClear(GL_COLOR_BUFFER_BIT); 
    
    // Compilation error: bitfield of group ClearBufferMask expected, got GLenum
    glClear(GL_FRAMEBUFFER); 
    
    // Valid bitfield combination
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    
    // Compilation error: bitfields share no group
    glClear(GL_COLOR_BUFFER_BIT | GL_MAP_READ_BIT); 
  3. Use alternative overloaded function signatures

    master

    The standard OpenGL API uses name encoding (e.g., glTexParameteri vs glTexParameterf) to handle different types. Because glbinding uses strong typing, these names can cause compilation errors. To solve this, glbinding provides alternative overloaded signatures that allow you to use the base function name with different types. These are enabled by default.

    #include <glbinding/gl/gl.h> 
    using namespace gl;
    
    // Works out-of-the-box due to overloaded signatures
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, 64, 64, 0, GL_RED, GL_UNSIGNED_BYTE, data);
  4. Manage multiple OpenGL contexts

    master

    glbinding supports multiple contexts, which is essential for multi-threaded applications. However, you must explicitly manage context switching so glbinding can dispatch function pointers correctly.

    1. Initialize per context: Call glbinding::initialize for each context. If using multiple contexts, provide a context identifier (e.g., 0, 1) as the first argument.
    2. Switch contexts: Use glbinding::useCurrentContext() to use the context currently active in the thread, or glbinding::useContext(ContextHandle context) to switch to a specific context identified by a platform-specific handle.

    Warning: While glbinding supports multiple threads, it does not provide locking for concurrent access to a single context. Each thread should ideally manage its own context.

    // Context 1 initialization
    glbinding::initialize(0, glfwGetProcAddress);
    
    // Context 2 initialization
    glbinding::initialize(1, glfwGetProcAddress);
    
    // In the render loop:
    // Switch to context 1
    glbinding::useContext(0);
    glClear(GL_COLOR_BUFFER_BIT);
    
    // Switch to context 2
    glbinding::useContext(1);
    glClear(GL_COLOR_BUFFER_BIT);
  5. Handle KHR/khrplatform.h dependency

    master

    Because the OpenGL API depends on KHR/khrplatform.h, glbinding manages this dependency in two ways. The choice is stored as a property of the glbinding CMake target and propagates to your downstream project automatically.

    1. System-wide headers: Set OPTION_BUILD_OWN_KHR_HEADERS to Off. This requires KHR/khrplatform.h to be available on your system (e.g., via libegl1-mesa-dev on Ubuntu).
    2. Internal headers: Set OPTION_BUILD_OWN_KHR_HEADERS to On. glbinding will use its own internal copy of the headers. This is the fallback if system headers are not found.
  6. Build glbinding from source

    master

    To build glbinding from source, you need a C++11 compliant compiler (GCC 4.8+, Clang 3.3+, or MSVC 2013 Update 3 or newer). The current release defaults to C++17, but you can enable C++14 or C++11 compatibility using the CMake options OPTION_CXX_14_COMPATABILITY and OPTION_CXX_11_COMPATABILITY respectively.

    Prerequisites

    • Mandatory: CMake 3.15+, git, and an OpenGL driver library (dynamically linked).
    • Optional: GLFW 3.2+ (examples/tools), GLEW 1.6+ (comparison example), cpplocate (examples), Qt5 5.0+ (Qt example), googletest (tests), Doxygen 1.8+ (documentation).
    # Clone and checkout a specific version
    git clone https://github.com/cginternals/glbinding.git
    cd glbinding
    git fetch --tags
    git checkout v3.5.0
    
    # Create build directory
    mkdir build
    cd build
    
    # Configure (example for Visual Studio 2022 x64)
    cmake .. -G "Visual Studio 17 2022" -A x64
    
    # Build
    cmake --build .
    
    # Build specific configuration
    cmake --build . --config Release
    cmake --build . --config Debug
  7. Use per-feature OpenGL headers for compatibility

    master

    To ensure your code is compliant with a specific OpenGL version (e.g., OpenGL 3.2 Core) and to prevent using deprecated or unavailable functions, use per-feature headers. This is particularly useful when targeting platforms with limited driver support like macOS or specific Linux configurations.

    Instead of using the general gl:: namespace from <glbinding/gl/gl.h>, use the namespace corresponding to the feature version, such as gl32core::.

    // Using OpenGL 3.2 Core headers for guaranteed compatibility
    #include <glbinding/gl32core/gl.h>
    
    // Use the version-specific namespace
    gl32core::glClear(gl32core::GL_COLOR_BUFFER_BIT | gl32core::GL_DEPTH_BUFFER_BIT);
    gl32core::glDrawElementsInstanced(gl32core::GL_TRIANGLES, 18, gl32core::GL_UNSIGNED_BYTE, 0, m_numcubes * m_numcubes);
  8. Integrate glbinding with CMake

    master

    To use glbinding in your own C++ project, use find_package in your CMakeLists.txt. You can link against the main library or the auxiliary library for extra features like logging and debugging.

    Available targets:

    • glbinding::glbinding: The core library.
    • glbinding::glbinding-aux: Auxiliary features (logging, meta information, debugging).
    # Find the package
    find_package(glbinding REQUIRED)
    
    # Link to your target
    target_link_libraries(${target} PUBLIC
        glbinding::glbinding
        glbinding::glbinding-aux
    )
  9. Install glbinding on Ubuntu

    master

    glbinding is available in the Ubuntu universe (since Artful Aardvark) or via the cginternals PPA for the most recent releases.

    To install glbinding with GLFW examples using the PPA:

    To use glbinding as a dependency (development package):

    # Install via PPA with GLFW examples
    > sudo apt-add-repository ppa:cginternals/ppa
    > sudo apt-get update
    > sudo apt-get install libglbinding-examples-glfw
    # start example
    > /usr/share/glbinding/cubescape
    
    # Install as a dependency
    > sudo apt-get install libglbinding-dev libglbinding-dbg