From dd4ee5d12ebbf505279d4248f2530ce5fc87c3b4 Mon Sep 17 00:00:00 2001 From: Sankhesh Jhaveri Date: Tue, 27 Jun 2017 23:53:39 -0400 Subject: [PATCH 1/3] Integrate VTK 8 OpenGL2 backend and use QVTKOpenGLWidget --- distro/superbuild/cmake/externals.cmake | 4 +- src/app/CMakeLists.txt | 11 +++++ src/app/ddQVTKWidgetView.cpp | 59 +++++++++++++++---------- src/app/ddQVTKWidgetView.h | 9 +++- src/app/drakeVisualizerApp.cpp | 15 ++++++- src/app/wrapped_methods.txt | 1 - 6 files changed, 70 insertions(+), 29 deletions(-) diff --git a/distro/superbuild/cmake/externals.cmake b/distro/superbuild/cmake/externals.cmake index f45688007..b2bffb711 100644 --- a/distro/superbuild/cmake/externals.cmake +++ b/distro/superbuild/cmake/externals.cmake @@ -367,7 +367,7 @@ else() ExternalProject_Add(vtk GIT_REPOSITORY git://vtk.org/VTK.git - GIT_TAG v7.1.1 + GIT_TAG v8.0.0 CMAKE_CACHE_ARGS ${default_cmake_args} ${python_args} @@ -375,7 +375,7 @@ else() -DBUILD_SHARED_LIBS:BOOL=ON -DBUILD_TESTING:BOOL=OFF -DBUILD_EXAMPLES:BOOL=OFF - -DVTK_RENDERING_BACKEND:STRING=OpenGL + -DVTK_RENDERING_BACKEND:STRING=OpenGL2 -DVTK_QT_VERSION:STRING=${DD_QT_VERSION} -DVTK_PYTHON_VERSION=2 -DModule_vtkGUISupportQt:BOOL=ON diff --git a/src/app/CMakeLists.txt b/src/app/CMakeLists.txt index 62a831675..d32c55b8f 100644 --- a/src/app/CMakeLists.txt +++ b/src/app/CMakeLists.txt @@ -34,8 +34,19 @@ include_directories(${PYTHON_INCLUDE_DIRS}) set(decorator_file ${CMAKE_CURRENT_BINARY_DIR}/ddPythonQtDecorators.h) +if(DD_QT_VERSION EQUAL 4) + file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/wrapped_methods_qvtk.txt + "QVTKWidget* ddQVTKWidgetView::vtkWidget() const;" + ) +else() + file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/wrapped_methods_qvtk.txt + "QVTKOpenGLWidget* ddQVTKWidgetView::vtkWidget() const;" + ) +endif() + set(wrap_files wrapped_methods.txt + ${CMAKE_CURRENT_BINARY_DIR}/wrapped_methods_qvtk.txt ) qt_wrap_cpp(moc_srcs diff --git a/src/app/ddQVTKWidgetView.cpp b/src/app/ddQVTKWidgetView.cpp index c1cc69225..345a6c4e1 100644 --- a/src/app/ddQVTKWidgetView.cpp +++ b/src/app/ddQVTKWidgetView.cpp @@ -1,33 +1,39 @@ #include "ddQVTKWidgetView.h" #include "ddFPSCounter.h" -#include "vtkTDxInteractorStyleCallback.h" #include "vtkSimpleActorInteractor.h" +#include "vtkTDxInteractorStyleCallback.h" +#include +#include #include -#include -#include -#include +#include +#include +#include +#include #include +#include +#include #include -#include #include +#include #include -#include -#include -#include #include -#include -#include +#include +#include #include -#include -#include -#include +#include +#include #include -#include -#include #include +#include + +#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0) + #include +#else + #include +#endif //----------------------------------------------------------------------------- class vtkCustomRubberBandStyle : public vtkInteractorStyleRubberBand3D @@ -43,7 +49,7 @@ class vtkCustomRubberBandStyle : public vtkInteractorStyleRubberBand3D { this->Interaction = ZOOMING; this->FindPokedRenderer( - this->Interactor->GetEventPosition()[0], + this->Interactor->GetEventPosition()[0], this->Interactor->GetEventPosition()[1]); this->InvokeEvent(vtkCommand::StartInteractionEvent); } @@ -69,7 +75,7 @@ class ddQVTKWidgetView::ddInternal this->RenderTimer.setInterval(1000/timerFramesPerSeconds); } - QVTKWidget* VTKWidget; + QVTKOpenGLWidget* VTKWidget; vtkSmartPointer Renderer; vtkSmartPointer RendererBase; @@ -98,25 +104,32 @@ ddQVTKWidgetView::ddQVTKWidgetView(QWidget* parent) : ddViewBase(parent) QVBoxLayout* layout = new QVBoxLayout(this); layout->setMargin(0); - this->Internal->VTKWidget = new QVTKWidget; + this->Internal->VTKWidget = new QVTKOpenGLWidget; layout->addWidget(this->Internal->VTKWidget); +#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0) + this->Internal->RenderWindow = + vtkSmartPointer::New(); +#else this->Internal->VTKWidget->SetUseTDx(true); - this->Internal->RenderWindow = vtkSmartPointer::New(); + this->Internal->VTKWidget->SetRenderWindow(this->Internal->RenderWindow); +#endif + this->Internal->VTKWidget->SetRenderWindow(this->Internal->RenderWindow); this->Internal->RenderWindow->SetMultiSamples(8); this->Internal->RenderWindow->StereoCapableWindowOn(); this->Internal->RenderWindow->SetStereoTypeToRedBlue(); this->Internal->RenderWindow->StereoRenderOff(); this->Internal->RenderWindow->StereoUpdate(); - this->Internal->VTKWidget->SetRenderWindow(this->Internal->RenderWindow); this->Internal->LightKit = vtkSmartPointer::New(); this->Internal->LightKit->SetKeyLightWarmth(0.5); this->Internal->LightKit->SetFillLightWarmth(0.5); - this->Internal->TDxInteractor = vtkSmartPointer::New(); - vtkInteractorStyle::SafeDownCast(this->Internal->RenderWindow->GetInteractor()->GetInteractorStyle())->SetTDxStyle(this->Internal->TDxInteractor); + this->Internal->TDxInteractor = + vtkSmartPointer::New(); + vtkInteractorStyle::SafeDownCast(this->Internal->RenderWindow->GetInteractor( + )->GetInteractorStyle())->SetTDxStyle(this->Internal->TDxInteractor); //this->Internal->RenderWindow->SetNumberOfLayers(2); @@ -192,7 +205,7 @@ vtkLightKit* ddQVTKWidgetView::lightKit() const } //----------------------------------------------------------------------------- -QVTKWidget* ddQVTKWidgetView::vtkWidget() const +QVTKOpenGLWidget* ddQVTKWidgetView::vtkWidget() const { return this->Internal->VTKWidget; } diff --git a/src/app/ddQVTKWidgetView.h b/src/app/ddQVTKWidgetView.h index d3da6642c..38fceadbf 100644 --- a/src/app/ddQVTKWidgetView.h +++ b/src/app/ddQVTKWidgetView.h @@ -10,7 +10,12 @@ class vtkOrientationMarkerWidget; class vtkRenderer; class vtkRenderWindow; class vtkLightKit; -class QVTKWidget; +#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0) + class QVTKOpenGLWidget; +#else + class QVTKWidget; + using QVTKOpenGLWidget = QVTKWidget; +#endif class DD_APP_EXPORT ddQVTKWidgetView : public ddViewBase { @@ -29,7 +34,7 @@ class DD_APP_EXPORT ddQVTKWidgetView : public ddViewBase QList lastTDxMotion() const; - QVTKWidget* vtkWidget() const; + QVTKOpenGLWidget* vtkWidget() const; vtkOrientationMarkerWidget* orientationMarkerWidget() const; void installImageInteractor(); diff --git a/src/app/drakeVisualizerApp.cpp b/src/app/drakeVisualizerApp.cpp index ede7d8074..31d3888f1 100644 --- a/src/app/drakeVisualizerApp.cpp +++ b/src/app/drakeVisualizerApp.cpp @@ -1,9 +1,22 @@ -#include +// Qt includes #include + +// director includes +#include #include "ddPythonManager.h" +// VTK includes +#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0) + #include + #include +#endif + int main(int argc, char **argv) { +#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0) + // Set the default surface format for the OpenGL view + QSurfaceFormat::setDefaultFormat(QVTKOpenGLWidget::defaultFormat()); +#endif QApplication app(argc, argv); ddPythonManager* pythonManager = new ddPythonManager; pythonManager->setSysArgv(QApplication::instance()->arguments()); diff --git a/src/app/wrapped_methods.txt b/src/app/wrapped_methods.txt index 5a12057bd..925ce4bfd 100644 --- a/src/app/wrapped_methods.txt +++ b/src/app/wrapped_methods.txt @@ -9,7 +9,6 @@ vtkRenderWindow* ddQVTKWidgetView::renderWindow() const; vtkRenderer* ddQVTKWidgetView::renderer() const; vtkRenderer* ddQVTKWidgetView::backgroundRenderer() const; vtkLightKit* ddQVTKWidgetView::lightKit() const; -QVTKWidget* ddQVTKWidgetView::vtkWidget() const; vtkOrientationMarkerWidget* ddQVTKWidgetView::orientationMarkerWidget() const; QList ddQVTKWidgetView::lastTDxMotion() const; void ddQVTKWidgetView::installImageInteractor(); From 950ae194af362b7e3e7d7970f9dbdc8ab7af67e5 Mon Sep 17 00:00:00 2001 From: Sankhesh Jhaveri Date: Wed, 28 Jun 2017 15:23:26 -0400 Subject: [PATCH 2/3] Moved the QVTKWidget configuration code to a CMake configured header Including QVTKOpenGLWidgetConfigure.h before using QVTKOpenGLWidget would automatically decide whether to use the old QVTKWidget or the QVTKOpenGLWidget. It typedefs QVTKWidget as QVTKOpenGLWidget if the former is used so that the application code can keep using QVTKOpenGLWidget. --- src/app/CMakeLists.txt | 11 ++++++++++- src/app/ddQVTKOpenGLWidgetConfigure.h.in | 22 ++++++++++++++++++++++ src/app/ddQVTKWidgetView.cpp | 6 ------ src/app/ddQVTKWidgetView.h | 7 +------ 4 files changed, 33 insertions(+), 13 deletions(-) create mode 100644 src/app/ddQVTKOpenGLWidgetConfigure.h.in diff --git a/src/app/CMakeLists.txt b/src/app/CMakeLists.txt index d32c55b8f..e0f95a097 100644 --- a/src/app/CMakeLists.txt +++ b/src/app/CMakeLists.txt @@ -34,7 +34,16 @@ include_directories(${PYTHON_INCLUDE_DIRS}) set(decorator_file ${CMAKE_CURRENT_BINARY_DIR}/ddPythonQtDecorators.h) -if(DD_QT_VERSION EQUAL 4) +# Configure whether to use the QVTKWidget or the QVTKOpenGLWidget +set(USE_QVTKWIDGET TRUE) +if((${Qt5Core_VERSION} VERSION_GREATER "5.4.0") AND + (${VTK_VERSION} VERSION_GREATER "7.1.0")) + set(USE_QVTKWIDGET FALSE) +endif() +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/ddQVTKOpenGLWidgetConfigure.h.in + ${CMAKE_CURRENT_BINARY_DIR}/ddQVTKOpenGLWidgetConfigure.h) + +if(USE_QVTKWIDGET) file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/wrapped_methods_qvtk.txt "QVTKWidget* ddQVTKWidgetView::vtkWidget() const;" ) diff --git a/src/app/ddQVTKOpenGLWidgetConfigure.h.in b/src/app/ddQVTKOpenGLWidgetConfigure.h.in new file mode 100644 index 000000000..a49870c52 --- /dev/null +++ b/src/app/ddQVTKOpenGLWidgetConfigure.h.in @@ -0,0 +1,22 @@ +#ifndef __ddQVTKOpenGLWidgetConfigure_h +#define __ddQVTKOpenGLWidgetConfigure_h + +/* + * Including QVTKOpenGLWidgetConfigure.h before using QVTKOpenGLWidget + * would automatically decide whether to use the old QVTKWidget or the + * QVTKOpenGLWidget based on the Qt and VTK versions used. + * It typedefs QVTKWidget as QVTKOpenGLWidget if the + * former is used so that the application code can keep using + * QVTKOpenGLWidget. + */ + +#cmakedefine01 USE_QVTKWIDGET + +#if USE_QVTKWIDGET + #include + using QVTKOpenGLWidget = QVTKWidget; +#else + #include +#endif + +#endif diff --git a/src/app/ddQVTKWidgetView.cpp b/src/app/ddQVTKWidgetView.cpp index 345a6c4e1..2fbe41528 100644 --- a/src/app/ddQVTKWidgetView.cpp +++ b/src/app/ddQVTKWidgetView.cpp @@ -29,12 +29,6 @@ #include #include -#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0) - #include -#else - #include -#endif - //----------------------------------------------------------------------------- class vtkCustomRubberBandStyle : public vtkInteractorStyleRubberBand3D { diff --git a/src/app/ddQVTKWidgetView.h b/src/app/ddQVTKWidgetView.h index 38fceadbf..c5d18264e 100644 --- a/src/app/ddQVTKWidgetView.h +++ b/src/app/ddQVTKWidgetView.h @@ -3,6 +3,7 @@ #include "ddViewBase.h" #include "ddAppConfigure.h" +#include "ddQVTKOpenGLWidgetConfigure.h" class vtkCamera; @@ -10,12 +11,6 @@ class vtkOrientationMarkerWidget; class vtkRenderer; class vtkRenderWindow; class vtkLightKit; -#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0) - class QVTKOpenGLWidget; -#else - class QVTKWidget; - using QVTKOpenGLWidget = QVTKWidget; -#endif class DD_APP_EXPORT ddQVTKWidgetView : public ddViewBase { From 39640642dfab967f053e8913f27ab725c0d65856 Mon Sep 17 00:00:00 2001 From: Sankhesh Jhaveri Date: Wed, 28 Jun 2017 16:31:03 -0400 Subject: [PATCH 3/3] Added a re-usable QVTKOpenGLInit class This class can be used to initialize the QSurfaceFormat for a QApplication that is used by OpenGL widgets within the application, specifically QVTKOpenGLWidget. --- src/app/CMakeLists.txt | 6 ++++-- src/app/QVTKOpenGLInit.cpp | 25 ++++++++++++++++++++++++ src/app/QVTKOpenGLInit.h | 35 ++++++++++++++++++++++++++++++++++ src/app/consoleApp.cpp | 2 ++ src/app/drakeVisualizerApp.cpp | 12 ++---------- src/app/main.cpp | 2 ++ 6 files changed, 70 insertions(+), 12 deletions(-) create mode 100644 src/app/QVTKOpenGLInit.cpp create mode 100644 src/app/QVTKOpenGLInit.h diff --git a/src/app/CMakeLists.txt b/src/app/CMakeLists.txt index e0f95a097..c535102a0 100644 --- a/src/app/CMakeLists.txt +++ b/src/app/CMakeLists.txt @@ -37,7 +37,8 @@ set(decorator_file ${CMAKE_CURRENT_BINARY_DIR}/ddPythonQtDecorators.h) # Configure whether to use the QVTKWidget or the QVTKOpenGLWidget set(USE_QVTKWIDGET TRUE) if((${Qt5Core_VERSION} VERSION_GREATER "5.4.0") AND - (${VTK_VERSION} VERSION_GREATER "7.1.0")) + (NOT ${VTK_VERSION} VERSION_LESS "8.0.0") AND + (${VTK_RENDERING_BACKEND} STREQUAL "OpenGL2")) set(USE_QVTKWIDGET FALSE) endif() configure_file(${CMAKE_CURRENT_SOURCE_DIR}/ddQVTKOpenGLWidgetConfigure.h.in @@ -94,8 +95,9 @@ set(srcs ${ui_srcs} ${resource_srcs} - ddLumberSelection.cpp + QVTKOpenGLInit.cpp ddGLWidgetView.cpp + ddLumberSelection.cpp ddMacrosManager.cpp ddMainWindow.cpp ddObjectTree.cpp diff --git a/src/app/QVTKOpenGLInit.cpp b/src/app/QVTKOpenGLInit.cpp new file mode 100644 index 000000000..ccea11ed1 --- /dev/null +++ b/src/app/QVTKOpenGLInit.cpp @@ -0,0 +1,25 @@ +#include "QVTKOpenGLInit.h" + +// Qt includes +#include + +// VTK includes +#include +#include + +#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0) && VTK_MAJOR_VERSION >= 8 +# ifdef VTK_OPENGL2 + #include + #include +# endif +#endif + +QVTKOpenGLInit::QVTKOpenGLInit() +{ +#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0) && VTK_MAJOR_VERSION >= 8 +# ifdef VTK_OPENGL2 + // Set the default surface format for the OpenGL view + QSurfaceFormat::setDefaultFormat(QVTKOpenGLWidget::defaultFormat()); +# endif +#endif +} diff --git a/src/app/QVTKOpenGLInit.h b/src/app/QVTKOpenGLInit.h new file mode 100644 index 000000000..1ea30f090 --- /dev/null +++ b/src/app/QVTKOpenGLInit.h @@ -0,0 +1,35 @@ +#ifndef __QVTKOpenGLInit_h +#define __QVTKOpenGLInit_h + +/* + * Initialization class that creates a valid QSurfaceFormat i.e. OpenGL context + * with the expected parameters for the QVTKOpenGLWidget. + * Typical use case is to construct the class before constructing a + * QApplication object in the main function. + * + * Typical usage for QVTKOpenGLInit is as follows: + * @code{.cpp} + * + * int main(int argc, char* argv[]) + * { + * // Initialize before constructing the QApplication + * QVTKOpenGLInit init; + * // Construct the QApplication + * QApplication app(argc, argv); + * // Show the application (that uses QVTKOpenGLWidget) + * app.exec(); + * return 0; + * } + * + * @endcode + */ + +#include "ddAppConfigure.h" + +class DD_APP_EXPORT QVTKOpenGLInit +{ +public: + QVTKOpenGLInit(); +}; + +#endif //__QVTKOpenGLInit_h diff --git a/src/app/consoleApp.cpp b/src/app/consoleApp.cpp index 030a4d475..c6e3f851c 100644 --- a/src/app/consoleApp.cpp +++ b/src/app/consoleApp.cpp @@ -2,9 +2,11 @@ #include #include #include "ddPythonManager.h" +#include "QVTKOpenGLInit.h" int main(int argc, char **argv) { + QVTKOpenGLInit init; QApplication app(argc, argv); ddPythonManager* pythonManager = new ddPythonManager; PythonQt::self()->addVariable(PythonQt::self()->importModule("sys"), "executable", QCoreApplication::applicationFilePath()); diff --git a/src/app/drakeVisualizerApp.cpp b/src/app/drakeVisualizerApp.cpp index 31d3888f1..c3a2f4475 100644 --- a/src/app/drakeVisualizerApp.cpp +++ b/src/app/drakeVisualizerApp.cpp @@ -4,19 +4,11 @@ // director includes #include #include "ddPythonManager.h" - -// VTK includes -#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0) - #include - #include -#endif +#include "QVTKOpenGLInit.h" int main(int argc, char **argv) { -#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0) - // Set the default surface format for the OpenGL view - QSurfaceFormat::setDefaultFormat(QVTKOpenGLWidget::defaultFormat()); -#endif + QVTKOpenGLInit init; QApplication app(argc, argv); ddPythonManager* pythonManager = new ddPythonManager; pythonManager->setSysArgv(QApplication::instance()->arguments()); diff --git a/src/app/main.cpp b/src/app/main.cpp index 6226f12b7..c32a49ee4 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -1,6 +1,7 @@ #include #include "ddMainWindow.h" #include "ddPythonManager.h" +#include "QVTKOpenGLInit.h" #define USE_TDX 0 #if USE_TDX @@ -9,6 +10,7 @@ int main(int argc, char **argv) { + QVTKOpenGLInit init; #if USE_TDX QVTKApplication app(argc, argv); #else