Original Post
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. [Edited by - shankhs on February 27, 2008 8:46:10 AM]
/*The program is to draw an atom.The window limits are:65.0f,65.0f,65.0f.*/
#include <windows.h>
#include <gl/glut.h>
// Rotation amounts
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
// Called to draw scene
void 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 transformation
glPushMatrix();
// Rotate by angle of revolution
glRotatef(fElect,0.0f,1.0f,0.0f);
// Translate out from origin to orbit distance
glTranslatef(80.0f,0.0f,0.0f);
// Draw the electron
glutSolidSphere(6.0f,15,15);
// Restore the viewing transformation
glPopMatrix();
//Second Electron orbit
glPushMatrix();
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 orbit
glPushMatrix();
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>360.0f)
fElect=0.0f;
glutSwapBuffers();
}
// Setup the rendering state
void SetupRC(void)
{
glEnable(GL_DEPTH_TEST); // Hidden surface removal
glFrontFace(GL_CCW); // Counter clock-wise polygons face out
glEnable(GL_CULL_FACE); // Do not calculate inside of jet
// Black background
glClearColor(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 > 356.0f)
xRot = 0.0f;
if(key < -1.0f)
xRot = 355.0f;
if(key > 356.0f)
yRot = 0.0f;
if(key < -1.0f)
yRot = 355.0f;
// Refresh the Window
glutPostRedisplay();
}
void ChangeSize(int w, int h)
{
GLfloat nRange = 100.0f;
// Prevent a divide by zero
if(h == 0)
h = 1;
// Set Viewport to window dimensions
glViewport(0, 0, w, h);
// Reset coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Establish clipping volume (left, right, bottom, top, near, far)
if (w <= 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 point
int 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;
}