Search code examples
openglglfwglm-math

How to move/pan OpenGL camera ONLY if right click is held down?


My program has a camera that moves/pans when the mouse is moved. How do I get the it to move/pan ONLY if the right mouse click is held down?

This is my function that moves/pans the camera. I tried adding an if statement with the GLFW_MOUSE_BUTTON_RIGHT but it doesn't work.

static void cursor_position_callback(GLFWwindow* window, double xpos, double ypos)
{
       if (GLFW_MOUSE_BUTTON_RIGHT && GLFW_PRESS) {
              // variables to store mouse cursor coordinates
              static double previous_xpos = xpos;
              static double previous_ypos = ypos;
              double delta_x = xpos - previous_xpos;
              double delta_y = ypos - previous_ypos;

              // pass mouse movement to camera class
              g_camera.updateYaw(delta_x);
              g_camera.updatePitch(delta_y);

              // update previous mouse coordinates
              previous_xpos = xpos;
              previous_ypos = ypos;
       }
}

Not sure if this matters either, but here is my mouse callback. There is a tweakbar in my program.

static void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
    {
        // pass mouse data to tweak bar
        TwEventMouseButtonGLFW(button, action);
    }

Solution

  • GLFW_MOUSE_BUTTON_RIGHT and GLFW_PRESS are macros defined using

    #define GLFW_PRESS 1
    
    #define GLFW_MOUSE_BUTTON_RIGHT GLFW_MOUSE_BUTTON_2
    
    #define GLFW_MOUSE_BUTTON_2   1
    

    So if (GLFW_MOUSE_BUTTON_RIGHT && GLFW_PRESS) { is if( 1 && 1 ) { after the preprocessing step of the compiler.

    You either need to store the current state of the mouse button in the mousebutton callback or query the state using glfwGetMouseButton:

    if ( glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) ==  GLFW_PRESS) {