VTK
|
00001 /*========================================================================= 00002 00003 Program: Visualization Toolkit 00004 Module: vtkOpenGLRGBTable.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 vtkOpenGLRGBTable_h_ 00017 #define vtkOpenGLRGBTable_h_ 00018 00019 #include <vtkColorTransferFunction.h> 00020 #include <vtk_glew.h> 00021 00022 //---------------------------------------------------------------------------- 00023 class vtkOpenGLRGBTable 00024 { 00025 public: 00026 //-------------------------------------------------------------------------- 00027 vtkOpenGLRGBTable() 00028 { 00029 this->Loaded = false; 00030 this->LastLinearInterpolation = false; 00031 this->TexutureWidth = 1024; 00032 this->NumberOfColorComponents = 3; 00033 this->TextureId = 0; 00034 this->LastRange[0] = this->LastRange[1] = 0; 00035 this->Table = 0; 00036 } 00037 00038 //-------------------------------------------------------------------------- 00039 ~vtkOpenGLRGBTable() 00040 { 00041 if(this->TextureId!=0) 00042 { 00043 glDeleteTextures(1,&this->TextureId); 00044 this->TextureId=0; 00045 } 00046 if(this->Table!=0) 00047 { 00048 delete[] this->Table; 00049 this->Table=0; 00050 } 00051 } 00052 00053 // Check if color transfer function texture is loaded. 00054 //-------------------------------------------------------------------------- 00055 bool IsLoaded() 00056 { 00057 return this->Loaded; 00058 } 00059 00060 // Bind texture. 00061 //-------------------------------------------------------------------------- 00062 void Bind(int textureUnit = 1) 00063 { 00064 // Activate texture 1 00065 glActiveTexture(GL_TEXTURE0 + textureUnit); 00066 glBindTexture(GL_TEXTURE_1D, this->TextureId); 00067 } 00068 00069 // Update color transfer function texture. 00070 //-------------------------------------------------------------------------- 00071 void Update(vtkColorTransferFunction* scalarRGB, 00072 double range[2], 00073 bool linearInterpolation, int textureUnit = 1) 00074 { 00075 // Activate texture 1 00076 glActiveTexture(GL_TEXTURE0 + textureUnit); 00077 00078 bool needUpdate = false; 00079 00080 if(this->TextureId == 0) 00081 { 00082 glGenTextures(1, &this->TextureId); 00083 needUpdate = true; 00084 } 00085 00086 if (range[0] != this->LastRange[0] || range[1] != this->LastRange[1]) 00087 { 00088 needUpdate=true; 00089 } 00090 00091 glBindTexture(GL_TEXTURE_1D, this->TextureId); 00092 if(needUpdate) 00093 { 00094 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, 00095 GL_CLAMP_TO_EDGE); 00096 } 00097 if(scalarRGB->GetMTime() > this->BuildTime || needUpdate || !this->Loaded) 00098 { 00099 this->Loaded = false; 00100 00101 // Create table if not created already 00102 if(this->Table==0) 00103 { 00104 this->Table = new float[this->TexutureWidth * 00105 this->NumberOfColorComponents]; 00106 } 00107 00108 scalarRGB->GetTable(range[0],range[1], this->TexutureWidth, this->Table); 00109 glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB16, this->TexutureWidth, 0, 00110 GL_RGB, GL_FLOAT, this->Table); 00111 00112 this->Loaded = true; 00113 this->BuildTime.Modified(); 00114 00115 this->LastRange[0] = range[0]; 00116 this->LastRange[1] = range[1]; 00117 } 00118 00119 needUpdate = needUpdate || 00120 this->LastLinearInterpolation!=linearInterpolation; 00121 if (needUpdate) 00122 { 00123 this->LastLinearInterpolation = linearInterpolation; 00124 GLint value; 00125 if (linearInterpolation) 00126 { 00127 value = GL_LINEAR; 00128 } 00129 else 00130 { 00131 value = GL_NEAREST; 00132 } 00133 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, value); 00134 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, value); 00135 } 00136 00137 glActiveTexture(GL_TEXTURE0); 00138 } 00139 00140 protected: 00141 00142 bool Loaded; 00143 bool LastLinearInterpolation; 00144 00145 int TexutureWidth; 00146 int NumberOfColorComponents; 00147 00148 GLuint TextureId; 00149 00150 double LastRange[2]; 00151 float* Table; 00152 vtkTimeStamp BuildTime; 00153 }; 00154 00155 #endif // vtkOpenGLRGBTable_h_ 00156 // VTK-HeaderTest-Exclude: vtkOpenGLRGBTable.h