Skip to main content
gamedev.net
🔒 Locked

"Timer" Out

Started by shankhs Feb 27, 2008 at 7:38 AM 1 replies 700+ views
Original Post
shankhs
shankhs
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 <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;
    }


[Edited by - shankhs on February 27, 2008 8:46:10 AM]
V-man
V-man
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 calls
glutTimerFunc(100, TimerFunc, 1);
so now he changes the timing to 100 ms. Very bizzare or should I say crap code.
songho
songho
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.