GLFW中的鼠标滑动事件

GLFW中的鼠标滑动事件

以下是glfw3.h中关于设置鼠标滑动回调函数的方法:

/*! @brief Sets the scroll callback.
 *
 *  This function sets the scroll callback of the specified window, which is
 *  called when a scrolling device is used, such as a mouse wheel or scrolling
 *  area of a touchpad.
 *
 *  The scroll callback receives all scrolling input, like that from a mouse
 *  wheel or a touchpad scrolling area.
 *
 *  @param[in] window The window whose callback to set.
 *  @param[in] cbfun The new scroll callback, or `NULL` to remove the currently
 *  set callback.
 *  @return The previously set callback, or `NULL` if no callback was set or an
 *  error occurred.
 *
 *  @ingroup input
 */
GLFWAPI GLFWscrollfun glfwSetScrollCallback(GLFWwindow* window, GLFWscrollfun cbfun);

/*! @brief The function signature for scroll callbacks.
 *
 *  This is the function signature for scroll callback functions.
 *
 *  @param[in] window The window that received the event.
 *  @param[in] xoffset The scroll offset along the x-axis.
 *  @param[in] yoffset The scroll offset along the y-axis.
 *
 *  @sa glfwSetScrollCallback
 *
 *  @ingroup input
 */
typedef void (* GLFWscrollfun)(GLFWwindow*,double,double);

实际使用中,只要调用这个方法,即可在鼠标或者触摸板产生滑动事件后收到回调事件。
需要注意的是:鼠标只会产生y方向的滑动,xoffset 始终为0。

...
void GLFWBackendInit(int argc, char** argv, bool WithDepth, bool WithStencil)
{
    GLFWmonitor* pMonitor = isFullScreen ? glfwGetPrimaryMonitor() : NULL;

    s_pWindow = glfwCreateWindow(Width, Height, pTitle, pMonitor, NULL);

    if (!s_pWindow) {
        OGLDEV_ERROR0("error creating window");
        exit(1);
    }
    
    glfwMakeContextCurrent(s_pWindow);
    
    if (glfwInit() != 1) {
        OGLDEV_ERROR0("Error initializing GLFW");
        exit(1);
    }
    
	glfwSetScrollCallback(s_pWindow,ScrollCallback);
	
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glFrontFace(GL_CW);
    glCullFace(GL_BACK);
    glEnable(GL_CULL_FACE);
    
    while (!glfwWindowShouldClose(s_pWindow)) {
        s_pCallbacks->RenderSceneCB();        
        glfwSwapBuffers(s_pWindow);
        glfwPollEvents();
    }
}
...

static void ScrollCallback(GLFWwindow* window, double xoffset, double yoffset) {
   	m_pGameCamera->m_pos.z += yoffset;
	m_pGameCamera->m_pos.x += xoffset;
	cout << "Scroll: (" << xoffset << "," << yoffset << ")" << endl;
}

输出:
输出信息
实测效果:滚轮每滑动一个单位(鼠标滚轮产生一次机械触感反馈),产生一次 yoffset=1.0的滑动事件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
glfw 是一个开源的跨平台的库,用于创建窗口和处理用户输入事件。对于鼠标点击事件glfw 提供了相应的函数和回调函数来处理。 首先,我们需要创建一个窗口,可以使用 glfwCreateWindow 函数来创建一个 GLFW 窗口。接下来,使用 glfwSetMouseButtonCallback 函数来注册一个回调函数,用于监听鼠标点击事件。 回调函数的原型如下: void mouse_button_callback(GLFWwindow* window, int button, int action, int mods); 其,window 参数是窗口指针,button 参数表示鼠标按下的按键,action 参数表示鼠标的动作(例如按下或释放),mods 参数表示按下的修饰键。 在回调函数,我们可以根据不同的 button 和 action 来处理特定的鼠标点击事件。例如,当 button 参数为 GLFW_MOUSE_BUTTON_LEFT 而 action 参数为 GLFW_PRESS 时,表示用户按下了鼠标左键。我们可以在这里执行相应的操作。 以下是一个示例代码片段,展示了如何使用 glfw 监听鼠标点击事件: #include <GLFW/glfw3.h> void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) { if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) { // 处理鼠标左键按下事件 } } int main() { // 初始化 glfw glfwInit(); // 创建一个窗口 GLFWwindow* window = glfwCreateWindow(800, 600, "GLFW Window", NULL, NULL); if (window == NULL) { // 窗口创建失败的处理 glfwTerminate(); return -1; } // 注册鼠标点击事件回调函数 glfwSetMouseButtonCallback(window, mouse_button_callback); // 主循环 while (!glfwWindowShouldClose(window)) { // 渲染和逻辑处理 // 交换缓冲区和处理窗口事件 glfwSwapBuffers(window); glfwPollEvents(); } // 清理并退出 glfwTerminate(); return 0; } 通过上述代码,我们可以监听鼠标点击事件并在回调函数执行相应的操作。当用户按下鼠标左键时,可以在回调函数添加所需的代码块。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值