tip
03-14-2005, 08:39 PM
I am using the glutTimerFunc to speed up an object. I need a way to slow it down. And change the translate to go in the opposite direction after the object has stopped.
This function is the timer handle used for the glutTimerFunc()
void moveTri(int value)
{
/* stop after 30 steps */
if(motion_speed>=5000)
{
return;
}
glutPostRedisplay;
if(back_flag)
step--;
else
step++;
/* call animate again in 200 mili-Secs */
glutTimerFunc(motion_speed,moveTri,step);
}
The special key handler function. When the up arrow is pressed, the triangle should accelerate. when the down arrow is pressed, the triangle slows down. Keep pressing will slow down more and eventually stop. How to make the triangle go in reverse after triangle has stopped?
void specialKeyPressed(int key, int x, int y)
{
if (key == GLUT_KEY_UP )
{
motion_speed--;
back_flag = 0;
glutTimerFunc(motion_speed/60,moveTri,step);
cout << "triangle moving forward" << endl;
}
else if (key == GLUT_KEY_DOWN)
{
motion_speed+=1500;
if (motion_speed > 6000)
back_flag = 1;
cout << motion_speed << endl;
glutTimerFunc(motion_speed,moveTri,step);
cout << "triangle slowing down" << endl;
}
else if (key == GLUT_KEY_LEFT)
{
turn_left();
cout << "triangle moving left" << endl;
}
else if (key == GLUT_KEY_RIGHT)
{
cout << "triangle moving right" << endl;
}
}
The following code is the display function:
void display(void)
{
rquad = 1.0;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glScaled(3,3.5,1);
glTranslatef(-10.0, -12.0,1.0);
get_list();
glTranslatef(0.0, step,0.0);
draw_tri();
glPopMatrix();
glFlush(); //clear buffers
glutSwapBuffers ();
}
This function is the timer handle used for the glutTimerFunc()
void moveTri(int value)
{
/* stop after 30 steps */
if(motion_speed>=5000)
{
return;
}
glutPostRedisplay;
if(back_flag)
step--;
else
step++;
/* call animate again in 200 mili-Secs */
glutTimerFunc(motion_speed,moveTri,step);
}
The special key handler function. When the up arrow is pressed, the triangle should accelerate. when the down arrow is pressed, the triangle slows down. Keep pressing will slow down more and eventually stop. How to make the triangle go in reverse after triangle has stopped?
void specialKeyPressed(int key, int x, int y)
{
if (key == GLUT_KEY_UP )
{
motion_speed--;
back_flag = 0;
glutTimerFunc(motion_speed/60,moveTri,step);
cout << "triangle moving forward" << endl;
}
else if (key == GLUT_KEY_DOWN)
{
motion_speed+=1500;
if (motion_speed > 6000)
back_flag = 1;
cout << motion_speed << endl;
glutTimerFunc(motion_speed,moveTri,step);
cout << "triangle slowing down" << endl;
}
else if (key == GLUT_KEY_LEFT)
{
turn_left();
cout << "triangle moving left" << endl;
}
else if (key == GLUT_KEY_RIGHT)
{
cout << "triangle moving right" << endl;
}
}
The following code is the display function:
void display(void)
{
rquad = 1.0;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glScaled(3,3.5,1);
glTranslatef(-10.0, -12.0,1.0);
get_list();
glTranslatef(0.0, step,0.0);
draw_tri();
glPopMatrix();
glFlush(); //clear buffers
glutSwapBuffers ();
}