VTK
vtkOpenGLGradientOpacityTable.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkOpenGLGradientOpacityTable.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 vtkOpenGLGradientOpacityTable_h_
17 #define vtkOpenGLGradientOpacityTable_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;
32  this->TextureWidth = width;
33  this->TextureHeight = 0;
34  this->LastSampleDistance = 1.0;
35  this->Table = 0;
36  this->Loaded = false;
37  this->LastLinearInterpolation = false;
38  this->LastRange[0] = this->LastRange[1] = 0.0;
39  }
40 
41  //--------------------------------------------------------------------------
43  {
44  if (this->TextureId != 0)
45  {
46  glDeleteTextures(1, &this->TextureId);
47  this->TextureId=0;
48  }
49 
50  if (this->Table!=0)
51  {
52  delete[] this->Table;
53  this->Table=0;
54  }
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 5
70  glActiveTexture(GL_TEXTURE5);
71  glBindTexture(GL_TEXTURE_1D, this->TextureId);
72  }
73 
74  // Update opacity tranfer function texture.
75  //--------------------------------------------------------------------------
76  void Update(vtkPiecewiseFunction* gradientOpacity,
77  double sampleDistance,
78  double range[2],
79  double vtkNotUsed(unitDistance),
80  bool linearInterpolation)
81  {
82  // Activate texture 5
83  glActiveTexture(GL_TEXTURE5);
84 
85  bool needUpdate=false;
86  if(this->TextureId == 0)
87  {
88  glGenTextures(1,&this->TextureId);
89  needUpdate = true;
90  }
91 
92  if (this->LastRange[0] != range[0] ||
93  this->LastRange[1] != range[1])
94  {
95  needUpdate = true;
96  this->LastRange[0] = range[0];
97  this->LastRange[1] = range[1];
98  }
99 
100  glBindTexture(GL_TEXTURE_1D, this->TextureId);
101  if(needUpdate)
102  {
103  glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S,
104  GL_CLAMP_TO_EDGE);
105  }
106 
107  if(gradientOpacity->GetMTime() > this->BuildTime ||
108  this->LastSampleDistance != sampleDistance ||
109  needUpdate || !this->Loaded)
110  {
111  this->Loaded = false;
112  if(this->Table == 0)
113  {
114  this->Table = new float[this->TextureWidth];
115  }
116 
117  gradientOpacity->GetTable(0, (range[1] - range[0]) * 0.25,
118  this->TextureWidth, this->Table);
119 
120  glTexImage1D(GL_TEXTURE_1D, 0, GL_ALPHA16, this->TextureWidth,
121  this->TextureHeight, GL_ALPHA, GL_FLOAT, this->Table);
122  this->Loaded = true;
123  this->BuildTime.Modified();
124  }
125 
126  needUpdate= needUpdate ||
127  this->LastLinearInterpolation!=linearInterpolation;
128  if(needUpdate)
129  {
130  this->LastLinearInterpolation = linearInterpolation;
131  GLint value = linearInterpolation ? GL_LINEAR : GL_NEAREST;
132  glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, value);
133  glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, value);
134  }
135 
136  glActiveTexture(GL_TEXTURE0);
137  }
138 
139 protected:
143 
146  float* Table;
147  bool Loaded;
149  double LastRange[2];
150 private:
153 };
154 
155 //-----------------------------------------------------------------------------
157 {
158 public:
159  //--------------------------------------------------------------------------
160  vtkOpenGLGradientOpacityTables(unsigned int numberOfTables)
161  {
162  this->Tables = new vtkOpenGLGradientOpacityTable[numberOfTables];
163  this->NumberOfTables = numberOfTables;
164  }
165 
166  //--------------------------------------------------------------------------
168  {
169  delete [] this->Tables;
170  }
171 
172  // Get opacity table at a given index.
173  //--------------------------------------------------------------------------
175  {
176  return &this->Tables[i];
177  }
178 
179  // Get number of tables.
180  //--------------------------------------------------------------------------
181  unsigned int GetNumberOfTables()
182  {
183  return this->NumberOfTables;
184  }
185 
186 private:
187  unsigned int NumberOfTables;
189 
190  // vtkOpenGLGradientOpacityTables (Not implemented)
192 
193  // vtkOpenGLGradientOpacityTables (Not implemented)
195 
196  // operator = (Not implemented)
198 };
199 
200 #endif // vtkOpenGLGradientOpacityTable_h_
201 // VTK-HeaderTest-Exclude: vtkOpenGLGradientOpacityTable.h
vtkOpenGLGradientOpacityTable * GetTable(unsigned int i)
#define vtkNotUsed(x)
Definition: vtkSetGet.h:547
vtkOpenGLGradientOpacityTables(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()
GLsizei const GLfloat * value
Definition: vtkgl.h:12021
void Update(vtkPiecewiseFunction *gradientOpacity, double sampleDistance, double range[2], double vtkNotUsed(unitDistance), bool linearInterpolation)
void GetTable(double x1, double x2, int size, float *table, int stride=1)
GLint GLint GLsizei width
Definition: vtkgl.h:11316
typedef GLint(APIENTRYP PFNGLGETATTRIBLOCATIONPROC)(GLuint program
GLenum GLint * range
Definition: vtkgl.h:14180