OpenGL在C++中鼠标点击时获得光标坐标
问题描述:
使用GLFW相当新颖,我希望在点击鼠标左键时将光标坐标输出到控制台上。但是,我没有得到任何输出。OpenGL在C++中鼠标点击时获得光标坐标
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
//ESC to quit
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, GL_TRUE);
return;
}
if (key == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
{
double xpos, ypos;
//getting cursor position
glfwGetCursorPos(window, &xpos, &ypos);
cout << "Cursor Position at (" << xpos << " : " << ypos << endl;
}
}
答
您正试图在键盘输入回调时获取鼠标输入。请注意,key
对应的值为GLFW_KEY_*
。您应该设置鼠标输入回调,而不是:
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
{
double xpos, ypos;
//getting cursor position
glfwGetCursorPos(window, &xpos, &ypos);
cout << "Cursor Position at (" << xpos << " : " << ypos << endl;
}
}
为什么在** key **事件的回调中这样做?如果您想在点击鼠标按钮时执行某些操作,则应该在鼠标按钮回调中检查该操作。 –