-
-
Save certik/5687727 to your computer and use it in GitHub Desktop.
#! /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() |
Does this support running on a headerless remote server?
@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.
@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 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 yourtest_offscreen.cpp
, it works.
thank you!
A small tip in case anyone is wondering: If you want a transparent (alpha 0) background:
renderWindow.SetAlphaBitPlanes(1)
andwindowToImageFilter.SetInputBufferTypeToRGBA()
.