我是否在GLFW中设置了回调函数

glfwSetCursorPosCallback(window, mouse);

检索光标位置的最明显方法是
vec2 m;

void mouse (GLFWwindow* window, GLdouble x, GLdouble y)
{
    m = vec2 (x, y);
}

但是,我宁愿不使用全局变量来这样做。能做到吗

最佳答案

您可以将用户指针关联到GLFWindow。参见 glfwSetWindowUserPointer

可以通过 GLFWWindow 一次从glfwGetWindowUserPointer对象检索指针

struct MyWindowData
{
  GLdouble x;
  GLdouble y;
}

将指向windowData的指针与window关联:
MyWindowData windowData;

glfwSetWindowUserPointer( window, &windowData );
glfwSetCursorPosCallback( window, mouse );

window获取指针,并将类型void*的指针转换为MyWindowData *(遗憾的是,必须进行强制转换)。
void mouse(GLFWwindow* window, GLdouble x, GLdouble y)
{
   MyWindowData *dataPtr = (MyWindowData*)glfwGetWindowUserPointer( window );
   dataPtr->x = x;
   dataPtr->y = y;
}

09-07 10:42