VTK
/Users/kitware/Dashboards/MyTests/VTK_BLD_Release_docs/Utilities/Doxygen/dox/Rendering/VolumeOpenGL2/vtkOpenGLVolumeRGBTable.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkOpenGLVolumeRGBTable.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 vtkOpenGLVolumeRGBTable_h_
00017 #define vtkOpenGLVolumeRGBTable_h_
00018 
00019 #include <vtkColorTransferFunction.h>
00020 #include <vtkTextureObject.h>
00021 #include <vtk_glew.h>
00022 
00023 //----------------------------------------------------------------------------
00024 class vtkOpenGLVolumeRGBTable
00025 {
00026 public:
00027   //--------------------------------------------------------------------------
00028   vtkOpenGLVolumeRGBTable()
00029     {
00030     this->TextureWidth = 1024;
00031     this->NumberOfColorComponents = 3;
00032     this->TextureObject = 0;
00033     this->LastInterpolation = -1;
00034     this->LastRange[0] = this->LastRange[1] = 0;
00035     this->Table = 0;
00036     }
00037 
00038   //--------------------------------------------------------------------------
00039   ~vtkOpenGLVolumeRGBTable()
00040     {
00041     if (this->TextureObject)
00042       {
00043       this->TextureObject->Delete();
00044       this->TextureObject = 0;
00045       }
00046     if(this->Table)
00047       {
00048       delete[] this->Table;
00049       this->Table=0;
00050       }
00051     }
00052 
00053   // Bind texture.
00054   //--------------------------------------------------------------------------
00055   void Bind(void)
00056     {
00057     if (!this->TextureObject)
00058       {
00059       return;
00060       }
00061     this->TextureObject->Activate();
00062     }
00063 
00064   // Update color transfer function texture.
00065   //--------------------------------------------------------------------------
00066   void Update(vtkColorTransferFunction* scalarRGB,
00067               double range[2],
00068               int filterValue,
00069               vtkOpenGLRenderWindow* renWin)
00070     {
00071     bool needUpdate = false;
00072 
00073     if (!this->TextureObject)
00074       {
00075       this->TextureObject = vtkTextureObject::New();
00076       }
00077 
00078     this->TextureObject->SetContext(renWin);
00079 
00080     if (range[0] != this->LastRange[0] || range[1] != this->LastRange[1])
00081       {
00082       this->LastRange[0] = range[0];
00083       this->LastRange[1] = range[1];
00084       needUpdate = true;
00085       }
00086 
00087     if (scalarRGB->GetMTime() > this->BuildTime ||
00088         this->TextureObject->GetMTime() > this->BuildTime ||
00089         needUpdate || !this->TextureObject->GetHandle())
00090       {
00091       // Create table if not created already
00092       if(this->Table==0)
00093         {
00094         this->Table = new float[this->TextureWidth *
00095           this->NumberOfColorComponents];
00096         }
00097 
00098       scalarRGB->GetTable(this->LastRange[0], this->LastRange[1],
00099                           this->TextureWidth, this->Table);
00100       this->TextureObject->SetWrapS(vtkTextureObject::ClampToEdge);
00101       this->TextureObject->SetMagnificationFilter(filterValue);
00102       this->TextureObject->SetMinificationFilter(filterValue);
00103       this->TextureObject->Create1DFromRaw(this->TextureWidth,
00104                                            this->NumberOfColorComponents,
00105                                            VTK_FLOAT,
00106                                            this->Table);
00107       this->LastInterpolation = filterValue;
00108       this->TextureObject->Activate();
00109       this->BuildTime.Modified();
00110       }
00111 
00112     if (this->LastInterpolation != filterValue)
00113       {
00114       this->LastInterpolation = filterValue;
00115       this->TextureObject->SetMagnificationFilter(filterValue);
00116       this->TextureObject->SetMinificationFilter(filterValue);
00117       }
00118     }
00119 
00120   // Get the texture unit
00121   //--------------------------------------------------------------------------
00122   int GetTextureUnit(void)
00123     {
00124     if (!this->TextureObject)
00125       {
00126       return -1;
00127       }
00128     return this->TextureObject->GetTextureUnit();
00129     }
00130 
00131   //--------------------------------------------------------------------------
00132   void ReleaseGraphicsResources(vtkWindow *window)
00133     {
00134     if (this->TextureObject)
00135       {
00136       this->TextureObject->ReleaseGraphicsResources(window);
00137       this->TextureObject->Delete();
00138       this->TextureObject = 0;
00139       }
00140     }
00141 
00142 protected:
00143   int TextureWidth;
00144   int NumberOfColorComponents;
00145 
00146   vtkTextureObject* TextureObject;
00147 
00148   int LastInterpolation;
00149   double LastRange[2];
00150   float* Table;
00151   vtkTimeStamp BuildTime;
00152 };
00153 
00154 //----------------------------------------------------------------------------
00155 class vtkOpenGLVolumeRGBTables
00156 {
00157 public:
00158   //--------------------------------------------------------------------------
00159   vtkOpenGLVolumeRGBTables(unsigned int numberOfTables)
00160     {
00161     this->Tables = new vtkOpenGLVolumeRGBTable[numberOfTables];
00162     this->NumberOfTables = numberOfTables;
00163     }
00164 
00165   //--------------------------------------------------------------------------
00166   ~vtkOpenGLVolumeRGBTables()
00167     {
00168     delete [] this->Tables;
00169     }
00170 
00171   // brief Get opacity table at a given index.
00172   //--------------------------------------------------------------------------
00173   vtkOpenGLVolumeRGBTable* GetTable(unsigned int i)
00174     {
00175     if (i >= this->NumberOfTables)
00176       {
00177       return NULL;
00178       }
00179     return &this->Tables[i];
00180     }
00181 
00182   // Get number of opacity tables.
00183   //--------------------------------------------------------------------------
00184   unsigned int GetNumberOfTables()
00185     {
00186     return this->NumberOfTables;
00187     }
00188 
00189   //--------------------------------------------------------------------------
00190   void ReleaseGraphicsResources(vtkWindow *window)
00191     {
00192     for (unsigned int i = 0; i <this->NumberOfTables; ++i)
00193       {
00194       this->Tables[i].ReleaseGraphicsResources(window);
00195       }
00196     }
00197 
00198 private:
00199   unsigned int NumberOfTables;
00200   vtkOpenGLVolumeRGBTable* Tables;
00201 
00202   // vtkOpenGLVolumeRGBTables (Not implemented)
00203   vtkOpenGLVolumeRGBTables();
00204 
00205   // vtkOpenGLVolumeRGBTables (Not implemented)
00206   vtkOpenGLVolumeRGBTables(const vtkOpenGLVolumeRGBTables &other);
00207 
00208   // operator = (Not implemented)
00209   vtkOpenGLVolumeRGBTables &operator=(const vtkOpenGLVolumeRGBTables &other);
00210 };
00211 
00212 #endif // vtkOpenGLVolumeRGBTable_h_
00213 // VTK-HeaderTest-Exclude: vtkOpenGLVolumeRGBTable.h