走進電影院觀看VTK


  • VTK影院模型:

  從這個模型去介紹VTK的應用,整個電影院就是VTK的顯示窗口(vtkRenderWindow),舞台就是VTK的渲染場景(vtkRenderer),場景中有不同的演員就是VTK的各種三維模型(vtkActor),演員身材長相動作表情各不相同就像模型有大有小顏色各異就是VTK模型屬性(vtkPorperty),台下的觀眾與演員進行交互就是VTK中窗口與鼠標等的交互(vtkRenderWindowInteractor),然而觀眾與演員的交互方式也不一樣,有的揮手有的吶喊就像VTK中有時用鼠標有時用鍵盤(vtkInteractorStyle),在我們觀看時,我們在電影院的前排后排靠左靠右買票時就決定了,因此對於一個人只有一個視角就像VTK中的相機(vtkCamera),根據距離遠近位置不同看到的舞台人物大小不同。舞台上的燈光就像VTK中的燈光(vtkLight)有很多燈光配合襯托舞台效果。下面是一個簡單地例子:

 

  • 代碼解釋如下:
 1 #include <vtkAutoInit.h> 
 2 VTK_MODULE_INIT(vtkRenderingOpenGL2);
 3 VTK_MODULE_INIT(vtkInteractionStyle);
 4 #include <vtkActor.h>
 5 #include <vtkCamera.h>
 6 #include <vtkLight.h>
 7 #include <vtkCylinderSource.h>
 8 #include <vtkConeSource.h>
 9 #include <vtkFloatArray.h>
10 #include <vtkNamedColors.h>
11 #include <vtkNew.h>
12 #include <vtkPointData.h>
13 #include <vtkPoints.h>
14 #include <vtkPolyData.h>
15 #include <vtkPolyDataMapper.h>
16 #include <vtkRenderer.h> 
17 #include <vtkRenderWindow.h>
18 #include <vtkRenderWindowInteractor.h>
19 #include <vtkInteractorStyleTrackballCamera.h>
20 #include <array>
21 
22 int main()
23 {//演員訓練成什么類型,想要一個圓柱樣子
24     vtkNew<vtkCylinderSource> cylinder;
25     cylinder->SetHeight(3.0);//柱體的高
26     cylinder->SetRadius(1.0);//柱體的橫截面半徑
27     cylinder->SetResolution(10);//柱體的等邊多邊形邊數
28 //根據想要的圓柱演員樣子,將他渲染訓練成該樣
29     vtkNew < vtkPolyDataMapper>cylinderMapper;
30     cylinderMapper->SetInputConnection(cylinder->GetOutputPort());
31     vtkNew < vtkActor>cylinderActor;//演員產生
32     cylinderActor->SetMapper(cylinderMapper);
33     vtkNew < vtkRenderer>renderer;//搭建舞台
34     renderer->AddActor(cylinderActor);//所有演員中的哪一個要上台
35     renderer->SetBackground(0.6, 0.5, 0.4);//舞台背景
36     vtkNew < vtkRenderWindow>renWin;//指定電影院
37     renWin->AddRenderer(renderer);//指定電影院要播放的電影(舞台)
38     renWin->SetSize(500, 500);//建立影院大小
39     
40     vtkNew<vtkRenderWindowInteractor> iren;//表演中有互動
41     iren->SetRenderWindow(renWin);//和那個演出互動
42     vtkNew<vtkInteractorStyleTrackballCamera>style;//互動的形式是什么
43 
44     iren->SetInteractorStyle(style);
45     iren->Initialize();
46     iren->Start();
47 
48     return EXIT_SUCCESS;
49 }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM