1 /*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: TestGPURayCastAdditive1.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 // This test covers additive method.
16 // This test volume renders a synthetic dataset with unsigned char values,
17 // with the additive method.
18
19 #include "vtkCamera.h"
20 #include "vtkColorTransferFunction.h"
21 #include "vtkDataArray.h"
22 #include "vtkGPUVolumeRayCastMapper.h"
23 #include "vtkImageData.h"
24 #include "vtkImageShiftScale.h"
25 #include "vtkPiecewiseFunction.h"
26 #include "vtkPointData.h"
27 #include "vtkRegressionTestImage.h"
28 #include "vtkRenderWindow.h"
29 #include "vtkRenderWindowInteractor.h"
30 #include "vtkRenderer.h"
31 #include "vtkTestUtilities.h"
32 #include "vtkVolumeProperty.h"
33
TestGPURayCastAdditive1(int argc,char * argv[])34 int TestGPURayCastAdditive1(int argc, char *argv[])
35 {
36 cout << "CTEST_FULL_OUTPUT (Avoid ctest truncation of output)" << endl;
37
38 // Create a spherical implicit function.
39 vtkSphere *shape=vtkSphere::New();
40 shape->SetRadius(0.1);
41 shape->SetCenter(0.0,0.0,0.0);
42
43 vtkSampleFunction *source=vtkSampleFunction::New();
44 source->SetImplicitFunction(shape);
45 shape->Delete();
46 source->SetOutputScalarTypeToDouble();
47 source->SetSampleDimensions(127,127,127); // intentional NPOT dimensions.
48 source->SetModelBounds(-1.0,1.0,-1.0,1.0,-1.0,1.0);
49 source->SetCapping(false);
50 source->SetComputeNormals(false);
51 source->SetScalarArrayName("values");
52
53 source->Update();
54
55 vtkDataArray *a=source->GetOutput()->GetPointData()->GetScalars("values");
56 double range[2];
57 a->GetRange(range);
58
59 vtkImageShiftScale *t=vtkImageShiftScale::New();
60 t->SetInputConnection(source->GetOutputPort());
61 source->Delete();
62 t->SetShift(-range[0]);
63 double magnitude=range[1]-range[0];
64 if(magnitude==0.0)
65 {
66 magnitude=1.0;
67 }
68 t->SetScale(255.0/magnitude);
69 t->SetOutputScalarTypeToUnsignedChar();
70
71 t->Update();
72
73 vtkRenderWindow *renWin=vtkRenderWindow::New();
74 vtkRenderer *ren1=vtkRenderer::New();
75 ren1->SetBackground(0.1,0.4,0.2);
76
77 renWin->AddRenderer(ren1);
78 ren1->Delete();
79 renWin->SetSize(301,300); // intentional odd and NPOT width/height
80
81 vtkRenderWindowInteractor *iren=vtkRenderWindowInteractor::New();
82 iren->SetRenderWindow(renWin);
83 renWin->Delete();
84
85 renWin->Render(); // make sure we have an OpenGL context.
86
87 vtkGPUVolumeRayCastMapper *volumeMapper;
88 vtkVolumeProperty *volumeProperty;
89 vtkVolume *volume;
90
91 volumeMapper=vtkGPUVolumeRayCastMapper::New();
92 volumeMapper->SetSampleDistance(0.01);
93 volumeMapper->SetBlendModeToComposite(); // composite first
94 volumeMapper->SetInputConnection(
95 t->GetOutputPort());
96
97 volumeProperty=vtkVolumeProperty::New();
98 volumeProperty->ShadeOff();
99 volumeProperty->SetInterpolationType(VTK_LINEAR_INTERPOLATION);
100
101 vtkPiecewiseFunction *additiveOpacity = vtkPiecewiseFunction::New();
102 additiveOpacity->AddPoint(0.0,0.0);
103 additiveOpacity->AddPoint(200.0,0.5);
104 additiveOpacity->AddPoint(200.1,1.0);
105 additiveOpacity->AddPoint(255.0,1.0);
106
107 vtkPiecewiseFunction *compositeOpacity = vtkPiecewiseFunction::New();
108 compositeOpacity->AddPoint(0.0,0.0);
109 compositeOpacity->AddPoint(80.0,1.0);
110 compositeOpacity->AddPoint(80.1,0.0);
111 compositeOpacity->AddPoint(255.0,0.0);
112 volumeProperty->SetScalarOpacity(compositeOpacity); // composite first.
113
114 vtkColorTransferFunction *color=vtkColorTransferFunction::New();
115 color->AddRGBPoint(0.0 ,0.0,0.0,1.0);
116 color->AddRGBPoint(40.0 ,1.0,0.0,0.0);
117 color->AddRGBPoint(255.0,1.0,1.0,1.0);
118 volumeProperty->SetColor(color);
119 color->Delete();
120
121 volume=vtkVolume::New();
122 volume->SetMapper(volumeMapper);
123 volume->SetProperty(volumeProperty);
124 ren1->AddViewProp(volume);
125
126 //int valid=volumeMapper->IsRenderSupported(renWin,volumeProperty);
127
128 int retVal;
129 if(1)
130 {
131 ren1->ResetCamera();
132
133 // Render composite.
134 renWin->Render();
135
136 // Switch to Additive
137 volumeMapper->SetBlendModeToAdditive();
138 volumeProperty->SetScalarOpacity(additiveOpacity);
139 renWin->Render();
140
141 retVal = vtkTesting::Test(argc, argv, renWin, 75);
142 if (retVal == vtkRegressionTester::DO_INTERACTOR)
143 {
144 iren->Start();
145 }
146 }
147 else
148 {
149 retVal=vtkTesting::PASSED;
150 cout << "Required extensions not supported." << endl;
151 }
152
153 volumeMapper->Delete();
154 volumeProperty->Delete();
155 volume->Delete();
156 iren->Delete();
157 t->Delete();
158 additiveOpacity->Delete();
159 compositeOpacity->Delete();
160
161 return !((retVal == vtkTesting::PASSED) || (retVal == vtkTesting::DO_INTERACTOR));
162 }
163