VTK
/Users/kitware/Dashboards/MyTests/VTK_BLD_Release_docs/Utilities/Doxygen/dox/Rendering/VolumeOpenGL2/vtkOpenGLVolumeOpacityTable.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkOpenGLVolumeOpacityTable.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 vtkOpenGLVolumeOpacityTable_h_
00017 #define vtkOpenGLVolumeOpacityTable_h_
00018 
00019 #include <vtkPiecewiseFunction.h>
00020 #include <vtkTextureObject.h>
00021 #include <vtkVolumeMapper.h>
00022 
00023 #include <vtk_glew.h>
00024 
00025 //----------------------------------------------------------------------------
00026 class vtkOpenGLVolumeOpacityTable
00027 {
00028 public:
00029   //--------------------------------------------------------------------------
00030   vtkOpenGLVolumeOpacityTable(int width = 1024)
00031     {
00032       this->TextureObject = 0;
00033       this->LastBlendMode = vtkVolumeMapper::MAXIMUM_INTENSITY_BLEND;
00034       this->TextureWidth = width;
00035       this->LastSampleDistance = 1.0;
00036       this->Table = 0;
00037       this->LastInterpolation = -1;
00038       this->LastRange[0] = this->LastRange[1] = 0.0;
00039     }
00040 
00041   //--------------------------------------------------------------------------
00042   ~vtkOpenGLVolumeOpacityTable()
00043     {
00044       if (this->TextureObject)
00045         {
00046         this->TextureObject->Delete();
00047         this->TextureObject = 0;
00048         }
00049 
00050       if (this->Table)
00051         {
00052         delete[] this->Table;
00053         this->Table=0;
00054         }
00055     }
00056 
00057   // Bind texture.
00058   //--------------------------------------------------------------------------
00059   void Bind()
00060     {
00061     if (!this->TextureObject)
00062       {
00063       return;
00064       }
00065     this->TextureObject->Activate();
00066     }
00067 
00068   // Update opacity tranfer function texture.
00069   //--------------------------------------------------------------------------
00070   void Update(vtkPiecewiseFunction* scalarOpacity,
00071               int blendMode,
00072               double sampleDistance,
00073               double range[2],
00074               double unitDistance,
00075               int filterValue,
00076               vtkOpenGLRenderWindow* renWin)
00077     {
00078     bool needUpdate = false;
00079     if (!this->TextureObject)
00080       {
00081       this->TextureObject = vtkTextureObject::New();
00082       }
00083 
00084     this->TextureObject->SetContext(renWin);
00085 
00086     if (this->LastRange[0] != range[0] ||
00087         this->LastRange[1] != range[1])
00088       {
00089       this->LastRange[0] = range[0];
00090       this->LastRange[1] = range[1];
00091       needUpdate = true;
00092       }
00093 
00094     if(scalarOpacity->GetMTime() > this->BuildTime ||
00095        this->TextureObject->GetMTime() > this->BuildTime ||
00096        (this->LastBlendMode != blendMode) ||
00097        (blendMode == vtkVolumeMapper::COMPOSITE_BLEND &&
00098         this->LastSampleDistance != sampleDistance) ||
00099        needUpdate || !this->TextureObject->GetHandle())
00100       {
00101       if(this->Table == 0)
00102         {
00103         this->Table = new float[this->TextureWidth];
00104         }
00105 
00106       scalarOpacity->GetTable(this->LastRange[0],
00107                               this->LastRange[1],
00108                               this->TextureWidth,
00109                               this->Table);
00110       this->LastBlendMode = blendMode;
00111 
00112       // Correct the opacity array for the spacing between the planes if we
00113       // are using a composite blending operation
00114       // TODO Fix this code for sample distance in three dimensions
00115         if(blendMode == vtkVolumeMapper::COMPOSITE_BLEND)
00116           {
00117           float* ptr = this->Table;
00118           double factor = sampleDistance/unitDistance;
00119           int i=0;
00120           while(i < this->TextureWidth)
00121             {
00122             if(*ptr > 0.0001f)
00123               {
00124               *ptr = static_cast<float>(1.0-pow(1.0-static_cast<double>(*ptr),
00125                                         factor));
00126               }
00127             ++ptr;
00128             ++i;
00129             }
00130           this->LastSampleDistance = sampleDistance;
00131           }
00132         else if (blendMode==vtkVolumeMapper::ADDITIVE_BLEND)
00133           {
00134           float* ptr = this->Table;
00135           double factor = sampleDistance/unitDistance;
00136           int i = 0;
00137           while( i < this->TextureWidth)
00138             {
00139             if(*ptr > 0.0001f)
00140               {
00141               *ptr = static_cast<float>(static_cast<double>(*ptr)*factor);
00142               }
00143             ++ptr;
00144             ++i;
00145             }
00146           this->LastSampleDistance = sampleDistance;
00147           }
00148 
00149       this->TextureObject->SetWrapS(vtkTextureObject::ClampToEdge);
00150       this->TextureObject->SetMagnificationFilter(filterValue);
00151       this->TextureObject->SetMinificationFilter(filterValue);
00152       this->TextureObject->CreateAlphaFromRaw(this->TextureWidth,
00153                                               vtkTextureObject::alpha16,
00154                                               VTK_FLOAT,
00155                                               this->Table);
00156       this->LastInterpolation = filterValue;
00157       this->TextureObject->Activate();
00158       this->BuildTime.Modified();
00159       }
00160 
00161     if(this->LastInterpolation != filterValue)
00162       {
00163       this->LastInterpolation = filterValue;
00164       this->TextureObject->SetMagnificationFilter(filterValue);
00165       this->TextureObject->SetMinificationFilter(filterValue);
00166       }
00167     }
00168 
00169   // Get the texture unit
00170   //--------------------------------------------------------------------------
00171   int GetTextureUnit(void)
00172     {
00173     if (!this->TextureObject)
00174       {
00175       return -1;
00176       }
00177     return this->TextureObject->GetTextureUnit();
00178     }
00179 
00180   //--------------------------------------------------------------------------
00181   void ReleaseGraphicsResources(vtkWindow *window)
00182     {
00183     if (this->TextureObject)
00184       {
00185       this->TextureObject->ReleaseGraphicsResources(window);
00186       this->TextureObject->Delete();
00187       this->TextureObject = 0;
00188       }
00189     }
00190 
00191 protected:
00192   vtkTextureObject * TextureObject;
00193   int LastBlendMode;
00194   int TextureWidth;
00195 
00196   double LastSampleDistance;
00197   vtkTimeStamp BuildTime;
00198   float *Table;
00199   int LastInterpolation;
00200   double LastRange[2];
00201 private:
00202   vtkOpenGLVolumeOpacityTable(const vtkOpenGLVolumeOpacityTable&);
00203   vtkOpenGLVolumeOpacityTable& operator=(const vtkOpenGLVolumeOpacityTable&);
00204 };
00205 
00206 //----------------------------------------------------------------------------
00207 class vtkOpenGLVolumeOpacityTables
00208 {
00209 public:
00210   //--------------------------------------------------------------------------
00211   vtkOpenGLVolumeOpacityTables(unsigned int numberOfTables)
00212     {
00213     this->Tables = new vtkOpenGLVolumeOpacityTable[numberOfTables];
00214     this->NumberOfTables = numberOfTables;
00215     }
00216 
00217   //--------------------------------------------------------------------------
00218   ~vtkOpenGLVolumeOpacityTables()
00219     {
00220     delete [] this->Tables;
00221     }
00222 
00223   // brief Get opacity table at a given index.
00224   //--------------------------------------------------------------------------
00225   vtkOpenGLVolumeOpacityTable* GetTable(unsigned int i)
00226     {
00227     if (i >= this->NumberOfTables)
00228       {
00229       return NULL;
00230       }
00231     return &this->Tables[i];
00232     }
00233 
00234   // Get number of opacity tables.
00235   //--------------------------------------------------------------------------
00236   unsigned int GetNumberOfTables()
00237     {
00238     return this->NumberOfTables;
00239     }
00240 
00241   //--------------------------------------------------------------------------
00242   void ReleaseGraphicsResources(vtkWindow *window)
00243     {
00244     for (unsigned int i = 0; i <this->NumberOfTables; ++i)
00245       {
00246       this->Tables[i].ReleaseGraphicsResources(window);
00247       }
00248     }
00249 
00250 private:
00251   unsigned int NumberOfTables;
00252   vtkOpenGLVolumeOpacityTable *Tables;
00253 
00254   // vtkOpenGLVolumeOpacityTables (Not implemented)
00255   vtkOpenGLVolumeOpacityTables();
00256 
00257   // vtkOpenGLVolumeOpacityTables (Not implemented)
00258   vtkOpenGLVolumeOpacityTables(const vtkOpenGLVolumeOpacityTables &other);
00259 
00260   // operator = (Not implemented)
00261   vtkOpenGLVolumeOpacityTables &operator=(const vtkOpenGLVolumeOpacityTables &other);
00262 };
00263 
00264 #endif // vtkOpenGLVolumeOpacityTable_h_
00265 // VTK-HeaderTest-Exclude: vtkOpenGLVolumeOpacityTable.h