3、VTK基础概念
在第2章里,我们已经接触了一个简单的VTK工程,也掌握了怎么使用CMake来构建VTK工程的步骤,本书后续章节的所有例子都是采用第2章介绍的步骤来构建VTK的工程。
本章我们先在第2章TestVTKInstall的基础上作一些更改,演示一个能够交互的VTK应用程序。与前面的风格类似,我们先是让你“知其然”,然后再慢慢地让你“知其所以然”。
3.1 一个稍微复杂的VTK程序
首先当然是写一个CMakeLists.txt文件。在D:\Toolkits\VTK\Examples\下新建一个文件夹,名为
3.1.1_RenderCylinder
,在该目录下新建CMakeLists.txt和RenderCylinder.cpp文件。
示例3.1.1_RenderCylinder的CMakeLists.txt |
1: cmake_minimum_required(VERSION 2.8)
2: project(
RenderCylinder
)
3: find_package(VTK REQUIRED)
4: include(${VTK_USE_FILE})
5: add_executable(${PROJECT_NAME}
RenderCylinder.cpp
)
6: target_link_libraries(${PROJECT_NAME} ${VTK_LIBRARIES})
示例3.1.1_RenderCylinder |
1: #include “vtkSmartPointer.h”
2: #include “vtkRenderWindow.h”
3: #include “vtkRenderer.h”
4: #include “vtkRenderWindowInteractor.h”
5: #include “vtkInteractorStyleTrackballCamera.h”
6: #include “vtkCylinderSource.h”
7: #include “vtkPolyDataMapper.h”
8: #include “vtkActor.h”
9:
10: int main()
11: {
12: vtkSmartPointer<vtkCylinderSource> cylinder =
13: vtkSmartPointer<vtkCylinderSource>::New();
14: cylinder->SetHeight( 3.0 );
15: cylinder->SetRadius( 1.0 );
16: cylinder->SetResolution( 10 );
17:
18: vtkSmartPointer<vtkPolyDataMapper> cylinderMapper =
19: vtkSmartPointer<vtkPolyDataMapper>::New();
20: cylinderMapper->SetInputConnection( cylinder->GetOutputPort() );
21:
22: vtkSmartPointer<vtkActor> cylinderActor =
23: vtkSmartPointer<vtkActor>::New();
24: cylinderActor->SetMapper( cylinderMapper );
25:
26: vtkSmartPointer<vtkRenderer> renderer =
27: vtkSmartPointer<vtkRenderer>::New();
28: renderer->AddActor( cylinderActor );
29: renderer->SetBackground( 0.1, 0.2, 0.4 );
30:
31: vtkSmartPointer<vtkRenderWindow> renWin =
32: vtkSmartPointer<vtkRenderWindow>::New();
33: renWin->AddRenderer( renderer );
34: renWin->SetSize( 300, 300 );
35:
36: vtkSmartPointer<vtkRenderWindowInteractor> iren =
37: vtkSmartPointer<vtkRenderWindowInteractor>::New();
38: iren->SetRenderWindow(renWin);
39:
40: vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
41: vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
42: iren->SetInteractorStyle(style);
43:
44: iren->Initialize();
45: iren->Start();
4