Atempt to render offscreen using vtkEGLRenderWindow

I am trying to render offscreen with EGL but I keep geting segmentation fault.

Thank you,

  std::cerr << "Colours" << std::endl;	              
        // Setup offscreen rendering
    vtkNew<vtkNamedColors> colors;
  std::cerr << "Pre-GraphicsFac Ok" << std::endl;	  
    vtkNew<vtkGraphicsFactory> graphics_factory;
    graphics_factory->SetOffScreenOnlyMode(1);
    graphics_factory->SetUseMesaClasses(1);
  std::cerr << "Pos-GraphicsFac Ok" << std::endl;	  	    

      // Create a sphere
   vtkNew<vtkSphereSource> sphereSource;
    sphereSource->SetThetaResolution(75);
    sphereSource->SetPhiResolution(75);

 //        vtkNew<vtkTexture> texture;
 //     texture->SetInputConnection(sphereSource->GetOutputPort());
  
  std::cerr << "Sphere Created Ok" << std::endl;	  
  // Create a mapper and actor
  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputConnection(sphereSource->GetOutputPort());

  // Apply the texture
  vtkNew<vtkTexture> texture;
  texture->SetInputConnection(sphereSource->GetOutputPort());


  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);
  actor->GetProperty()->SetColor(colors->GetColor3d("White").GetData());

  
  actor->SetMapper(mapper);
  actor->SetTexture(texture);


  // A renderer and render window
  vtkNew<vtkOpenGLRenderer> renderer;

  
  vtkNew<vtkEGLRenderWindow> renderWindow;
  std::cerr << "Number of Devices:  "<< renderWindow->GetNumberOfDevices() << std::endl;	  
  

  renderWindow->MakeCurrent();
  renderWindow->SetOffScreenRendering(1);
  renderWindow->OffScreenRenderingOn();
  renderWindow->SetShowWindow(false);
  renderWindow->WindowInitialize();
  renderWindow->MakeCurrent();


  renderWindow->AddRenderer(renderer);
  renderer->AddActor(actor);
  renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
  std::cerr << "Render Created Ok" << std::endl;
  
  renderWindow->Render();

  std::cerr << "Render Window Created Ok" << std::endl;
  
  // Add the actors to the scene
  
  // Perform offscreen rendering

  std::cerr << "Actor Added Ok" << std::endl;


  std::cerr << "Render Ok" << std::endl;

  vtkNew<vtkWindowToImageFilter> windowToImageFilter;
  windowToImageFilter->SetInput(renderWindow);
  windowToImageFilter->Update();
  
  std::cerr << "Render-Window to image Ok" << std::endl;
  

  vtkNew<vtkPNGWriter> writer;
  writer->SetFileName("/home/3484681/Área de Trabalho/PRISMAMESH/createmesh/screenshot.png");
  writer->SetInputConnection(windowToImageFilter->GetOutputPort());
  writer->Write();

EGL didn’t initialize correctly with eglinfo

This worked:

      #ifdef ENABLE_OFFSCREEN_RENDERING
       
    	
  std::cerr << "Colours" << std::endl;	              
      // Setup offscreen rendering
  vtkNew<vtkNamedColors> colors;
    

  // Create a sphere
  vtkNew<vtkSphereSource> sphereSource;
  sphereSource->SetThetaResolution(100);
  sphereSource->SetPhiResolution(100);

  
  std::cerr << "Sphere Created Ok" << std::endl;	  
  // Create a mapper and actor
  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputConnection(sphereSource->GetOutputPort());



  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);
  actor->GetProperty()->SetColor(colors->GetColor3d("White").GetData());
//  actor->SetTexture(texture);


  // A renderer and render window
  vtkNew<vtkRenderer> renderer;
  renderer->AddActor(actor);
  renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
  
  
  vtkNew<vtkEGLRenderWindow> renderWindow;
  std::cerr << "Number of Devices:  "<< renderWindow->GetNumberOfDevices() << std::endl;	

 for (int i = 0; i < renderWindow->GetNumberOfDevices(); ++i) {
    std::cerr << "Device " << i << ": " << renderWindow->GetDeviceIndex() << std::endl;
}
int numberOfDevices = renderWindow->GetNumberOfDevices();

// Ensure the device index is within a valid range
int desiredDeviceIndex = 3;  // Use the index that corresponds to your Quadro P5000
    
    	vtkNew<vtkRenderWindowInteractor>renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);

    renderWindow->SetDeviceIndex(desiredDeviceIndex);
    renderWindow->OffScreenRenderingOn();
    renderWindow->SetShowWindow(false);
    renderWindow->WindowInitialize();
    renderWindow->AddRenderer(renderer);
    renderWindow->SetSize(640, 480);
    renderWindow->SetWindowName("EarthSource2");
    renderWindow->Render();

  std::cerr << "Render Created Ok" << std::endl;

  
  // Add the actors to the scene
  
  // Perform offscreen rendering

  std::cerr << "Actor Added Ok" << std::endl;


  std::cerr << "Render Ok" << std::endl;

  vtkNew<vtkWindowToImageFilter> windowToImageFilter;
  windowToImageFilter->SetInput(renderWindow);
  windowToImageFilter->Update();
  
  std::cerr << "Render-Window to image Ok" << std::endl;


  vtkNew<vtkPNGWriter> writer;
  writer->SetFileName("/home/3484681/Área de Trabalho/PRISMAMESH/createmesh/screenshot.png");
  writer->SetInputConnection(windowToImageFilter->GetOutputPort());
  writer->Write();
  
  std::cerr << "Write Ok" << std::endl;
  
  #endif

screenshot

1 Like