VTK
/Users/kitware/Dashboards/MyTests/VTK_BLD_Release_docs/Utilities/Doxygen/dox/Rendering/VolumeOpenGLNew/vtkOpenGLGradientOpacityTable.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkOpenGLGradientOpacityTable.h
00005 
00006   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007   All rights reserved.
00008   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00015 
00016 #ifndef vtkOpenGLGradientOpacityTable_h_
00017 #define vtkOpenGLGradientOpacityTable_h_
00018 
00019 #include <vtkPiecewiseFunction.h>
00020 #include <vtkVolumeMapper.h>
00021 
00022 #include <vtk_glew.h>
00023 
00024 //----------------------------------------------------------------------------
00025 class vtkOpenGLGradientOpacityTable
00026 {
00027 public:
00028   //--------------------------------------------------------------------------
00029   vtkOpenGLGradientOpacityTable(int width = 1024)
00030     {
00031       this->TextureId = 0;
00032       this->TextureWidth = width;
00033       this->TextureHeight = 0;
00034       this->LastSampleDistance = 1.0;
00035       this->Table = 0;
00036       this->Loaded = false;
00037       this->LastLinearInterpolation = false;
00038       this->LastRange[0] = this->LastRange[1] = 0.0;
00039     }
00040 
00041   //--------------------------------------------------------------------------
00042   ~vtkOpenGLGradientOpacityTable()
00043     {
00044       if (this->TextureId != 0)
00045         {
00046         glDeleteTextures(1, &this->TextureId);
00047         this->TextureId=0;
00048         }
00049 
00050       if (this->Table!=0)
00051         {
00052         delete[] this->Table;
00053         this->Table=0;
00054         }
00055     }
00056 
00057 
00058   // Check if opacity transfer function texture is loaded.
00059   //--------------------------------------------------------------------------
00060   bool IsLoaded()
00061     {
00062     return this->Loaded;
00063     }
00064 
00065   // Bind texture.
00066   //--------------------------------------------------------------------------
00067   void Bind()
00068     {
00069     // Activate texture 5
00070     glActiveTexture(GL_TEXTURE5);
00071     glBindTexture(GL_TEXTURE_1D, this->TextureId);
00072     }
00073 
00074   // Update opacity tranfer function texture.
00075   //--------------------------------------------------------------------------
00076   void Update(vtkPiecewiseFunction* gradientOpacity,
00077               double sampleDistance,
00078               double range[2],
00079               double vtkNotUsed(unitDistance),
00080               bool linearInterpolation)
00081     {
00082     // Activate texture 5
00083     glActiveTexture(GL_TEXTURE5);
00084 
00085     bool needUpdate=false;
00086     if(this->TextureId == 0)
00087       {
00088       glGenTextures(1,&this->TextureId);
00089       needUpdate = true;
00090       }
00091 
00092     if (this->LastRange[0] != range[0] ||
00093         this->LastRange[1] != range[1])
00094       {
00095       needUpdate = true;
00096       this->LastRange[0] = range[0];
00097       this->LastRange[1] = range[1];
00098       }
00099 
00100     glBindTexture(GL_TEXTURE_1D, this->TextureId);
00101     if(needUpdate)
00102       {
00103       glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S,
00104                       GL_CLAMP_TO_EDGE);
00105       }
00106 
00107     if(gradientOpacity->GetMTime() > this->BuildTime ||
00108        this->LastSampleDistance != sampleDistance ||
00109        needUpdate || !this->Loaded)
00110       {
00111       this->Loaded = false;
00112       if(this->Table == 0)
00113         {
00114         this->Table = new float[this->TextureWidth];
00115         }
00116 
00117       gradientOpacity->GetTable(0, (range[1] - range[0]) * 0.25,
00118         this->TextureWidth, this->Table);
00119 
00120       glTexImage1D(GL_TEXTURE_1D, 0, GL_ALPHA16, this->TextureWidth,
00121                    this->TextureHeight, GL_ALPHA, GL_FLOAT, this->Table);
00122       this->Loaded = true;
00123       this->BuildTime.Modified();
00124       }
00125 
00126     needUpdate= needUpdate ||
00127       this->LastLinearInterpolation!=linearInterpolation;
00128     if(needUpdate)
00129       {
00130       this->LastLinearInterpolation = linearInterpolation;
00131       GLint value = linearInterpolation ? GL_LINEAR : GL_NEAREST;
00132       glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, value);
00133       glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, value);
00134       }
00135 
00136     glActiveTexture(GL_TEXTURE0);
00137     }
00138 
00139 protected:
00140   GLuint TextureId;
00141   int TextureWidth;
00142   int TextureHeight;
00143 
00144   double LastSampleDistance;
00145   vtkTimeStamp BuildTime;
00146   float* Table;
00147   bool Loaded;
00148   bool LastLinearInterpolation;
00149   double LastRange[2];
00150 private:
00151   vtkOpenGLGradientOpacityTable(const vtkOpenGLGradientOpacityTable&);
00152   vtkOpenGLGradientOpacityTable& operator=(const vtkOpenGLGradientOpacityTable&);
00153 };
00154 
00155 //-----------------------------------------------------------------------------
00156 class vtkOpenGLGradientOpacityTables
00157 {
00158 public:
00159   //--------------------------------------------------------------------------
00160   vtkOpenGLGradientOpacityTables(unsigned int numberOfTables)
00161     {
00162     this->Tables = new vtkOpenGLGradientOpacityTable[numberOfTables];
00163     this->NumberOfTables = numberOfTables;
00164     }
00165 
00166   //--------------------------------------------------------------------------
00167   ~vtkOpenGLGradientOpacityTables()
00168     {
00169     delete [] this->Tables;
00170     }
00171 
00172   // Get opacity table at a given index.
00173   //--------------------------------------------------------------------------
00174   vtkOpenGLGradientOpacityTable* GetTable(unsigned int i)
00175     {
00176     return &this->Tables[i];
00177     }
00178 
00179   // Get number of tables.
00180   //--------------------------------------------------------------------------
00181   unsigned int GetNumberOfTables()
00182     {
00183     return this->NumberOfTables;
00184     }
00185 
00186 private:
00187   unsigned int NumberOfTables;
00188   vtkOpenGLGradientOpacityTable* Tables;
00189 
00190   // vtkOpenGLGradientOpacityTables (Not implemented)
00191   vtkOpenGLGradientOpacityTables();
00192 
00193   // vtkOpenGLGradientOpacityTables (Not implemented)
00194   vtkOpenGLGradientOpacityTables(const vtkOpenGLGradientOpacityTables &other);
00195 
00196   // operator = (Not implemented)
00197   vtkOpenGLGradientOpacityTables &operator=(const vtkOpenGLGradientOpacityTables &other);
00198 };
00199 
00200 #endif // vtkOpenGLGradientOpacityTable_h_
00201 // VTK-HeaderTest-Exclude: vtkOpenGLGradientOpacityTable.h