Hi! There is my code:
bool isMouseGrabbed(GLFWwindow* window)
{
return glfwGetInputMode(window, GLFW_CURSOR) != GLFW_CURSOR_NORMAL;
}
void setMouseGrabbed(GLFWwindow* window, bool grabbed)
{
if (grabbed)
{
oldMousePositionX = windowWidth / 2.0;
oldMousePositionY = windowHeight / 2.0;
glfwSetCursorPos(window, oldMousePositionX, oldMousePositionY);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
}
else if (isMouseGrabbed(window))
{
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
}
void glfw_callback_MouseButton(GLFWwindow* window, int button, int action, int mods)
{
if (action == GLFW_PRESS && button == GLFW_MOUSE_BUTTON_LEFT && !isMouseGrabbed(window))
{
std::cout << "button callback" << std::endl;
double mousePositionX;
double mousePositionY;
glfwGetCursorPos(window, &mousePositionX, &mousePositionY);
std::cout << "Left mouse click at [" << mousePositionX << "," << mousePositionY << "]" << std::endl;
setMouseGrabbed(window, true);
std::cout << "Mouse is grabbed." << std::endl;
}
}
void glfw_callback_MouseMove(GLFWwindow* window, double x, double y)
{
if (isMouseGrabbed(window))
{
int mouseDeltaX = static_cast<int>(x - oldMousePositionX);
int mouseDeltaY = static_cast<int>(y - oldMousePositionY);
std::cout << "Deltas: " << mouseDeltaX << "," << mouseDeltaY << std::endl;
if (mouseDeltaX || mouseDeltaY)
{
//const float mouseSensitivity = 10.0f;
//rotateCamera(camera, mouseDeltaX, mouseDeltaY, mouseSensitivity);
oldMousePositionX = x;
oldMousePositionY = y;
}
}
}
My problem is:
When my window is unfocused, i click on it and sometimes i see the output to the console Deltas: ...
with some X
/ Y
values. This leads to unnecessary rotation of the camera.
There is my log:
Left mouse click at [564,216]
Mouse is grabbed.
Mouse is released.
button callback
Left mouse click at [540,45]
Mouse is grabbed.
Deltas: 220,-195
Deltas: -220,195
Mouse is released.
button callback
Left mouse click at [496,18]
Mouse is grabbed.
Mouse is released.
button callback
Left mouse click at [505,140]
Mouse is grabbed.
Mouse is released.
button callback
Left mouse click at [491,88]
Mouse is grabbed.
Deltas: 171,-152
Deltas: -171,152
Mouse is released.
This bug(?) only appears on Windows. I have tested it on my old Linux notebook, but it’s fine.