33 #ifndef vtkOpenGLContextDevice2DPrivate_h 34 #define vtkOpenGLContextDevice2DPrivate_h 73 struct CacheElement:
public std::pair<Key, CacheData>
85 : std::pair<Key,
CacheData>(key, cacheData){}
92 return this->first == other.first;
129 typename std::list<CacheElement >::iterator it;
130 for (it = this->
Cache.begin(); it != this->
Cache.end(); ++it)
132 it->second.Texture->ReleaseGraphicsResources(window);
148 this->
Cache.pop_back();
150 this->
Cache.push_front(CacheElement(key, cacheData));
151 return this->
Cache.begin()->second;
158 std::list<CacheElement >
Cache;
171 typename std::list<CacheElement>::iterator it =
172 std::find(this->
Cache.begin(), this->
Cache.end(), CacheElement(key));
173 if (it != this->
Cache.end())
180 cacheData.Texture->SetInputData(cacheData.ImageData);
187 template <
class StringType>
204 vtkTypeUInt32 hash =
static_cast<vtkTypeUInt32
>(id);
208 assert(
"Hash is really a uint32" && static_cast<size_t>(hash) ==
id);
229 this->TextPropertyId = GetIdFromTextProperty(textProperty);
233 this->
Color.Set(static_cast<unsigned char>(color[0] * 255),
234 static_cast<unsigned char>(color[1] * 255),
235 static_cast<unsigned char>(color[2] * 255),
236 static_cast<unsigned char>(textProperty->
GetOpacity() * 255));
255 this->DPI == other.
DPI;
258 unsigned short FontSize;
261 vtkTypeUInt32 TextPropertyId;
277 this->SpriteTexture = NULL;
278 this->SavedDepthTest = GL_TRUE;
279 this->SavedAlphaTest = GL_TRUE;
280 this->SavedStencilTest = GL_TRUE;
281 this->SavedBlend = GL_TRUE;
282 this->SavedDrawBuffer = 0;
283 this->SavedClearColor[0] = this->SavedClearColor[1] =
284 this->SavedClearColor[2] =
285 this->SavedClearColor[3] = 0.0f;
286 this->TextCounter = 0;
287 this->GLExtensionsLoaded =
true;
289 this->PowerOfTwoTextures =
false;
299 if (this->SpriteTexture)
301 this->SpriteTexture->
Delete();
302 this->SpriteTexture = NULL;
308 this->SavedDepthTest = glIsEnabled(GL_DEPTH_TEST);
312 this->SavedAlphaTest = glIsEnabled(GL_ALPHA_TEST);
313 this->SavedStencilTest = glIsEnabled(GL_STENCIL_TEST);
314 this->SavedBlend = glIsEnabled(GL_BLEND);
315 glGetFloatv(GL_COLOR_CLEAR_VALUE, this->SavedClearColor);
316 glGetIntegerv(GL_DRAW_BUFFER, &this->SavedDrawBuffer);
322 this->SetGLCapability(GL_DEPTH_TEST, this->SavedDepthTest);
326 this->SetGLCapability(GL_ALPHA_TEST, this->SavedAlphaTest);
327 this->SetGLCapability(GL_STENCIL_TEST, this->SavedStencilTest);
328 this->SetGLCapability(GL_BLEND, this->SavedBlend);
330 if(this->SavedDrawBuffer != GL_BACK_LEFT)
332 glDrawBuffer(this->SavedDrawBuffer);
336 bool colorDiffer =
false;
337 while(!colorDiffer && i < 4)
339 colorDiffer=this->SavedClearColor[i++] != 0.0;
343 glClearColor(this->SavedClearColor[0],
344 this->SavedClearColor[1],
345 this->SavedClearColor[2],
346 this->SavedClearColor[3]);
355 glEnable(capability);
359 glDisable(capability);
365 float* texCoord =
new float[2*n];
366 float minX = f[0];
float minY = f[1];
367 float maxX = f[0];
float maxY = f[1];
369 for(
int i = 0; i < n; ++i)
371 minX = fptr[0] < minX ? fptr[0] : minX;
372 maxX = fptr[0] > maxX ? fptr[0] : maxX;
373 minY = fptr[1] < minY ? fptr[1] : minY;
374 maxY = fptr[1] > maxY ? fptr[1] : maxY;
381 float rangeX = (textureBounds[1] - textureBounds[0]) ?
382 textureBounds[1] - textureBounds[0] : 1.;
383 float rangeY = (textureBounds[3] - textureBounds[2]) ?
384 textureBounds[3] - textureBounds[2] : 1.;
385 for (
int i = 0; i < n; ++i)
387 texCoord[i*2] = (fptr[0]-minX) / rangeX;
388 texCoord[i*2+1] = (fptr[1]-minY) / rangeY;
394 float rangeX = (maxX - minX)? maxX - minX : 1.f;
395 float rangeY = (maxY - minY)? maxY - minY : 1.f;
396 for (
int i = 0; i < n; ++i)
398 texCoord[i*2] = (fptr[0]-minX)/rangeX;
399 texCoord[i*2+1] = (fptr[1]-minY)/rangeY;
409 for (
int i = 0; i < 2; ++i)
411 while (pow2[i] < size[i])
423 vtkGenericWarningMacro(
"Invalid image format: expected unsigned char.");
431 for (
int i = 0; i < 2; ++i)
433 texCoords[i] = size[i] /
float(newImg[i]);
436 unsigned char *dataPtr =
437 new unsigned char[newImg[0] * newImg[1] * bytesPerPixel];
438 unsigned char *origPtr =
441 for (
int i = 0; i < newImg[0]; ++i)
443 for (
int j = 0; j < newImg[1]; ++j)
445 for (
int k = 0; k < bytesPerPixel; ++k)
447 if (i < size[0] && j < size[1])
449 dataPtr[i * bytesPerPixel + j * newImg[0] * bytesPerPixel + k] =
450 origPtr[i * bytesPerPixel + j * size[0] * bytesPerPixel + k];
454 dataPtr[i * bytesPerPixel + j * newImg[0] * bytesPerPixel + k] =
462 GLint glFormat = bytesPerPixel == 3 ? GL_RGB : GL_RGBA;
463 GLint glInternalFormat = bytesPerPixel == 3 ? GL_RGB8 : GL_RGBA8;
465 glGenTextures(1, &tmpIndex);
466 glBindTexture(GL_TEXTURE_2D, tmpIndex);
468 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
469 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
470 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
472 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
475 glTexImage2D(GL_TEXTURE_2D, 0 , glInternalFormat,
476 newImg[0], newImg[1], 0, glFormat,
477 GL_UNSIGNED_BYTE, static_cast<const GLvoid *>(dataPtr));
486 cout <<
"Error = not an unsigned char..." << endl;
493 unsigned char *dataPtr =
496 GLint glFormat = bytesPerPixel == 3 ? GL_RGB : GL_RGBA;
497 GLint glInternalFormat = bytesPerPixel == 3 ? GL_RGB8 : GL_RGBA8;
499 glGenTextures(1, &tmpIndex);
500 glBindTexture(GL_TEXTURE_2D, tmpIndex);
502 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
503 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
504 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
506 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
509 glTexImage2D(GL_TEXTURE_2D, 0 , glInternalFormat,
510 size[0], size[1], 0, glFormat,
511 GL_UNSIGNED_BYTE, static_cast<const GLvoid *>(dataPtr));
516 unsigned int TextureProperties;
519 GLboolean SavedDepthTest;
520 GLboolean SavedAlphaTest;
521 GLboolean SavedStencilTest;
522 GLboolean SavedBlend;
523 GLint SavedDrawBuffer;
524 GLfloat SavedClearColor[4];
529 bool GLExtensionsLoaded;
531 bool PowerOfTwoTextures;
590 this->Colors = colors;
591 this->CellColors->SetNumberOfComponents(colors->GetNumberOfComponents());
596 this->DrawLines(cellArray, scalarMode, x, y, scale);
600 this->DrawPolygons(cellArray, scalarMode, x, y, scale);
612 void MapCurrentCell (
float const posX,
float const posY,
float const scale,
615 this->CellPoints.reserve(this->NumPointsCell * 2);
616 this->CellColors->SetNumberOfTuples(this->NumPointsCell);
617 for (
int i = 0; i < this->NumPointsCell; i++)
620 this->Points->GetPoint(this->PointIds[i], point);
623 float const x =
static_cast<float>(point[0]) + posX;
624 float const y =
static_cast<float>(point[1]) + posY;
625 this->CellPoints.push_back(x * scale);
626 this->CellPoints.push_back(y * scale);
633 mappedColorId = this->PointIds[i];
636 mappedColorId = cellId;
639 std::cerr <<
"Scalar mode not supported!" << std::endl;
643 this->CellColors->SetTuple(i, mappedColorId, this->Colors);
652 void DrawLines(
vtkCellArray* cellArray,
int scalarMode,
float const x,
653 float const y,
float const scale)
655 if (cellArray->
GetMTime() > this->LinesLoadingTime)
658 this->LineColors->Reset();
662 this->Lines.reserve(numVertices * 2);
663 this->LineColors->SetNumberOfComponents(this->Colors->GetNumberOfComponents());
664 this->LineColors->SetNumberOfTuples(numVertices);
669 this->PointIds); cellId++)
671 this->MapCurrentCell(x, y, scale, cellId, scalarMode);
674 for (
int i = 0; i < this->NumPointsCell; i++)
676 this->Lines.push_back(this->CellPoints[2 * i]);
677 this->Lines.push_back(this->CellPoints[2 * i + 1]);
679 double* color4 = this->CellColors->GetTuple(i);
680 this->LineColors->InsertTuple4(vertOffset + i, color4[0], color4[1], color4[2],
684 vertOffset += this->NumPointsCell;
685 this->CellColors->Reset();
686 this->CellPoints.clear();
689 this->LinesLoadingTime.Modified();
692 this->Device->DrawLines(&this->Lines[0], this->Lines.size() / 2,
693 static_cast<unsigned char*
>(this->LineColors->GetVoidPointer(0)),
694 this->LineColors->GetNumberOfComponents());
706 this->PointIds); cellId++)
708 numTriVert += 3 * (this->NumPointsCell - 2);
719 void DrawPolygons(
vtkCellArray* cellArray,
int scalarMode,
float const x,
720 float const y,
float const scale)
722 if (cellArray->
GetMTime() > this->PolygonsLoadingTime)
724 this->PolyTri.clear();
725 this->PolyColors->Reset();
728 vtkIdType const totalTriVert = this->GetCountTriangleVertices(cellArray);
729 this->PolyTri.reserve(totalTriVert * 2);
730 this->PolyColors->SetNumberOfComponents(this->Colors->GetNumberOfComponents());
731 this->PolyColors->SetNumberOfTuples(totalTriVert);
736 this->PolyColors->SetNumberOfComponents(this->Colors->GetNumberOfComponents());
738 this->PointIds); cellId++)
740 this->MapCurrentCell(x, y, scale, cellId, scalarMode);
743 for (
int i = 0; i < this->NumPointsCell - 2; i++)
745 this->PolyTri.push_back(this->CellPoints[0]);
746 this->PolyTri.push_back(this->CellPoints[1]);
747 this->PolyTri.push_back(this->CellPoints[i * 2 + 2]);
748 this->PolyTri.push_back(this->CellPoints[i * 2 + 3]);
749 this->PolyTri.push_back(this->CellPoints[i * 2 + 4]);
750 this->PolyTri.push_back(this->CellPoints[i * 2 + 5]);
753 vtkIdType const triangOffset = vertOffset + 3 * i;
754 double* color4 = this->CellColors->GetTuple(0);
755 this->PolyColors->InsertTuple4(triangOffset, color4[0], color4[1],
756 color4[2], color4[3]);
758 color4 = this->CellColors->GetTuple(i + 1);
759 this->PolyColors->InsertTuple4(triangOffset + 1, color4[0], color4[1],
760 color4[2], color4[3]);
762 color4 = this->CellColors->GetTuple(i + 2);
763 this->PolyColors->InsertTuple4(triangOffset + 2, color4[0], color4[1],
764 color4[2], color4[3]);
767 vertOffset += 3 * (this->NumPointsCell - 2);
768 this->CellColors->Reset();
769 this->CellPoints.clear();
772 this->PolygonsLoadingTime.Modified();
775 this->Device->CoreDrawTriangles(this->PolyTri,
776 static_cast<unsigned char*>(this->PolyColors->GetVoidPointer(0)), 4);
790 std::vector<float> CellPoints;
798 std::vector<float> PolyTri;
807 std::vector<float> Lines;
812 #endif // VTKOPENGLCONTEXTDEVICE2DPRIVATE_H
std::list< CacheElement > Cache
List of a pair of key and cache data.
GLuint TextureFromImage(vtkImageData *image)
virtual int GetJustification()
Set/Get the horizontal justification to left (default), centered, or right.
bool operator==(const CacheElement &other) const
void SetGLCapability(GLenum capability, GLboolean state)
double * GetBounds()
Return a pointer to the geometry bounding box in the form (xmin,xmax, ymin,ymax, zmin,zmax).
vtkTextRenderer::Metrics Metrics
record modification and/or execution time
Class for drawing 2D primitives using OpenGL 1.1+.
void InitTraversal()
A cell traversal methods that is more efficient than vtkDataSet traversal methods.
static int GetNumberOfScalarComponents(vtkInformation *meta_data)
Set/Get the number of scalar components for points.
void Draw(int cellType, vtkCellArray *cellArray, vtkPoints *points, float x, float y, float scale, int scalarMode, vtkUnsignedCharArray *colors=NULL)
Draw primitives as specified by cellType.
GLuint TextureFromImage(vtkImageData *image, vtkVector2f &texCoords)
vtkTypeUInt32 TextPropertyId
TextPropertyKey(vtkTextProperty *textProperty, const StringType &text, int dpi)
Creates a TextPropertyKey.
virtual vtkIdType GetNumberOfCells()
Get the number of cells in the array.
CellArrayHelper(vtkOpenGLContextDevice2D *device)
vtkSmartPointer< vtkTexture > Texture
static vtkSmartPointer< T > New()
Create an instance of a VTK object.
CacheElement(const Key &key, const CacheData &cacheData)
void ReleaseGraphicsResources(vtkWindow *window)
Release all the OpenGL Pixel Buffer Object(PBO) associated with the textures of the cache list...
vtkSmartPointer< vtkImageData > ImageData
float * TexCoords(float *f, int n)
TextPropertyKey< vtkStdString > UTF8TextPropertyKey
window superclass for vtkRenderWindow
size_t MaxSize
Maximum size the cache list can be.
bool operator==(const TextPropertyKey &other) const
Compares two TextPropertyKeys with each other.
#define VTK_SCALAR_MODE_USE_POINT_DATA
virtual int * GetDimensions()
Get dimensions of this structured points dataset.
static int GetScalarType(vtkInformation *meta_data)
topologically and geometrically regular array of data
vtkImageData * GetInput()
Get the input as a vtkImageData object.
virtual vtkMTimeType GetMTime()
Return this object's modified time.
Some derived classes for the different vectors commonly used.
virtual int GetVerticalJustification()
Set/Get the vertical justification to bottom (default), middle, or top.
CacheData & GetCacheData(const Key &key)
Return the cache associated to a key.
bool IsKeyInCache(const Key &key) const
Search the cache list to see if a given key already exists.
handles properties associated with a texture map
vtkTextureImageCache()
Construct a texture image cache with a maximum number of texture of 50.
virtual double GetOpacity()
Set/Get the text's opacity.
represent text properties.
dynamic, self-adjusting array of unsigned char
object to represent cell connectivity
CacheElement(const Key &key)
virtual double * GetColor()
Set the color of the text.
#define VTK_UNSIGNED_CHAR
virtual void * GetScalarPointer(int coordinates[3])
Access the native pointer for the scalar data.
int GetNextCell(vtkIdType &npts, vtkIdType *&pts)
A cell traversal methods that is more efficient than vtkDataSet traversal methods.
CacheElement associates a unique key to some cache.
vtkVector2i FindPowerOfTwo(const vtkVector2i &size)
virtual int GetFontSize()
Set/Get the font size (in points).
TextPropertyKey< vtkUnicodeString > UTF16TextPropertyKey
static vtkTypeUInt32 GetIdFromTextProperty(vtkTextProperty *tprop)
Transform a text property into an unsigned long.
#define VTK_SCALAR_MODE_USE_CELL_DATA
CacheData & AddCacheData(const Key &key, const CacheData &cacheData)
Add a new cache entry into the cache list.
represent and manipulate 3D points
void RestoreGLState(bool colorBuffer=false)
void SaveGLState(bool colorBuffer=false)
virtual void Delete()
Delete a VTK object.