根据描述,我认为代码是这样的
while(condition)
{
model = glm::rotate(model, glm::radians(rotationAngle.x * glfwGetTime()), glm::vec3(1.0f, 0.0f, 0.0f));
model = glm::rotate(model, glm::radians(rotationAngle.y * glfwGetTime())), glm::vec3(0.0f, 1.0f, 0.0f));
model = glm::rotate(model, glm::radians(rotationAngle.z * glfwGetTime())), glm::vec3(0.0f, 0.0f, 1.0f));
if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
{
rotationAngle.x += 0.01;
}
}
当你按住键时,rotationAngle 每次增加 0.01,并且每次循环迭代,rotate 函数每次循环旋转一个较大的量。为防止这种情况发生,if 语句应仅在从“未按下”变为“按下”时激活。我们可以用一个标志来做到这一点。
while(condition)
{
static bool keyDown = false;
model = glm::rotate(model, glm::radians(rotationAngle.x * glfwGetTime()), glm::vec3(1.0f, 0.0f, 0.0f));
model = glm::rotate(model, glm::radians(rotationAngle.y * glfwGetTime())), glm::vec3(0.0f, 1.0f, 0.0f));
model = glm::rotate(model, glm::radians(rotationAngle.z * glfwGetTime())), glm::vec3(0.0f, 0.0f, 1.0f));
if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
{
// We don't combine !keyDown in the outer if statement because
// we don't want the else statement to be activated whenever either
// statement is false.
// If key was not previously pressed, then activate. Otherwise,
// key is already down and we ignore this.
if(!keyDown)
{
rotationAngle.x += 0.01;
keyDown = true;
}
}
// Once you let go of the key, this part activates and resets the flag
// as well as the rotation angle.
else
{
rotationAngle.x = 0.0;
keydown = false;
}
}
它应该或多或少像这样。我不知道 glfwGetKey 的细节,所以您可能需要更多条件来检查密钥的状态。