Hi. I do not understand why VTK produces aliased images when rendering off screen. I have the following code
#include <vtkCamera.h>
#include <vtkConeSource.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkPolyDataMapper.h>
#include <vtkCamera.h>
#include <vtkOutlineFilter.h>
#include <vtkProperty.h>
#include <vtkPNGWriter.h>
#include <vtkWindowToImageFilter.h>
int main(int, char *[])
{
//Create a cone
vtkNew<vtkConeSource> cone;
cone->SetHeight(300.0);
cone->SetRadius(120.0);
cone->SetResolution(10);
// Create a new outline box for the data using the outline filter. It seems
// All filters require SetInputConnection for receiving data.
// SetInputConnection connects to the output port(pointer) to the data.
vtkNew<vtkOutlineFilter> outlineFilter;
outlineFilter->SetInputConnection(cone->GetOutputPort());
// Set the properties of the outlining box
vtkNew<vtkProperty> outlineProperty;
outlineProperty->SetColor(0,0,0);
outlineProperty->SetLineWidth(0.4);
//Create mappers
vtkNew<vtkPolyDataMapper> coneMapper;
coneMapper->SetInputConnection(cone->GetOutputPort());
vtkNew<vtkPolyDataMapper> outlineMapper;
outlineMapper->SetInputConnection(outlineFilter->GetOutputPort());
// Create actors for outline and cone
vtkNew<vtkActor> outline;
outline->SetMapper(outlineMapper);
outline->SetProperty(outlineProperty);
vtkNew<vtkActor> coneActor;
coneActor->SetMapper(coneMapper);
//Create a renderer and add cone & outline to it.
vtkNew<vtkRenderer> ren1;
ren1->AddActor(coneActor);
ren1->AddActor(outline);
// Set background color of the renderer
vtkNew<vtkNamedColors> colors;
ren1->SetBackground(colors->GetColor3d("White").GetData());
// Create a new visualization window and add the renderer to it.
vtkNew<vtkRenderWindow> renWin;
renWin->SetLineSmoothing(1);
renWin->AddRenderer(ren1);
renWin->SetOffScreenRendering(1);
renWin->SetSize(600,600);
// Create a virtual camera
vtkNew<vtkCamera> orientation;
orientation->SetFocalPoint(0,0,0);
orientation->SetPosition(500,500,500);
ren1->SetActiveCamera(orientation);
// Make the render window interacting
//vtkNew<vtkRenderWindowInteractor> renWinInteractor;
//renWinInteractor->SetRenderWindow(renWin);
//Render and interact
renWin->Render();
// renWinInteractor->Start();
//-------------------------------------------------------------------------------
// Create a PNG image writer
vtkNew<vtkPNGWriter> pngWriter;
// Create a filter from window to image
vtkNew<vtkWindowToImageFilter> window_to_image_filter;
// Let the render window go through the filter
window_to_image_filter->SetInput(renWin);
window_to_image_filter->SetInputBufferTypeToRGB(); // record from the back buffer with RGB colors
window_to_image_filter->ReadFrontBufferOff(); // read from back buffer (read about double buffering to understand font and back buffers)
window_to_image_filter->Update();
// Write to the file
pngWriter->SetFileName("cone_aliased.png");
pngWriter->SetInputConnection(window_to_image_filter->GetOutputPort());
pngWriter->Write();
return EXIT_SUCCESS;
}
The code above without window interactor creates the following aliased image,
If we turn the interactor on and switch off off-screen rendering, I get the following smooth anti-aliased image
I am wondering why this is happening ? Is this because I am not using OSMESA drivers ? In the VTK user guide, it is mentioned that Multisampling depends on OpenGL implementation. Is there anyway I can get rid of aliasing when rendering off screen (using GPU) ? I tried setting renWin->SetMultiSamples(8)
as well, but that did not help.
Thank you and best regards.