GLFW: input & timeGLFW 3.0

glfwSetScrollCallback

Registers a function called when the mouse wheel or touchpad scrolls.

Signature

GLFWscrollfun glfwSetScrollCallback(GLFWwindow* window, GLFWscrollfun callback);

How it works

The callback receives xoffset and yoffset: how much was scrolled since the last event, not a position. A normal wheel gives yoffset = +1 or -1 per notch (positive = away from you). Touchpads give small fractions and can scroll sideways (xoffset).

The classic use in the lessons is zoom: change the camera's field of view by yoffset.

Parameters

windowGLFWwindow*

The window to listen to.

callbackGLFWscrollfun

Your function, void f(GLFWwindow* window, double xoffset, double yoffset), or NULL.

Return value

The callback that was set before, or NULL.

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

example.cpp
1glfwSetScrollCallback(window, [](GLFWwindow*, double, double yoffset) {
2    fov = std::clamp(fov - (float)yoffset, 1.0f, 45.0f);   // wheel up = zoom in
3});
Official GLFW documentation