vtkGenericOpenGLRenderWindow vtkOrientationMarkerWidget crash

I ran the example of the file

vtk-examples/src/Cxx/Widgets/OrientationMarkerWidget.cxx at master · Kitware/vtk-examples · GitHub

and the program ran very well, but I just changed vtkRenderWindow to vtkGenericOpenGLRenderWindow Then it crashed (vtk v9.2.6)(vs2019), the screenshot of the crash is as follows:

Below is my modified code:

#include <vtkActor.h>
#include <vtkAnnotatedCubeActor.h>
#include <vtkCamera.h>
#include <vtkCubeSource.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkOrientationMarkerWidget.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkGenericOpenGLRenderWindow.h>

int main(int argc, char* argv[])
{
  vtkNew<vtkNamedColors> colors;

  // create a rendering window and renderer;
  vtkNew<vtkRenderer> ren;
  //vtkNew<vtkRenderWindow> renWin;
  vtkNew<vtkGenericOpenGLRenderWindow> renWin;
  renWin->AddRenderer(ren);
  renWin->SetWindowName("OrientationMarkerWidget");

  // create a renderwindowinteractor;
  vtkNew<vtkRenderWindowInteractor> iren;
  iren->SetRenderWindow(renWin);

  vtkNew<vtkCubeSource> cube;
  cube->SetXLength(200);
  cube->SetYLength(200);
  cube->SetZLength(200);
  cube->Update();
  vtkNew<vtkPolyDataMapper> cm;
  cm->SetInputConnection(cube->GetOutputPort());
  vtkNew<vtkActor> ca;
  ca->SetMapper(cm);
  ca->GetProperty()->SetColor(colors->GetColor3d("BurlyWood").GetData());
  ca->GetProperty()->EdgeVisibilityOn();
  ca->GetProperty()->SetEdgeColor(colors->GetColor3d("Red").GetData());

  // assign actor to the renderer;
  ren->AddActor(ca);
  ren->SetBackground(colors->GetColor3d("CornflowerBlue").GetData());

  vtkNew<vtkAnnotatedCubeActor> axesActor;
  axesActor->SetXPlusFaceText("L");
  axesActor->SetXMinusFaceText("R");
  axesActor->SetYMinusFaceText("I");
  axesActor->SetYPlusFaceText("S");
  axesActor->SetZMinusFaceText("P");
  axesActor->SetZPlusFaceText("A");
  axesActor->GetTextEdgesProperty()->SetColor(
      colors->GetColor3d("Yellow").GetData());
  axesActor->GetTextEdgesProperty()->SetLineWidth(2);
  axesActor->GetCubeProperty()->SetColor(colors->GetColor3d("Blue").GetData());
  vtkNew<vtkOrientationMarkerWidget> axes;
  axes->SetOrientationMarker(axesActor);
  axes->SetInteractor(iren);
  axes->EnabledOn();
  axes->InteractiveOn();
  ren->ResetCamera();

  // enable user interface interactor;
  iren->Initialize();
  renWin->Render();
  ren->GetActiveCamera()->Azimuth(45);
  ren->GetActiveCamera()->Elevation(30);
  renWin->Render();
  iren->Start();

  return EXIT_SUCCESS;
}

How to solve this problem? (Only vtkGenericOpenGLRenderWindow can be used in my code)

It looks like something is wrong with the OpenGL context. Does this work if you use the vtk Python wheel to do the same calls from a Python script doing the same setup?

I have this problem on both my own computer and my company’s computer

@mwestphal Ideas?

AFAIK the vtkGenericOpenGLRenderWindow cannot be used on its own. You can’t just swap a platform specific render window with one that does not create a OpenGL context.

vtkGenericOpenGLRenderWindow is intended to be used inside Qt or another framework.

The imgui-vtk library seems to only use vtkGenericOpenGLRenderWindow, but does not use Qt or other frameworks.

There’s probably an object factory mechanism going on; are you using CMake to build your example? Or doing autoinit manually if not?

Yes, I use cmake. I just cloned the code vtk-examples and then cmake. I just changed the vtkRenderWindow in the file OrientationMarkerWidget.cxx to vtkGenericOpenGLRenderWindow and it crashed (vtk v9.2.6)(vs2019). I think you should be able to reproduce this problem.

If you look at the “main.cpp” in the imgui-vtk library that you linked, you can see that it uses glfw to create a window with an OpenGL context:

// Create window with graphics context
  GLFWwindow* window = glfwCreateWindow(1280, 720, "Dear ImGui VTKViewer Example", NULL, NULL);

This is what Mathieu was referring to. The vtkGenericOpenGLRenderWindow needs something else to create the window and the OpenGL context. Otherwise, it will crash whenever you try to use it.

Thank you. It should be because vtkGenericOpenGLRenderWindow cannot be used alone.

This class is not intended to be used alone, you can read its documentation:
https://vtk.org/doc/nightly/html/classvtkGenericOpenGLRenderWindow.html#details

vtkGenericOpenGLRenderWindow provides a skeleton for implementing a render window using one’s own OpenGL context and drawable.

You must provide an opengl context.

But if you do not want to create the opengl context yourself, just use the platform specific window instead.

So, vtkRenderWindow creates an OpenGL context?

No, creating an openGL context is very different on each platform, that is why we have platform specific render window.

Just use the platform specific render window.

Can I think so: vtkRenderWindow is common to all platforms, and an OpenGL context will be created internally?

This is what happens when you use the factories. The plaform specific version of the vtkRenderWindow will be created for you.

I understand, thank you