VTK 实例66:GPU加速光线投影体绘制

发布时间 2023-08-16 09:20:37作者: 一杯清酒邀明月

 1 #include <vtkAutoInit.h>
 2 VTK_MODULE_INIT(vtkRenderingOpenGL2);
 3 VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);
 4 VTK_MODULE_INIT(vtkRenderingFreeType);
 5 VTK_MODULE_INIT(vtkInteractionStyle);
 6 
 7 #include <vtkSmartPointer.h>
 8 #include <vtkStructuredPoints.h>
 9 #include <vtkStructuredPointsReader.h>
10 #include <vtkGPUVolumeRayCastMapper.h>
11 #include <vtkColorTransferFunction.h>
12 #include <vtkPiecewiseFunction.h>
13 #include <vtkRenderer.h>
14 #include <vtkRenderWindow.h>
15 #include <vtkRenderWindowInteractor.h>
16 #include <vtkVolumeProperty.h>
17 //#include <vtkVolumeRayCastIsosurfaceFunction.h>
18 
19 int main(int argc, char* argv[])
20 {
21     vtkSmartPointer<vtkStructuredPointsReader> reader =
22         vtkSmartPointer<vtkStructuredPointsReader>::New();
23     reader->SetFileName("C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\vtk_图像处理学习\\第七章_VTK体绘制\\data\\mummy.128.vtk");
24     reader->Update();
25 
26 
27     vtkSmartPointer<vtkGPUVolumeRayCastMapper> volumeMapper =
28         vtkSmartPointer<vtkGPUVolumeRayCastMapper>::New();
29     volumeMapper->SetInputData(reader->GetOutput());;
30     volumeMapper->SetSampleDistance(volumeMapper->GetSampleDistance() / 2);    //设置光线采样距离
31     //volumeMapper->SetAutoAdjustSampleDistances(0);//设置图像采样步长
32     //volumeMapper->SetImageSampleDistance(4);
33     /*************************************************************************/
34     vtkSmartPointer<vtkVolumeProperty> volumeProperty =
35         vtkSmartPointer<vtkVolumeProperty>::New();
36     volumeProperty->SetInterpolationTypeToLinear();
37     volumeProperty->ShadeOn();  //打开或者关闭阴影测试
38     volumeProperty->SetAmbient(0.4);
39     volumeProperty->SetDiffuse(0.6);  //漫反射
40     volumeProperty->SetSpecular(0.2); //镜面反射
41     //设置不透明度
42     vtkSmartPointer<vtkPiecewiseFunction> compositeOpacity =
43         vtkSmartPointer<vtkPiecewiseFunction>::New();
44     compositeOpacity->AddPoint(70, 0.00);
45     compositeOpacity->AddPoint(90, 0.40);
46     compositeOpacity->AddPoint(180, 0.60);
47     volumeProperty->SetScalarOpacity(compositeOpacity); //设置不透明度传输函数
48     //设置梯度不透明属性
49     vtkSmartPointer<vtkPiecewiseFunction> volumeGradientOpacity =
50         vtkSmartPointer<vtkPiecewiseFunction>::New();
51     volumeGradientOpacity->AddPoint(10, 0.0);
52     volumeGradientOpacity->AddPoint(90, 0.5);
53     volumeGradientOpacity->AddPoint(100, 1.0);
54     volumeProperty->SetGradientOpacity(volumeGradientOpacity);//设置梯度不透明度效果对比
55     //设置颜色属性
56     vtkSmartPointer<vtkColorTransferFunction> color =
57         vtkSmartPointer<vtkColorTransferFunction>::New();
58     color->AddRGBPoint(0.000, 0.00, 0.00, 0.00);
59     color->AddRGBPoint(64.00, 1.00, 0.52, 0.30);
60     color->AddRGBPoint(190.0, 1.00, 1.00, 1.00);
61     color->AddRGBPoint(220.0, 0.20, 0.20, 0.20);
62     volumeProperty->SetColor(color);
63     /********************************************************************************/
64     vtkSmartPointer<vtkVolume> volume =
65         vtkSmartPointer<vtkVolume>::New();
66     volume->SetMapper(volumeMapper);
67     volume->SetProperty(volumeProperty);
68 
69     vtkSmartPointer<vtkRenderer> ren = vtkSmartPointer<vtkRenderer>::New();
70     ren->SetBackground(0, 1, 0);
71     ren->AddVolume(volume);
72 
73     vtkSmartPointer<vtkRenderWindow> rw = vtkSmartPointer<vtkRenderWindow>::New();
74     rw->AddRenderer(ren);
75     rw->SetSize(640, 480);
76     rw->Render();
77     rw->SetWindowName("VolumeRendering");
78 
79     vtkSmartPointer<vtkRenderWindowInteractor> rwi =
80         vtkSmartPointer<vtkRenderWindowInteractor>::New();
81     rwi->SetRenderWindow(rw);
82 
83     ren->ResetCamera();
84     rw->Render();
85     rwi->Start();
86 
87     return 0;
88 }