我有一篇是同一个窗体两个物体的相互控制联动,这次是不同的窗体的,加入了一个回调函数类来控制两个物体行动一致
#include <iostream>
#include <QCoreApplication>
#include <qdir.h>
#include <QDebug.h>
#include "vtkConeSource.h"
#include "vtkCubeSource.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkCamera.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkCommand.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkSmartPointer.h"
#include "vtkInteractorStyleSwitch.h"
#include "QDateTime.h"
static vtkSmartPointer<vtkRenderer> renderer01;
static vtkSmartPointer<vtkRenderer> renderer02;
static vtkSmartPointer<vtkRenderWindow> renderWindow01;
static vtkSmartPointer<vtkRenderWindow> renderWindow02;
class vtkMyCamerCallback : public vtkCommand
{
public:
static vtkMyCamerCallback *New()
{
return new vtkMyCamerCallback;
}
//设置两个窗体一致的关键代码
virtual void Execute(vtkObject *caller,
unsigned long, void*)
{
vtkCamera *camera = static_cast<vtkCamera*>(caller);
double camPos[3];
double focalPos[3];
double upVector[3];
camera->GetPosition(camPos);
camera->GetFocalPoint(focalPos);
camera->GetViewUp(upVector);
renderer02->SetActiveCamera(camera);
renderWindow02->Render();
renderer01->SetActiveCamera(camera);
renderWindow01->Render();
}
};
int main(int argc, int *argv[])
{
vtkSmartPointer<vtkConeSource> cone =
vtkSmartPointer<vtkConeSource>::New();
vtkSmartPointer<vtkCubeSource> cube =
vtkSmartPointer<vtkCubeSource>::New();
cone->SetResolution(20);
cone->Update();
cube->Update();
vtkSmartPointer<vtkPolyDataMapper> coneMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
vtkSmartPointer<vtkPolyDataMapper> cubeMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
coneMapper->SetInputData(cone->GetOutput());
cubeMapper->SetInputData(cube->GetOutput());
coneMapper->Update();
cubeMapper->Update();
vtkSmartPointer<vtkActor> coneActor =
vtkSmartPointer<vtkActor>::New();
vtkSmartPointer<vtkActor> cubeActor =
vtkSmartPointer<vtkActor>::New();
coneActor->SetMapper(coneMapper);
cubeActor->SetMapper(cubeMapper);
renderer01 = vtkSmartPointer<vtkRenderer>::New();
renderer02 = vtkSmartPointer<vtkRenderer>::New();
renderer01->AddActor(coneActor); // 锥形
renderer02->AddActor(cubeActor); // 立方体
renderWindow01 = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow02 = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow01->AddRenderer(renderer01);
renderWindow02->AddRenderer(renderer02);
// 使renderWindow01获得交互机制
vtkSmartPointer<vtkRenderWindowInteractor> windowInteractor01 =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleSwitch> trackballStyle =
vtkSmartPointer<vtkInteractorStyleSwitch>::New();
trackballStyle->SetCurrentStyleToTrackballCamera();
windowInteractor01->SetInteractorStyle(trackballStyle);
windowInteractor01->SetRenderWindow(renderWindow01);
vtkSmartPointer<vtkRenderWindowInteractor> windowInteractor02 =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleSwitch> trackballStyle1 =
vtkSmartPointer<vtkInteractorStyleSwitch>::New();
trackballStyle1->SetCurrentStyleToTrackballCamera();
windowInteractor02->SetInteractorStyle(trackballStyle1);
windowInteractor02->SetRenderWindow(renderWindow02);
vtkSmartPointer<vtkMyCamerCallback> cameraCallback =
vtkSmartPointer<vtkMyCamerCallback>::New();
//添加观察函数
renderer01->GetActiveCamera()->AddObserver(vtkCommand::ModifiedEvent, cameraCallback);
renderer02->GetActiveCamera()->AddObserver(vtkCommand::ModifiedEvent, cameraCallback);
renderer01->ResetCamera();
renderer02->ResetCamera();
windowInteractor01->Initialize();
windowInteractor01->Start();
windowInteractor02->Initialize();
windowInteractor02->Start();
return 0;
}