Double Buffering
- glFlush
- We have been using this at the end of our display function
- This tells openGL to flush any buffers where it might be storing
commands
- This could be due to conditions like network buffers, or hardware buffers.
- This tells opengl to completely render whatever we have in the pipeline
- This is something we do when we want the scene to be "complete"
- glFlush will return before the drawing is complete.
- If we want to wait for the scene to be finished before continuing we should call glFinish which does the same thing but is blocking.
- When producing animation, (which is what we have been doing)
- It is usefull to use double buffering
- This is a technique where (at least) two frame buffers are employed
- one for drawing
- one for displaying.
- If the scene is too complex to be drawn in a single refresh cycle,
we will see parts of the scene drawn in different frames.
- This can lead to strange, undesired effects
- or flickering
- To avoid this draw in one (frame) buffer
- Display from another (frame) buffer
- The display buffer may be used multiple times for refresh while the scene is being drawn.
- When the scene is complete swap bufferes, (probably just a pointer change in the display call).
- The glut man page says that the swap takes place only after the refresh is done, (or durring the vertical retrace) if possible
- It also says that while gl commands can be issued, they are not executed until the buffer swap is complete.
- Thus a partial scene is never drawn.
- Glut needs two bits of information to do double buffering
- glutInitDisplayMode(GLUT_DOUBLE);
- glutSwapBuffers();
- glutSwapBuffers calls a glFlush on all buffers.
- Some code (run it -d to turn double buffering off)
- Notice, double buffering does not speed up the computation, it just makes it look better.
- This is a standard animation technique.
- And a classic trade off, in this case memory for quality.
- Notice, we introduced the idle function glutIdleFunc
- This calls a void func(void) when there is nothing else to do.
- This means there are no other events.
- In reasonable programs, this should not do very much (otherwise the system will "stall" while it completes its work.
- This is the way to link programs where both want the main loop.
- He suggests a glutPostRedisplay() instead of calling display();
- setting the function to Null will disable the use of this function.
- glutPostRedisplay()
- Creates a redisplay event
- But if there are multiple redisplay calls only one is issued.
- This allows events to be properly processed.