13

When reading the redbook I found:

glutDisplayFunc(void (*func)(void)) is the first and most important event callback function you will see. Whenever GLUT determines that the contents of the window need to be redisplayed, the callback function registered by glutDisplayFunc() is executed. Therefore, you should put all the routines you need to redraw the scene in the display callback function.

If your program changes the contents of the window, sometimes you will have to call glutPostRedisplay(), which gives glutMainLoop() a nudge to call the registered display callback at its next opportunity

Which are times in which glutPostRedisplay() should be called? From this paragraph, I don't understand why its functionality is needed.

andandandand
  • 21,946
  • 60
  • 170
  • 271

2 Answers2

19

glutDisplayFunc is called whenever your window must be redrawn. This includes the time when one calls glutPostRedisplay :)

When does a window need to be redrawn?

  • When its size changes
  • when it becomes visible
  • when some parts of it become visible
  • when it is moved
  • etc

But what if your display function paints a triangle at position x;y where x;y; are determined by the mouse position? In this case you must ask the system to redraw the window whenever the mouse is moved right? That's why you'll call glutPostRedisplay from MouseFunc(). Actually when you call glutPostRedisplay, the redraw event is queued along with other window-events, like mouse click ets. Essentially what your mainLoop does it pick events from that queue and call their handlers

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • I don't understand why you wouldn't call glutDisplayFunc instead of glutPostRedisplay in the case you mention considering glutPostRedisplay eventually ends up calling glutDisplayFunc. What's the importance of the queue? Can it delay rendering while waiting for a condition? – andandandand Nov 17 '10 at 17:07
  • 2
    @omgzor: In the above example, you could do that. But that's not the best choice. You see, it's better not call it directly but put it in a queue. If for example you want animation and call displayFunc directly from displayFunc, your program will not respond. Because you will have blocked the loop. That's why it's best to post the event and let the main loop handle it in its turn – Armen Tsirunyan Nov 17 '10 at 17:17
  • @omgzor: It won't delay rendering. It will delay everything else :) – Armen Tsirunyan Nov 17 '10 at 17:18
4

The function you pass to glutDisplayFunc is only called it is needed: that means when the window is resized, or when an another window has hidden it. If you use glutMouseFunc, for instance, you perhaps want to update (redraw) your window content according to that clic. Also, if you draw animations, you need to call glutPostRedisplay from your idle function.

tibur
  • 11,531
  • 2
  • 37
  • 39