1.编写的程序
1 #include <vtkAutoInit.h>
2 VTK_MODULE_INIT(vtkRenderingOpenGL);
3 ///
4 #include "vtkCylinderSource.h"
5 #include "vtkPolyDataMapper.h"
6 #include "vtkActor.h"
7 #include "vtkProperty.h"
8 #include "vtkRenderer.h"
9 #include "vtkRenderWindow.h"
10 #include "vtkRenderWindowInteractor.h"
11 #include "vtkCamera.h"
12
13 int main()
14 {
15 /Visualize Pipeline: Source->Mapper->Actor
16 //
17 vtkCylinderSource* cylinder = vtkCylinderSource::New();
18 cylinder->SetResolution(10);
19
20 vtkPolyDataMapper* cylinderMapper = vtkPolyDataMapper::New();
21 cylinderMapper->SetInputConnection(cylinder->GetOutputPort());
22
23 vtkActor* cylinderActor = vtkActor::New();
24 cylinderActor->SetMapper(cylinderMapper);
25 ///Actor property
26 cylinderActor->GetProperty()->SetColor(1.0,0.32,0.27);
27 cylinderActor->RotateX(30.0);
28 cylinderActor->RotateY(-45);
29
30 //Render Engine:Renderer->RenderWindow->RenderWindowInteractor
31 //
32 vtkRenderer* cylinderRenderer = vtkRenderer::New();
33 vtkRenderWindow* cylinderRenderWindow = vtkRenderWindow::New();
34 vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
35 cylinderRenderWindow->AddRenderer(cylinderRenderer);
36 iren->SetRenderWindow(cylinderRenderWindow);
37
38 //Visualize Pipeline -> Render Engine
39 //
40 cylinderRenderer->AddActor(cylinderActor);
41 cylinderRenderer->SetBackground(0.1,0.2,0.4); // render background property
42 cylinderRenderWindow->SetSize(400,400); //render window size
43
44 //Camera
45 // The renderer renders into the
46 // render window. The render window interactor captures mouse events
47 // and will perform appropriate camera or actor manipulation
48 // depending on the nature of the events.
49 cylinderRenderer->ResetCamera();
50 cylinderRenderer->GetActiveCamera()->Zoom(1.5); //focus
51
52 /Window Hold On
53 iren->Start();
54
55 /Clear Memory
56 iren->Delete();
57 cylinderRenderWindow->Delete();
58 cylinderRenderer->Delete();
59 cylinderActor->Delete();
60 cylinderMapper->Delete();
61 cylinder->Delete();
62 return 0;
63 }
2.心得体会
2.1 对“Visualize Pipeline & Render Engine”设计体系的理解
VTK程序设计严格遵循流水线设计风格。根据我的经验,建议程序设计过程中分模块设计。Part1:Visualize Pipeline(Source -> Mapper -> Actor) 此部分还应该包括Actor Property(不知道中文该怎么翻译,权且当给演员化妆)。Part2:Render Engine(Renderer -> RenderWindow -> RenderWindowInteractor) 这一部分主要工作就是显示窗口可以容纳多少RenderWindow,我们可以理解为窗口可以装的下多少演员!Part3:Communication 这是最重要的环节,以上两个部分搭建好了之后,我们要把Actor装进Renderer(通俗的讲,就是要把妆都画好了的演员送到舞台上)。此外这一部分也会涉及到Renderer(渲染器/舞台)背景设置、以及RenderWindow(观众们看到的窗口)的尺寸。
2.2 窗口一闪即逝
调试的过程中,我发现最终呈现给我的窗口始终不能稳定的存在(一闪即逝)。这是正常现象,因为程序就是这样设计的,如果想让窗口停留一段时间,就要用到类RenderWindowInteractor(用户与窗口交互),并调用Start函数。
3.3 摄像机Camera
Camera在这里用处是不大的,只有在用户交互时才会体会到它的强大。
3.未解决的问题
3.1 Procedural Source Object 怎么翻译?
一脸懵逼,重点应该在Procedural,“程序上的”?
3.2 关于TCL语言与C++语言相互转换
User Guide上给的功能还包括一个快捷键调用,可惜不能把它从Tcl转换成C++;东灵的那本书更是对用户手册的翻译,并没有更加深刻地阐明这个事儿,还需要去探索,回头再解决。
1 # Associate the “u” keypress with a UserEvent and start the event loop
2 iren AddObserver UserEvent {wm deiconify .vtkInteract}
3 iren Initialize
4 # suppress the tk window
5 wm withdraw .