VTK系列18_柱体显示与交互
原创
©著作权归作者所有:来自51CTO博客作者亦我飞也的原创作品,请联系作者获取转载授权,否则将追究法律责任
实例18:柱体的交互
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkCylinderSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkProperty.h>
int main()
{
//定义一个vtkCylinderSource类型的对象cylinder
//vtkCylinderSource主要是生成一个中心在渲染场景原点的柱体,类型为(vtkPolyData)
vtkSmartPointer<vtkCylinderSource> cylinder =
vtkSmartPointer<vtkCylinderSource>::New();
cylinder->SetHeight( 3.0 );//设置柱体的高
cylinder->SetRadius( 1.0 );//设置柱体横截面半径
cylinder->SetResolution( 10 ); //设置柱体横截面的等边多边形的边数
//将输入的数据转换为几何图元(点、线、多边形)进行渲染
vtkSmartPointer<vtkPolyDataMapper> cylinderMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
cylinderMapper->SetInputConnection( cylinder->GetOutputPort() );
vtkSmartPointer<vtkActor> cylinderActor =
vtkSmartPointer<vtkActor>::New();
//设置生成几何图元的Mapper,即连接一个Actor到可视化管线的末端
cylinderActor->SetMapper( cylinderMapper );
cylinderActor->GetProperty()->SetColor(1.0, 0.0, 0.0);
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor( cylinderActor );
renderer->SetBackground( 1.0, 1.0, 1.0 );
//将操作系统与VTK渲染引擎连接到一起
vtkSmartPointer<vtkRenderWindow> renWin =
vtkSmartPointer<vtkRenderWindow>::New();
renWin->AddRenderer( renderer );//加入vtkRenderer对象
renWin->SetSize( 640, 480 );//设置窗口大小,像素为单位
renWin->Render();//场景渲染
renWin->SetWindowName("RenderCylinder");
//提供平台独立的响应鼠标、键盘和时钟事件的交互机制
vtkSmartPointer<vtkRenderWindowInteractor> iren =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow(renWin);
//交互器样式的一种,可通过控制相机对物体作旋转、放大、缩小等操作
vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
iren->SetInteractorStyle(style);
iren->Initialize();
iren->Start();
return EXIT_SUCCESS;
}
VTK系列目录: