VTK
vtkOpenGLVolumeGradientOpacityTable.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkOpenGLVolumeGradientOpacityTable.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 vtkOpenGLVolumeGradientOpacityTable_h
17 #define vtkOpenGLVolumeGradientOpacityTable_h
18 
19 #include <vector>
20 
21 #include <vtkObjectFactory.h>
22 #include <vtkPiecewiseFunction.h>
23 #include <vtkTextureObject.h>
24 #include <vtkVolumeMapper.h>
25 #include <vtkMath.h>
26 #include <vtk_glew.h>
27 
28 
29 //----------------------------------------------------------------------------
31 {
32 public:
33 
35 
36  // activate texture.
37  //--------------------------------------------------------------------------
38  void Activate()
39  {
40  if (!this->TextureObject)
41  {
42  return;
43  }
44  this->TextureObject->Activate();
45  }
46 
47  void Deactivate()
48  {
49  if (!this->TextureObject)
50  {
51  return;
52  }
53  this->TextureObject->Deactivate();
54  }
55 
56  // Update opacity tranfer function texture.
57  //--------------------------------------------------------------------------
58  void Update(vtkPiecewiseFunction* gradientOpacity,
59  double sampleDistance,
60  double range[2],
61  double vtkNotUsed(unitDistance),
62  int filterValue,
63  vtkOpenGLRenderWindow* renWin)
64  {
65  bool needUpdate=false;
66 
67  if (!this->TextureObject)
68  {
70  }
71 
72  this->TextureObject->SetContext(renWin);
73 
74  if (this->LastRange[0] != range[0] ||
75  this->LastRange[1] != range[1])
76  {
77  this->LastRange[0] = range[0];
78  this->LastRange[1] = range[1];
79  needUpdate = true;
80  }
81 
82  if(gradientOpacity->GetMTime() > this->BuildTime ||
83  this->TextureObject->GetMTime() > this->BuildTime ||
84  this->LastSampleDistance != sampleDistance ||
85  needUpdate || !this->TextureObject->GetHandle())
86  {
87  int const idealW = gradientOpacity->EstimateMinNumberOfSamples(this->LastRange[0],
88  this->LastRange[1]);
89  int const newWidth = this->GetMaximumSupportedTextureWidth(renWin, idealW);
90 
91  if(this->Table == NULL || this->TextureWidth != newWidth)
92  {
93  this->TextureWidth = newWidth;
94  delete [] this->Table;
95  this->Table = new float[this->TextureWidth];
96  }
97 
98  gradientOpacity->GetTable(0,
99  (this->LastRange[1] - this->LastRange[0]) * 0.25,
100  this->TextureWidth, this->Table);
101 
102  this->TextureObject->Create2DFromRaw(this->TextureWidth, 1, 1,
103  VTK_FLOAT,
104  this->Table);
105 
107  this->TextureObject->SetMagnificationFilter(filterValue);
108  this->TextureObject->SetMinificationFilter(filterValue);
109  this->BuildTime.Modified();
110  }
111 
112  if(this->LastInterpolation != filterValue)
113  {
114  this->LastInterpolation = filterValue;
115  this->TextureObject->SetMagnificationFilter(filterValue);
116  this->TextureObject->SetMinificationFilter(filterValue);
117  }
118  }
119 
120  //--------------------------------------------------------------------------
122  int idealWidth)
123  {
124  if (!this->TextureObject)
125  {
126  vtkErrorMacro("vtkTextureObject not initialized!");
127  return -1;
128  }
129 
130  // Try to match the next power of two.
131  idealWidth = vtkMath::NearestPowerOfTwo(idealWidth);
132  int const maxWidth = this->TextureObject->GetMaximumTextureSize(renWin);
133  if (maxWidth < 0)
134  {
135  vtkErrorMacro("Failed to query max texture size! using default 1024.");
136  return 1024;
137  }
138 
139  if (maxWidth >= idealWidth)
140  {
141  idealWidth = vtkMath::Max(1024, idealWidth);
142  return idealWidth;
143  }
144 
145  vtkWarningMacro("This OpenGL implementation does not support the required "
146  "texture size of " << idealWidth << ", falling back to maximum allowed, "
147  << maxWidth << "." << "This may cause an incorrect color table mapping.");
148 
149  return maxWidth;
150  }
151 
152  // Get the texture unit
153  //--------------------------------------------------------------------------
154  int GetTextureUnit(void)
155  {
156  if (!this->TextureObject)
157  {
158  return -1;
159  }
160  return this->TextureObject->GetTextureUnit();
161  }
162 
163  //--------------------------------------------------------------------------
165  {
166  if (this->TextureObject)
167  {
169  this->TextureObject->Delete();
170  this->TextureObject = 0;
171  }
172  }
173 
174 protected:
175  //--------------------------------------------------------------------------
177  {
178  this->TextureObject = NULL;
179  this->TextureWidth = width;
180  this->LastSampleDistance = 1.0;
181  this->Table = NULL;
182  this->LastInterpolation = -1;
183  this->LastRange[0] = this->LastRange[1] = 0.0;
184  }
185 
186  //--------------------------------------------------------------------------
188  {
189  if (this->TextureObject)
190  {
191  this->TextureObject->Delete();
192  this->TextureObject = NULL;
193  }
194 
195  delete[] this->Table;
196  }
197 
200 
203  float* Table;
205  double LastRange[2];
206 
207 private:
209  VTK_DELETE_FUNCTION;
211  const vtkOpenGLVolumeGradientOpacityTable&) VTK_DELETE_FUNCTION;
212 };
213 
215 
216 
219 {
220 public:
221  //--------------------------------------------------------------------------
222  vtkOpenGLVolumeGradientOpacityTables(unsigned int numberOfTables)
223  {
224  this->Tables.reserve(static_cast<size_t>(numberOfTables));
225 
226  for (unsigned int i = 0; i < numberOfTables; i++)
227  {
228  vtkOpenGLVolumeGradientOpacityTable* table =
230  this->Tables.push_back(table);
231  }
232  }
233 
234  //--------------------------------------------------------------------------
236  {
237  size_t const size = this->Tables.size();
238  for (size_t i = 0; i < size; i++)
239  {
240  this->Tables[i]->Delete();
241  }
242  }
243 
244  // Get opacity table at a given index.
245  //--------------------------------------------------------------------------
246  vtkOpenGLVolumeGradientOpacityTable* GetTable(unsigned int i)
247  {
248  if (i >= this->Tables.size())
249  {
250  return NULL;
251  }
252  return this->Tables[i];
253  }
254 
255  // Get number of tables.
256  //--------------------------------------------------------------------------
258  {
259  return this->Tables.size();
260  }
261 
262  //--------------------------------------------------------------------------
264  {
265  size_t const size = this->Tables.size();
266  for (size_t i = 0; i < size; ++i)
267  {
268  this->Tables[i]->ReleaseGraphicsResources(window);
269  }
270  }
271 private:
272  std::vector<vtkOpenGLVolumeGradientOpacityTable*> Tables;
273 
274  vtkOpenGLVolumeGradientOpacityTables() VTK_DELETE_FUNCTION;
275 
276  vtkOpenGLVolumeGradientOpacityTables(const vtkOpenGLVolumeGradientOpacityTables &other) VTK_DELETE_FUNCTION;
277 
278  vtkOpenGLVolumeGradientOpacityTables &operator=(const vtkOpenGLVolumeGradientOpacityTables &other) VTK_DELETE_FUNCTION;
279 };
280 
281 #endif // vtkOpenGLVolumeGradientOpacityTable_h
282 // VTK-HeaderTest-Exclude: vtkOpenGLVolumeGradientOpacityTable.h
OpenGL rendering window.
abstract base class for most VTK objects
Definition: vtkObject.h:59
void SetContext(vtkRenderWindow *)
Get/Set the context.
vtkStandardNewMacro(vtkOpenGLVolumeGradientOpacityTable)
Defines a 1D piecewise function.
static vtkOpenGLVolumeGradientOpacityTable * New()
record modification and/or execution time
Definition: vtkTimeStamp.h:35
void Modified()
Set this objects time to the current time.
void Deactivate(unsigned int texUnit)
Set the active tex unit and bind (using our bind).
int GetMaximumSupportedTextureWidth(vtkOpenGLRenderWindow *renWin, int idealWidth)
bool Create2DFromRaw(unsigned int width, unsigned int height, int numComps, int dataType, void *data)
Create a 2D texture from client memory numComps must be in [1-4].
void GetTable(double x1, double x2, int size, float *table, int stride=1)
Fills in an array of function values evaluated at regular intervals.
vtkOpenGLVolumeGradientOpacityTables(unsigned int numberOfTables)
window superclass for vtkRenderWindow
Definition: vtkWindow.h:37
void Update(vtkPiecewiseFunction *gradientOpacity, double sampleDistance, double range[2], double vtkNotUsed(unitDistance), int filterValue, vtkOpenGLRenderWindow *renWin)
#define VTK_FLOAT
Definition: vtkType.h:58
static int NearestPowerOfTwo(int x)
Compute the nearest power of two that is not less than x.
Definition: vtkMath.h:1230
virtual void SetMinificationFilter(int)
Minification filter mode.
virtual vtkMTimeType GetMTime()
Return this object's modified time.
virtual unsigned int GetHandle()
Returns the OpenGL handle.
void Activate(unsigned int texUnit)
Set the active tex unit and bind (using our bind).
abstracts an OpenGL texture object.
int EstimateMinNumberOfSamples(double const &x1, double const &x2)
Estimates the minimum size of a table such that it would correctly sample this function.
static int GetMaximumTextureSize(vtkOpenGLRenderWindow *context)
Query and return maximum texture size (dimension) supported by the OpenGL driver for a particular con...
void ReleaseGraphicsResources(vtkWindow *win)
Deactivate and UnBind the texture.
static vtkTextureObject * New()
virtual void SetWrapS(int)
Wrap mode for the first texture coordinate "s" Valid values are:
vtkMTimeType GetMTime() override
Data objects are composite objects and need to check each part for MTime.
int GetTextureUnit()
Return the texture unit used for this texture.
vtkOpenGLVolumeGradientOpacityTable * GetTable(unsigned int i)
static T Max(const T &a, const T &b)
Returns the maximum of the two arugments provided.
Definition: vtkMath.h:1268
virtual void Delete()
Delete a VTK object.
virtual void SetMagnificationFilter(int)
Magnification filter mode.