PDA

View Full Version : Keyboard triggers animation



AK_N00B
06-26-2012, 01:20 PM
Guys, I'm totally lost on this one... could someone help?

I have to move smoothly an object from one side of my 3d room to another, every time a single key is pressed. I declare a global variable with the initial x position of it, and then render it:


glPushMatrix(); glTranslatef(positionObject, 1, -6);
object->Render();
glPopMatrix();

So I guess that, for the smooth movement, I'll need a timer that shows the object position several times per second, right? I'm doing it like this (not the proper way to do it, I'm aware):


void Timer(int value) {
if (positionObject == -1.5)
{
while (positionObject <= -2.3)
positionObject += -0.1;
}
else
{
while (positionObject != -1.5)
positionObject -= -0.1;
}


glutPostRedisplay();
glutTimerFunc(33, Timer, 1);
}

And my final doubt is: what do I call in my keyboard callback function for it to work?


void KeyboardCallback(unsigned char key, int x, int y){
switch (key)
{
case 'r':
case 'R':
//do sth;
break;

Any help will be appreciated.