Link to home
Start Free TrialLog in
Avatar of Kirby27
Kirby27

asked on

OpenGL

I am writing a simple openGL program and I am using the the 'r' key as a toggle to spin the head of an object. Is there a way to use the 'r' key as a true toggle so that the head will spin continually with one tap of the key and spin until the key is pressed again?

Here is how I have it setup now:

void keyboard (unsigned char key, int x, int y)
{
     switch (key)
     {
     case 'r':
          head = (head + 10) % 360;  //update global variable day
          glutPostRedisplay ();  //force a call to display
          break;

     case 'R':
          head = (head + 10) % 360;  //update global variable head
          glutPostRedisplay ();  //force a call to display
          break;

Thanks.
Avatar of skyDaemon
skyDaemon

Spawn a timer when the key is pressed.  If the timer is already running, then kill the timer.  

When the timer fires it executes your spin code.  This will let one keypress spin it forever at whatever rate your timer fires until the key is pressed again.

Avatar of Kirby27

ASKER

Thanks for the input but this is my very first attempt with openGL and you'll have to be more specific.
ASKER CERTIFIED SOLUTION
Avatar of ivec
ivec

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What programming program?? VC++?
I suggest an easier solution without using timer. I guess you have an infinite loop in your programm which draws a frame and asks for keyboard input. Make another variable like

    int incHead = 0;

Change your keyboard handler so it will operate on the new variable:

    case 'r':
         incHead = 10;

    case 'R':
         incHead = 0;

And change you infinite loop to contain the following somewhere at the end:

    head += incHead;
    head %= 360;

That means that you'll keep spinning each frame all the time while your incHead is 10 and stop when it's 0.
By changing the 10 to something else you change the speed of the rotation.

Hope it helps.
Slawa.