1 #include <vtkAutoInit.h>
2 VTK_MODULE_INIT(vtkRenderingOpenGL);
3 VTK_MODULE_INIT(vtkRenderingVolumeOpenGL);
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 <vtkVolumeProperty.h>
11 #include <vtkPiecewiseFunction.h>
12 #include <vtkColorTransferFunction.h>
13 #include <vtkGPUVolumeRayCastMapper.h>
14 #include <vtkVolume.h>
15 #include <vtkLODProp3D.h>
16 #include <vtkRenderer.h>
17 #include <vtkRenderWindow.h>
18 #include <vtkRenderWindowInteractor.h>
19 #include <vtkCamera.h>
20
21 int main(int argc, char *argv[])
22 {
23 vtkSmartPointer<vtkStructuredPointsReader> reader =
24 vtkSmartPointer<vtkStructuredPointsReader>::New();
25 reader->SetFileName("mummy.128.vtk");
26 reader->Update();
27
28 vtkSmartPointer<vtkVolumeProperty> volumeProperty =
29 vtkSmartPointer<vtkVolumeProperty>::New();
30 volumeProperty->SetInterpolationTypeToLinear();
31 volumeProperty->ShadeOn();
32 volumeProperty->SetAmbient(0.4);
33 volumeProperty->SetDiffuse(0.6);
34 volumeProperty->SetSpecular(0.2);
35
36 vtkSmartPointer<vtkPiecewiseFunction> compositeOpacity =
37 vtkSmartPointer<vtkPiecewiseFunction>::New();
38 compositeOpacity->AddPoint(70, 0.00);
39 compositeOpacity->AddPoint(90, 0.40);
40 compositeOpacity->AddPoint(180, 0.60);
41 volumeProperty->SetScalarOpacity(compositeOpacity);
42
43 vtkSmartPointer<vtkColorTransferFunction> color =
44 vtkSmartPointer<vtkColorTransferFunction>::New();
45 color->AddRGBPoint(0.000, 0.00, 0.00, 0.00);
46 color->AddRGBPoint(64.00, 1.00, 0.52, 0.30);
47 color->AddRGBPoint(190.0, 1.00, 1.00, 1.00);
48 color->AddRGBPoint(220.0, 0.20, 0.20, 0.20);
49 volumeProperty->SetColor(color);
50 /******************************************************************/
51 vtkSmartPointer<vtkGPUVolumeRayCastMapper> hiresMapper =
52 vtkSmartPointer<vtkGPUVolumeRayCastMapper>::New();
53 hiresMapper->SetInputData(reader->GetOutput());
54 hiresMapper->SetAutoAdjustSampleDistances(0); //关闭自动调整功能
55
56 vtkSmartPointer<vtkGPUVolumeRayCastMapper> lowresMapper =
57 vtkSmartPointer<vtkGPUVolumeRayCastMapper>::New();
58 lowresMapper->SetInputData(reader->GetOutput());
59 lowresMapper->SetAutoAdjustSampleDistances(0);
60 lowresMapper->SetSampleDistance(4 * hiresMapper->GetSampleDistance());
61 lowresMapper->SetImageSampleDistance(4 * hiresMapper->GetImageSampleDistance());
62 /******************************************************************/
63 vtkSmartPointer<vtkLODProp3D> prop =
64 vtkSmartPointer<vtkLODProp3D>::New();
65 prop->AddLOD(lowresMapper, volumeProperty, 0.0);
66 prop->AddLOD(hiresMapper, volumeProperty, 0.0);
67 vtkSmartPointer<vtkVolume> volume =
68 vtkSmartPointer<vtkVolume>::New();
69 volume->SetMapper(hiresMapper);
70 volume->SetProperty(volumeProperty);
71
72 double volumeView[4] = { 0, 0, 0.5, 1 };
73 double lodpropView[4] = { 0.5, 0, 1, 1 };
74
75 vtkSmartPointer<vtkRenderer> volumeRender = vtkSmartPointer<vtkRenderer>::New();
76 volumeRender->SetBackground(1, 1, 0);
77 volumeRender->AddVolume(volume);
78 volumeRender->SetViewport(volumeView);
79
80 vtkSmartPointer<vtkRenderer> propRender = vtkSmartPointer<vtkRenderer>::New();
81 propRender->SetBackground(0, 1, 0);
82 propRender->AddVolume(prop);
83 propRender->SetViewport(lodpropView);
84 ///
85 vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
86 renWin->AddRenderer(volumeRender);
87 renWin->AddRenderer(propRender);
88 renWin->SetSize(640, 320);
89 renWin->Render();
90 renWin->SetWindowName("Volume By vtkLODProp3D");
91
92 vtkSmartPointer<vtkRenderWindowInteractor> rwi =
93 vtkSmartPointer<vtkRenderWindowInteractor>::New();
94 rwi->SetRenderWindow(renWin);
95
96 volumeRender->GetActiveCamera()->SetPosition(0, -1, 0);
97 volumeRender->GetActiveCamera()->SetFocalPoint(0, 0, 0);
98 volumeRender->GetActiveCamera()->SetViewUp(0, 0, 1);
99 volumeRender->GetActiveCamera()->Azimuth(30);
100 volumeRender->GetActiveCamera()->Elevation(30);
101 volumeRender->ResetCamera();
102 propRender->SetActiveCamera(volumeRender->GetActiveCamera());
103
104 renWin->Render();
105 rwi->Start();
106
107 return 0;
108 }