Skip to content

Instantly share code, notes, and snippets.

@certik
Last active February 23, 2023 22:50
Show Gist options
  • Save certik/5687727 to your computer and use it in GitHub Desktop.
Save certik/5687727 to your computer and use it in GitHub Desktop.
Test offscreen rendering with VTK 6
#! /bin/bash
set -e
g++ -o test_offscreen.o -c test_offscreen.cpp -I$PYTHONHPC/include/vtk-6.0
g++ -o test_offscreen test_offscreen.o $PYTHONHPC/lib/libvtk*.so
LD_LIBRARY_PATH=$PYTHONHPC/lib/ ./test_offscreen
// These 2 lines are needed due to:
// http://www.vtk.org/Wiki/VTK/VTK_6_Migration/Factories_now_require_defines
#define vtkRenderingCore_AUTOINIT 4(vtkInteractionStyle,vtkRenderingFreeType,vtkRenderingFreeTypeOpenGL,vtkRenderingOpenGL)
#define vtkRenderingVolume_AUTOINIT 1(vtkRenderingVolumeOpenGL)
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkPolyData.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkWindowToImageFilter.h>
#include <vtkPNGWriter.h>
#include <vtkGraphicsFactory.h>
int main ()
{
//sphere 1
vtkSmartPointer<vtkSphereSource> sphereSource = vtkSmartPointer<vtkSphereSource>::New();
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(sphereSource->GetOutputPort());
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
// a renderer and render window
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
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();
vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter =
vtkSmartPointer<vtkWindowToImageFilter>::New();
windowToImageFilter->SetInput(renderWindow);
windowToImageFilter->Update();
vtkSmartPointer<vtkPNGWriter> writer = vtkSmartPointer<vtkPNGWriter>::New();
writer->SetFileName("sphere.png");
writer->SetInputConnection(windowToImageFilter->GetOutputPort());
writer->Write();
return 0;
}
#! ./local/bin/python
from vtk import (vtkSphereSource, vtkPolyDataMapper, vtkActor, vtkRenderer,
vtkRenderWindow, vtkWindowToImageFilter, vtkPNGWriter)
sphereSource = vtkSphereSource()
mapper = vtkPolyDataMapper()
mapper.SetInputConnection(sphereSource.GetOutputPort())
actor = vtkActor()
actor.SetMapper(mapper)
renderer = vtkRenderer()
renderWindow = vtkRenderWindow()
renderWindow.SetOffScreenRendering(1)
renderWindow.AddRenderer(renderer)
renderer.AddActor(actor)
renderer.SetBackground(1, 1, 1)
renderWindow.Render()
windowToImageFilter = vtkWindowToImageFilter()
windowToImageFilter.SetInput(renderWindow)
windowToImageFilter.Update()
writer = vtkPNGWriter()
writer.SetFileName("sphere.png")
writer.SetInputConnection(windowToImageFilter.GetOutputPort())
writer.Write()
@estan
Copy link

estan commented Jun 21, 2016

A small tip in case anyone is wondering: If you want a transparent (alpha 0) background: renderWindow.SetAlphaBitPlanes(1) and windowToImageFilter.SetInputBufferTypeToRGBA().

@Guptajakala
Copy link

Does this support running on a headerless remote server?

@certik
Copy link
Author

certik commented Dec 14, 2019

@Guptajakala I think it did, but this is a very old set of scripts, over 6 years old at this point, so it might not compile properly anymore.

@bingliang-zh
Copy link

@Guptajakala I think it did, but this is a very old set of scripts, over 6 years old at this point, so it might not compile properly anymore.

I just compiled VTK-9.0.1 with OSMESA enabled.

Following the guide and replace the OffScreenRendering.cxx file content with your test_offscreen.cpp, it works.

@Guptajakala
Copy link

@Guptajakala I think it did, but this is a very old set of scripts, over 6 years old at this point, so it might not compile properly anymore.

I just compiled VTK-9.0.1 with OSMESA enabled.

Following the guide and replace the OffScreenRendering.cxx file content with your test_offscreen.cpp, it works.

thank you!

@certik
Copy link
Author

certik commented Jul 1, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment