Be careful what you grab!

Grabbing a rendered image is a fairly common thing to do in VTK applications. Many do that as follows:

This seemingly innocuous code has a serious flaw. You’re actually grabbing undefined contents! Let’s look at what’s going on a little more closely.

vtkRenderWindow, by default, is setup to use double-buffering. When using double-buffering, it is also setup to swap back and front buffers at the end of each render. The kicker is that despite its name (and common perception), this swap is not defined as a true swap. It merely implies back buffer contents being copied to the front buffer. What is left in the back buffer at the end of the swap is not defined. As a result, when vtkWindowToImage filter goes to grab the back-buffer after the render, it invariable is reading undefined contents — although one may be getting lucky in most cases.

The correct way to address this would be as follows:

Another thing to note is that if you are telling vtkWindowToImage filter to not re-render i.e. vtkWindowToImage::ShouldRerenderOff(), then make sure that the SwapBuffers is turned off before the latest Render call on the render-window.

Questions or comments are always welcome!