   "Timer" Out
  hey I am a newbie to openGL and donno much about animation(I am still learning it)I came across a code that uses timer function to do animation .Can somebody please explain this timer function to me./*The program is to draw an atom.The window limits are:65.0f,65.0f,65.0f.*/#include &lt;windows.h&gt;#include &lt;gl/glut.h&gt;// Rotation amountsstatic GLfloat xRot = 0.0f;static GLfloat yRot = 0.0f;// Called to draw scenevoid RenderScene(void){static GLfloat fElect = 0.0f;glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glTranslatef(0.0f,0.0f,-90.0f);glColor3ub(255,0,0);glutSolidSphere(10.0f,15,15);glColor3ub(255,255,0);//One Electron orbit// Save viewing transformationglPushMatrix();// Rotate by angle of revolutionglRotatef(fElect,0.0f,1.0f,0.0f);// Translate out from origin to orbit distanceglTranslatef(80.0f,0.0f,0.0f);// Draw the electronglutSolidSphere(6.0f,15,15); // Restore the viewing transformationglPopMatrix();//Second Electron orbitglPushMatrix();glRotatef(45.0f,0.0f,0.0f,1.0f);glRotatef(fElect,0.0f,1.0f,0.0f);glTranslatef(-80.0f,0.0f,0.0f);glutSolidSphere(6.0f,15,15);glPopMatrix();//Third Electron orbitglPushMatrix();glRotatef(360.0f-45.0f,0.0f,0.0f,1.0f);glRotatef(fElect,0.0f,1.0f,0.0f);glTranslatef(0.0f,0.0f,80.0f);glutSolidSphere(6.0f,15,15);glPopMatrix();fElect+=10.0f;if(fElect&gt;360.0f)fElect=0.0f;glutSwapBuffers();}// Setup the rendering statevoid SetupRC(void)    {    glEnable(GL_DEPTH_TEST);// Hidden surface removalglFrontFace(GL_CCW);// Counter clock-wise polygons face outglEnable(GL_CULL_FACE);// Do not calculate inside of jet// Black backgroundglClearColor(0.0f, 0.0f, 0.0f, 1.0f );    }void TimerFunc(int value)    {    glutPostRedisplay();    glutTimerFunc(100, TimerFunc, 1);    }void handleKeyPress(unsigned char key,int x,int y){switch(key){case 27:exit(0);case 32:exit(0);case 13:exit(0);default:break;}glutPostRedisplay();}void SpecialKeys(int key, int x, int y){if(key == GLUT_KEY_UP)xRot-= 5.0f;if(key == GLUT_KEY_DOWN)xRot += 5.0f;if(key == GLUT_KEY_LEFT)yRot -= 5.0f;if(key == GLUT_KEY_RIGHT)yRot += 5.0f;if(key &gt; 356.0f)xRot = 0.0f;if(key &lt; -1.0f)xRot = 355.0f;if(key &gt; 356.0f)yRot = 0.0f;if(key &lt; -1.0f)yRot = 355.0f;// Refresh the WindowglutPostRedisplay();}void ChangeSize(int w, int h){GLfloat nRange = 100.0f;// Prevent a divide by zeroif(h == 0)h = 1;// Set Viewport to window dimensions    glViewport(0, 0, w, h);// Reset coordinate systemglMatrixMode(GL_PROJECTION);glLoadIdentity();// Establish clipping volume (left, right, bottom, top, near, far)    if (w &lt;= h) glOrtho (-nRange, nRange, nRange*h/w, -nRange*h/w, -nRange*2.0f, nRange*2.0f);    else glOrtho (-nRange*w/h, nRange*w/h, nRange, -nRange, -nRange*2.0f, nRange*2.0f);glMatrixMode(GL_MODELVIEW);glLoadIdentity();  }// Main program entry pointint main(int argc, char* argv[])    {glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);glutCreateWindow("Shankhs Atom");glutReshapeFunc(ChangeSize);glutSpecialFunc(SpecialKeys);glutDisplayFunc(RenderScene);    glutTimerFunc(500, TimerFunc, 1);SetupRC();glutKeyboardFunc(handleKeyPress);glutMainLoop();return 0;    }[Edited by - shankhs on February 27, 2008 8:46:10 AM]
  In main(), he has written glutTimerFunc(500, TimerFunc, 1);so every 500 milliseconds, TimerFunc will be called.void TimerFunc(int value){    glutPostRedisplay();    glutTimerFunc(100, TimerFunc, 1);}he is not making use of value.He callsglutTimerFunc(100, TimerFunc, 1);so now he changes the timing to 100 ms. Very bizzare or should I say crap code.;  The first param of glutTimerFunc() is the waiting time (ms) before calling your callback function (the second param).The last param is the argument that is passed to your callback function. It is very useful to set a constant drawing interval (constant frame rate) when you register the timer callback.For example;// register timer callback// call TimerFunc initially after 500 ms// and set the draw interval to 100 msglutTimerFunc(500, TimerFunc, 100);...// timer callback// Notice that the argument "value" is used for the next call of glutTimerFunc() // in order to get a constant speed of the frame rate.void TimerFunc(int value){    glutTimerFunc(value, TimerFunc, value);    glutPostRedisplay();}Note:1. The resolution of GLUT timer is not high enough. It is about 10 ms, so, using 1 ms in glutTimerFunc() is not good. If you want full speed of rendering, then, use glutIdleFunc() instead.2. Call glutTimerFunc() first before glutPostRedisplay() in the timer callback to achieve the constant draw interval. Your display callback may spend a long period time.   