?
Solved

OpenGL

Posted on 2003-03-24
5
Medium Priority
?
486 Views
Last Modified: 2008-02-26
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.
0
Comment
Question by:Kirby27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 2

Expert Comment

by:skyDaemon
ID: 8198934
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.

0
 

Author Comment

by:Kirby27
ID: 8200015
Thanks for the input but this is my very first attempt with openGL and you'll have to be more specific.
0
 
LVL 1

Accepted Solution

by:
ivec earned 75 total points
ID: 8201128
Here's one way to do it.
First add the following constant, global variable and function:

enum { kTimerDelay = 100; };
int headStep = 0; // angle increment at each iteration

void timerFunc(int val)
{
  // update the head (more params could be added)
  head = (head + headStep) % 360; // kept as is

  glutPostRedisplay ();  // request redisplay

  // schedule the next callback
  glutTimerFunc( kTimerDelay , &timeFunc );
}


Right after your glut initialization code, add a call to:
  glutTimerFunc( kTimerDelay , &timerFunc );
to initiate the repeated timer.

Then update your keyboard callback as follows:
  switch( key )
  {
    case 'r': case 'R':  // can be just combined this way
      if( headStep == 0 )
        headStep = 10;
      else
        headStep = 0; //NB: instead, could inc/dec-rement.

This should do...
0
 

Expert Comment

by:Erik4545
ID: 8202449
What programming program?? VC++?
0
 

Expert Comment

by:slawa
ID: 8202674
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.
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question