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
33#ifndef vtkOpenGLContextDevice2DPrivate_h
34#define vtkOpenGLContextDevice2DPrivate_h
35
37
38#include "vtkColor.h"
39#include "vtkTextProperty.h"
40#include "vtkFreeTypeTools.h"
41#include "vtkStdString.h"
42#include "vtkUnicodeString.h"
43
44#include <algorithm>
45#include <list>
46#include <utility>
47
48// .NAME vtkTextureImageCache - store vtkTexture and vtkImageData identified by
49// a unique key.
50// .SECTION Description
51// Creating and initializing a texture can be time consuming,
52// vtkTextureImageCache offers the ability to reuse them as much as possible.
53template <class Key>
55{
56public:
57 struct CacheData
58 {
61 // Dimensions of the text. Used for generating texture coords when the image
62 // dimensions are scaled to a power of two.
63 int TextWidth;
64 int TextHeight;
65 };
66
68
71 struct CacheElement: public std::pair<Key, CacheData>
72 {
73 // Default constructor
75 : std::pair<Key, CacheData>(Key(), CacheData()){}
76 // Construct a partial CacheElement with no CacheData
77 // This can be used for temporary CacheElement used to search a given
78 // key into the cache list.
79 CacheElement(const Key& key)
80 : std::pair<Key, CacheData>(key, CacheData()){}
81 // Standard constructor of CacheElement
82 CacheElement(const Key& key, const CacheData& cacheData)
83 : std::pair<Key, CacheData>(key, cacheData){}
84 // Operator tuned to be used when searching into the cache list using
85 // std::find()
86 bool operator==(const CacheElement& other)const
87 {
88 // Here we cheat and make the comparison only on the key, this allows
89 // us to use std::find() to search for a given key.
90 return this->first == other.first;
91 }
92 };
94
99 {
100 this->MaxSize = 50;
101 }
102
107 bool IsKeyInCache(const Key& key)const
108 {
109 return std::find(this->Cache.begin(), this->Cache.end(), key) != this->Cache.end();
110 }
111
119
121
126 {
127 typename std::list<CacheElement >::iterator it;
128 for (it = this->Cache.begin(); it != this->Cache.end(); ++it)
129 {
130 it->second.Texture->ReleaseGraphicsResources(window);
131 }
132 }
134
135protected:
137
141 CacheData& AddCacheData(const Key& key, const CacheData& cacheData)
142 {
143 assert(!this->IsKeyInCache(key));
144 if (this->Cache.size() >= this->MaxSize)
145 {
146 this->Cache.pop_back();
147 }
148 this->Cache.push_front(CacheElement(key, cacheData));
149 return this->Cache.begin()->second;
150 }
152
156 std::list<CacheElement > Cache;
158
161 size_t MaxSize;
162};
164
165template<class Key>
167::GetCacheData(const Key& key)
168{
169 typename std::list<CacheElement>::iterator it =
170 std::find(this->Cache.begin(), this->Cache.end(), CacheElement(key));
171 if (it != this->Cache.end())
172 {
173 return it->second;
174 }
175 CacheData cacheData;
176 cacheData.ImageData = vtkSmartPointer<vtkImageData>::New();
177 cacheData.Texture = vtkSmartPointer<vtkTexture>::New();
178 cacheData.Texture->SetInputData(cacheData.ImageData);
179 cacheData.TextWidth = 0;
180 cacheData.TextHeight = 0;
181 return this->AddCacheData(key, cacheData);
182}
183
184// .NAME TextPropertyKey - unique key for a vtkTextProperty and text
185// .SECTION Description
186// Uniquely describe a pair of vtkTextProperty and text.
187template <class StringType>
188struct TextPropertyKey
189{
191
194 static unsigned int GetIdFromTextProperty(vtkTextProperty* textProperty)
195 {
196 size_t id;
198 // Truncation on 64-bit machines! The id is a pointer.
199 return static_cast<unsigned int>(id);
200 }
202
204
207 TextPropertyKey(vtkTextProperty* textProperty, const StringType& text,
208 int dpi)
209 {
210 this->TextPropertyId = GetIdFromTextProperty(textProperty);
211 this->FontSize = textProperty->GetFontSize();
212 double color[3];
213 textProperty->GetColor(color);
214 this->Color.Set(static_cast<unsigned char>(color[0] * 255),
215 static_cast<unsigned char>(color[1] * 255),
216 static_cast<unsigned char>(color[2] * 255),
217 static_cast<unsigned char>(textProperty->GetOpacity() * 255));
218 this->Text = text;
219 this->DPI = dpi;
220 }
222
227 bool operator==(const TextPropertyKey& other)const
228 {
229 return this->TextPropertyId == other.TextPropertyId &&
230 this->FontSize == other.FontSize &&
231 this->Text == other.Text &&
232 this->Color[0] == other.Color[0] &&
233 this->Color[1] == other.Color[1] &&
234 this->Color[2] == other.Color[2] &&
235 this->Color[3] == other.Color[3] &&
236 this->DPI == other.DPI;
237 }
238
239 unsigned short FontSize;
241 // States in the function not to use more than 32 bits - int works fine here.
242 unsigned int TextPropertyId;
243 StringType Text;
244 int DPI;
245};
246
249
251{
252public:
254 {
255 this->Texture = NULL;
258 this->SpriteTexture = NULL;
259 this->SavedDepthTest = GL_TRUE;
260 this->SavedAlphaTest = GL_TRUE;
261 this->SavedStencilTest = GL_TRUE;
262 this->SavedBlend = GL_TRUE;
263 this->SavedDrawBuffer = 0;
264 this->SavedClearColor[0] = this->SavedClearColor[1] =
265 this->SavedClearColor[2] =
266 this->SavedClearColor[3] = 0.0f;
267 this->TextCounter = 0;
268 this->GLExtensionsLoaded = true;
269 this->GLSL = true;
270 this->PowerOfTwoTextures = false;
271 }
272
274 {
275 if (this->Texture)
276 {
277 this->Texture->Delete();
278 this->Texture = NULL;
279 }
280 if (this->SpriteTexture)
281 {
282 this->SpriteTexture->Delete();
283 this->SpriteTexture = NULL;
284 }
285 }
286
287 void SaveGLState(bool colorBuffer = false)
288 {
289 this->SavedDepthTest = glIsEnabled(GL_DEPTH_TEST);
290
291 if (colorBuffer)
292 {
293 this->SavedAlphaTest = glIsEnabled(GL_ALPHA_TEST);
294 this->SavedStencilTest = glIsEnabled(GL_STENCIL_TEST);
295 this->SavedBlend = glIsEnabled(GL_BLEND);
296 glGetFloatv(GL_COLOR_CLEAR_VALUE, this->SavedClearColor);
297 glGetIntegerv(GL_DRAW_BUFFER, &this->SavedDrawBuffer);
298 }
299 }
300
301 void RestoreGLState(bool colorBuffer = false)
302 {
303 this->SetGLCapability(GL_DEPTH_TEST, this->SavedDepthTest);
304
305 if (colorBuffer)
306 {
307 this->SetGLCapability(GL_ALPHA_TEST, this->SavedAlphaTest);
308 this->SetGLCapability(GL_STENCIL_TEST, this->SavedStencilTest);
309 this->SetGLCapability(GL_BLEND, this->SavedBlend);
310
311 if(this->SavedDrawBuffer != GL_BACK_LEFT)
312 {
313 glDrawBuffer(this->SavedDrawBuffer);
314 }
315
316 int i = 0;
317 bool colorDiffer = false;
318 while(!colorDiffer && i < 4)
319 {
320 colorDiffer=this->SavedClearColor[i++] != 0.0;
321 }
322 if(colorDiffer)
323 {
324 glClearColor(this->SavedClearColor[0],
325 this->SavedClearColor[1],
326 this->SavedClearColor[2],
327 this->SavedClearColor[3]);
328 }
329 }
330 }
331
332 void SetGLCapability(GLenum capability, GLboolean state)
333 {
334 if (state)
335 {
336 glEnable(capability);
337 }
338 else
339 {
340 glDisable(capability);
341 }
342 }
343
344 float* TexCoords(float* f, int n)
345 {
346 float* texCoord = new float[2*n];
347 float minX = f[0]; float minY = f[1];
348 float maxX = f[0]; float maxY = f[1];
349 float* fptr = f;
350 for(int i = 0; i < n; ++i)
351 {
352 minX = fptr[0] < minX ? fptr[0] : minX;
353 maxX = fptr[0] > maxX ? fptr[0] : maxX;
354 minY = fptr[1] < minY ? fptr[1] : minY;
355 maxY = fptr[1] > maxY ? fptr[1] : maxY;
356 fptr+=2;
357 }
358 fptr = f;
360 {
361 double* textureBounds = this->Texture->GetInput()->GetBounds();
362 float rangeX = (textureBounds[1] - textureBounds[0]) ?
363 textureBounds[1] - textureBounds[0] : 1.;
364 float rangeY = (textureBounds[3] - textureBounds[2]) ?
365 textureBounds[3] - textureBounds[2] : 1.;
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 else // this->TextureProperties & vtkContextDevice2D::Stretch
374 {
375 float rangeX = (maxX - minX)? maxX - minX : 1.f;
376 float rangeY = (maxY - minY)? maxY - minY : 1.f;
377 for (int i = 0; i < n; ++i)
378 {
379 texCoord[i*2] = (fptr[0]-minX)/rangeX;
380 texCoord[i*2+1] = (fptr[1]-minY)/rangeY;
381 fptr+=2;
382 }
383 }
384 return texCoord;
385 }
386
388 {
389 vtkVector2i pow2(1, 1);
390 for (int i = 0; i < 2; ++i)
391 {
392 while (pow2[i] < size[i])
393 {
394 pow2[i] *= 2;
395 }
396 }
397 return pow2;
398 }
399
401 {
402 if (image->GetScalarType() != VTK_UNSIGNED_CHAR)
403 {
404 vtkGenericWarningMacro("Invalid image format: expected unsigned char.");
405 return 0;
406 }
407 int bytesPerPixel = image->GetNumberOfScalarComponents();
408 int size[3];
409 image->GetDimensions(size);
410 vtkVector2i newImg = this->FindPowerOfTwo(vtkVector2i(size[0], size[1]));
411
412 for (int i = 0; i < 2; ++i)
413 {
414 texCoords[i] = size[i] / float(newImg[i]);
415 }
416
417 unsigned char *dataPtr =
418 new unsigned char[newImg[0] * newImg[1] * bytesPerPixel];
419 unsigned char *origPtr =
420 static_cast<unsigned char*>(image->GetScalarPointer());
421
422 for (int i = 0; i < newImg[0]; ++i)
423 {
424 for (int j = 0; j < newImg[1]; ++j)
425 {
426 for (int k = 0; k < bytesPerPixel; ++k)
427 {
428 if (i < size[0] && j < size[1])
429 {
430 dataPtr[i * bytesPerPixel + j * newImg[0] * bytesPerPixel + k] =
431 origPtr[i * bytesPerPixel + j * size[0] * bytesPerPixel + k];
432 }
433 else
434 {
435 dataPtr[i * bytesPerPixel + j * newImg[0] * bytesPerPixel + k] =
436 k == 3 ? 0 : 255;
437 }
438 }
439 }
440 }
441
442 GLuint tmpIndex(0);
443 GLint glFormat = bytesPerPixel == 3 ? GL_RGB : GL_RGBA;
444 GLint glInternalFormat = bytesPerPixel == 3 ? GL_RGB8 : GL_RGBA8;
445
446 glGenTextures(1, &tmpIndex);
447 glBindTexture(GL_TEXTURE_2D, tmpIndex);
448
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 GL_CLAMP_TO_EDGE );
453 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
454 GL_CLAMP_TO_EDGE );
455
456 glTexImage2D(GL_TEXTURE_2D, 0 , glInternalFormat,
457 newImg[0], newImg[1], 0, glFormat,
458 GL_UNSIGNED_BYTE, static_cast<const GLvoid *>(dataPtr));
459 delete [] dataPtr;
460 return tmpIndex;
461 }
462
464 {
465 if (image->GetScalarType() != VTK_UNSIGNED_CHAR)
466 {
467 cout << "Error = not an unsigned char..." << endl;
468 return 0;
469 }
470 int bytesPerPixel = image->GetNumberOfScalarComponents();
471 int size[3];
472 image->GetDimensions(size);
473
474 unsigned char *dataPtr =
475 static_cast<unsigned char*>(image->GetScalarPointer());
476 GLuint tmpIndex(0);
477 GLint glFormat = bytesPerPixel == 3 ? GL_RGB : GL_RGBA;
478 GLint glInternalFormat = bytesPerPixel == 3 ? GL_RGB8 : GL_RGBA8;
479
480 glGenTextures(1, &tmpIndex);
481 glBindTexture(GL_TEXTURE_2D, tmpIndex);
482
483 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
484 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
485 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
486 GL_CLAMP_TO_EDGE );
487 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
488 GL_CLAMP_TO_EDGE );
489
490 glTexImage2D(GL_TEXTURE_2D, 0 , glInternalFormat,
491 size[0], size[1], 0, glFormat,
492 GL_UNSIGNED_BYTE, static_cast<const GLvoid *>(dataPtr));
493 return tmpIndex;
494 }
495
497 unsigned int TextureProperties;
499 // Store the previous GL state so that we can restore it when complete
500 GLboolean SavedDepthTest;
501 GLboolean SavedAlphaTest;
502 GLboolean SavedStencilTest;
503 GLboolean SavedBlend;
504 GLint SavedDrawBuffer;
505 GLfloat SavedClearColor[4];
506
507 int TextCounter;
511 bool GLSL;
513
515
521};
523
524#endif // VTKOPENGLCONTEXTDEVICE2DPRIVATE_H
525// VTK-HeaderTest-Exclude: vtkOpenGLContextDevice2DPrivate.h
TextPropertyKey< vtkUnicodeString > UTF16TextPropertyKey
TextPropertyKey< vtkStdString > UTF8TextPropertyKey
void Set(const T &red, const T &green, const T &blue)
Set the red, green and blue components of the color.
Definition: vtkColor.h:129
double * GetBounds()
Return a pointer to the geometry bounding box in the form (xmin,xmax, ymin,ymax, zmin,...
void MapTextPropertyToId(vtkTextProperty *tprop, size_t *tprop_cache_id)
Given a text property 'tprop', get its unique ID in our cache framework.
static vtkFreeTypeTools * GetInstance()
Return the singleton instance with no reference counting.
topologically and geometrically regular array of data
Definition: vtkImageData.h:46
virtual void Delete()
Delete a VTK object.
vtkTextureImageCache< UTF16TextPropertyKey > TextTextureCache
Cache for text images.
vtkVector2i FindPowerOfTwo(const vtkVector2i &size)
vtkTextureImageCache< UTF8TextPropertyKey > MathTextTextureCache
GLuint TextureFromImage(vtkImageData *image, vtkVector2f &texCoords)
void SetGLCapability(GLenum capability, GLboolean state)
static vtkSmartPointer< T > New()
Create an instance of a VTK object.
represent text properties.
virtual double GetOpacity()
virtual int GetFontSize()
virtual double * GetColor()
bool IsKeyInCache(const Key &key) const
Search the cache list to see if a given key already exists.
std::list< CacheElement > Cache
List of a pair of key and cache data.
CacheData & GetCacheData(const Key &key)
Return the cache associated to a key.
CacheData & AddCacheData(const Key &key, const CacheData &cacheData)
Add a new cache entry into the cache list.
void ReleaseGraphicsResources(vtkWindow *window)
Release all the OpenGL Pixel Buffer Object(PBO) associated with the textures of the cache list.
vtkTextureImageCache()
Construct a texture image cache with a maximum number of texture of 50.
size_t MaxSize
Maximum size the cache list can be.
CacheData & GetCacheData(const Key &key)
Return the cache associated to a key.
handles properties associated with a texture map
Definition: vtkTexture.h:71
vtkImageData * GetInput()
Get the input as a vtkImageData object.
Some derived classes for the different vectors commonly used.
Definition: vtkVector.h:328
window superclass for vtkRenderWindow
Definition: vtkWindow.h:35
@ key
Definition: vtkX3D.h:257
@ color
Definition: vtkX3D.h:221
@ image
Definition: vtkX3D.h:374
@ size
Definition: vtkX3D.h:253
TextPropertyKey(vtkTextProperty *textProperty, const StringType &text, int dpi)
Creates a TextPropertyKey.
static unsigned int GetIdFromTextProperty(vtkTextProperty *textProperty)
Transform a text property into an unsigned long.
bool operator==(const TextPropertyKey &other) const
Compares two TextPropertyKeys with each other.
vtkSmartPointer< vtkImageData > ImageData
CacheElement associates a unique key to some cache.
CacheElement(const Key &key, const CacheData &cacheData)
bool operator==(const CacheElement &other) const
#define VTK_UNSIGNED_CHAR
Definition: vtkType.h:51