xjjgjmeng
(xjjgjmeng)
November 8, 2023, 1:59pm
1
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)
ben.boeckel
(Ben Boeckel (Kitware))
November 17, 2023, 4:05am
2
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?
xjjgjmeng
(xjjgjmeng)
November 17, 2023, 11:35am
3
I have this problem on both my own computer and my company’s computer
mwestphal
(Mathieu Westphal (Kitware))
November 17, 2023, 1:48pm
5
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.
xjjgjmeng
(xjjgjmeng)
November 18, 2023, 1:39pm
6
The imgui-vtk library seems to only use vtkGenericOpenGLRenderWindow, but does not use Qt or other frameworks.
ben.boeckel
(Ben Boeckel (Kitware))
November 18, 2023, 1:42pm
7
There’s probably an object factory mechanism going on; are you using CMake to build your example? Or doing autoinit manually if not?
xjjgjmeng
(xjjgjmeng)
November 18, 2023, 2:28pm
8
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.
dgobbi
(David Gobbi)
November 20, 2023, 3:13am
9
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.
xjjgjmeng
(xjjgjmeng)
November 20, 2023, 5:07am
10
Thank you. It should be because vtkGenericOpenGLRenderWindow cannot be used alone.
mwestphal
(Mathieu Westphal (Kitware))
November 20, 2023, 7:06am
11
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.
xjjgjmeng
(xjjgjmeng)
November 20, 2023, 8:11am
12
So, vtkRenderWindow creates an OpenGL context?
mwestphal
(Mathieu Westphal (Kitware))
November 20, 2023, 8:59am
13
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.
xjjgjmeng
(xjjgjmeng)
November 20, 2023, 9:22am
14
xjjgjmeng:
vtkRenderWindow
Can I think so: vtkRenderWindow is common to all platforms, and an OpenGL context will be created internally?
mwestphal
(Mathieu Westphal (Kitware))
November 20, 2023, 9:28am
15
This is what happens when you use the factories. The plaform specific version of the vtkRenderWindow will be created for you.