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 <vtkPiecewiseFunction.h>
20 #include <vtkTextureObject.h>
21 #include <vtkVolumeMapper.h>
22 
23 #include <vtk_glew.h>
24 
25 //----------------------------------------------------------------------------
26 class vtkOpenGLVolumeGradientOpacityTable
27 {
28 public:
29   //--------------------------------------------------------------------------
30   vtkOpenGLVolumeGradientOpacityTable(int width = 1024)
31     {
32       this->TextureObject = 0;
33       this->TextureWidth = width;
34       this->LastSampleDistance = 1.0;
35       this->Table = 0;
36       this->LastInterpolation = -1;
37       this->LastRange[0] = this->LastRange[1] = 0.0;
38     }
39 
40   //--------------------------------------------------------------------------
~vtkOpenGLVolumeGradientOpacityTable()41   ~vtkOpenGLVolumeGradientOpacityTable()
42     {
43       if (this->TextureObject)
44         {
45         this->TextureObject->Delete();
46         this->TextureObject = 0;
47         }
48 
49       if (this->Table)
50         {
51         delete[] this->Table;
52         this->Table=0;
53         }
54     }
55 
56   // Bind texture.
57   //--------------------------------------------------------------------------
Bind()58   void Bind()
59     {
60     if (!this->TextureObject)
61       {
62       return;
63       }
64     this->TextureObject->Activate();
65     }
66 
67   // Update opacity tranfer function texture.
68   //--------------------------------------------------------------------------
Update(vtkPiecewiseFunction * gradientOpacity,double sampleDistance,double range[2],double vtkNotUsed (unitDistance),int filterValue,vtkOpenGLRenderWindow * renWin)69   void Update(vtkPiecewiseFunction* gradientOpacity,
70               double sampleDistance,
71               double range[2],
72               double vtkNotUsed(unitDistance),
73               int filterValue,
74               vtkOpenGLRenderWindow* renWin)
75     {
76     bool needUpdate=false;
77 
78     if (!this->TextureObject)
79       {
80       this->TextureObject = vtkTextureObject::New();
81       }
82 
83     this->TextureObject->SetContext(renWin);
84 
85     if (this->LastRange[0] != range[0] ||
86         this->LastRange[1] != range[1])
87       {
88       this->LastRange[0] = range[0];
89       this->LastRange[1] = range[1];
90       needUpdate = true;
91       }
92 
93     if(gradientOpacity->GetMTime() > this->BuildTime ||
94        this->TextureObject->GetMTime() > this->BuildTime ||
95        this->LastSampleDistance != sampleDistance ||
96        needUpdate || !this->TextureObject->GetHandle())
97       {
98       if(this->Table == 0)
99         {
100         this->Table = new float[this->TextureWidth];
101         }
102 
103       gradientOpacity->GetTable(0,
104                               (this->LastRange[1] - this->LastRange[0]) * 0.25,
105                               this->TextureWidth, this->Table);
106 
107       this->TextureObject->CreateAlphaFromRaw(this->TextureWidth,
108                                               vtkTextureObject::alpha16,
109                                               VTK_FLOAT,
110                                               this->Table);
111 
112       this->TextureObject->Activate();
113       this->TextureObject->SetWrapS(vtkTextureObject::ClampToEdge);
114       this->TextureObject->SetMagnificationFilter(filterValue);
115       this->TextureObject->SetMinificationFilter(filterValue);
116       this->BuildTime.Modified();
117       }
118 
119     if(this->LastInterpolation != filterValue)
120       {
121       this->LastInterpolation = filterValue;
122       this->TextureObject->SetMagnificationFilter(filterValue);
123       this->TextureObject->SetMinificationFilter(filterValue);
124       }
125     }
126 
127   // Get the texture unit
128   //--------------------------------------------------------------------------
GetTextureUnit(void)129   int GetTextureUnit(void)
130     {
131     if (!this->TextureObject)
132       {
133       return -1;
134       }
135     return this->TextureObject->GetTextureUnit();
136     }
137 
138   //--------------------------------------------------------------------------
ReleaseGraphicsResources(vtkWindow * window)139   void ReleaseGraphicsResources(vtkWindow *window)
140     {
141     if (this->TextureObject)
142       {
143       this->TextureObject->ReleaseGraphicsResources(window);
144       this->TextureObject->Delete();
145       this->TextureObject = 0;
146       }
147     }
148 
149 protected:
150 //  GLuint TextureId;
151   vtkTextureObject* TextureObject;
152   int TextureWidth;
153 
154   double LastSampleDistance;
155   vtkTimeStamp BuildTime;
156   float* Table;
157   int LastInterpolation;
158   double LastRange[2];
159 private:
160   vtkOpenGLVolumeGradientOpacityTable(const vtkOpenGLVolumeGradientOpacityTable&);
161   vtkOpenGLVolumeGradientOpacityTable& operator=(const vtkOpenGLVolumeGradientOpacityTable&);
162 };
163 
164 //-----------------------------------------------------------------------------
165 class vtkOpenGLVolumeGradientOpacityTables
166 {
167 public:
168   //--------------------------------------------------------------------------
vtkOpenGLVolumeGradientOpacityTables(unsigned int numberOfTables)169   vtkOpenGLVolumeGradientOpacityTables(unsigned int numberOfTables)
170     {
171     this->Tables = new vtkOpenGLVolumeGradientOpacityTable[numberOfTables];
172     this->NumberOfTables = numberOfTables;
173     }
174 
175   //--------------------------------------------------------------------------
~vtkOpenGLVolumeGradientOpacityTables()176   ~vtkOpenGLVolumeGradientOpacityTables()
177     {
178     delete [] this->Tables;
179     }
180 
181   // Get opacity table at a given index.
182   //--------------------------------------------------------------------------
GetTable(unsigned int i)183   vtkOpenGLVolumeGradientOpacityTable* GetTable(unsigned int i)
184     {
185     if (i >= this->NumberOfTables)
186       {
187       return NULL;
188       }
189     return &this->Tables[i];
190     }
191 
192   // Get number of tables.
193   //--------------------------------------------------------------------------
GetNumberOfTables()194   unsigned int GetNumberOfTables()
195     {
196     return this->NumberOfTables;
197     }
198 
199   //--------------------------------------------------------------------------
ReleaseGraphicsResources(vtkWindow * window)200   void ReleaseGraphicsResources(vtkWindow *window)
201     {
202     for (unsigned int i = 0; i < this->NumberOfTables; ++i)
203       {
204       this->Tables[i].ReleaseGraphicsResources(window);
205       }
206     }
207 private:
208   unsigned int NumberOfTables;
209   vtkOpenGLVolumeGradientOpacityTable* Tables;
210 
211   // vtkOpenGLVolumeGradientOpacityTables (Not implemented)
212   vtkOpenGLVolumeGradientOpacityTables();
213 
214   // vtkOpenGLVolumeGradientOpacityTables (Not implemented)
215   vtkOpenGLVolumeGradientOpacityTables(const vtkOpenGLVolumeGradientOpacityTables &other);
216 
217   // operator = (Not implemented)
218   vtkOpenGLVolumeGradientOpacityTables &operator=(const vtkOpenGLVolumeGradientOpacityTables &other);
219 };
220 
221 #endif // vtkOpenGLVolumeGradientOpacityTable_h_
222 // VTK-HeaderTest-Exclude: vtkOpenGLVolumeGradientOpacityTable.h
223