vtk多窗口同步: 相机同步+共享数据

下面的一份数据被复制到多个窗口中用不同的renderer渲染。

不同窗口的同步使用相机回调函数来遍历所有的render,重新设置所有的窗口,已达到同步的目的。

#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 "vtkCallbackCommand.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkSmartPointer.h"
#include "vtkInteractorStyleSwitch.h"

static vtkSmartPointer renderer01;
static vtkSmartPointer renderer02;
static vtkSmartPointer renderWindow01;
static vtkSmartPointer renderWindow02;

class vtkMyCameraCallback : public vtkCommand
{
public:
	static vtkMyCameraCallback *New()
	{
		return new vtkMyCameraCallback;
	}
	virtual void Execute(vtkObject *caller, unsigned long, void*)
	{
		vtkCamera *camera = static_cast(caller);
		double camPos[3];
		double focalPos[3];
		double upVector[3];
		camera->GetPosition(camPos);
		camera->GetFocalPoint(focalPos);
		camera->GetViewUp(upVector);

		//遍历所有renderer,都设置一遍最新的相机
		renderer01->SetActiveCamera(camera);
		renderer02->SetActiveCamera(camera);
		renderWindow01->Render();
		renderWindow02->Render();
	}
	vtkSmartPointer m_renderer;
};

int main(int argc, char *argv[])
{
	vtkSmartPointer cone1 = vtkSmartPointer::New();
	cone1->SetResolution(20);
	cone1->Update();
	vtkSmartPointer coneMapper1 = vtkSmartPointer::New();
	coneMapper1->SetInputData(cone1->GetOutput());
	coneMapper1->Update();
	vtkSmartPointer coneActor1 = vtkSmartPointer::New();
	vtkSmartPointer cubeActor2 = vtkSmartPointer::New();
	coneActor1->SetMapper(coneMapper1);

	renderer01 = vtkSmartPointer::New();
	renderer02 = vtkSmartPointer::New();

	//渲染时使用同样的数据
	renderer01->AddActor(coneActor1);
	renderer02->AddActor(coneActor1);

	renderWindow01 = vtkSmartPointer::New();
	renderWindow02 = vtkSmartPointer::New();

	renderWindow01->AddRenderer(renderer01);
	renderWindow02->AddRenderer(renderer02);

	vtkSmartPointer windowInteractor01 = vtkSmartPointer::New();
	vtkSmartPointer windowInteractor02 = vtkSmartPointer::New();
	windowInteractor01->SetRenderWindow(renderWindow01);
	windowInteractor02->SetRenderWindow(renderWindow02);

	vtkSmartPointer cameraCallback1 = vtkSmartPointer::New();
	cameraCallback1->m_renderer = renderer01;
	vtkSmartPointer cameraCallback2 = vtkSmartPointer::New();
	cameraCallback2->m_renderer = renderer02;
	renderer01->GetActiveCamera()->AddObserver(vtkCommand::ModifiedEvent, cameraCallback1);
	renderer02->GetActiveCamera()->AddObserver(vtkCommand::ModifiedEvent, cameraCallback2);
	renderer01->ResetCamera();
	renderer02->ResetCamera();
	windowInteractor01->Initialize();
	windowInteractor02->Initialize();
	windowInteractor01->Start();
	windowInteractor02->Start();

	return 0;

}

vtk多窗口同步: 相机同步+共享数据_第1张图片

你可能感兴趣的:(VTK/ITK)