ShareCameraQt 演示了如何让另外一个窗口与第一个窗口共享一样的摄像机视角,
关键代码如下
#if VTK890 this->ui->qvtkWidgetLeft->renderWindow()->AddObserver(vtkCommand::ModifiedEvent, this, &ShareCameraQt::ModifiedHandler); #else this->ui->qvtkWidgetLeft->GetRenderWindow()->AddObserver(vtkCommand::ModifiedEvent, this, &ShareCameraQt::ModifiedHandler); #endif
因此,只有鼠标拖动左侧圆球,才会让右边的立方体与左侧同步转动。如果拖动右侧的立方体,左侧的是不会动的。
有个疑问,为什么每次都是鼠标释放后,右侧才同步,不能立刻响应。
#include#include #include #include ShareCameraQt.h //下面的原来没有,但是我用vtk9,必须加上去,其他版本不知道 #include VTK_MODULE_INIT(vtkRenderingOpenGL2); VTK_MODULE_INIT(vtkInteractionStyle); int main( int argc, char** argv ) { // needed to ensure appropriate OpenGL context is created for VTK rendering. QSurfaceFormat::setDefaultFormat(QVTKOpenGLNativeWidget::defaultFormat()); // QT Stuff QApplication app( argc, argv ); ShareCameraQt shareCameraQt; shareCameraQt.show(); return app.exec(); }
#ifndef ShareCameraQt_H #define ShareCameraQt_H #includenamespace Ui { class ShareCameraQt; } class ShareCameraQt : public QMainWindow { Q_OBJECT public: // Constructor/Destructor explicit ShareCameraQt(QWidget* parent = nullptr); virtual ~ShareCameraQt() = default; private: // Designer form Ui::ShareCameraQt *ui; public slots: void slotExit(); protected: void ModifiedHandler(); }; #endif
#include ShareCameraQt.h #include ui_ShareCameraQt.h #include#include #include #include #include #include #include #include #include #include #include #include #include #include #include #if VTK_VERSION_NUMBER >= 89000000000ULL #define VTK890 1 #endif // Constructor ShareCameraQt::ShareCameraQt(QWidget* parent) : QMainWindow(parent), ui(new Ui::ShareCameraQt) { this->ui->setupUi(this); vtkNew colors; vtkNew renderWindowLeft; vtkNew renderWindowRight; #if VTK890 this->ui->qvtkWidgetLeft->setRenderWindow(renderWindowLeft); this->ui->qvtkWidgetRight->setRenderWindow(renderWindowRight); #else this->ui->qvtkWidgetLeft->SetRenderWindow(renderWindowLeft); this->ui->qvtkWidgetRight->SetRenderWindow(renderWindowRight); #endif // Sphere vtkNew sphereSource; sphereSource->Update(); vtkNew sphereMapper; sphereMapper->SetInputConnection(sphereSource->GetOutputPort()); vtkNew sphereActor; sphereActor->SetMapper(sphereMapper); sphereActor->GetProperty()->SetColor(colors->GetColor4d(Tomato).GetData()); // Cube vtkNew cubeSource; cubeSource->Update(); vtkNew cubeMapper; cubeMapper->SetInputConnection(cubeSource->GetOutputPort()); vtkNew cubeActor; cubeActor->SetMapper(cubeMapper); cubeActor->GetProperty()->SetColor(colors->GetColor4d(MediumSeaGreen).GetData()); // VTK Renderer vtkNew leftRenderer; leftRenderer->AddActor(sphereActor); leftRenderer->SetBackground(colors->GetColor3d(LightSteelBlue).GetData()); vtkNew rightRenderer; // Add Actor to renderer rightRenderer->AddActor(cubeActor); rightRenderer->SetBackground(colors->GetColor3d(LightSteelBlue).GetData()); // VTK/Qt wedded #if VTK890 this->ui->qvtkWidgetLeft->renderWindow()->AddRenderer(leftRenderer); this->ui->qvtkWidgetRight->renderWindow()->AddRenderer(rightRenderer); #else this->ui->qvtkWidgetLeft->GetRenderWindow()->AddRenderer(leftRenderer); this->ui->qvtkWidgetRight->GetRenderWindow()->AddRenderer(rightRenderer); #endif rightRenderer->ResetCamera(); leftRenderer->ResetCamera(); // Here we share the camera rightRenderer->SetActiveCamera(leftRenderer->GetActiveCamera()); // Position the cube using the left renderer active camera leftRenderer->GetActiveCamera()->SetPosition(1.0, 0.8, 1.0); leftRenderer->GetActiveCamera()->SetFocalPoint(0, 0, 0); leftRenderer->ResetCamera(); leftRenderer->GetActiveCamera()->Zoom(0.8); // Set up action signals and slots connect(this->ui->actionExit, SIGNAL(triggered()), this, SLOT(slotExit())); #if VTK890 this->ui->qvtkWidgetLeft->renderWindow()->AddObserver(vtkCommand::ModifiedEvent, this, &ShareCameraQt::ModifiedHandler); #else this->ui->qvtkWidgetLeft->GetRenderWindow()->AddObserver(vtkCommand::ModifiedEvent, this, &ShareCameraQt::ModifiedHandler); #endif } void ShareCameraQt::ModifiedHandler() { #if VTK890 this->ui->qvtkWidgetRight->renderWindow()->Render(); #else this->ui->qvtkWidgetRight->GetRenderWindow()->Render(); #endif } void ShareCameraQt::slotExit() { qApp->exit(); }
ShareCameraQt 0 0 990 583 ShareCameraQt :/Icons/help.png :/Icons/help.png22 22 10 20 511 541 540 20 441 541 true :/Icons/fileopen.png :/Icons/fileopen.pngOpen File... . .Exit :/Icons/print.png :/Icons/print.png:/Icons/help.png :/Icons/help.pngHelp :/Icons/filesave.png :/Icons/filesave.pngSave QVTKOpenGLNativeWidget QWidget QVTKOpenGLNativeWidget.h
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)