JoeyDeVries / LearnOpenGL

Code repository of all OpenGL chapters from the book and its accompanying website https://learnopengl.com
https://learnopengl.com
Other
11.06k stars 2.8k forks source link

Key callbacks can be replaced with glfwGetKey() #53

Closed charlesdong closed 7 years ago

charlesdong commented 7 years ago

Hi, Joey. I found that you use a key callback to handle key input in the LearnOpenGL tutorial. But GLFW also provides the glfwGetKey() function to query if a key is down. Also in the Camera tutorial, the keys array is not required if you use glfwGetKey() (because GLFW already stores it for you).

For example, the following function:

void do_movement()
{
  GLfloat cameraSpeed = 0.01f;
  if(keys[GLFW_KEY_W])
    cameraPos += cameraSpeed * cameraFront;
  if(keys[GLFW_KEY_S])
    cameraPos -= cameraSpeed * cameraFront;
  if(keys[GLFW_KEY_A])
    cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
  if(keys[GLFW_KEY_D])
    cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
}

Can be replaced with:

void do_movement()
{
// 'window' is the GLFW window handle
  GLfloat cameraSpeed = 0.01f;
  if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
    cameraPos += cameraSpeed * cameraFront;
  if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
    cameraPos -= cameraSpeed * cameraFront;
  if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
    cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
  if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
    cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
}
JoeyDeVries commented 7 years ago

I agree, it makes everything much cleaner. I'm currently working on a major code overhaul for all tutorials and I'll be sure to include your suggestion in, thanks!

JoeyDeVries commented 7 years ago

Should be in the code overhaul at this moment. Has yet to be reflected in the text content, which I'll work on the upcoming weeks.