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  //--------------------------------------------------------------------------
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  if (this->Table)
51  {
52  delete[] this->Table;
53  this->Table=0;
54  }
55  }
56 
57  // Bind texture.
58  //--------------------------------------------------------------------------
59  void Bind()
60  {
61  if (!this->TextureObject)
62  {
63  return;
64  }
65  this->TextureObject->Activate();
66  }
67 
68  // Update opacity tranfer function texture.
69  //--------------------------------------------------------------------------
70  void Update(vtkPiecewiseFunction* scalarOpacity,
71  int blendMode,
72  double sampleDistance,
73  double range[2],
74  double unitDistance,
75  int filterValue,
76  vtkOpenGLRenderWindow* renWin)
77  {
78  bool needUpdate = false;
79  if (!this->TextureObject)
80  {
82  }
83 
84  this->TextureObject->SetContext(renWin);
85 
86  if (this->LastRange[0] != range[0] ||
87  this->LastRange[1] != range[1])
88  {
89  this->LastRange[0] = range[0];
90  this->LastRange[1] = range[1];
91  needUpdate = true;
92  }
93 
94  if(scalarOpacity->GetMTime() > this->BuildTime ||
95  this->TextureObject->GetMTime() > this->BuildTime ||
96  (this->LastBlendMode != blendMode) ||
97  (blendMode == vtkVolumeMapper::COMPOSITE_BLEND &&
98  this->LastSampleDistance != sampleDistance) ||
99  needUpdate || !this->TextureObject->GetHandle())
100  {
101  if(this->Table == 0)
102  {
103  this->Table = new float[this->TextureWidth];
104  }
105 
106  scalarOpacity->GetTable(this->LastRange[0],
107  this->LastRange[1],
108  this->TextureWidth,
109  this->Table);
110  this->LastBlendMode = blendMode;
111 
112  // Correct the opacity array for the spacing between the planes if we
113  // are using a composite blending operation
114  // TODO Fix this code for sample distance in three dimensions
115  if(blendMode == vtkVolumeMapper::COMPOSITE_BLEND)
116  {
117  float* ptr = this->Table;
118  double factor = sampleDistance/unitDistance;
119  int i=0;
120  while(i < this->TextureWidth)
121  {
122  if(*ptr > 0.0001f)
123  {
124  *ptr = static_cast<float>(1.0-pow(1.0-static_cast<double>(*ptr),
125  factor));
126  }
127  ++ptr;
128  ++i;
129  }
130  this->LastSampleDistance = sampleDistance;
131  }
132  else if (blendMode==vtkVolumeMapper::ADDITIVE_BLEND)
133  {
134  float* ptr = this->Table;
135  double factor = sampleDistance/unitDistance;
136  int i = 0;
137  while( i < this->TextureWidth)
138  {
139  if(*ptr > 0.0001f)
140  {
141  *ptr = static_cast<float>(static_cast<double>(*ptr)*factor);
142  }
143  ++ptr;
144  ++i;
145  }
146  this->LastSampleDistance = sampleDistance;
147  }
148 
150  this->TextureObject->SetMagnificationFilter(filterValue);
151  this->TextureObject->SetMinificationFilter(filterValue);
154  VTK_FLOAT,
155  this->Table);
156  this->LastInterpolation = filterValue;
157  this->TextureObject->Activate();
158  this->BuildTime.Modified();
159  }
160 
161  if(this->LastInterpolation != filterValue)
162  {
163  this->LastInterpolation = filterValue;
164  this->TextureObject->SetMagnificationFilter(filterValue);
165  this->TextureObject->SetMinificationFilter(filterValue);
166  }
167  }
168 
169  // Get the texture unit
170  //--------------------------------------------------------------------------
171  int GetTextureUnit(void)
172  {
173  if (!this->TextureObject)
174  {
175  return -1;
176  }
177  return this->TextureObject->GetTextureUnit();
178  }
179 
180  //--------------------------------------------------------------------------
182  {
183  if (this->TextureObject)
184  {
186  this->TextureObject->Delete();
187  this->TextureObject = 0;
188  }
189  }
190 
191 protected:
195 
198  float *Table;
200  double LastRange[2];
201 private:
204 };
205 
206 //----------------------------------------------------------------------------
208 {
209 public:
210  //--------------------------------------------------------------------------
211  vtkOpenGLVolumeOpacityTables(unsigned int numberOfTables)
212  {
213  this->Tables = new vtkOpenGLVolumeOpacityTable[numberOfTables];
214  this->NumberOfTables = numberOfTables;
215  }
216 
217  //--------------------------------------------------------------------------
219  {
220  delete [] this->Tables;
221  }
222 
223  // brief Get opacity table at a given index.
224  //--------------------------------------------------------------------------
226  {
227  if (i >= this->NumberOfTables)
228  {
229  return NULL;
230  }
231  return &this->Tables[i];
232  }
233 
234  // Get number of opacity tables.
235  //--------------------------------------------------------------------------
236  unsigned int GetNumberOfTables()
237  {
238  return this->NumberOfTables;
239  }
240 
241  //--------------------------------------------------------------------------
243  {
244  for (unsigned int i = 0; i <this->NumberOfTables; ++i)
245  {
246  this->Tables[i].ReleaseGraphicsResources(window);
247  }
248  }
249 
250 private:
251  unsigned int NumberOfTables;
253 
254  // vtkOpenGLVolumeOpacityTables (Not implemented)
256 
257  // vtkOpenGLVolumeOpacityTables (Not implemented)
259 
260  // operator = (Not implemented)
262 };
263 
264 #endif // vtkOpenGLVolumeOpacityTable_h_
265 // VTK-HeaderTest-Exclude: vtkOpenGLVolumeOpacityTable.h
OpenGL rendering window.
GLclampf f
Definition: vtkgl.h:14181
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()
vtkOpenGLVolumeOpacityTable * GetTable(unsigned int i)
void ReleaseGraphicsResources(vtkWindow *window)
void GetTable(double x1, double x2, int size, float *table, int stride=1)
window superclass for vtkRenderWindow
Definition: vtkWindow.h:33
#define VTK_FLOAT
Definition: vtkType.h:35
virtual unsigned long GetMTime()
virtual void SetMinificationFilter(int)
GLint GLint GLsizei width
Definition: vtkgl.h:11316
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)
bool CreateAlphaFromRaw(unsigned int width, int internalFormat, int rawType, void *raw)
GLenum GLint * range
Definition: vtkgl.h:14180
virtual void Delete()
virtual void SetMagnificationFilter(int)