Reputation: 191
I'm not sure where problem is.
Though, I have two modes in my application. By pressing "R" - key the modes are switching. The application-class is responsible for the entire app's demeanor. It contains the private member, the mode-flag (rotation_mode
). In the key handling method I set this flag to the opposite value if the R-key has been pressed.
void Application::keyCallBack()
{
if (glfwGetKey(this->window, GLFW_KEY_ESCAPE) == GLFW_PRESS) this->setWindowShouldClose();
if (glfwGetKey(this->window, GLFW_KEY_R) == GLFW_PRESS) this->rotation_mode = !this->rotation_mode;
...
}
After, this flag uses as mode-definition. If a rotation mode is off, then we we should rotate a camera via mouse, otherwise we should rotate a model via mouse (when the left mouse button pressed), whereas the camera's direction is freezed.
...
if (this->rotation_mode)
{
std::cout << "rotation mode\n";
if (glfwGetMouseButton(this->window, GLFW_MOUSE_BUTTON_1) == GLFW_PRESS)
{
if (glfwGetKey(this->window, GLFW_KEY_LEFT_ALT) == GLFW_PRESS)
{
std::cout << "rotate" << std::endl;
this->model.rotation_angle_x += this->y_offset * 6.0f * this->delta_time;
}
else
this->model.rotation_angle_z += this->x_offset * 6.0f * this->delta_time;
this->model.rotation_angle_y += this->y_offset * 6.0f * this->delta_time;
}
}
else
this->camera.camera_obj.ProcessMouseMovement(this->x_offset, this->y_offset);
...
Do not blame me for the implementation:)) It's not done, yet. The problem is that, when I press "R", occasionally the mode is not turning and the output "rotation mode" is still going on.
Note: I'm not using glfwSetKeyCallback
, due to some implementation's troubles. I update it in the infinite while-loop.
Upvotes: 1
Views: 939
Reputation: 5868
According to the reference: "This function returns the last state reported for the...".
Possible that the states gets somehow overwritten or there is a race condition? Best would be to retrieve the input from the callback mechanism.
According to Thread Safety:
"This function must only be called from the main thread."
That's the thread where you initialized glfW and created your window. Where do you run your implementation?
Besides:
glfwGetMouseButton(this->window, GLFW_KEY_LEFT_ALT)
Can you do that? The reference lists only the 'GLFW_MOUSE_BUTTON_X'. Shouldn't that be glfWGetKey?
Edit:
I looked into the source code of glfW. The event processing function calls in case of KeyPress and KeyRelease the same function which sets the states of the window and calls the callback. Remember, after a Press there is a Release, which is handled by glfW but not by you.
The glfwGetKey function only reads the states of the specific window, which are set by the event processing function.
p.s. im under linux and i know X, so this was the place where i have done my research, how it is implemented for the other platforms, i don't know, but i think similar.
Upvotes: 1