VTK window doesn't show up?!...

In a very simple case, VTK window doesn’t show up?!.. Any clue why?

Here the sample:

>> cat vtkTest.cpp 
// STL.
#include <iostream> // cout
#include <stdexcept> // exception

// VTK.
#include <vtkNew.h>
#include <vtkPolyData.h>
#include <vtkPoints.h>
#include <vtkPointData.h>
#include <vtkUnsignedCharArray.h>
#include <vtkVertexGlyphFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballActor.h>

int run() {
  // Create data to render.
  vtkNew<vtkPolyData> polyData;

  // Create points.
  vtkNew<vtkPoints> points;
  points->InsertNextPoint(0.0, 0.0, 0.0);
  points->InsertNextPoint(1.0, 0.0, 0.0);
  points->InsertNextPoint(0.0, 1.0, 0.0);
  points->InsertNextPoint(0.0, 0.0, 1.0);
  polyData->SetPoints(points);

  // Setup colors.
  vtkNew<vtkUnsignedCharArray> colors;
  colors->SetNumberOfComponents(3);
  colors->SetName("Colors");
  unsigned char black[3] = {0, 0, 0};
  unsigned char red[3] = {255, 0, 0};
  unsigned char green[3] = {0, 255, 0};
  unsigned char blue[3] = {0, 0, 255};
  colors->InsertNextTypedTuple(black);
  colors->InsertNextTypedTuple(red);
  colors->InsertNextTypedTuple(green);
  colors->InsertNextTypedTuple(blue);
  vtkPointData * pointData = polyData->GetPointData();
  if (pointData) {
    pointData->SetScalars(colors);
  }

  // Setup filter.
  vtkNew<vtkVertexGlyphFilter> vertexFilter;
  vertexFilter->SetInputData(polyData);

  // Setup mapper.
  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputConnection(vertexFilter->GetOutputPort());

  // Setup actor.
  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);
  vtkProperty * prop = actor->GetProperty();
  if (prop) {
    prop->SetPointSize(10);
  }

  // Setup renderer.
  vtkNew<vtkRenderer> renderer;
  renderer->AddActor(actor);
  renderer->SetBackground(1., 1., 1.);

  // Setup window.
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("vtkTest");

  // Setup interactor.
  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  vtkNew<vtkInteractorStyleTrackballActor> style;
  renderWindowInteractor->SetInteractorStyle(style);
  renderWindowInteractor->SetRenderWindow(renderWindow);
  renderWindowInteractor->Initialize();

  // Render scene.
  renderWindow->Render();
  renderWindowInteractor->Start();

  return 0;
}

int main() {
  int rc = 1;

  try {
    rc = run();
  }
  catch(std::exception & e) {
    std::cerr << "Error - " << e.what() << std::endl;
    rc = 1;
  }
  std::cout << "Main thread: exiting..." << std::endl;

  return rc;
}

>> cat CMakeLists.txt 
cmake_minimum_required (VERSION 3.20)

# Define project
project(vtkTest C CXX)

# Get VTK targets to compile / link with.
find_package(VTK
             COMPONENTS
               CommonCore
               CommonDataModel
               FiltersGeneral
               InteractionStyle
               RenderingCore
             REQUIRED)
message(STATUS "VTK VERSION=${VTK_MAJOR_VERSION}.${VTK_MINOR_VERSION}")

# Define binary
add_executable(vtkTest vtkTest.cpp)
vtk_module_autoinit(TARGETS vtkTest MODULES "${VTK_LIBRARIES}")
target_include_directories(vtkTest PRIVATE "${VTK_INCLUDE_DIRS}")
target_link_libraries(vtkTest PRIVATE "${VTK_LIBRARIES}")


Now, build seems OK:

>> cd build/

>> cmake ..
-- VTK VERSION=9.1
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/vtkTest/build

>> make VERBOSE=1
[ 50%] Building CXX object CMakeFiles/vtkTest.dir/vtkTest.cpp.o
/usr/bin/c++ -Dkiss_fft_scalar=double -DvtkRenderingCore_AUTOINIT_INCLUDE=\"/tmp/vtkTest/build/CMakeFiles/vtkModuleAutoInit_9b5895f873bcba09752b0c704bf968d6.h\" -I/tmp/vtkTest -isystem /usr/include/vtk-9.1  -MD -MT CMakeFiles/vtkTest.dir/vtkTest.cpp.o -MF CMakeFiles/vtkTest.dir/vtkTest.cpp.o.d -o CMakeFiles/vtkTest.dir/vtkTest.cpp.o -c /tmp/vtkTest/vtkTest.cpp
[100%] Linking CXX executable vtkTest
/usr/bin/cmake -E cmake_link_script CMakeFiles/vtkTest.dir/link.txt --verbose=1
/usr/bin/c++ CMakeFiles/vtkTest.dir/vtkTest.cpp.o -o vtkTest  /usr/lib/x86_64-linux-gnu/libvtkInteractionStyle-9.1.so.9.1.0 /usr/lib/x86_64-linux-gnu/libvtkRenderingCore-9.1.so.9.1.0 /usr/lib/x86_64-linux-gnu/libvtkFiltersGeneral-9.1.so.9.1.0 /usr/lib/x86_64-linux-gnu/libvtkFiltersCore-9.1.so.9.1.0 /usr/lib/x86_64-linux-gnu/libvtkCommonExecutionModel-9.1.so.9.1.0 /usr/lib/x86_64-linux-gnu/libvtkCommonDataModel-9.1.so.9.1.0 /usr/lib/x86_64-linux-gnu/libvtkCommonTransforms-9.1.so.9.1.0 /usr/lib/x86_64-linux-gnu/libvtkCommonMisc-9.1.so.9.1.0 /usr/lib/x86_64-linux-gnu/libvtkCommonMath-9.1.so.9.1.0 /usr/lib/x86_64-linux-gnu/libvtkkissfft-9.1.so.9.1.0 /usr/lib/x86_64-linux-gnu/libvtkCommonCore-9.1.so.9.1.0 /usr/lib/x86_64-linux-gnu/libtbb.so.12.8 /usr/lib/x86_64-linux-gnu/libvtksys-9.1.so.9.1.0 -ldl 


