[vtkusers] Offscreen Rendering

Michael Hoffer info at michaelhoffer.de
Sun May 27 15:48:23 EDT 2012


Hello,

i am totally new to VTK. First of all, I was really surprised how easy it
is to setup VTK with Java. I compiled it on Linux and OS X. Well done!

Now my question: as I am interested in offscreen rendering I tried the
example below. It worked but unfortunately an empty window was shown. I
didn't expect a window. Do I really have to completely switch hardware
acceleration off by using VTK_OPENGL_HAS_OSMESA? Couldn't I just hide the
window? If it is possible, how do I do that?

My intention is to write a pure Swing component that uses offscreen
rendering. I know that this will slow down everything. But my views will be
relatively small in size (e.g. 300x200 pixel). Thus, the performance should
be ok as long as getting the native memory isn't too time consuming. I have
seen that there are some approaches that also tried that. But all code
samples I have seen looped over the vtk array to copy it to an image. This
is definitely too slow. But using WritableRaster, DataBuffer etc. should
help.

Thanks for your help!

Regards,
Michael

//
// code originally from
http://www.vtk.org/Wiki/VTK/Examples/Cxx/Utilities/OffScreenRendering
 //
import vtk.*;

/**
 *
 * @author Michael Hoffer info at michaelhoffer.de
 */
public class VTKOffscreenSample {

    public static void main(String[] args) {

        loadLibraries();

        // Setup offscreen rendering
        vtkGraphicsFactory graphics_factory = new vtkGraphicsFactory();
        graphics_factory.SetOffScreenOnlyMode(1);
        graphics_factory.SetUseMesaClasses(1);

        vtkImagingFactory imaging_factory = new vtkImagingFactory();
        imaging_factory.SetUseMesaClasses(1);

        // Create a sphere
        vtkSphereSource sphereSource = new vtkSphereSource();

        // Create a mapper and actor
        vtkPolyDataMapper mapper = new vtkPolyDataMapper();
        mapper.SetInputConnection(sphereSource.GetOutputPort());

        vtkActor actor = new vtkActor();
        actor.SetMapper(mapper);

        // A renderer and render window
        vtkRenderer renderer = new vtkRenderer();
        vtkRenderWindow renderWindow = new vtkRenderWindow();
        renderWindow.SetOffScreenRendering(1);
        renderWindow.AddRenderer(renderer);

        // Add the actors to the scene
        renderer.AddActor(actor);
        renderer.SetBackground(1, 1, 1); // Background color white

        renderWindow.Render();

        vtkWindowToImageFilter windowToImageFilter = new
vtkWindowToImageFilter();
        windowToImageFilter.SetInput(renderWindow);
        windowToImageFilter.Update();

        vtkPNGWriter writer = new vtkPNGWriter();
        writer.SetFileName("screenshot.png");
        writer.SetInputConnection(windowToImageFilter.GetOutputPort());
        writer.Write();
    }

    private static void loadLibraries() {

        if (!vtkNativeLibrary.LoadAllNativeLibraries()) {
            for (vtkNativeLibrary lib : vtkNativeLibrary.values()) {
                if (!lib.IsLoaded()) {
                    System.out.println(lib.GetLibraryName() + " not
loaded");
                }
            }

            System.out.println("Make sure the search path is correct: ");
            System.out.println(System.getProperty("java.library.path"));
        }

        vtkNativeLibrary.DisableOutputWindow(null);
    }
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120527/332661a1/attachment.htm>


More information about the vtkusers mailing list