### Run Example Script Source: https://github.com/dav1dde/glad/blob/glad2/example/rust/gl-glfw-mx/README.md Use this command to initialize the glad-gl crate and run the example. ```sh ./init.sh && cargo run ``` -------------------------------- ### Glad Initialization Example Source: https://github.com/dav1dde/glad/wiki/C An example demonstrating how to initialize OpenGL using `gladLoadGL` with `glfwGetProcAddress` and subsequently extract the major and minor versions of the loaded OpenGL context. ```APIDOC ## Initialization Example This example shows how to initialize Glad with `glfwGetProcAddress` and retrieve the OpenGL version. ```c int version = gladLoadGL(glfwGetProcAddress); if (version == 0) { std::cout << "Failed to initialize OpenGL context" << std::endl; return -1; } // Successfully loaded OpenGL std::cout << "Loaded OpenGL " << GLAD_VERSION_MAJOR(version) << "." << GLAD_VERSION_MINOR(version) << std::endl; ``` ``` -------------------------------- ### CMake Project Setup Source: https://github.com/dav1dde/glad/blob/glad2/example/c/vulkan_tri_glfw/CMakeLists.txt Configures the minimum CMake version and names the C project 'glad_examples_c_vulkan_tri_glfw'. ```cmake cmake_minimum_required(VERSION 3.1) project(glad_examples_c_vulkan_tri_glfw C) ``` -------------------------------- ### CMake Project Setup Source: https://github.com/dav1dde/glad/blob/glad2/example/c++/multiwin_mx/CMakeLists.txt Initializes the CMake build system and defines the project name and languages. ```cmake cmake_minimum_required(VERSION 3.1) project(glad_examples_c_multiwin_mx C CXX) ``` -------------------------------- ### Initializing Glad and Extracting Version Information Source: https://github.com/dav1dde/glad/wiki/C This example demonstrates initializing OpenGL using Glad with `glfwGetProcAddress` and then extracting the major and minor version numbers from the returned integer. It includes basic error handling for initialization failure. ```c int version = gladLoadGL(glfwGetProcAddress); if (version == 0) { std::cout << "Failed to initialize OpenGL context" << std::endl; return -1; } // Successfully loaded OpenGL std::cout << "Loaded OpenGL " << GLAD_VERSION_MAJOR(version) << "." << GLAD_VERSION_MINOR(version) << std::endl; ``` -------------------------------- ### Initializing Glad with Windowing Frameworks Source: https://github.com/dav1dde/glad/wiki/C Examples of integrating Glad with GLFW and SDL for OpenGL context loading. The `gladLoadGL` function takes a loader function pointer from the respective windowing library. ```c // GLFW + GL int version = gladLoadGL(glfwGetProcAddress); // SDL + GL int version = gladLoadGL((GLADloadfunc) SDL_GL_GetProcAddress); int major = GLAD_VERSION_MAJOR(version); int minor = GLAD_VERSION_MINOR(version); printf("Loaded OpenGL version %d.%d\n", major, minor); ``` -------------------------------- ### Initialize and Use GLAD with Multi-Context in C++ Source: https://github.com/dav1dde/glad/wiki/C Example of initializing GLAD with a multi-context structure and using OpenGL functions through the context pointer. This is essential for applications supporting multiple OpenGL contexts. ```cpp // ... setup GL context GladGLContext context; int version = gladLoadGLContext(&context, glfwGetProcAddress); // Alternatively use the internal loader // int version = gladLoaderLoadGLContext(&context); if (version == 0) { std::cout << "Failed to initialize OpenGL context" << std::endl; return -1; } gl->Viewport(0, 0, WIDTH, HEIGHT); while (!glfwWindowShouldClose(window)) { glfwPollEvents(); gl->ClearColor(0.2f, 0.3f, 0.3f, 1.0f); gl->Clear(GL_COLOR_BUFFER_BIT); glfwSwapBuffers(window); } ``` -------------------------------- ### Using Glad's Internal Loader Source: https://github.com/dav1dde/glad/wiki/C This example shows how to use Glad's internal loader functions. `gladLoaderLoadGL` is called to initialize OpenGL, and `gladLoaderUnloadGL` is used for cleanup. This approach avoids the need for external windowing library loader functions. ```c int main(void) { // ... setup context int version = gladLoaderLoadGL(); if (!version) { printf("Unable to load OpenGL\n"); return 1; } // ... render gladLoaderUnloadGL(); return 0; } ``` -------------------------------- ### GLAD Alias Resolution Example in C Source: https://github.com/dav1dde/glad/wiki/C Demonstrates how GLAD resolves function aliases at runtime. This ensures compatibility across different OpenGL versions and extensions. ```c // executed after initial loading phase: if (glVertexAttrib2sv == NULL && glVertexAttrib2svARB != NULL) glVertexAttrib2sv = (PFNGLVERTEXATTRIB2SVPROC)glVertexAttrib2svARB; if (glVertexAttrib2sv == NULL && glVertexAttrib2svNV != NULL) glVertexAttrib2sv = (PFNGLVERTEXATTRIB2SVPROC)glVertexAttrib2svNV; ``` -------------------------------- ### Initialize OpenGL with GLFW Source: https://github.com/dav1dde/glad/blob/glad2/README.md Demonstrates initializing OpenGL using gladLoadGL with GLFW. Ensure glad is included after GLFW. The function returns a version number, with 0 indicating failure. ```c #include // GLFW (include after glad) #include int main() { // -- snip -- GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", NULL, NULL); glfwMakeContextCurrent(window); int version = gladLoadGL(glfwGetProcAddress); if (version == 0) { printf("Failed to initialize OpenGL context\n"); return -1; } // Successfully loaded OpenGL printf("Loaded OpenGL %d.%d\n", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version)); // -- snip -- } ``` -------------------------------- ### Initialize GLAD with SDL Source: https://github.com/dav1dde/glad/wiki/C Initialize GLAD after creating an OpenGL context with SDL and print the GL version. ```c SDL_GLContext context = SDL_GL_CreateContext(window); int version = gladLoadGL((GLADloadfunc) SDL_GL_GetProcAddress); printf("GL %d.%d\n", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version)); ``` -------------------------------- ### Initialize GLAD with GLFW Source: https://github.com/dav1dde/glad/wiki/C Initialize GLAD after creating an OpenGL context with GLFW and print the GL version. ```c GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "[glad] GL with GLFW", NULL, NULL); glfwMakeContextCurrent(window); int version = gladLoadGL(glfwGetProcAddress); printf("GL %d.%d\n", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version)); ``` -------------------------------- ### Glad Initialization Functions Source: https://github.com/dav1dde/glad/wiki/C Glad provides two primary functions for initializing the OpenGL context: `gladLoad{API}` and `gladLoad{API}UserPtr`. `gladLoad{API}UserPtr` allows for a user-defined loader function with a user pointer, useful for state-independent loading. `gladLoad{API}` is commonly used with windowing libraries like GLFW or SDL. ```APIDOC ## Initialization Functions Glad requires initialization through a loader function. Two main functions are exposed: ```c typedef void (*GLADapiproc)(void); typedef GLADapiproc (*GLADloadfunc)(const char *name); typedef GLADapiproc (*GLADuserptrloadfunc)(void *userptr, const char *name); int gladLoad{API}(GLADloadfunc load); int gladLoad{API}UserPtr(GLADuserptrloadfunc load, void *userptr); ``` `gladLoad{API}UserPtr` supports passing a `userptr` to the loader function, beneficial for GUI frameworks. `gladLoad{API}` integrates with libraries like GLFW and SDL. ### Example Usage with GLFW and SDL ```c // GLFW + GL int version = gladLoadGL(glfwGetProcAddress); // SDL + GL int version = gladLoadGL((GLADloadfunc) SDL_GL_GetProcAddress); int major = GLAD_VERSION_MAJOR(version); int minor = GLAD_VERSION_MINOR(version); printf("Loaded OpenGL version %d.%d\n", major, minor); ``` The return value indicates the loaded API version. Use `GLAD_VERSION_MAJOR(version)` and `GLAD_VERSION_MINOR(version)` to extract major and minor versions. ``` -------------------------------- ### Initialize GLAD with Built-In Loader Source: https://github.com/dav1dde/glad/wiki/C Load GLAD using its built-in loader after creating an OpenGL context. Remember to unload GLAD before exiting. ```c // Create OpenGL context first // Load glad int version = gladLoaderLoadGL(); printf("GL %d.%d\n", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version)); // .. Render // Unload glad before exiting gladLoaderUnloadGL(); ``` -------------------------------- ### Initialize glad with GLFW Source: https://github.com/dav1dde/glad/blob/glad2/example/rust/gl-glfw-mx/README.md This single line initializes glad for OpenGL context loading using GLFW's raw function pointer retrieval. ```rust gl::load(|e| glfw.get_proc_address_raw(e) as *const std::os::raw::c_void); ``` -------------------------------- ### Include GLAD before SDL Source: https://github.com/dav1dde/glad/wiki/C Include the GLAD header before the SDL and SDL_opengl headers for proper initialization. ```c #include #include #include ``` -------------------------------- ### Define Executable and Link Libraries Source: https://github.com/dav1dde/glad/blob/glad2/example/c/vulkan_tri_glfw/CMakeLists.txt Defines the main executable 'vulkan_tri_glfw' and links it against the generated GLAD library and GLFW. This makes the Vulkan and GLFW functionalities available to the application. ```cmake add_executable(vulkan_tri_glfw vulkan_tri_glfw.c ) target_link_libraries(vulkan_tri_glfw PUBLIC glad_vulkan_12 glfw ) ``` -------------------------------- ### Initialize GLAD with GLFW Source: https://github.com/dav1dde/glad/blob/glad2/example/rust/gl-glfw/README.md Add this single line to your Rust code to initialize GLAD, enabling OpenGL function loading. It requires a GLFW context. ```rust gl::load(|e| glfw.get_proc_address_raw(e) as *const std::os::raw::c_void); ``` -------------------------------- ### Glad Header-Only Implementation Source: https://github.com/dav1dde/glad/wiki/C To use Glad as a header-only library, define `GLAD_GL_IMPLEMENTATION` in exactly one source file before including the Glad header. This combines the implementation and header into a single file. ```c #define GLAD_GL_IMPLEMENTATION #include int main(void) { // ... setup context if (!gladLoadGL(glfwGetProcAddress)) { return -1; } // ... } ``` -------------------------------- ### Define Executable and Link Libraries Source: https://github.com/dav1dde/glad/blob/glad2/example/c++/multiwin_mx/CMakeLists.txt Defines the main executable for the multi-window application and links it with the generated GLAD library and GLFW. ```cmake add_executable(multiwin_mx multiwin_mx.cpp ) target_link_libraries(multiwin_mx PUBLIC glad_gl_core_mx_33 glfw ) ``` -------------------------------- ### Generate GLAD Library with CMake Source: https://github.com/dav1dde/glad/wiki/C Configure CMake to generate a GLAD library with specific API and version settings, then link it to your executable. ```cmake cmake_minimum_required(VERSION 3.1) project(my_project C CXX) find_package(glfw3 REQUIRED) # Path to glad directory set(GLAD_SOURCES_DIR "${PROJECT_SOURCE_DIR}/external/glad/") # Path to glad cmake files add_subdirectory("${GLAD_SOURCES_DIR}/cmake" glad_cmake) # Specify glad settings glad_add_library(glad_gl_core_33 REPRODUCIBLE API gl:core=3.3) add_executable(multiwin_mx multiwin_mx.cpp ) # Add glad to your project target_link_libraries(multiwin_mx PUBLIC glad_gl_core_33 glfw ) ``` -------------------------------- ### Glad Runtime Checks Source: https://github.com/dav1dde/glad/wiki/C After initialization, Glad populates global boolean variables for each supported version and extension. These variables are set to true (1) if the corresponding version or extension was successfully loaded. ```APIDOC ## Runtime Checks Upon successful initialization, Glad defines global boolean variables for each loaded version and extension. ```c int GLAD_{API}_VERSION_3_0; int GLAD_{API}_VERSION_3_1; // ... int GLAD_{API}_EXT_texture_buffer_object; int GLAD_{API}_KHR_debug; // ... ``` These variables will be set to `1` if the respective version or extension is available. ``` -------------------------------- ### Include GLAD before GLFW Source: https://github.com/dav1dde/glad/wiki/C Include the GLAD header before the GLFW header to ensure proper initialization. ```c #include #include ``` -------------------------------- ### Glad Internal Loader Functions Source: https://github.com/dav1dde/glad/wiki/C Glad can provide its own loader functions if a windowing library does not offer one. `gladLoaderLoad{API}` initializes Glad, and `gladLoaderUnload{API}` cleans up resources. Note that `gladLoaderUnload{API}` may not exist for all APIs. ```c int gladLoaderLoad{API}(void); void gladLoaderUnload{API}(void); // <- may not exist for certain APIs ``` -------------------------------- ### Glad Loader Function Signatures Source: https://github.com/dav1dde/glad/wiki/C These are the default function signatures for loading OpenGL functions with Glad. `gladLoad{API}UserPtr` allows passing a user pointer for state-independent loaders, while `gladLoad{API}` is typically used with windowing frameworks. ```c typedef void (*GLADapiproc)(void); typedef GLADapiproc (*GLADloadfunc)(const char *name); typedef GLADapiproc (*GLADuserptrloadfunc)(void *userptr, const char *name); int gladLoad{API}(GLADloadfunc load); int gladLoad{API}UserPtr(GLADuserptrloadfunc load, void *userptr); ``` -------------------------------- ### Find GLFW Package Source: https://github.com/dav1dde/glad/blob/glad2/example/c++/multiwin_mx/CMakeLists.txt Locates and loads the GLFW library, which is required for window creation and input handling. ```cmake find_package(glfw3 REQUIRED) ``` -------------------------------- ### Glad Runtime Version and Extension Checks Source: https://github.com/dav1dde/glad/wiki/C After initialization, Glad populates global boolean variables for each supported OpenGL version and extension. These can be used for runtime checks to determine feature availability. ```c int GLAD_{API}_VERSION_3_0; int GLAD_{API}_VERSION_3_1; // ... int GLAD_{API}_EXT_texture_buffer_object; int GLAD_{API}_KHR_debug; // ... ``` -------------------------------- ### Header Only Compilation Source: https://github.com/dav1dde/glad/wiki/C When using the header-only option, Glad's implementation is included within the header file. Define `GLAD_{API}_IMPLEMENTATION` in a single source file before including the Glad header to ensure proper compilation. ```APIDOC ## Header Only To use Glad as a header-only library, define `GLAD_{API}_IMPLEMENTATION` in exactly one source file before including the Glad header. ```c #define GLAD_GL_IMPLEMENTATION #include int main(void) { // ... setup context if (!gladLoadGL(glfwGetProcAddress)) { return -1; } // ... } ``` ``` -------------------------------- ### Cargo.toml Dependency Source: https://github.com/dav1dde/glad/blob/glad2/example/rust/gl-glfw-mx/README.md Configure your Cargo.toml to reference the glad-gl crate from the local build directory. ```toml [dependencies] glad-gl = { path = "./build/glad-gl" } ``` -------------------------------- ### Generate GLAD Vulkan Library Source: https://github.com/dav1dde/glad/blob/glad2/example/c/vulkan_tri_glfw/CMakeLists.txt Generates the GLAD library for Vulkan 1.2, ensuring reproducible builds. This command is specific to the GLAD build system. ```cmake glad_add_library(glad_vulkan_12 REPRODUCIBLE LOADER API vulkan=1.2) ``` -------------------------------- ### Include GLAD Sources with CMake Source: https://github.com/dav1dde/glad/wiki/C Include GLAD source files in your CMake project by specifying include directories and globbing C files. ```cmake include_directories(${CMAKE_CURRENT_SOURCE_DIR}/glad/include) file(GLOB BUTTERFLIES_SOURCES_C ${CMAKE_CURRENT_SOURCE_DIR} *.c glad/src/gl.c) ``` -------------------------------- ### Generate GLAD OpenGL Library Source: https://github.com/dav1dde/glad/blob/glad2/example/c++/multiwin_mx/CMakeLists.txt Creates a GLAD library for OpenGL core profile version 3.3, ensuring compatibility and reproducibility. ```cmake glad_add_library(glad_gl_core_mx_33 REPRODUCIBLE MX API gl:core=3.3) ``` -------------------------------- ### Include GLAD CMake Modules Source: https://github.com/dav1dde/glad/blob/glad2/example/c++/multiwin_mx/CMakeLists.txt Adds the GLAD CMake build scripts to the project, enabling GLAD library generation. ```cmake set(GLAD_SOURCES_DIR "${PROJECT_SOURCE_DIR}/../../..") add_subdirectory("${GLAD_SOURCES_DIR}/cmake" glad_cmake) ``` -------------------------------- ### Glad Compiletime Checks Source: https://github.com/dav1dde/glad/wiki/C Glad generates preprocessor macros based on generation options, allowing for glad-agnostic code. These macros indicate the enabled API, loader options, and specific features. ```APIDOC ## Compiletime Checks Glad generates preprocessor macros to facilitate writing glad-agnostic code. ```c #define GLAD_{API} #define GLAD_OPTION_{API}_LOADER #define GLAD_OPTION_{API}_ALIAS #define GLAD_OPTION_{API}_{OPTION} // {OPTION} is the enabled loader option ``` These macros can be used to conditionally compile code based on the Glad configuration. ``` -------------------------------- ### Define GLAD Debug Callbacks in C Source: https://github.com/dav1dde/glad/wiki/C Set pre and post callbacks for debugging OpenGL calls. These callbacks allow interception and modification of function calls. ```c typedef void (*GLADprecallback)(const char *name, GLADapiproc apiproc, int len_args, ...); typedef void (*GLADpostcallback)(void *ret, const char *name, GLADapiproc apiproc, int len_args, ...); void gladSet{API}PreCallback(GLADprecallback cb); void gladSet{API}PostCallback(GLADpostcallback cb); ``` -------------------------------- ### Add Custom OpenGL Specification to Glad Source: https://github.com/dav1dde/glad/wiki/Extending Glad Define a custom specification by inheriting from `glad.parse.Specification` and setting static variables for display name, API URL, and name. This is used for integrating new API definitions. ```python class GL(Specification): DISPLAY_NAME = 'OpenGL' API = 'https://raw.githubusercontent.com/KhronosGroup/OpenGL-Registry/master/xml/' NAME = 'gl' ``` -------------------------------- ### Glad Compile-time Checks Source: https://github.com/dav1dde/glad/wiki/C Glad generates preprocessor macros based on generation options, allowing for compile-time checks and conditional compilation of Glad-agnostic code. ```c #define GLAD_{API} #define GLAD_OPTION_{API}_LOADER #define GLAD_OPTION_{API}_ALIAS #define GLAD_OPTION_{API}_{OPTION} // {OPTION} is the enabled loader option ``` -------------------------------- ### Internal Loader Source: https://github.com/dav1dde/glad/wiki/C The loader option integrates an internal loader into Glad, useful when a windowing library does not provide one or for custom windowing solutions. It provides `gladLoaderLoad{API}` and `gladLoaderUnload{API}` functions. ```APIDOC ## Loader Glad can include an internal loader, useful when your windowing library lacks one. ### Functions ```c int gladLoaderLoad{API}(void); void gladLoaderUnload{API}(void); // May not exist for all APIs ``` Using `gladLoaderLoad{API}` eliminates the need to call `gladLoad{API}` separately. ### Example Usage ```c int main(void) { // ... setup context int version = gladLoaderLoadGL(); if (!version) { printf("Unable to load OpenGL\n"); return 1; } // ... render gladLoaderUnloadGL(); return 0; } ``` Note that `gladLoaderUnload{API}` only needs to be called once, even if `gladLoaderLoad{API}` was called multiple times. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.