18 #include <vtkImageData.h>
19 #include <vtkPointData.h>
20 #include <vtkUnsignedCharArray.h>
21 #include <vtkUnsignedShortArray.h>
22 #include <boost/cstdint.hpp>
28 #define GL_GLEXT_PROTOTYPES //Needed to compile on Linux?
32 #include <OpenGL/glu.h>
78 std::cout <<
"error: bad buffer initialization: null image" << std::endl;
100 std::cout <<
"error: bad volume buffer initialization" << std::endl;
129 GLenum size,internalType;
130 boost::uint32_t dimx =
mTexture ->GetDimensions( )[0];
131 boost::uint32_t dimy =
mTexture ->GetDimensions( )[1];
132 boost::uint32_t dimz =
mTexture ->GetDimensions( )[2];
140 glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER );
141 glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER );
142 glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER );
143 glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
144 glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
148 case VTK_UNSIGNED_CHAR:
150 size = GL_UNSIGNED_BYTE;
151 internalType = GL_RED;
154 case VTK_UNSIGNED_SHORT:
156 size = GL_UNSIGNED_SHORT;
157 internalType = GL_RED;
164 std::cout <<
"Bit size not supported!" << std::endl;
165 QString dataType(
mTexture->GetScalarTypeAsString());
166 CX_LOG_ERROR() << QString(
"Attempt to update 3D GL texture from type %1 failed. Only unsigned types supported").arg(dataType);
171 if (
mTexture->GetNumberOfScalarComponents()==1)
173 void* data =
mTexture->GetPointData()->GetScalars()->GetVoidPointer(0);
174 glTexImage3D(GL_TEXTURE_3D, 0, internalType, dimx, dimy, dimz, 0, GL_RED, size, data);
176 else if (
mTexture->GetNumberOfScalarComponents()==3)
178 internalType = GL_RGB;
179 void* data =
mTexture->GetPointData()->GetScalars()->GetVoidPointer(0);
180 glTexImage3D(GL_TEXTURE_3D, 0, internalType, dimx, dimy, dimz, 0, GL_RGB, size, data);
185 std::cout <<
"unsupported number of image components" << std::endl;
197 virtual void bind(
int textureUnitIndex)
203 std::cout <<
"error: called bind() on unallocated volume buffer" << std::endl;
217 switch (textureUnitIndex)
219 case 0:
return GL_TEXTURE0;
220 case 1:
return GL_TEXTURE2;
221 case 2:
return GL_TEXTURE4;
222 case 3:
return GL_TEXTURE6;
223 case 4:
return GL_TEXTURE8;
257 return mTable->GetNumberOfTuples() *
mTable->GetNumberOfComponents() *
sizeof(float);
276 std::cout <<
"error: bad lut buffer initialization" << std::endl;
282 glActiveTexture(GL_TEXTURE6);
291 if (mMTime ==
mTable->GetMTime())
297 glActiveTexture(GL_TEXTURE6);
304 std::vector<float> lut;
305 int lutSize =
mTable->GetNumberOfTuples();
306 int lutDataSize = lutSize *
mTable->GetNumberOfComponents();
307 lut.resize(lutDataSize);
308 unsigned char* ptr =
mTable->GetPointer(0);
310 for (
int i = 0; i < lut.size(); ++i)
312 lut[i] = ((float) *ptr) / 255.0;
317 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
318 glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
319 glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
322 glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA,
324 GL_RGBA, GL_FLOAT, &(*lut.begin()));
328 virtual void bind(
int textureUnitIndex)
333 std::cout <<
"error: called bind() on unallocated lut buffer" << std::endl;
348 return mTable->GetNumberOfTuples();
361 switch (textureUnitIndex)
363 case 0:
return GL_TEXTURE1;
364 case 1:
return GL_TEXTURE3;
365 case 2:
return GL_TEXTURE5;
366 case 3:
return GL_TEXTURE7;
367 case 4:
return GL_TEXTURE9;
376 retval->SetImage(volume);
383 retval->SetColorMap(lut);
387 template<
class BUFFER,
class DATA_PTR>
408 GPUImageBufferRepository* GPUImageBufferRepository::mInstance = NULL;
411 template<
class DATA_PTR,
class BUFFER>
446 for (
typename BufferMap::iterator iter=mRemovedData.begin(); iter!=mRemovedData.end(); )
448 if (!iter->second.lock())
450 typename BufferMap::iterator temp = iter;
452 mRemovedData.erase(temp);
461 if (mRemovedData.count(data))
463 BufferPtr object = mRemovedData[data].lock();
466 mData.push_front(BufferStore(data,
object));
467 mRemovedData.erase(data);
473 for (
typename std::list<BufferStore>::iterator iter=mData.begin(); iter!=mData.end(); ++iter)
475 if (iter->mData==data)
477 retval = iter->mBuffer;
478 mData.push_front(*iter);
487 retval = createGPUImageBuffer<BUFFER>(data);
488 mData.push_front(BufferStore(data, retval));
492 while (mData.size()>mMaxBuffers)
494 mRemovedData[mData.back().mData] = mData.back().mBuffer;
505 *textures = mData.size();
507 for (
typename std::list<BufferStore>::iterator iter=mData.begin(); iter!=mData.end(); ++iter)
509 mem += iter->mBuffer->getMemorySize();
514 typedef std::map<DATA_PTR, BufferWeakPtr> BufferMap;
515 BufferMap mRemovedData;
516 std::list<BufferStore> mData;
517 unsigned mMaxBuffers;
536 GPUImageBufferRepository::GPUImageBufferRepository()
541 GPUImageBufferRepository::~GPUImageBufferRepository()
571 void GPUImageBufferRepository::tearDown()
603 void GPUImageBufferRepository::tearDown() {;}