VTK 提供了多个方法来与图形对象交互,其中的一个类是 vtkRenderWindowInteractor,这是一个操作相机。
对象和拾取工具的类,提供平台独立的相应鼠标、键盘和时钟时间的交互机制,通过VTK的观察者/命令模式将监听到的特定平台的鼠标和时钟事件交由vtkInteratorObserver或其子类进行处理。
下面是一个交互器的初识Demo
/*
* 窗口互操作
* vtkRenderWindowInteractor
* 属性
* vtkProperty
*
*
*/
//圆锥的类 数据源
#include <vtkConeSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkInteractorStyleTrackballActor.h>
#include <vtkProperty.h>
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2) ;
VTK_MODULE_INIT(vtkInteractionStyle) ;
VTK_MODULE_INIT(vtkRenderingFreeType);
int main_02(int argc,char* argv[])
{
vtkConeSource* coneSource = vtkConeSource::New();
//分辨率
coneSource->SetResolution(100);
//高度
coneSource->SetHeight(5);
//底面半径
coneSource->SetRadius(2);
//映射器
vtkPolyDataMapper* mapper = vtkPolyDataMapper::New();
mapper->SetInputConnection(coneSource->GetOutputPort());
vtkActor* actor = vtkActor::New();
actor->SetMapper(mapper);
//设置颜色
//actor->GetProperty()->SetColor(1,0,0);
vtkProperty* prop = vtkProperty::New();
prop->SetColor(0,1,0);
//关掉灯光 失去三维效果
//prop->SetLighting(false);
actor->SetProperty(prop);
vtkRenderer* renderer = vtkRenderer::New();
renderer->AddActor(actor);
renderer->SetBackground(0.1,0.2,0.4);
vtkRenderWindow* window = vtkRenderWindow::New();
window->SetSize(500,500);
window->AddRenderer(renderer);
vtkRenderWindowInteractor* interactor = vtkRenderWindowInteractor::New();
interactor->SetRenderWindow(window);
vtkInteractorStyleTrackballCamera* camera = vtkInteractorStyleTrackballCamera::New();
vtkInteractorStyleTrackballActor* actorStyle = vtkInteractorStyleTrackballActor::New();
interactor->SetInteractorStyle(actorStyle);
window->Render();
renderer->GetActiveCamera()->Azimuth(1);
interactor->Initialize();
interactor->Start();
//实现一系列操作
//左键(按住)旋转 右键放大缩小
/*
vtkRenderWindowInteractor* interactor = vtkRenderWindowInteractor::New();
interactor->SetRenderWindow(window);
window->Render();
renderer->GetActiveCamera()->Azimuth(1);
interactor->Initialize();
interactor->Start();
*/
//实现拖动旋转、放大
/*
vtkRenderWindowInteractor* interactor = vtkRenderWindowInteractor::New();
interactor->SetRenderWindow(window);
vtkInteractorStyleTrackballCamera* camera = vtkInteractorStyleTrackballCamera::New();
interactor->SetInteractorStyle(camera);
window->Render();
renderer->GetActiveCamera()->Azimuth(1);
interactor->Initialize();
interactor->Start();
*/
window->Delete();
renderer->Delete();
mapper->Delete();
actor->Delete();
coneSource->Delete();
interactor->Delete();
camera->Delete();
return 0;
}