VTK
vtkOpenGLContextDevice2DPrivate.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkOpenGLContextDevice2DPrivate.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
15 
32 #ifndef vtkOpenGLContextDevice2DPrivate_h
33 #define vtkOpenGLContextDevice2DPrivate_h
34 
36 
37 #include "vtkColor.h"
38 #include "vtkTextProperty.h"
39 #include "vtkFreeTypeTools.h"
40 #include "vtkStdString.h"
41 #include "vtkUnicodeString.h"
42 
43 #include <algorithm>
44 #include <list>
45 #include <utility>
46 
47 // .NAME vtkTextureImageCache - store vtkTexture and vtkImageData identified by
48 // a unique key.
49 // .SECTION Description
50 // Creating and initializing a texture can be time consuming,
51 // vtkTextureImageCache offers the ability to reuse them as much as possible.
52 template <class Key>
54 {
55 public:
56  struct CacheData
57  {
60  // Dimensions of the text. Used for generating texture coords when the image
61  // dimensions are scaled to a power of two.
62  int TextWidth;
64  };
65 
67 
68  struct CacheElement: public std::pair<Key, CacheData>
69  {
70  // Default constructor
72  : std::pair<Key, CacheData>(Key(), CacheData()){}
73  // Construct a partial CacheElement with no CacheData
74  // This can be used for temporary CacheElement used to search a given
75  // key into the cache list.
76  CacheElement(const Key& key)
77  : std::pair<Key, CacheData>(key, CacheData()){}
78  // Standard constructor of CacheElement
79  CacheElement(const Key& key, const CacheData& cacheData)
80  : std::pair<Key, CacheData>(key, cacheData){}
81  // Operator tuned to be used when searching into the cache list using
82  // std::find()
83  bool operator==(const CacheElement& other)const
84  {
85  // Here we cheat and make the comparison only on the key, this allows
86  // us to use std::find() to search for a given key.
87  return this->first == other.first;
88  }
89  };
91 
93 
96  {
97  this->MaxSize = 50;
98  }
100 
102 
104  bool IsKeyInCache(const Key& key)const
105  {
106  return std::find(this->Cache.begin(), this->Cache.end(), key) != this->Cache.end();
107  }
109 
114  CacheData& GetCacheData(const Key& key);
115 
117 
120  {
121  typename std::list<CacheElement >::iterator it;
122  for (it = this->Cache.begin(); it != this->Cache.end(); ++it)
123  {
124  it->second.Texture->ReleaseGraphicsResources(window);
125  }
126  }
128 
129 protected:
131 
133  CacheData& AddCacheData(const Key& key, const CacheData& cacheData)
134  {
135  assert(!this->IsKeyInCache(key));
136  if (this->Cache.size() >= this->MaxSize)
137  {
138  this->Cache.pop_back();
139  }
140  this->Cache.push_front(CacheElement(key, cacheData));
141  return this->Cache.begin()->second;
142  }
144 
146  std::list<CacheElement > Cache;
148 
149  size_t MaxSize;
150 };
152 
153 template<class Key>
155 ::GetCacheData(const Key& key)
156 {
157  typename std::list<CacheElement>::iterator it =
158  std::find(this->Cache.begin(), this->Cache.end(), CacheElement(key));
159  if (it != this->Cache.end())
160  {
161  return it->second;
162  }
163  CacheData cacheData;
166  cacheData.Texture->SetInputData(cacheData.ImageData);
167  cacheData.TextWidth = 0;
168  cacheData.TextHeight = 0;
169  return this->AddCacheData(key, cacheData);
170 }
171 
172 // .NAME TextPropertyKey - unique key for a vtkTextProperty and text
173 // .SECTION Description
174 // Uniquely describe a pair of vtkTextProperty and text.
175 template <class StringType>
177 {
179 
180  static unsigned int GetIdFromTextProperty(vtkTextProperty* textProperty)
181  {
182  size_t id;
184  // Truncation on 64-bit machines! The id is a pointer.
185  return static_cast<unsigned int>(id);
186  }
188 
190 
191  TextPropertyKey(vtkTextProperty* textProperty, const StringType& text,
192  int dpi)
193  {
194  this->TextPropertyId = GetIdFromTextProperty(textProperty);
195  this->FontSize = textProperty->GetFontSize();
196  double color[3];
197  textProperty->GetColor(color);
198  this->Color.Set(static_cast<unsigned char>(color[0] * 255),
199  static_cast<unsigned char>(color[1] * 255),
200  static_cast<unsigned char>(color[2] * 255),
201  static_cast<unsigned char>(textProperty->GetOpacity() * 255));
202  this->Text = text;
203  this->DPI = dpi;
204  }
206 
208 
210  bool operator==(const TextPropertyKey& other)const
211  {
212  return this->TextPropertyId == other.TextPropertyId &&
213  this->FontSize == other.FontSize &&
214  this->Text == other.Text &&
215  this->Color[0] == other.Color[0] &&
216  this->Color[1] == other.Color[1] &&
217  this->Color[2] == other.Color[2] &&
218  this->Color[3] == other.Color[3] &&
219  this->DPI == other.DPI;
220  }
222 
223  unsigned short FontSize;
225  // States in the function not to use more than 32 bits - int works fine here.
226  unsigned int TextPropertyId;
227  StringType Text;
228  int DPI;
229 };
230 
233 
235 {
236 public:
238  {
239  this->Texture = NULL;
242  this->SpriteTexture = NULL;
243  this->SavedLighting = GL_TRUE;
244  this->SavedDepthTest = GL_TRUE;
245  this->SavedAlphaTest = GL_TRUE;
246  this->SavedStencilTest = GL_TRUE;
247  this->SavedBlend = GL_TRUE;
248  this->SavedDrawBuffer = 0;
249  this->SavedClearColor[0] = this->SavedClearColor[1] =
250  this->SavedClearColor[2] =
251  this->SavedClearColor[3] = 0.0f;
252  this->TextCounter = 0;
253  this->GLExtensionsLoaded = false;
254  this->OpenGL15 = false;
255  this->OpenGL20 = false;
256  this->GLSL = false;
257  this->PowerOfTwoTextures = true;
258  }
259 
261  {
262  if (this->Texture)
263  {
264  this->Texture->Delete();
265  this->Texture = NULL;
266  }
267  if (this->SpriteTexture)
268  {
269  this->SpriteTexture->Delete();
270  this->SpriteTexture = NULL;
271  }
272  }
273 
274  void SaveGLState(bool colorBuffer = false)
275  {
276  this->SavedLighting = glIsEnabled(GL_LIGHTING);
277  this->SavedDepthTest = glIsEnabled(GL_DEPTH_TEST);
278 
279  if (colorBuffer)
280  {
281  this->SavedAlphaTest = glIsEnabled(GL_ALPHA_TEST);
282  this->SavedStencilTest = glIsEnabled(GL_STENCIL_TEST);
283  this->SavedBlend = glIsEnabled(GL_BLEND);
284  glGetFloatv(GL_COLOR_CLEAR_VALUE, this->SavedClearColor);
285  glGetIntegerv(GL_DRAW_BUFFER, &this->SavedDrawBuffer);
286  }
287  }
288 
289  void RestoreGLState(bool colorBuffer = false)
290  {
291  this->SetGLCapability(GL_LIGHTING, this->SavedLighting);
292  this->SetGLCapability(GL_DEPTH_TEST, this->SavedDepthTest);
293 
294  if (colorBuffer)
295  {
296  this->SetGLCapability(GL_ALPHA_TEST, this->SavedAlphaTest);
297  this->SetGLCapability(GL_STENCIL_TEST, this->SavedStencilTest);
298  this->SetGLCapability(GL_BLEND, this->SavedBlend);
299 
300  if(this->SavedDrawBuffer != GL_BACK_LEFT)
301  {
302  glDrawBuffer(this->SavedDrawBuffer);
303  }
304 
305  int i = 0;
306  bool colorDiffer = false;
307  while(!colorDiffer && i < 4)
308  {
309  colorDiffer=this->SavedClearColor[i++] != 0.0;
310  }
311  if(colorDiffer)
312  {
313  glClearColor(this->SavedClearColor[0],
314  this->SavedClearColor[1],
315  this->SavedClearColor[2],
316  this->SavedClearColor[3]);
317  }
318  }
319  }
320 
321  void SetGLCapability(GLenum capability, GLboolean state)
322  {
323  if (state)
324  {
325  glEnable(capability);
326  }
327  else
328  {
329  glDisable(capability);
330  }
331  }
332 
333  float* TexCoords(float* f, int n)
334  {
335  float* texCoord = new float[2*n];
336  float minX = f[0]; float minY = f[1];
337  float maxX = f[0]; float maxY = f[1];
338  float* fptr = f;
339  for(int i = 0; i < n; ++i)
340  {
341  minX = fptr[0] < minX ? fptr[0] : minX;
342  maxX = fptr[0] > maxX ? fptr[0] : maxX;
343  minY = fptr[1] < minY ? fptr[1] : minY;
344  maxY = fptr[1] > maxY ? fptr[1] : maxY;
345  fptr+=2;
346  }
347  fptr = f;
349  {
350  double* textureBounds = this->Texture->GetInput()->GetBounds();
351  float rangeX = (textureBounds[1] - textureBounds[0]) ?
352  textureBounds[1] - textureBounds[0] : 1.;
353  float rangeY = (textureBounds[3] - textureBounds[2]) ?
354  textureBounds[3] - textureBounds[2] : 1.;
355  for (int i = 0; i < n; ++i)
356  {
357  texCoord[i*2] = (fptr[0]-minX) / rangeX;
358  texCoord[i*2+1] = (fptr[1]-minY) / rangeY;
359  fptr+=2;
360  }
361  }
362  else // this->TextureProperties & vtkContextDevice2D::Stretch
363  {
364  float rangeX = (maxX - minX)? maxX - minX : 1.f;
365  float rangeY = (maxY - minY)? maxY - minY : 1.f;
366  for (int i = 0; i < n; ++i)
367  {
368  texCoord[i*2] = (fptr[0]-minX)/rangeX;
369  texCoord[i*2+1] = (fptr[1]-minY)/rangeY;
370  fptr+=2;
371  }
372  }
373  return texCoord;
374  }
375 
377  {
378  vtkVector2i pow2(1, 1);
379  for (int i = 0; i < 2; ++i)
380  {
381  while (pow2[i] < size[i])
382  {
383  pow2[i] *= 2;
384  }
385  }
386  return pow2;
387  }
388 
390  {
391  if (image->GetScalarType() != VTK_UNSIGNED_CHAR)
392  {
393  cout << "Error = not an unsigned char..." << endl;
394  return 0;
395  }
396  int bytesPerPixel = image->GetNumberOfScalarComponents();
397  int size[3];
398  image->GetDimensions(size);
399  vtkVector2i newImg = this->FindPowerOfTwo(vtkVector2i(size[0], size[1]));
400 
401  for (int i = 0; i < 2; ++i)
402  {
403  texCoords[i] = size[i] / float(newImg[i]);
404  }
405 
406  unsigned char *dataPtr =
407  new unsigned char[newImg[0] * newImg[1] * bytesPerPixel];
408  unsigned char *origPtr =
409  static_cast<unsigned char*>(image->GetScalarPointer());
410 
411  for (int i = 0; i < newImg[0]; ++i)
412  {
413  for (int j = 0; j < newImg[1]; ++j)
414  {
415  for (int k = 0; k < bytesPerPixel; ++k)
416  {
417  if (i < size[0] && j < size[1])
418  {
419  dataPtr[i * bytesPerPixel + j * newImg[0] * bytesPerPixel + k] =
420  origPtr[i * bytesPerPixel + j * size[0] * bytesPerPixel + k];
421  }
422  else
423  {
424  dataPtr[i * bytesPerPixel + j * newImg[0] * bytesPerPixel + k] =
425  k == 3 ? 0 : 255;
426  }
427  }
428  }
429  }
430 
431  GLuint tmpIndex(0);
432  GLint glFormat = bytesPerPixel == 3 ? GL_RGB : GL_RGBA;
433  GLint glInternalFormat = bytesPerPixel == 3 ? GL_RGB8 : GL_RGBA8;
434 
435  glGenTextures(1, &tmpIndex);
436  glBindTexture(GL_TEXTURE_2D, tmpIndex);
437 
438  glTexEnvf(GL_TEXTURE_ENV, vtkgl::COMBINE_RGB, GL_REPLACE);
439  glTexEnvf(GL_TEXTURE_ENV, vtkgl::COMBINE_ALPHA, GL_REPLACE);
440 
441  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
442  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
443  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
444  vtkgl::CLAMP_TO_EDGE );
445  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
446  vtkgl::CLAMP_TO_EDGE );
447 
448  glTexImage2D(GL_TEXTURE_2D, 0 , glInternalFormat,
449  newImg[0], newImg[1], 0, glFormat,
450  GL_UNSIGNED_BYTE, static_cast<const GLvoid *>(dataPtr));
451  glAlphaFunc(GL_GREATER, static_cast<GLclampf>(0));
452  glEnable(GL_ALPHA_TEST);
453  glMatrixMode(GL_TEXTURE);
454  glLoadIdentity();
455  glMatrixMode(GL_MODELVIEW);
456  glEnable(GL_TEXTURE_2D);
457  delete [] dataPtr;
458  return tmpIndex;
459  }
460 
462  {
463  if (image->GetScalarType() != VTK_UNSIGNED_CHAR)
464  {
465  cout << "Error = not an unsigned char..." << endl;
466  return 0;
467  }
468  int bytesPerPixel = image->GetNumberOfScalarComponents();
469  int size[3];
470  image->GetDimensions(size);
471 
472  unsigned char *dataPtr =
473  static_cast<unsigned char*>(image->GetScalarPointer());
474  GLuint tmpIndex(0);
475  GLint glFormat = bytesPerPixel == 3 ? GL_RGB : GL_RGBA;
476  GLint glInternalFormat = bytesPerPixel == 3 ? GL_RGB8 : GL_RGBA8;
477 
478  glGenTextures(1, &tmpIndex);
479  glBindTexture(GL_TEXTURE_2D, tmpIndex);
480 
481  glTexEnvf(GL_TEXTURE_ENV, vtkgl::COMBINE_RGB, GL_REPLACE);
482  glTexEnvf(GL_TEXTURE_ENV, vtkgl::COMBINE_ALPHA, GL_REPLACE);
483 
484  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
485  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
486  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
487  vtkgl::CLAMP_TO_EDGE );
488  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
489  vtkgl::CLAMP_TO_EDGE );
490 
491  glTexImage2D(GL_TEXTURE_2D, 0 , glInternalFormat,
492  size[0], size[1], 0, glFormat,
493  GL_UNSIGNED_BYTE, static_cast<const GLvoid *>(dataPtr));
494  glAlphaFunc(GL_GREATER, static_cast<GLclampf>(0));
495  glEnable(GL_ALPHA_TEST);
496  glMatrixMode(GL_TEXTURE);
497  glLoadIdentity();
498  glMatrixMode(GL_MODELVIEW);
499  glEnable(GL_TEXTURE_2D);
500  return tmpIndex;
501  }
502 
504  unsigned int TextureProperties;
506  // Store the previous GL state so that we can restore it when complete
507  GLboolean SavedLighting;
508  GLboolean SavedDepthTest;
509  GLboolean SavedAlphaTest;
510  GLboolean SavedStencilTest;
511  GLboolean SavedBlend;
513  GLfloat SavedClearColor[4];
514 
519  bool OpenGL15;
520  bool OpenGL20;
521  bool GLSL;
523 
525 
529 };
531 
532 #endif // VTKOPENGLCONTEXTDEVICE2DPRIVATE_H
533 // VTK-HeaderTest-Exclude: vtkOpenGLContextDevice2DPrivate.h
std::list< CacheElement > Cache
bool operator==(const CacheElement &other) const
void SetGLCapability(GLenum capability, GLboolean state)
double * GetBounds()
static int GetNumberOfScalarComponents(vtkInformation *meta_data)
GLuint TextureFromImage(vtkImageData *image, vtkVector2f &texCoords)
bool IsKeyInCache(const Key &key) const
TextPropertyKey(vtkTextProperty *textProperty, const StringType &text, int dpi)
static vtkSmartPointer< T > New()
CacheElement(const Key &key, const CacheData &cacheData)
void SetInputData(vtkDataObject *)
void ReleaseGraphicsResources(vtkWindow *window)
vtkSmartPointer< vtkImageData > ImageData
bool operator==(const TextPropertyKey &other) const
TextPropertyKey< vtkStdString > UTF8TextPropertyKey
window superclass for vtkRenderWindow
Definition: vtkWindow.h:36
virtual int * GetDimensions()
static int GetScalarType(vtkInformation *meta_data)
topologically and geometrically regular array of data
Definition: vtkImageData.h:44
vtkImageData * GetInput()
CacheData & GetCacheData(const Key &key)
void Set(const T &red, const T &green, const T &blue)
Definition: vtkColor.h:114
handles properties associated with a texture map
Definition: vtkTexture.h:69
virtual double GetOpacity()
represent text properties.
static vtkFreeTypeTools * GetInstance()
vtkTextureImageCache< UTF16TextPropertyKey > TextTextureCache
static unsigned int GetIdFromTextProperty(vtkTextProperty *textProperty)
virtual double * GetColor()
#define VTK_UNSIGNED_CHAR
Definition: vtkType.h:28
virtual void * GetScalarPointer(int coordinates[3])
void MapTextPropertyToId(vtkTextProperty *tprop, size_t *tprop_cache_id)
vtkVector2i FindPowerOfTwo(const vtkVector2i &size)
virtual int GetFontSize()
TextPropertyKey< vtkUnicodeString > UTF16TextPropertyKey
CacheData & AddCacheData(const Key &key, const CacheData &cacheData)
virtual void Delete()
vtkTextureImageCache< UTF8TextPropertyKey > MathTextTextureCache