VTK 学习笔记(七)ShareCameraQt

VTK 学习笔记(七)ShareCameraQt,第1张

VTK 学习笔记(七)ShareCameraQt

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

#include 


namespace 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.png
  
  
   
    22
    22
   
  
  
   
    
     
      10
      20
      511
      541
     
    
   
   
    
     
      540
      20
      441
      541
     
    
   
  
  
   
    true
   
   
    
     :/Icons/fileopen.png:/Icons/fileopen.png
   
   
    Open File...
   
  
  
   
    
     ..
   
   
    Exit
   
  
  
   
    
     :/Icons/print.png:/Icons/print.png
   
   
    Print
   
  
  
   
    
     :/Icons/help.png:/Icons/help.png
   
   
    Help
   
  
  
   
    
     :/Icons/filesave.png:/Icons/filesave.png
   
   
    Save
   
  
 
 
  
   QVTKOpenGLNativeWidget
   QWidget
   
QVTKOpenGLNativeWidget.h

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5711312.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存