1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkOpenGLVolumeOpacityTable.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#ifndef vtkOpenGLVolumeOpacityTable_h_
#define vtkOpenGLVolumeOpacityTable_h_
#include <vtkPiecewiseFunction.h>
#include <vtkTextureObject.h>
#include <vtkVolumeMapper.h>
#include <vtk_glew.h>
//----------------------------------------------------------------------------
class vtkOpenGLVolumeOpacityTable
{
public:
//--------------------------------------------------------------------------
vtkOpenGLVolumeOpacityTable(int width = 1024)
{
this->TextureObject = 0;
this->LastBlendMode = vtkVolumeMapper::MAXIMUM_INTENSITY_BLEND;
this->TextureWidth = width;
this->LastSampleDistance = 1.0;
this->Table = 0;
this->LastInterpolation = -1;
this->LastRange[0] = this->LastRange[1] = 0.0;
}
//--------------------------------------------------------------------------
~vtkOpenGLVolumeOpacityTable()
{
if (this->TextureObject)
{
this->TextureObject->Delete();
this->TextureObject = 0;
}
delete[] this->Table;
}
// Activate texture.
//--------------------------------------------------------------------------
void Activate()
{
if (!this->TextureObject)
{
return;
}
this->TextureObject->Activate();
}
// Deactivate texture.
//--------------------------------------------------------------------------
void Deactivate()
{
if (!this->TextureObject)
{
return;
}
this->TextureObject->Deactivate();
}
// Update opacity tranfer function texture.
//--------------------------------------------------------------------------
void Update(vtkPiecewiseFunction* scalarOpacity,
int blendMode,
double sampleDistance,
double range[2],
double unitDistance,
int filterValue,
vtkOpenGLRenderWindow* renWin)
{
bool needUpdate = false;
if (!this->TextureObject)
{
this->TextureObject = vtkTextureObject::New();
}
this->TextureObject->SetContext(renWin);
if (this->LastRange[0] != range[0] ||
this->LastRange[1] != range[1])
{
this->LastRange[0] = range[0];
this->LastRange[1] = range[1];
needUpdate = true;
}
if(scalarOpacity->GetMTime() > this->BuildTime ||
this->TextureObject->GetMTime() > this->BuildTime ||
(this->LastBlendMode != blendMode) ||
(blendMode == vtkVolumeMapper::COMPOSITE_BLEND &&
this->LastSampleDistance != sampleDistance) ||
needUpdate || !this->TextureObject->GetHandle())
{
if(this->Table == 0)
{
this->Table = new float[this->TextureWidth];
}
scalarOpacity->GetTable(this->LastRange[0],
this->LastRange[1],
this->TextureWidth,
this->Table);
this->LastBlendMode = blendMode;
// Correct the opacity array for the spacing between the planes if we
// are using a composite blending operation
// TODO Fix this code for sample distance in three dimensions
if(blendMode == vtkVolumeMapper::COMPOSITE_BLEND)
{
float* ptr = this->Table;
double factor = sampleDistance/unitDistance;
int i=0;
while(i < this->TextureWidth)
{
if(*ptr > 0.0001f)
{
*ptr = static_cast<float>(1.0-pow(1.0-static_cast<double>(*ptr),
factor));
}
++ptr;
++i;
}
this->LastSampleDistance = sampleDistance;
}
else if (blendMode==vtkVolumeMapper::ADDITIVE_BLEND)
{
float* ptr = this->Table;
double factor = sampleDistance/unitDistance;
int i = 0;
while( i < this->TextureWidth)
{
if(*ptr > 0.0001f)
{
*ptr = static_cast<float>(static_cast<double>(*ptr)*factor);
}
++ptr;
++i;
}
this->LastSampleDistance = sampleDistance;
}
this->TextureObject->SetWrapS(vtkTextureObject::ClampToEdge);
this->TextureObject->SetMagnificationFilter(filterValue);
this->TextureObject->SetMinificationFilter(filterValue);
this->TextureObject->CreateAlphaFromRaw(this->TextureWidth,
vtkTextureObject::alpha16,
VTK_FLOAT,
this->Table);
this->LastInterpolation = filterValue;
this->BuildTime.Modified();
}
if(this->LastInterpolation != filterValue)
{
this->LastInterpolation = filterValue;
this->TextureObject->SetMagnificationFilter(filterValue);
this->TextureObject->SetMinificationFilter(filterValue);
}
}
// Get the texture unit
//--------------------------------------------------------------------------
int GetTextureUnit(void)
{
if (!this->TextureObject)
{
return -1;
}
return this->TextureObject->GetTextureUnit();
}
//--------------------------------------------------------------------------
void ReleaseGraphicsResources(vtkWindow *window)
{
if (this->TextureObject)
{
this->TextureObject->ReleaseGraphicsResources(window);
this->TextureObject->Delete();
this->TextureObject = 0;
}
}
protected:
vtkTextureObject * TextureObject;
int LastBlendMode;
int TextureWidth;
double LastSampleDistance;
vtkTimeStamp BuildTime;
float *Table;
int LastInterpolation;
double LastRange[2];
private:
vtkOpenGLVolumeOpacityTable(const vtkOpenGLVolumeOpacityTable&);
vtkOpenGLVolumeOpacityTable& operator=(const vtkOpenGLVolumeOpacityTable&);
};
//----------------------------------------------------------------------------
class vtkOpenGLVolumeOpacityTables
{
public:
//--------------------------------------------------------------------------
vtkOpenGLVolumeOpacityTables(unsigned int numberOfTables)
{
this->Tables = new vtkOpenGLVolumeOpacityTable[numberOfTables];
this->NumberOfTables = numberOfTables;
}
//--------------------------------------------------------------------------
~vtkOpenGLVolumeOpacityTables()
{
delete [] this->Tables;
}
// brief Get opacity table at a given index.
//--------------------------------------------------------------------------
vtkOpenGLVolumeOpacityTable* GetTable(unsigned int i)
{
if (i >= this->NumberOfTables)
{
return NULL;
}
return &this->Tables[i];
}
// Get number of opacity tables.
//--------------------------------------------------------------------------
unsigned int GetNumberOfTables()
{
return this->NumberOfTables;
}
//--------------------------------------------------------------------------
void ReleaseGraphicsResources(vtkWindow *window)
{
for (unsigned int i = 0; i <this->NumberOfTables; ++i)
{
this->Tables[i].ReleaseGraphicsResources(window);
}
}
private:
unsigned int NumberOfTables;
vtkOpenGLVolumeOpacityTable *Tables;
// vtkOpenGLVolumeOpacityTables (Not implemented)
vtkOpenGLVolumeOpacityTables();
// vtkOpenGLVolumeOpacityTables (Not implemented)
vtkOpenGLVolumeOpacityTables(const vtkOpenGLVolumeOpacityTables &other);
// operator = (Not implemented)
vtkOpenGLVolumeOpacityTables &operator=(const vtkOpenGLVolumeOpacityTables &other);
};
#endif // vtkOpenGLVolumeOpacityTable_h_
// VTK-HeaderTest-Exclude: vtkOpenGLVolumeOpacityTable.h
|