GLFW: window & contextGLFW 1.0

glfwGetProcAddress

Finds the address of an OpenGL function in the driver. You hand it to glad, which loads all of them.

Signature

GLFWglproc glfwGetProcAddress(const char* procname);

How it works

Modern OpenGL functions are not in a library you link against: they live in the GPU driver, and their addresses are only known at run time. Each one has to be looked up by name.

Nobody does that by hand for hundreds of functions. A loader such as glad calls glfwGetProcAddress for every name it knows and fills its function pointers. That is why gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) (glad 1) or gladLoadGL(glfwGetProcAddress) (glad 2) comes right after glfwMakeContextCurrent.

Parameters

procnameconst char*

The function name in ASCII, e.g. "glGenBuffers".

Return value

The function's address, or NULL if this context does not have it.

Tips & pitfalls

  • A context must be current. Called too early, glad gets NULL everywhere and its load function returns 0.

  • A non-NULL address does not prove the function works on this GPU; check the OpenGL version too.

Errors

When the call is invalid it does nothing and sets an error, which glGetError returns:

GLFW_NOT_INITIALIZED
Called before glfwInit or after glfwTerminate.
GLFW_NO_CURRENT_CONTEXT
No OpenGL context is current on this thread: call glfwMakeContextCurrent first.
GLFW_PLATFORM_ERROR
The operating system or window system reported a failure; the description says which.

Example

example.cpp
1glfwMakeContextCurrent(window);
2if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
3    std::fprintf(stderr, "could not load OpenGL functions\n");
4    return 1;
5}
6std::printf("%s\n", (const char*)glGetString(GL_VERSION));
Official GLFW documentation