简体   繁体   中英

VTK 7 & Qt5.6 OpenGL Rendering conflict on OSX 10.11

After upgrading my Qt 5.3.2 dependency to Qt 5.6, VTK rendering doesn't work anymore.

Here's the 5.3.2 code :

#include <QtWidgets>
#include <QGLWidget>
#include <vtkActor.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <QVTKWidget.h>
#include <vtkRenderWindow.h>

int main( int argc, char** argv ){

    // create QApplication
    QApplication l_app( argc, argv );

    // Create widget and layout
    QWidget* l_main_widget = new QWidget;
    QHBoxLayout* l_main_layout = new QHBoxLayout;
    l_main_widget->setLayout( l_main_layout );

    // create a QVtkWidget with a simple sphere inside
    QVTKWidget* l_view = new QVTKWidget;
    l_view->setFixedSize( 300, 300 );
    vtkSmartPointer<vtkSphereSource> l_sphere_source = vtkSmartPointer<vtkSphereSource>::New();
    vtkSmartPointer<vtkPolyDataMapper> l_sphere_mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    l_sphere_mapper->SetInputConnection( l_sphere_source->GetOutputPort() );
    vtkSmartPointer<vtkActor> l_sphere_actor = vtkSmartPointer<vtkActor>::New();
    l_sphere_actor->SetMapper( l_sphere_mapper );
    vtkSmartPointer<vtkRenderer> l_renderer = vtkSmartPointer<vtkRenderer>::New();
    l_renderer->AddActor( l_sphere_actor );
    l_view->GetRenderWindow()->AddRenderer( l_renderer );
    l_main_layout->addWidget( l_view );

    // create a QWebEngineView
    QGLWidget* l_gl_widget = new QGLWidget;
    l_main_layout->addWidget(l_gl_widget);
    l_main_widget->show();

    return l_app.exec();
}

I simply updated the QGLWidget to QOpenGLWidget for Qt 5.6 :

QOpenGLWidget* l_gl_widget = new QOpenGLWidget;

And now, my QVTKWidget is simply rendering a black image (see end of post). I already stepped on this thread about making QVTKWidget inherits QOpenGLWidget , but I can't afford to do so, because of QOpenGLWidget & compositing

I've tried to subclass QVTKWidget & some of its methods (PaintEvent & co.) to manually manage its rendering using stuff like vtkGenericOpenGLRenderWindow, but either I'm facing weird problems that I'm still trying to solve, or I'm not skilled enough with these.

Especially, when dealing with QOpenGLContext & QSurfaceFormat, it seems I cannot get them to work with OpenGL 3.2 (which is required by VTK 7 OpenGL2 backend). They seems to be stuck with 2.1 version.

I should mention that I don't have this problem on Windows.

If you've got some clues or help to offer, I would gladly accept.

Last remark: This problem appeared after replacing QWebKit by QWebEngine, which is used in my app, but never displayed at the same time as a VTK view. Yet, as soon as I display a QWebEngineView, all the VTK views of my app stop working.

Thanks for your time :)

VTK渲染问题

Manual page of QOpenGLWidget says...

"Note: Calling QSurfaceFormat::setDefaultFormat() before constructing the QApplication instance is mandatory on some platforms (for example, macOS) when an OpenGL core profile context is requested. This is to ensure that resource sharing between contexts stays functional as all internal contexts are created using the correct version and profile."

Can this be a source of your problem?

And I realized one difference between QGLWidget and QOpenGLWidget is that QGL~ uses the native window in the widget while QOpenGLWidget is not. QOpenGLWidget always uses offscreen rendering. I believe it will enhancements of renndering performance. One shortcoming for VTK side is that the program will crash if you try to draw something (any vtk widget) on QOpenGLWidget (I mean QVTKWidget? inheriting QOpenGLWidget) before it's initialization.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM