glfwSetFramebufferSizeCallback
Registers a function GLFW calls whenever the framebuffer changes size, so you can update glViewport.
Signature
GLFWframebuffersizefun glfwSetFramebufferSizeCallback(GLFWwindow* window, GLFWframebuffersizefun callback);
How it works
When the user resizes the window (or drags it to a monitor with another scale), the framebuffer gets a new size, but the viewport keeps the old one. The picture then looks stretched or cut off.
The callback receives the window and the new width and height in pixels. The classic body is a single glViewport call. Projection matrices that use the aspect ratio should read the new size too.
Parameters
The window to watch.
Your function, void f(GLFWwindow* window, int width, int height), or NULL to remove it.
Return value
The callback that was set before, or NULL.
Tips & pitfalls
It runs inside
glfwPollEvents, on the main thread, with the context still current: gl* calls are fine there.It is NOT called for the initial size. Call
glViewportonce yourself after creating the window.Prefer it over
glfwSetWindowSizeCallbackfor the viewport: that one reports screen coordinates, not pixels.
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.
Example
1void onFramebufferSize(GLFWwindow*, int w, int h) {
2 glViewport(0, 0, w, h);
3}
4
5glfwSetFramebufferSizeCallback(window, onFramebufferSize);