32 #ifndef __vtkOpenGLContextDevice2DPrivate_h
33 #define __vtkOpenGLContextDevice2DPrivate_h
37 #include "vtkFreeTypeTools.h"
71 : std::pair<Key,
CacheData>(key, cacheData){}
78 return this->first == other.first;
97 return std::find(this->
Cache.begin(), this->
Cache.end(), key) != this->
Cache.end();
112 typename std::list<CacheElement >::iterator it;
113 for (it = this->
Cache.begin(); it != this->
Cache.end(); ++it)
115 it->second.Texture->ReleaseGraphicsResources(window);
129 this->
Cache.pop_back();
131 this->
Cache.push_front(CacheElement(key, cacheData));
132 return this->
Cache.begin()->second;
149 typename std::list<CacheElement>::iterator it =
150 std::find(this->Cache.begin(), this->Cache.end(),
CacheElement(key));
151 if (it != this->Cache.end())
159 return this->AddCacheData(key, cacheData);
172 vtkFreeTypeTools::GetInstance()->MapTextPropertyToId(textProperty, &
id);
271 bool colorDiffer =
false;
272 while(!colorDiffer && i < 4)
290 glEnable(capability);
294 glDisable(capability);
300 float* texCoord =
new float[2*n];
301 float minX = f[0];
float minY = f[1];
302 float maxX = f[0];
float maxY = f[1];
304 for(
int i = 0; i < n; ++i)
306 minX = fptr[0] < minX ? fptr[0] : minX;
307 maxX = fptr[0] > maxX ? fptr[0] : maxX;
308 minY = fptr[1] < minY ? fptr[1] : minY;
309 maxY = fptr[1] > maxY ? fptr[1] : maxY;
315 double* textureBounds = this->
Texture->GetInput()->GetBounds();
316 float rangeX = (textureBounds[1] - textureBounds[0]) ?
317 textureBounds[1] - textureBounds[0] : 1.;
318 float rangeY = (textureBounds[3] - textureBounds[2]) ?
319 textureBounds[3] - textureBounds[2] : 1.;
320 for (
int i = 0; i < n; ++i)
322 texCoord[i*2] = (fptr[0]-minX) / rangeX;
323 texCoord[i*2+1] = (fptr[1]-minY) / rangeY;
329 float rangeX = (maxX - minX)? maxX - minX : 1.f;
330 float rangeY = (maxY - minY)? maxY - minY : 1.f;
331 for (
int i = 0; i < n; ++i)
333 texCoord[i*2] = (fptr[0]-minX)/rangeX;
334 texCoord[i*2+1] = (fptr[1]-minY)/rangeY;
344 for (
int i = 0; i < 2; ++i)
346 while (pow2[i] < size[i])
358 cout <<
"Error = not an unsigned char..." << endl;
366 for (
int i = 0; i < 2; ++i)
368 texCoords[i] = size[i] / float(newImg[i]);
371 unsigned char *dataPtr =
372 new unsigned char[newImg[0] * newImg[1] * bytesPerPixel];
373 unsigned char *origPtr =
376 for (
int i = 0; i < newImg[0]; ++i)
378 for (
int j = 0; j < newImg[1]; ++j)
380 for (
int k = 0; k < bytesPerPixel; ++k)
382 if (i < size[0] && j < size[1])
384 dataPtr[i * newImg[0] * bytesPerPixel + j * bytesPerPixel + k] =
385 origPtr[i * size[0] * bytesPerPixel + j * bytesPerPixel + k];
389 dataPtr[i * newImg[0] * bytesPerPixel + j * bytesPerPixel + k] =
397 GLint glFormat = bytesPerPixel == 3 ? GL_RGB : GL_RGBA;
398 GLint glInternalFormat = bytesPerPixel == 3 ? GL_RGB8 : GL_RGBA8;
400 glGenTextures(1, &tmpIndex);
401 glBindTexture(GL_TEXTURE_2D, tmpIndex);
403 glTexEnvf(GL_TEXTURE_ENV, vtkgl::COMBINE_RGB, GL_REPLACE);
404 glTexEnvf(GL_TEXTURE_ENV, vtkgl::COMBINE_ALPHA, GL_REPLACE);
406 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
407 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
408 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
409 vtkgl::CLAMP_TO_EDGE );
410 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
411 vtkgl::CLAMP_TO_EDGE );
413 glTexImage2D(GL_TEXTURE_2D, 0 , glInternalFormat,
414 newImg[0], newImg[1], 0, glFormat,
415 GL_UNSIGNED_BYTE, static_cast<const GLvoid *>(dataPtr));
416 glAlphaFunc(GL_GREATER, static_cast<GLclampf>(0));
417 glEnable(GL_ALPHA_TEST);
418 glMatrixMode(GL_TEXTURE);
420 glMatrixMode(GL_MODELVIEW);
421 glEnable(GL_TEXTURE_2D);
430 cout <<
"Error = not an unsigned char..." << endl;
437 unsigned char *dataPtr =
440 GLint glFormat = bytesPerPixel == 3 ? GL_RGB : GL_RGBA;
441 GLint glInternalFormat = bytesPerPixel == 3 ? GL_RGB8 : GL_RGBA8;
443 glGenTextures(1, &tmpIndex);
444 glBindTexture(GL_TEXTURE_2D, tmpIndex);
446 glTexEnvf(GL_TEXTURE_ENV, vtkgl::COMBINE_RGB, GL_REPLACE);
447 glTexEnvf(GL_TEXTURE_ENV, vtkgl::COMBINE_ALPHA, GL_REPLACE);
449 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
450 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
451 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
452 vtkgl::CLAMP_TO_EDGE );
453 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
454 vtkgl::CLAMP_TO_EDGE );
456 glTexImage2D(GL_TEXTURE_2D, 0 , glInternalFormat,
457 size[0], size[1], 0, glFormat,
458 GL_UNSIGNED_BYTE, static_cast<const GLvoid *>(dataPtr));
459 glAlphaFunc(GL_GREATER, static_cast<GLclampf>(0));
460 glEnable(GL_ALPHA_TEST);
461 glMatrixMode(GL_TEXTURE);
463 glMatrixMode(GL_MODELVIEW);
464 glEnable(GL_TEXTURE_2D);
496 #endif // VTKOPENGLCONTEXTDEVICE2DPRIVATE_H
Wrapper around vtkstd::string to keep symbols short.
GLuint TextureFromImage(vtkImageData *image)
bool operator==(const CacheElement &other) const
void SetGLCapability(GLenum capability, GLboolean state)
bool operator==(const TextPropertyKey &other) const
GLfloat SavedClearColor[4]
static unsigned long GetIdFromTextProperty(vtkTextProperty *textProperty)
GLuint TextureFromImage(vtkImageData *image, vtkVector2f &texCoords)
bool IsKeyInCache(const Key &key) const
int GetNumberOfScalarComponents()
TextPropertyKey(vtkTextProperty *textProperty, const vtkStdString &text)
std::list< CacheElement > Cache
static vtkSmartPointer< T > New()
CacheElement(const Key &key, const CacheData &cacheData)
void ReleaseGraphicsResources(vtkWindow *window)
vtkSmartPointer< vtkTexture > Texture
float * TexCoords(float *f, int n)
window superclass for vtkRenderWindow
virtual int * GetDimensions()
topologically and geometrically regular array of data
vtkTexture * SpriteTexture
CacheData & GetCacheData(const Key &key)
CacheElement(const Key &key)
#define VTK_UNSIGNED_CHAR
virtual void * GetScalarPointer(int coordinates[3])
unsigned int TextureProperties
vtkVector2i FindPowerOfTwo(const vtkVector2i &size)
CacheData & AddCacheData(const Key &key, const CacheData &cacheData)
unsigned long TextPropertyId
vtkSmartPointer< vtkImageData > ImageData
vtkTextureImageCache< TextPropertyKey > TextTextureCache
GLboolean SavedStencilTest
void RestoreGLState(bool colorBuffer=false)
void SaveGLState(bool colorBuffer=false)