PDA

View Full Version : glutIdleFunc and animation



gazsux
03-21-2003, 03:45 AM
Im trying to impliment some basic equation based animation. Using glutGet for the time and idle to keep it animating at a steady pace. Ive been told to not put any drawing type code in the idle function itself, also opengl should realise when the function is idle and call its own idle function. But im not sure if i have this right. Only when i impliment it, the animation doesnt occur. Here's some code, do i need to call the idle func from the display one? (surely that will just be based on the computer speed if i did that)

if you have any ideas of where im going wrong, please help me




void idle(void)
{
float fTime, fSimTime;
float static fLastIdleTime=0;

fTime=glutGet(GLUT_ELAPSED_TIME)/1000;

fSimTime=fTime-fLastIdleTime;

animation(fSimTime);

fLastIdleTime=fTime;

glutPostRedisplay();
}

void visible(int iVis)
{
if(iVis==GLUT_VISIBLE)
glutIdleFunc(idle);
else
glutIdleFunc(NULL);

return;
}

void animation(float fSimTime)
{
float fAngle;

glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);

fAngle=fSimTime*iSpeed;

glTranslatef( 26.00f, 2.00f, 18.95f);
glRotatef(fAngle, 0, 0, 1);
glColor3ub(100, 100, 100);
glCallList(cog);
glRotatef(-fAngle, 0, 0, 1);
glTranslatef(-26.00f,-2.00f,-18.95f);

glTranslatef( 23.00f, 2.00f, 18.95f);
glRotatef(fAngle+25, 0, 0, 1);
glColor3ub(100, 100, 100);
glCallList(cog);
glRotatef(-fAngle-25, 0, 0, 1);
glTranslatef(-23.00f,-2.00f,-18.95f);

glDisable(GL_COLOR_MATERIAL);

}

mikael_aronsson
03-21-2003, 03:55 AM
Hi !

You should not call animation() from the idle function, this should be called from the display callback, the glutPostReDisplay() will tell the OS to redraw the contents of your window and the display function will be called.

In the idle function you should only update variables that track the state of the animation (position, size or what ever).

I hope that helps
Mikael

gazsux
03-21-2003, 03:57 AM
ahh thank you http://www.opengl.org/discussion_boards/ubb/smile.gif it does help, cheers