开发者

keyboard event in OpenGL with glut

开发者 https://www.devze.com 2023-03-24 13:12 出处:网络
I\'m programming on OpenGL using the glut library and I\'ve got a problem with controlling the position of an object with keyboard.

I'm programming on OpenGL using the glut library and I've got a problem with controlling the position of an object with keyboard.

I have the following callback function for glutKeyboardFunc

void handleKeypress (unsigned char key, int x, int y)
{
 switch (key)
{
case 27:
  exit (0); break;
case 'w':
  position_y += 0.2f; break;
case 's':
  position_y -= 0.2f; break;
case 'a':
  position_x -= 0.2f; break;
case 'd':
  position_x += 0.2f; break;
case 'q':
  depth += 0.2f; break;
case 'e':
  depth -= 0.2f; break;
case 'z': 
  angle += 0.2f; break;
case 'c':
  angle -= 0.2f; break;
}
}`

Nonetheless, I know this is not enough. I have to add glutPostRedisplay() if I want to have the changes take place-but where? Do I have开发者_高级运维 to put it just before every "break" in the above codes? I have tried to add it to a glutTimerFunc callback and a glutIdleFunc callback but the objects keep moving after a keystroke.


Yes, you can add it after every break. But you could also just add it at the very bottom of the function.

If you add it to your glutIdleFunc callback, the screen will be redrawn even when there are no changes. This should not cause any problems though.

I don't understand why your object keep moving. Maybe you forgot to reset the modelview matrix with glLoadIdentity (); in your glutDisplayFunc callback.

0

精彩评论

暂无评论...
验证码 换一张
取 消