VTK
vtkOpenGLOpacityTable.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkOpenGLOpacityTable.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 <vtkVolumeMapper.h>
21 
22 #include <vtk_glew.h>
23 
24 //----------------------------------------------------------------------------
26 {
27 public:
28  //--------------------------------------------------------------------------
30  {
31  this->TextureId = 0;
33  this->TextureWidth = width;
34  this->TextureHeight = 0;
35  this->LastSampleDistance = 1.0;
36  this->Table = 0;
37  this->Loaded = false;
38  this->LastLinearInterpolation = false;
39  this->LastRange[0] = this->LastRange[1] = 0.0;
40  }
41 
42  //--------------------------------------------------------------------------
44  {
45  if (this->TextureId != 0)
46  {
47  glDeleteTextures(1, &this->TextureId);
48  this->TextureId=0;
49  }
50 
51  if (this->Table!=0)
52  {
53  delete[] this->Table;
54  this->Table=0;
55  }
56  }
57 
58  // Check if opacity transfer function texture is loaded.
59  //--------------------------------------------------------------------------
60  bool IsLoaded()
61  {
62  return this->Loaded;
63  }
64 
65  // Bind texture.
66  //--------------------------------------------------------------------------
67  void Bind()
68  {
69  // Activate texture 2
70  glActiveTexture(GL_TEXTURE2);
71  glBindTexture(GL_TEXTURE_1D, this->TextureId);
72  }
73 
74  // Update opacity tranfer function texture.
75  //--------------------------------------------------------------------------
76  void Update(vtkPiecewiseFunction* scalarOpacity,
77  int blendMode,
78  double sampleDistance,
79  double range[2],
80  double unitDistance,
81  bool linearInterpolation)
82  {
83  // Activate texture 2
84  glActiveTexture(GL_TEXTURE2);
85 
86  bool needUpdate=false;
87  if(this->TextureId == 0)
88  {
89  glGenTextures(1,&this->TextureId);
90  needUpdate = true;
91  }
92 
93  if (this->LastRange[0] != range[0] ||
94  this->LastRange[1] != range[1])
95  {
96  needUpdate = true;
97  this->LastRange[0] = range[0];
98  this->LastRange[1] = range[1];
99  }
100 
101  glBindTexture(GL_TEXTURE_1D,this->TextureId);
102  if(needUpdate)
103  {
104  glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S,
105  GL_CLAMP_TO_EDGE);
106  }
107 
108  if(scalarOpacity->GetMTime() > this->BuildTime ||
109  (this->LastBlendMode != blendMode) ||
110  (blendMode == vtkVolumeMapper::COMPOSITE_BLEND &&
111  this->LastSampleDistance != sampleDistance) ||
112  needUpdate || !this->Loaded)
113  {
114  this->Loaded = false;
115  if(this->Table == 0)
116  {
117  this->Table = new float[this->TextureWidth];
118  }
119 
120  scalarOpacity->GetTable(range[0], range[1], this->TextureWidth, this->Table);
121  this->LastBlendMode = blendMode;
122 
123  // Correct the opacity array for the spacing between the planes if we
124  // are using a composite blending operation
125  // TODO Fix this code for sample distance in three dimensions
126  if(blendMode == vtkVolumeMapper::COMPOSITE_BLEND)
127  {
128  float* ptr = this->Table;
129  double factor = sampleDistance/unitDistance;
130  int i=0;
131  while(i < this->TextureWidth)
132  {
133  if(*ptr > 0.0001f)
134  {
135  *ptr = static_cast<float>(1.0-pow(1.0-static_cast<double>(*ptr),
136  factor));
137  }
138  ++ptr;
139  ++i;
140  }
141  this->LastSampleDistance = sampleDistance;
142  }
143  else if (blendMode==vtkVolumeMapper::ADDITIVE_BLEND)
144  {
145  float* ptr = this->Table;
146  double factor = sampleDistance/unitDistance;
147  int i = 0;
148  while( i < this->TextureWidth)
149  {
150  if(*ptr > 0.0001f)
151  {
152  *ptr = static_cast<float>(static_cast<double>(*ptr)*factor);
153  }
154  ++ptr;
155  ++i;
156  }
157  this->LastSampleDistance = sampleDistance;
158  }
159 
160  glTexImage1D(GL_TEXTURE_1D, 0, GL_ALPHA16, this->TextureWidth,
161  this->TextureHeight, GL_ALPHA, GL_FLOAT, this->Table);
162  this->Loaded = true;
163  this->BuildTime.Modified();
164  }
165 
166  needUpdate= needUpdate ||
167  this->LastLinearInterpolation!=linearInterpolation;
168  if(needUpdate)
169  {
170  this->LastLinearInterpolation = linearInterpolation;
171  GLint value = linearInterpolation ? GL_LINEAR : GL_NEAREST;
172  glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, value);
173  glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, value);
174  }
175 
176  glActiveTexture(GL_TEXTURE0);
177  }
178 
179 protected:
184 
187  float *Table;
188  bool Loaded;
190  double LastRange[2];
191 private:
193  vtkOpenGLOpacityTable& operator=(const vtkOpenGLOpacityTable&);
194 };
195 
196 //----------------------------------------------------------------------------
198 {
199 public:
200  //--------------------------------------------------------------------------
201  vtkOpenGLOpacityTables(unsigned int numberOfTables)
202  {
203  this->Tables = new vtkOpenGLOpacityTable[numberOfTables];
204  this->NumberOfTables = numberOfTables;
205  }
206 
207  //--------------------------------------------------------------------------
209  {
210  delete [] this->Tables;
211  }
212 
213  // brief Get opacity table at a given index.
214  //--------------------------------------------------------------------------
216  {
217  return &this->Tables[i];
218  }
219 
220  // Get number of opacity tables.
221  //--------------------------------------------------------------------------
222  unsigned int GetNumberOfTables()
223  {
224  return this->NumberOfTables;
225  }
226 
227 private:
228  unsigned int NumberOfTables;
229  vtkOpenGLOpacityTable *Tables;
230 
231  // vtkOpenGLOpacityTables (Not implemented)
233 
234  // vtkOpenGLOpacityTables (Not implemented)
236 
237  // operator = (Not implemented)
238  vtkOpenGLOpacityTables &operator=(const vtkOpenGLOpacityTables &other);
239 };
240 
241 #endif // vtkOpenGLVolumeOpacityTable_h_
242 // VTK-HeaderTest-Exclude: vtkOpenGLOpacityTable.h
GLclampf f
Definition: vtkgl.h:14181
vtkOpenGLOpacityTables(unsigned int numberOfTables)
Defines a 1D piecewise function.
typedef GLuint(APIENTRYP PFNGLCREATEPROGRAMPROC)(void)
record modification and/or execution time
Definition: vtkTimeStamp.h:34
unsigned long int GetMTime()
void Modified()
void Update(vtkPiecewiseFunction *scalarOpacity, int blendMode, double sampleDistance, double range[2], double unitDistance, bool linearInterpolation)
GLsizei const GLfloat * value
Definition: vtkgl.h:12021
void GetTable(double x1, double x2, int size, float *table, int stride=1)
GLint GLint GLsizei width
Definition: vtkgl.h:11316
vtkOpenGLOpacityTable(int width=1024)
typedef GLint(APIENTRYP PFNGLGETATTRIBLOCATIONPROC)(GLuint program
vtkOpenGLOpacityTable * GetTable(unsigned int i)
GLenum GLint * range
Definition: vtkgl.h:14180