gpt4 book ai didi

c++ - OpenGL在C++中单击鼠标获取光标坐标

转载 作者:搜寻专家 更新时间:2023-10-31 00:54:30 26 4
gpt4 key购买 nike

对使用 GLFW 相当陌生,我想在单击鼠标左键时将光标坐标输出到控制台。但是,我根本没有得到任何输出。

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;
}
}

关于c++ - OpenGL在C++中单击鼠标获取光标坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45130391/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com