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