Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL get cursor coordinate on mouse click in C++

Tags:

c++

opengl

glfw

Fairly new to using GLFW, I want to output the cursor coordinate onto the console whenever the left mouse button in clicked. However, I am not getting any output at all.

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;
    }
}
like image 223
Monomoni Avatar asked Jul 16 '17 15:07

Monomoni


1 Answers

You are trying to get mouse input at keyboard input callback. Notice that key corresponds to GLFW_KEY_* values. You should set mouse input callback instead:

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;
    }
}
like image 55
user7860670 Avatar answered Oct 29 '22 10:10

user7860670