PDA

View Full Version : Help on glutIdleFunc



Vasilis
11-23-2000, 12:56 AM
Can somebosy please give me an example on how to use glutIdleFunc(display) because I am trying to remove the FOR loop that currently annimates my prog within the display function.

thanks

Vasilis

Gavin
11-23-2000, 06:10 AM
Have you looked at the example code/apps that you get in the sdk?

gav

Moz
11-23-2000, 07:00 AM
An example of a glutIdleFunc that I use in one of my programs :

void anim(void)
{
static bool drawn;
if(!pause)
{
angle += sign ;
if (angle >= 360.0)
angle -= 360.0 ;
glutPostRedisplay(); // This actually forces glut to call your display function in the next loop;
drawn = false;
}
else
{
if(!drawn)
{
glutPostRedisplay(); // idem
drawn = true;
}
}
}

It's registered here :
int main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize (200, 170);
glutInitWindowPosition (580, 40);
glutCreateWindow ("Whatever");
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(anim); // here it is.
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

Vasilis
11-23-2000, 01:54 PM
How about sth more simpler?
I have been doing this and I want to get rid of the FOR loop and use the IdleFunc...Can you improvise??
Thanks

#include <windows.h>
#include <GL/gl.h>
#include <glut.h>

void display(void){
float angle=0;

glMatrixMode(GL_MODELVIEW);
for(angle=0;angle<=10000;angle++){
glLoadIdentity();
glTranslatef(0,0,0);
glRotatef(angle,0,0,1);
glTranslatef(0,0,0);
glRotatef(angle,0,0,1);
glBegin(GL_POLYGON) ;
glColor3f(0.0,0.0,0.0);
glVertex2f(-15.0,-15.0);
glColor3f(1.0,0.0,0.0);
glVertex2f(15.0,-15.0);
glColor3f(0.0,1.0,0.0);
glVertex2f(15.0,15.0);
glColor3f(0.0,0.0,1.0);
glVertex2f(-15.0,15.0);
glEnd();
glutSwapBuffers();
glClear(GL_COLOR_BUFFER_BIT);
}
}

void init(void){
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-200.0, 200.0, -200.0, 200.0, -100.0, 100.0);
}

int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(401, 401);
glutInitWindowPosition(100,100);
glutCreateWindow("Rotating a Square");
glutDisplayFunc(display);
// glutIdleFunc(display);
init();
glutMainLoop();
return 0;
}


[This message has been edited by Vasilis (edited 11-23-2000).]

11-23-2000, 02:50 PM
Now now vasilis you shouldnt be asking others to write your university assignments for you!!!!

11-23-2000, 02:55 PM
try romving the loop and putting the angle as a global variable. increment the angle within the display func as the last line. put the glutidlefunc just before the mainloop call and remove the return 0;
might help

you were lucky by the way we hit the post atleast twice.

Vasilis
11-23-2000, 03:48 PM
thanks for the help matey...
I got it working before you told me actually...

I'll try to fix the second moon by the way ;o)

And u did get the bar 3 times so it could have benn 2-5....but thank god it wasn't...
But I'm optiomistic for the return game....

[This message has been edited by Vasilis (edited 11-23-2000).]