VTK
vtkOpenGLVolumeOpacityTable.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkOpenGLVolumeOpacityTable.h
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 #ifndef vtkOpenGLVolumeOpacityTable_h
17 #define vtkOpenGLVolumeOpacityTable_h
18 
19 #include <vtkPiecewiseFunction.h>
20 #include <vtkTextureObject.h>
21 #include <vtkVolumeMapper.h>
22 
23 #include <vtk_glew.h>
24 
25 //----------------------------------------------------------------------------
27 {
28 public:
29  //--------------------------------------------------------------------------
30  vtkOpenGLVolumeOpacityTable(int width = 1024)
31  {
32  this->TextureObject = 0;
34  this->TextureWidth = width;
35  this->LastSampleDistance = 1.0;
36  this->Table = 0;
37  this->LastInterpolation = -1;
38  this->LastRange[0] = this->LastRange[1] = 0.0;
39  }
40 
41  //--------------------------------------------------------------------------
43  {
44  if (this->TextureObject)
45  {
46  this->TextureObject->Delete();
47  this->TextureObject = 0;
48  }
49 
50  delete[] this->Table;
51  }
52 
53  // Activate texture.
54  //--------------------------------------------------------------------------
55  void Activate()
56  {
57  if (!this->TextureObject)
58  {
59  return;
60  }
61  this->TextureObject->Activate();
62  }
63 
64  // Deactivate texture.
65  //--------------------------------------------------------------------------
66  void Deactivate()
67  {
68  if (!this->TextureObject)
69  {
70  return;
71  }
72  this->TextureObject->Deactivate();
73  }
74 
75  // Update opacity tranfer function texture.
76  //--------------------------------------------------------------------------
77  void Update(vtkPiecewiseFunction* scalarOpacity,
78  int blendMode,
79  double sampleDistance,
80  double range[2],
81  double unitDistance,
82  int filterValue,
83  vtkOpenGLRenderWindow* renWin)
84  {
85  bool needUpdate = false;
86  if (!this->TextureObject)
87  {
89  }
90 
91  this->TextureObject->SetContext(renWin);
92 
93  if (this->LastRange[0] != range[0] ||
94  this->LastRange[1] != range[1])
95  {
96  this->LastRange[0] = range[0];
97  this->LastRange[1] = range[1];
98  needUpdate = true;
99  }
100 
101  if(scalarOpacity->GetMTime() > this->BuildTime ||
102  this->TextureObject->GetMTime() > this->BuildTime ||
103  (this->LastBlendMode != blendMode) ||
104  (blendMode == vtkVolumeMapper::COMPOSITE_BLEND &&
105  this->LastSampleDistance != sampleDistance) ||
106  needUpdate || !this->TextureObject->GetHandle())
107  {
108  if(this->Table == 0)
109  {
110  this->Table = new float[this->TextureWidth];
111  }
112 
113  scalarOpacity->GetTable(this->LastRange[0],
114  this->LastRange[1],
115  this->TextureWidth,
116  this->Table);
117  this->LastBlendMode = blendMode;
118 
119  // Correct the opacity array for the spacing between the planes if we
120  // are using a composite blending operation
121  // TODO Fix this code for sample distance in three dimensions
122  if(blendMode == vtkVolumeMapper::COMPOSITE_BLEND)
123  {
124  float* ptr = this->Table;
125  double factor = sampleDistance/unitDistance;
126  int i=0;
127  while(i < this->TextureWidth)
128  {
129  if(*ptr > 0.0001f)
130  {
131  *ptr = static_cast<float>(1.0-pow(1.0-static_cast<double>(*ptr),
132  factor));
133  }
134  ++ptr;
135  ++i;
136  }
137  this->LastSampleDistance = sampleDistance;
138  }
139  else if (blendMode==vtkVolumeMapper::ADDITIVE_BLEND)
140  {
141  float* ptr = this->Table;
142  double factor = sampleDistance/unitDistance;
143  int i = 0;
144  while( i < this->TextureWidth)
145  {
146  if(*ptr > 0.0001f)
147  {
148  *ptr = static_cast<float>(static_cast<double>(*ptr)*factor);
149  }
150  ++ptr;
151  ++i;
152  }
153  this->LastSampleDistance = sampleDistance;
154  }
155 
157  this->TextureObject->SetMagnificationFilter(filterValue);
158  this->TextureObject->SetMinificationFilter(filterValue);
159  this->TextureObject->Create2DFromRaw(this->TextureWidth, 1, 1,
160  VTK_FLOAT,
161  this->Table);
162  this->LastInterpolation = filterValue;
163  this->BuildTime.Modified();
164  }
165 
166  if(this->LastInterpolation != filterValue)
167  {
168  this->LastInterpolation = filterValue;
169  this->TextureObject->SetMagnificationFilter(filterValue);
170  this->TextureObject->SetMinificationFilter(filterValue);
171  }
172  }
173 
174  // Get the texture unit
175  //--------------------------------------------------------------------------
176  int GetTextureUnit(void)
177  {
178  if (!this->TextureObject)
179  {
180  return -1;
181  }
182  return this->TextureObject->GetTextureUnit();
183  }
184 
185  //--------------------------------------------------------------------------
187  {
188  if (this->TextureObject)
189  {
191  this->TextureObject->Delete();
192  this->TextureObject = 0;
193  }
194  }
195 
196 protected:
200 
203  float *Table;
205  double LastRange[2];
206 private:
209 };
210 
211 //----------------------------------------------------------------------------
213 {
214 public:
215  //--------------------------------------------------------------------------
216  vtkOpenGLVolumeOpacityTables(unsigned int numberOfTables)
217  {
218  this->Tables = new vtkOpenGLVolumeOpacityTable[numberOfTables];
219  this->NumberOfTables = numberOfTables;
220  }
221 
222  //--------------------------------------------------------------------------
224  {
225  delete [] this->Tables;
226  }
227 
228  // brief Get opacity table at a given index.
229  //--------------------------------------------------------------------------
231  {
232  if (i >= this->NumberOfTables)
233  {
234  return NULL;
235  }
236  return &this->Tables[i];
237  }
238 
239  // Get number of opacity tables.
240  //--------------------------------------------------------------------------
241  unsigned int GetNumberOfTables()
242  {
243  return this->NumberOfTables;
244  }
245 
246  //--------------------------------------------------------------------------
248  {
249  for (unsigned int i = 0; i <this->NumberOfTables; ++i)
250  {
251  this->Tables[i].ReleaseGraphicsResources(window);
252  }
253  }
254 
255 private:
256  unsigned int NumberOfTables;
258 
259  // vtkOpenGLVolumeOpacityTables (Not implemented)
261 
262  // vtkOpenGLVolumeOpacityTables (Not implemented)
264 
265  // operator = (Not implemented)
267 };
268 
269 #endif // vtkOpenGLVolumeOpacityTable_h
270 // VTK-HeaderTest-Exclude: vtkOpenGLVolumeOpacityTable.h
OpenGL rendering window.
vtkOpenGLVolumeOpacityTables(unsigned int numberOfTables)
void ReleaseGraphicsResources(vtkWindow *window)
void SetContext(vtkRenderWindow *)
Defines a 1D piecewise function.
record modification and/or execution time
Definition: vtkTimeStamp.h:34
unsigned long int GetMTime()
void Modified()
void Deactivate(unsigned int texUnit)
vtkOpenGLVolumeOpacityTable * GetTable(unsigned int i)
void ReleaseGraphicsResources(vtkWindow *window)
bool Create2DFromRaw(unsigned int width, unsigned int height, int numComps, int dataType, void *data)
void GetTable(double x1, double x2, int size, float *table, int stride=1)
window superclass for vtkRenderWindow
Definition: vtkWindow.h:36
#define VTK_FLOAT
Definition: vtkType.h:35
virtual unsigned long GetMTime()
virtual void SetMinificationFilter(int)
void Update(vtkPiecewiseFunction *scalarOpacity, int blendMode, double sampleDistance, double range[2], double unitDistance, int filterValue, vtkOpenGLRenderWindow *renWin)
virtual unsigned int GetHandle()
void Activate(unsigned int texUnit)
abstracts an OpenGL texture object.
void ReleaseGraphicsResources(vtkWindow *win)
static vtkTextureObject * New()
virtual void SetWrapS(int)
virtual void Delete()
virtual void SetMagnificationFilter(int)