VTK FXAA透明度

时间:2019-03-26 14:16:42

标签: c++ vtk

我正在VTK 8.1中将VTK场景渲染为具有透明背景的png文件。

这很好,但是一旦我通过FXAA启用抗锯齿功能,透明背景就会变成纯黑色背景。

是否可以使用FXAA而不会失去透明度?

一个最小的工作示例:

#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkWindowToImageFilter.h>
#include <vtkPNGWriter.h>
#include <vtkCamera.h>

int main()
{
  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);

  vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
  renderer->AddViewProp(actor);

  vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->SetOffScreenRendering(true);
  renderWindow->SetSize(480, 480);
  renderWindow->SetAlphaBitPlanes(1);
  renderWindow->AddRenderer(renderer);

  // Uncommenting this line removes the transparent background :(
  // renderer->SetUseFXAA(1);

  vtkSmartPointer<vtkCamera> camera = vtkSmartPointer<vtkCamera>::New();
  camera->SetPosition(0, 0, 5);
  renderer->SetActiveCamera(camera);

  vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter = vtkSmartPointer<vtkWindowToImageFilter>::New();
  windowToImageFilter->SetInputBufferTypeToRGBA();
  windowToImageFilter->SetInput(renderWindow);
  windowToImageFilter->Update();

  vtkSmartPointer<vtkPNGWriter> writer = vtkSmartPointer<vtkPNGWriter>::New();
  writer->SetCompressionLevel(50);
  writer->SetFileName("example.png");
  writer->SetInputConnection(windowToImageFilter->GetOutputPort());
  writer->Write();
}

禁用FXAA的渲染如下:

FXAA disabled

启用FXAA的方式如下:

FXAA enabled

0 个答案:

没有答案