Sign in to follow this  
judge dreadz

what exactly does glutMainLoop() do?

Recommended Posts

this one has really got me stumped. i have a ps->update() function that updates data stored in a texture on the GPU. i was originally testing this by just calling it a bunch of times and checking the results like so: int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); glutInitWindowSize(800, 600); glutCreateWindow("SmokeProject"); glutDisplayFunc(display); initGL(); ps = new ParticleSystem(2, 2); for (int i=0; i<10; i++) ps->update(); glutMainLoop(); } no problems there, everything works fine. however, when i move ps->update() and put it in the display method so it calls repeatedly, it doesnt work. i removed ALL other code from the display method, because i thought it might be something to do with calls to clear or swap buffers in the display method that was effecting the frame buffer object that my textures are attached to. this is all my display method is: void display(void) { ps->update(); } so i'm a bit confused as to why the top code sample functions perfectly and the bottom one doesnt. i can only think there is some hiden background work that the glutMainLoop() function is doing thats messing things up. any ideas? i also removed the glutReshapeFunction because i wondered if the perspective mapping i was doing there was intefering with the 1:1 mapping set up between my frame buffer object and the texture. the error that is occouring is that the pixel data in the texture is getting copied from the first pixel. so for example, for a 2*2 RGBA texture the initial data might look like this. ( 1, 1, 1, 1 ) ( 2, 2, 2, 2 ) ( 3, 3, 3, 3 ) ( 4, 4, 4, 4 ) if i sent this through the a shader program that multiplied every value by 2, it would come back with ( 2, 2, 2, 2 ) at every pixel. so the first pixel is getting updated ok but all the other pixels become copies of the first pixel. sorry for such a long post!

Share this post


Link to post
Share on other sites
glutMainLoop() is a function that loops within itself, processing events and triggering your callback functions when necessary (glutDisplayFunc, etc). After a call to glutMainLoop(), you have no control over the execution of your program besides the callback functions you initialized before the call.

And another thing, the glutDisplayFunc() callback only gets triggered when your app's window receives a refresh event, unless you explicity post a redisplay. If you want your display function to be called repeatedly (i.e. for a game), you need to set your glutIdleFunc to post a redisplay.

Something like this:

int main()
{
/* ... */
glutIdleFunc(updateGame);
/* ... */

glutMainLoop();
}

void updateGame()
{
glutPostRedisplay();
}


Make sense? :-)

Share this post


Link to post
Share on other sites
yes i had a redisplay function, i took it out because i took EVERYTHING out that wasnt absolutely essential to initialise a window and call the code.

as far as i can tell, glut HAS to be doing something in the background that is making things go wrong.

here is an example to show what i mean -

1st i ran the program like so:

-----------------------------

void main(args)
{
... init everything;
ps->update();
glutMainLoop();
}

display() {} // (empty glutDisplayFunc)

-----------------------------


then i tried like this:


-----------------------------

bool updated = false;

void main(args)
{
... init everything;
glutMainLoop();
}

display()
{
if (updated != true)
{ ps->update; updated=true; }
}

-----------------------------

the first code snippet functions fine, the second does not. they are, at least in appearance, identical. both simply call ps->update() once. the one which calls it before glutMainLoop() works, the other one doesnt.

Share this post


Link to post
Share on other sites
Setup all of your callbacks before you call glutMainLoop(). Use glutDisplayFunc() for your draw function, glutIdleFunc() for your update function, and then call glutMainLoop() to start calling those functions when needed.

In your Idle function, you should do all of your update stuff, and then right at the end, call glutPostRedisplay().

In your draw function, you should call glClear(GL_COLOR_BUFFER_BIT), followed by all of your drawing code and then glutSwapBuffers().

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this