1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestGPURayCastVolumeUpdate.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 /**
16  * This test volume tests whether updating the volume MTime updates the ,
17  * geometry in the volume mapper.
18  *
19  * Added renderer to expand coverage for vtkDualDepthPeelingPass.
20  */
21 
22 #include <vtkColorTransferFunction.h>
23 #include <vtkDataArray.h>
24 #include <vtkGPUVolumeRayCastMapper.h>
25 #include <vtkImageData.h>
26 #include <vtkInteractorStyleTrackballCamera.h>
27 #include <vtkNew.h>
28 #include <vtkOutlineFilter.h>
29 #include <vtkOpenGLRenderer.h>
30 #include <vtkPiecewiseFunction.h>
31 #include <vtkPointDataToCellData.h>
32 #include <vtkPolyDataMapper.h>
33 #include <vtkProperty.h>
34 #include <vtkRegressionTestImage.h>
35 #include <vtkRenderer.h>
36 #include <vtkRenderWindow.h>
37 #include <vtkRenderWindowInteractor.h>
38 #include <vtkRTAnalyticSource.h>
39 #include <vtkSmartPointer.h>
40 #include <vtkSphereSource.h>
41 #include <vtkTesting.h>
42 #include <vtkTestingObjectFactory.h>
43 #include <vtkTestUtilities.h>
44 #include <vtkVolumeProperty.h>
45 #include <vtkXMLImageDataReader.h>
46 
47 
TestGPURayCastCellData(int argc,char * argv[])48 int TestGPURayCastCellData(int argc, char *argv[])
49 {
50   // Volume peeling is only supported through the dual depth peeling algorithm.
51   // If the current system only supports the legacy peeler, skip this test:
52   vtkNew<vtkRenderWindow> renWin;
53   vtkNew<vtkRenderWindowInteractor> iren;
54   iren->SetRenderWindow(renWin);
55   renWin->Render(); // Create the context
56 
57   vtkNew<vtkRenderer> ren;
58   renWin->AddRenderer(ren);
59   vtkOpenGLRenderer *oglRen = vtkOpenGLRenderer::SafeDownCast(ren);
60   assert(oglRen); // This test should only be enabled for OGL2 backend.
61   // This will print details about why depth peeling is unsupported:
62   oglRen->SetDebug(1);
63   bool supported = oglRen->IsDualDepthPeelingSupported();
64   oglRen->SetDebug(0);
65   if (!supported)
66   {
67     std::cerr << "Skipping test; volume peeling not supported.\n";
68     return VTK_SKIP_RETURN_CODE;
69   }
70 
71   cout << "CTEST_FULL_OUTPUT (Avoid ctest truncation of output)" << endl;
72 
73   double scalarRange[2];
74 
75   vtkNew<vtkActor> outlineActor;
76   vtkNew<vtkPolyDataMapper> outlineMapper;
77   vtkNew<vtkGPUVolumeRayCastMapper> volumeMapper;
78 
79   vtkNew<vtkXMLImageDataReader> reader;
80   char* volumeFile = vtkTestUtilities::ExpandDataFileName(
81                             argc, argv, "Data/vase_1comp.vti");
82   reader->SetFileName(volumeFile);
83   delete[] volumeFile;
84 
85   vtkNew<vtkPointDataToCellData> pointToCell;
86   pointToCell->SetInputConnection(reader->GetOutputPort());
87   volumeMapper->SetInputConnection(pointToCell->GetOutputPort());
88 
89   // Add outline filter
90   vtkNew<vtkOutlineFilter> outlineFilter;
91   outlineFilter->SetInputConnection(pointToCell->GetOutputPort());
92   outlineMapper->SetInputConnection(outlineFilter->GetOutputPort());
93   outlineActor->SetMapper(outlineMapper);
94 
95   volumeMapper->GetInput()->GetScalarRange(scalarRange);
96   volumeMapper->SetSampleDistance(0.1);
97   volumeMapper->SetAutoAdjustSampleDistances(0);
98   volumeMapper->SetBlendModeToComposite();
99 
100   renWin->SetMultiSamples(0);
101   renWin->SetSize(800, 400);
102 
103   vtkNew<vtkInteractorStyleTrackballCamera> style;
104   iren->SetInteractorStyle(style);
105 
106   // Initialize OpenGL context
107   renWin->Render();
108 
109   // Renderer without translucent geometry
110   ren->SetViewport(0.0, 0.0, 0.5, 1.0);
111   ren->SetBackground(0.2, 0.2, 0.5);
112 
113   vtkNew<vtkPiecewiseFunction> scalarOpacity;
114   scalarOpacity->AddPoint(50, 0.0);
115   scalarOpacity->AddPoint(75, 1.0);
116 
117   vtkNew<vtkVolumeProperty> volumeProperty;
118   volumeProperty->ShadeOn();
119   volumeProperty->SetInterpolationType(VTK_LINEAR_INTERPOLATION);
120   volumeProperty->SetScalarOpacity(scalarOpacity);
121 
122   vtkNew<vtkColorTransferFunction> colorTransferFunction;
123   colorTransferFunction->RemoveAllPoints();
124   colorTransferFunction->AddRGBPoint(scalarRange[0], 0.6, 0.4, 0.1);
125   volumeProperty->SetColor(colorTransferFunction);
126 
127   vtkNew<vtkVolume> volume;
128   volume->SetMapper(volumeMapper);
129   volume->SetProperty(volumeProperty);
130 
131   ren->AddVolume(volume);
132   ren->AddActor(outlineActor);
133 
134   // Renderer with translucent geometry
135   vtkNew<vtkSphereSource> sphereSource;
136   sphereSource->SetCenter(80.0, 60.0, 30.0);
137   sphereSource->SetRadius(30.0);
138 
139   vtkNew<vtkActor> sphereActor;
140   vtkProperty* sphereProperty = sphereActor->GetProperty();
141   sphereProperty->SetColor(1., 0.9, 1);
142   sphereProperty->SetOpacity(0.4);
143 
144   vtkNew<vtkPolyDataMapper> sphereMapper;
145   sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
146   sphereActor->SetMapper(sphereMapper);
147 
148   vtkNew<vtkRenderer> ren2;
149   ren2->SetViewport(0.5, 0.0, 1.0, 1.0);
150   ren2->SetBackground(0.2, 0.2, 0.5);
151   ren2->SetActiveCamera(ren->GetActiveCamera());
152 
153   ren2->SetUseDepthPeeling(1);
154   ren2->SetOcclusionRatio(0.0);
155   ren2->SetMaximumNumberOfPeels(5);
156   ren2->SetUseDepthPeelingForVolumes(true);
157 
158   ren2->AddVolume(volume);
159   ren2->AddActor(outlineActor);
160   ren2->AddActor(sphereActor);
161   renWin->AddRenderer(ren2);
162 
163   ren->ResetCamera();
164 
165   renWin->Render();
166   ren->ResetCamera();
167 
168   iren->Initialize();
169 
170   int retVal = vtkRegressionTestImage( renWin );
171   if( retVal == vtkRegressionTester::DO_INTERACTOR)
172   {
173     iren->Start();
174   }
175 
176   return !retVal;
177 }
178