This guide introduces the OpenGL and OpenGL ES context related functions of GLFW. For details on a specific function in this category, see the Context reference. There are also guides for the other areas of the GLFW API.
A window object encapsulates both a top-level window and an OpenGL or OpenGL ES context. It is created with glfwCreateWindow and destroyed with glfwDestroyWindow or glfwTerminate. See Window creation for more information.
As the window and context are inseparably linked, the window object also serves as the context handle.
To test the creation of various kinds of contexts and see their properties, run the glfwinfo
test program.
GLFW_NO_API
. For more information, see the Vulkan guide.There are a number of hints, specified using glfwWindowHint, related to what kind of context is created. See context related hints in the window guide.
When creating a window and its OpenGL or OpenGL ES context with glfwCreateWindow, you can specify another window whose context the new one should share its objects (textures, vertex and element buffers, etc.) with.
Object sharing is implemented by the operating system and graphics driver. On platforms where it is possible to choose which types of objects are shared, GLFW requests that all types are shared.
See the relevant chapter of the OpenGL or OpenGL ES reference documents for more information. The name and number of this chapter unfortunately varies between versions and APIs, but has at times been named Shared Objects and Multiple Contexts.
GLFW comes with a barebones object sharing example program called sharing
.
GLFW doesn't support creating contexts without an associated window. However, contexts with hidden windows can be created with the GLFW_VISIBLE window hint.
The window never needs to be shown and its context can be used as a plain offscreen context. Depending on the window manager, the size of a hidden window's framebuffer may not be usable or modifiable, so framebuffer objects are recommended for rendering with such contexts.
You should still process events as long as you have at least one window, even if none of them are visible.
macOS: The first time a window is created the menu bar is created. This is not desirable for example when writing a command-line only application. Menu bar creation can be disabled with the GLFW_COCOA_MENUBAR init hint.
You can disable context creation by setting the GLFW_CLIENT_API hint to GLFW_NO_API
. Windows without contexts must not be passed to glfwMakeContextCurrent or glfwSwapBuffers.
Before you can make OpenGL or OpenGL ES calls, you need to have a current context of the correct type. A context can only be current for a single thread at a time, and a thread can only have a single context current at a time.
When moving a context between threads, you must make it non-current on the old thread before making it current on the new one.
The context of a window is made current with glfwMakeContextCurrent.
The window of the current context is returned by glfwGetCurrentContext.
The following GLFW functions require a context to be current. Calling any these functions without a current context will generate a GLFW_NO_CURRENT_CONTEXT error.
See Buffer swapping in the window guide.
One of the benefits of OpenGL and OpenGL ES is their extensibility. Hardware vendors may include extensions in their implementations that extend the API before that functionality is included in a new version of the OpenGL or OpenGL ES specification, and some extensions are never included and remain as extensions until they become obsolete.
An extension is defined by:
GL_ARB_gl_spirv
)GL_SPIR_V_BINARY_ARB
)glSpecializeShaderARB
)Note the ARB
affix, which stands for Architecture Review Board and is used for official extensions. The extension above was created by the ARB, but there are many different affixes, like NV
for Nvidia and AMD
for, well, AMD. Any group may also use the generic EXT
affix. Lists of extensions, together with their specifications, can be found at the OpenGL Registry and OpenGL ES Registry.
An extension loader library is the easiest and best way to access both OpenGL and OpenGL ES extensions and modern versions of the core OpenGL or OpenGL ES APIs. They will take care of all the details of declaring and loading everything you need. One such library is glad and there are several others.
The following example will use glad but all extension loader libraries work similarly.
First you need to generate the source files using the glad Python script. This example generates a loader for any version of OpenGL, which is the default for both GLFW and glad, but loaders for OpenGL ES, as well as loaders for specific API versions and extension sets can be generated. The generated files are written to the output
directory.
The --no-loader
option is added because GLFW already provides a function for loading OpenGL and OpenGL ES function pointers, one that automatically uses the selected context creation API, and glad can call this instead of having to implement its own. There are several other command-line options as well. See the glad documentation for details.
Add the generated output/src/glad.c
, output/include/glad/glad.h
and output/include/KHR/khrplatform.h
files to your build. Then you need to include the glad header file, which will replace the OpenGL header of your development environment. By including the glad header before the GLFW header, it suppresses the development environment's OpenGL or OpenGL ES header.
Finally you need to initialize glad once you have a suitable current context.
Once glad has been loaded, you have access to all OpenGL core and extension functions supported by both the context you created and the glad loader you generated and you are ready to start rendering.
You can specify a minimum required OpenGL or OpenGL ES version with context hints. If your needs are more complex, you can check the actual OpenGL or OpenGL ES version with context attributes, or you can check whether a specific version is supported by the current context with the GLAD_GL_VERSION_x_x
booleans.
To check whether a specific extension is supported, use the GLAD_GL_xxx
booleans.
Do not use this technique unless it is absolutely necessary. An extension loader library will save you a ton of tedious, repetitive, error prone work.
To use a certain extension, you must first check whether the context supports that extension and then, if it introduces new functions, retrieve the pointers to those functions. GLFW provides glfwExtensionSupported and glfwGetProcAddress for manual loading of extensions and new API functions.
This section will demonstrate manual loading of OpenGL extensions. The loading of OpenGL ES extensions is identical except for the name of the extension header.
The glext.h
extension header is a continually updated file that defines the interfaces for all OpenGL extensions. The latest version of this can always be found at the OpenGL Registry. There are also extension headers for the various versions of OpenGL ES at the OpenGL ES Registry. It it strongly recommended that you use your own copy of the extension header, as the one included in your development environment may be several years out of date and may not include the extensions you wish to use.
The header defines function pointer types for all functions of all extensions it supports. These have names like PFNGLSPECIALIZESHADERARBPROC
(for glSpecializeShaderARB
), i.e. the name is made uppercase and PFN
(pointer to function) and PROC
(procedure) are added to the ends.
To include the extension header, define GLFW_INCLUDE_GLEXT before including the GLFW header.
A given machine may not actually support the extension (it may have older drivers or a graphics card that lacks the necessary hardware features), so it is necessary to check at run-time whether the context supports the extension. This is done with glfwExtensionSupported.
The argument is a null terminated ASCII string with the extension name. If the extension is supported, glfwExtensionSupported returns GLFW_TRUE
, otherwise it returns GLFW_FALSE
.
Many extensions, though not all, require the use of new OpenGL functions. These functions often do not have entry points in the client API libraries of your operating system, making it necessary to fetch them at run time. You can retrieve pointers to these functions with glfwGetProcAddress.
In general, you should avoid giving the function pointer variables the (exact) same name as the function, as this may confuse your linker. Instead, you can use a different prefix, like above, or some other naming scheme.
Now that all the pieces have been introduced, here is what they might look like when used together.
Last update on Fri Jul 22 2022 for GLFW 3.3.8