Set An Image As Render Background
std::string bgImgPath = "./resource/bg.png";
vtkNew<vtkPNGReader> imgReader;
imgReader->SetFileName(bgImgPath.c_str());
vtkNew<vtkTexture> backgroundTexture;
backgroundTexture->SetInputConnection(imgReader->GetOutputPort());
m_Renderer->GradientBackgroundOff();
backgroundTexture->Update();
m_Renderer->SetBackgroundTexture(backgroundTexture);
m_Renderer->SetTexturedBackground(true);
Save 3D Model Screen Shot With Transparent Background
Don’t set textured background for renderer object.
Then set alpha bits for renderer window object, read RGBA information from back buffer when save the screen shot.
Save the ouput of vtkWindowToImageFilter as a png file.
#ifdef __EMSCRIPTEN__
std::string UGenerateData::ScreenShot(vSP<vtkSDL2OpenGLRenderWindow> renderWin)
#else
std::string UGenerateData::ScreenShot(vSP<vtkRenderWindow> renderWin)
#endif
{
vtkInteractorObserver *style = renderWin->GetInteractor()->GetInteractorStyle();
vtkRenderer *renderer = style->GetCurrentRenderer();
renderer->SetTexturedBackground(false);
renderWin->SetAlphaBitPlanes( 1 );
vtkNew<vtkWindowToImageFilter> windowToImageFilter;
windowToImageFilter->SetInput(renderWin);
windowToImageFilter->SetInputBufferTypeToRGBA();
windowToImageFilter->ReadFrontBufferOff(); // read from the back buffer
windowToImageFilter->SetViewport(0, 0, 1, 1);
windowToImageFilter->Update();
std::string fileName = "screenshot.png";
vtkNew<vtkPNGWriter> writer;
writer->SetFileName( fileName.c_str() );
writer->SetInputConnection(windowToImageFilter->GetOutputPort());
writer->Write();
renderer->SetTexturedBackground(true);
renderWin->SetAlphaBitPlanes( 0 );
renderWin->Render();
return fileName;
}