1
votes

I'm creating an OpenGL application in c++ using GLFW. Based on this tutorial i managed to create FPS like camera(WASD movement + pitch-yaw with mouse movement).

For camera mouse movement i'm using

glfwSetCursorPosCallback(window, mouse_callback);
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
    if (firstMouse)
    {
        lastX = xpos;
        lastY = ypos;
        firstMouse = false;
    }

    float xoffset = xpos - lastX;
    float yoffset = lastY - ypos; 
    lastX = xpos;
    lastY = ypos;

    float sensitivity = 0.1f;
    xoffset *= sensitivity;
    yoffset *= sensitivity;

    yaw += xoffset;
    pitch += yoffset;

    if (pitch > 89.0f)
        pitch = 89.0f;
    if (pitch < -89.0f)
        pitch = -89.0f;

    glm::vec3 front;
    front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
    front.y = sin(glm::radians(pitch));
    front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
    cameraFront = glm::normalize(front);
}

This is working fine, but problem is that my application window is not full screen, so if i want to rotate my mouse cursor gets out of window and then it's hard to control camera.

Is it possible to do the same thing as glfwSetCursorPosCallback, but only if left click is pressed? I want the camera do the same thing as its doing now, but only if i pressed left click.

1
I didnt understand exactly what you were asking, how you rotate a camera in glfw or how you receive the mouseposition outside of your window? - user11220832
i have the rotation done based on mouse cursor movement, what i'd like to do is to do the rotation only if left mouse click is pressed - mereth

1 Answers

2
votes

Is it possible to do the same thing as glfwSetCursorPosCallback, but only if left click is pressed? I want the camera do the same thing as its doing now, but only if i pressed left click.

glfwSetMouseButtonCallback sets a callback, which is notified when a mouse button is pressed.
The current mouse (cursor) position can be get by glfwGetCursorPos.

Add a mouse button callback:

glfwSetMouseButtonCallback(window, mouse_button_callback);

And get the mouse position in the callback:

void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
    if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
    {
        double xpos, ypos;     
        glfwGetCursorPos(window, &xpos, &ypos); 

        // [...]
    }
}