… But, the application doesn’t open any VTK window and returns?!..

>> ./vtkTest 
Main thread: exiting...

This is happening on debian/testing with apt-install vtk:


>> cmake --debug-find ..
...
  find_package considered the following locations for VTK's Config module:
  The file was found at
    /usr/lib/x86_64-linux-gnu/cmake/vtk-9.1/vtk-config.cmake


>> apt-file search /usr/lib/x86_64-linux-gnu/cmake/vtk-9.1/vtk-config.cmake
libvtk9-dev: /usr/lib/x86_64-linux-gnu/cmake/vtk-9.1/vtk-config.cmake



>> dpkg -l libvtk9-dev
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name           Version                   Architecture Description
+++-==============-=========================-============-=================================
ii  libvtk9-dev    9.1.0+really9.1.0+dfsg2-5 amd64        VTK header files


Any clue on why the window doesn’t pop up?!..

From the cmake output, it looks like you aren’t linking all of the necessary libraries.

In CMakeLists.txt, add RenderingOpenGL2 and RenderingFreeType to COMPONENTS.

Thanks, that works now!.. But if I missed component how could this has succeeded to link?!..

Using ./Utilities/Maintenance/FindNeededModules.py I just discovered that RenderingOpenGL2 and RenderingFreeType are reported as “suggestions”: that’s why I didn’t add them (as link was OK). Why did it compile/link without these component anyway?

What is the logic to know which component to use for compile / link? How to know which “suggested” component are indeed mandatory: how to choose from the list of suggested component?!

Any explanation on these questions would be helpful

Many rendering classes in VTK are factory classes. To make a long story short, this allows some classes to be substituted by others according to what libraries are linked. The actual substitution occurs at runtime, but it depends on which libraries are loaded by the process.

So if you link to RenderingOpenGL2, then any call to vtkRenderer::New() will actually return a vtkOpenGLRenderer object. Because of VTK’s factory mechanism that’s built into the New() method of many VTK classes.

The default vtkRenderWindow and vtkRenderer don’t actually contain any code for rendering onto the screen. They must be overridden or nothing is rendered.

Which of the many “suggested” components are needed will depend on what classes your program uses. There aren’t any tools in VTK to provide a definite answer. But if all features of your program are working as expected, you know that you’re linking all the necessary libraries.

I’ve done it: compile / link is OK, running vtkTest trigger a window that shows up… But I realize I can not use the mouse to navigate into the viewer / scene?!.. The scene is frozen.

How to fix this?

Add RenderingUI to COMPONENTS.
It’s a new library for VTK 9, it didn’t exist in VTK 8 and I had forgotten about it.

I’ve just tried: same thing, window pops-up, no mouse control?!..

>> ./vtkTest 
X Error of failed request:  BadWindow (invalid Window parameter)
  Major opcode of failed request:  38 (X_QueryPointer)
  Resource id in failed request:  0x3000002
  Serial number of failed request:  519
  Current serial number in output stream:  519

I can confirm this behaviour in Windows with the VTK master.

If you comment out renderWindowInteractor->SetInteractorStyle(style); you can interact with the default windows interactor. However "vtkInteractorStyleTrackballActor’ doesn’t seem to work.

I can replicate this behaviour using ColoredPoints also with VertexGlyphFilter.

Yet it works hereTrackballActor

I suspect it is related to something in vtkVertexGlyphFilter.

Does module order matter? If yes, how to know the correct order?

It shouldn’t matter.

Before reading your reply, I was just testing this!.. And get the same result as you describe: using vtkInteractorStyleTrackballActor creates the problem even if, as far as I have checked in VTK sources / modules, this seems to me to be implemented in the InteractionStyle module which is part of required component.

I need trackball!..

My understanding is that from v9.1 trackball is broken so maybe I’ll use a previous version.

Is there any tests/fix I could try to fix this? Is it worth I open an issue in the vtk gitlab?

Yet it works hereTrackballActor
I suspect it is related to something in vtkVertexGlyphFilter

I see TrackballActor works but doesn’t use vtkVertexGlyphFilter

I see TrackballActor works but doesn’t use vtkVertexGlyphFilter

Using vtkInteractorStyleTrackballActor but removing vtkVertexGlyphFilter (setting input data directly to the mapper), I get an empty window! :wink:

Gitlab doesn’t allow me to create an issue?!.. I get “The form contains the following error: Your issue has been recognized as spam. Please, change the content to proceed.” and no way to know why!

If somebody can open an issue would be good I guess

I’ll open an issue for you.

@fghoussen An issue for this has been opened here issue 9023.

@amaclean: Thanks!

Is there another filter able to render points I could use to replace vtkVertexGlyphFilter?

I am not sure what you want to do but you could try vtkGlyph3DMapper. There are about 30 examples in the VTKExamples.