OpenGL在C ++中单击鼠标即可获取光标坐标

Mon*_*oni 3 c++ opengl glfw

对于使用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;
    }
}
Run Code Online (Sandbox Code Playgroud)

use*_*670 5

您试图在键盘输入回调中获取鼠标输入。请注意,它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;
    }
}
Run Code Online (Sandbox Code Playgroud)