33 #ifndef vtkOpenGLContextDevice2DPrivate_h
34 #define vtkOpenGLContextDevice2DPrivate_h
101 return this->first == other.first;
117 return std::find(this->
Cache.begin(), this->Cache.end(),
key) != this->
Cache.end();
135 typename std::list<CacheElement>::iterator it;
136 for (it = this->
Cache.begin(); it != this->Cache.end(); ++it)
138 it->second.Texture->ReleaseGraphicsResources(window);
152 if (this->
Cache.size() >= this->MaxSize)
154 this->
Cache.pop_back();
156 this->
Cache.push_front(CacheElement(
key, cacheData));
157 return this->
Cache.begin()->second;
177 typename std::list<CacheElement>::iterator it =
178 std::find(this->Cache.begin(), this->Cache.end(), CacheElement(
key));
179 if (it != this->Cache.end())
186 cacheData.Texture->SetInputData(cacheData.ImageData);
187 return this->AddCacheData(
key, cacheData);
193 template <
class StringType>
210 vtkTypeUInt32 hash =
static_cast<vtkTypeUInt32
>(id);
214 assert(
"Hash is really a uint32" &&
static_cast<size_t>(hash) ==
id);
238 this->
Color.
Set(
static_cast<unsigned char>(color[0] * 255),
239 static_cast<unsigned char>(
color[1] * 255),
static_cast<unsigned char>(
color[2] * 255),
240 static_cast<unsigned char>(textProperty->
GetOpacity() * 255));
328 ::glDrawBuffers(1, bufs);
338 float* texCoord =
new float[2 * n];
344 for (
int i = 0; i < n; ++i)
346 minX = fptr[0] < minX ? fptr[0] : minX;
347 maxX = fptr[0] > maxX ? fptr[0] : maxX;
348 minY = fptr[1] < minY ? fptr[1] : minY;
349 maxY = fptr[1] > maxY ? fptr[1] : maxY;
357 (textureBounds[1] - textureBounds[0]) ? textureBounds[1] - textureBounds[0] : 1.;
359 (textureBounds[3] - textureBounds[2]) ? textureBounds[3] - textureBounds[2] : 1.;
360 for (
int i = 0; i < n; ++i)
362 texCoord[i * 2] = (fptr[0] - minX) / rangeX;
363 texCoord[i * 2 + 1] = (fptr[1] - minY) / rangeY;
369 float rangeX = (maxX - minX) ? maxX - minX : 1.f;
370 float rangeY = (maxY - minY) ? maxY - minY : 1.f;
371 for (
int i = 0; i < n; ++i)
373 texCoord[i * 2] = (fptr[0] - minX) / rangeX;
374 texCoord[i * 2 + 1] = (fptr[1] - minY) / rangeY;
384 for (
int i = 0; i < 2; ++i)
386 while (pow2[i] <
size[i])
398 vtkGenericWarningMacro(
"Invalid image format: expected unsigned char.");
401 int bytesPerPixel =
image->GetNumberOfScalarComponents();
406 for (
int i = 0; i < 2; ++i)
408 texCoords[i] =
size[i] / float(newImg[i]);
411 unsigned char* dataPtr =
new unsigned char[newImg[0] * newImg[1] * bytesPerPixel];
412 unsigned char* origPtr =
static_cast<unsigned char*
>(
image->GetScalarPointer());
414 for (
int i = 0; i < newImg[0]; ++i)
416 for (
int j = 0; j < newImg[1]; ++j)
418 for (
int k = 0; k < bytesPerPixel; ++k)
422 dataPtr[i * bytesPerPixel + j * newImg[0] * bytesPerPixel + k] =
423 origPtr[i * bytesPerPixel + j *
size[0] * bytesPerPixel + k];
427 dataPtr[i * bytesPerPixel + j * newImg[0] * bytesPerPixel + k] = k == 3 ? 0 : 255;
434 GLint glFormat = bytesPerPixel == 3 ? GL_RGB : GL_RGBA;
435 GLint glInternalFormat = bytesPerPixel == 3 ? GL_RGB8 : GL_RGBA8;
437 glGenTextures(1, &tmpIndex);
438 glBindTexture(GL_TEXTURE_2D, tmpIndex);
440 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
441 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
442 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
443 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
445 glTexImage2D(GL_TEXTURE_2D, 0, glInternalFormat, newImg[0], newImg[1], 0, glFormat,
446 GL_UNSIGNED_BYTE,
static_cast<const GLvoid*
>(dataPtr));
455 cout <<
"Error = not an unsigned char..." << endl;
458 int bytesPerPixel =
image->GetNumberOfScalarComponents();
462 unsigned char* dataPtr =
static_cast<unsigned char*
>(
image->GetScalarPointer());
464 GLint glFormat = bytesPerPixel == 3 ? GL_RGB : GL_RGBA;
465 GLint glInternalFormat = bytesPerPixel == 3 ? GL_RGB8 : GL_RGBA8;
467 glGenTextures(1, &tmpIndex);
468 glBindTexture(GL_TEXTURE_2D, tmpIndex);
470 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
471 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
472 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
473 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
475 glTexImage2D(GL_TEXTURE_2D, 0, glInternalFormat,
size[0],
size[1], 0, glFormat,
476 GL_UNSIGNED_BYTE,
static_cast<const GLvoid*
>(dataPtr));
545 this->cache =
new PolyDataCache();
557 this->Colors = colors;
558 this->CellColors->SetNumberOfComponents(colors->GetNumberOfComponents());
563 this->DrawLines(polyData, scalarMode, x, y,
scale);
567 this->DrawPolygons(polyData, scalarMode, x, y,
scale);
578 struct PolyDataCacheItem
582 std::vector<float> PolyTri;
586 std::vector<float> Lines;
595 std::map<vtkPolyData*, PolyDataCacheItem*>::iterator itPrev = this->PrevFrameCache.begin();
596 for (; itPrev != this->PrevFrameCache.end(); ++itPrev)
598 delete itPrev->second;
601 std::map<vtkPolyData*, PolyDataCacheItem*>::iterator it = this->CurrentFrameCache.begin();
602 for (; it != this->CurrentFrameCache.end(); ++it)
610 PolyDataCacheItem* cacheItem = this->CurrentFrameCache[
key];
611 if (cacheItem ==
nullptr)
613 cacheItem = this->PrevFrameCache[
key];
614 if (cacheItem ==
nullptr)
616 cacheItem =
new PolyDataCacheItem();
623 this->PrevFrameCache.erase(
key);
627 this->CurrentFrameCache[
key] = cacheItem;
638 std::map<vtkPolyData*, PolyDataCacheItem*>::iterator itPrev = this->PrevFrameCache.begin();
639 for (; itPrev != this->PrevFrameCache.end(); ++itPrev)
641 delete itPrev->second;
645 this->PrevFrameCache.clear();
648 std::swap(this->PrevFrameCache, this->CurrentFrameCache);
653 std::map<vtkPolyData*, PolyDataCacheItem*> PrevFrameCache;
654 std::map<vtkPolyData*, PolyDataCacheItem*> CurrentFrameCache;
661 float const posX,
float const posY,
float const scale,
vtkIdType cellId,
int scalarMode)
663 this->CellPoints.reserve(this->NumPointsCell * 2);
664 this->CellColors->SetNumberOfTuples(this->NumPointsCell);
665 for (
int i = 0; i < this->NumPointsCell; i++)
671 float const x =
static_cast<float>(
point[0]) + posX;
672 float const y =
static_cast<float>(
point[1]) + posY;
673 this->CellPoints.push_back(x *
scale);
674 this->CellPoints.push_back(y *
scale);
681 mappedColorId = this->PointIds[i];
684 mappedColorId = cellId;
687 std::cerr <<
"Scalar mode not supported!" << std::endl;
691 this->CellColors->SetTuple(i, mappedColorId, this->Colors);
701 vtkPolyData* polyData,
int scalarMode,
float const x,
float const y,
float const scale)
703 PolyDataCacheItem* cacheItem = this->cache->GetCacheEntry(polyData);
705 if (polyData->
GetMTime() > cacheItem->LinesLoadingTime)
708 cacheItem->Lines.clear();
709 cacheItem->LineColors->Reset();
713 cacheItem->Lines.reserve(numVertices * 2);
715 cacheItem->LineColors->SetNumberOfTuples(numVertices);
727 vtkIdType actualNumPointsCell = genericCell->GetNumberOfPoints();
729 for (
int i = 0; i < actualNumPointsCell - 1; ++i)
731 this->NumPointsCell = 2;
732 this->PointIds = genericCell->GetPointIds()->
GetPointer(i);
734 this->MapCurrentCell(x, y,
scale, cellId, scalarMode);
737 for (
int j = 0; j < this->NumPointsCell; j++)
739 cacheItem->Lines.push_back(this->CellPoints[2 * j]);
740 cacheItem->Lines.push_back(this->CellPoints[2 * j + 1]);
742 double* color4 = this->CellColors->GetTuple(j);
743 cacheItem->LineColors->InsertTuple4(
744 vertOffset + j, color4[0], color4[1], color4[2], color4[3]);
747 vertOffset += this->NumPointsCell;
748 this->CellColors->
Reset();
749 this->CellPoints.clear();
754 cacheItem->LinesLoadingTime.Modified();
758 if (cacheItem->Lines.size() > 0)
760 this->Device->
DrawLines(&cacheItem->Lines[0],
static_cast<int>(cacheItem->Lines.size() / 2),
761 static_cast<unsigned char*
>(cacheItem->LineColors->GetVoidPointer(0)),
762 cacheItem->LineColors->GetNumberOfComponents());
781 this->NumPointsCell = genericCell->GetNumberOfPoints();
782 this->PointIds = genericCell->GetPointIds()->
GetPointer(0);
783 numTriVert += 3 * (this->NumPointsCell - 2);
796 vtkPolyData* polyData,
int scalarMode,
float const x,
float const y,
float const scale)
798 PolyDataCacheItem* cacheItem = this->cache->GetCacheEntry(polyData);
800 if (polyData->
GetMTime() > cacheItem->PolygonsLoadingTime)
802 cacheItem->PolyTri.clear();
803 cacheItem->PolyColors->Reset();
806 vtkIdType const totalTriVert = this->GetCountTriangleVertices(polyData);
807 cacheItem->PolyTri.reserve(totalTriVert * 2);
809 cacheItem->PolyColors->SetNumberOfTuples(totalTriVert);
826 this->NumPointsCell = genericCell->GetNumberOfPoints();
827 this->PointIds = genericCell->GetPointIds()->
GetPointer(0);
829 this->MapCurrentCell(x, y,
scale, cellId, scalarMode);
832 for (
int i = 0; i < this->NumPointsCell - 2; i++)
834 cacheItem->PolyTri.push_back(this->CellPoints[0]);
835 cacheItem->PolyTri.push_back(this->CellPoints[1]);
836 cacheItem->PolyTri.push_back(this->CellPoints[i * 2 + 2]);
837 cacheItem->PolyTri.push_back(this->CellPoints[i * 2 + 3]);
838 cacheItem->PolyTri.push_back(this->CellPoints[i * 2 + 4]);
839 cacheItem->PolyTri.push_back(this->CellPoints[i * 2 + 5]);
842 vtkIdType const triangOffset = vertOffset + 3 * i;
843 double* color4 = this->CellColors->GetTuple(0);
844 cacheItem->PolyColors->InsertTuple4(
845 triangOffset, color4[0], color4[1], color4[2], color4[3]);
847 color4 = this->CellColors->GetTuple(i + 1);
848 cacheItem->PolyColors->InsertTuple4(
849 triangOffset + 1, color4[0], color4[1], color4[2], color4[3]);
851 color4 = this->CellColors->GetTuple(i + 2);
852 cacheItem->PolyColors->InsertTuple4(
853 triangOffset + 2, color4[0], color4[1], color4[2], color4[3]);
856 vertOffset += 3 * (this->NumPointsCell - 2);
857 this->CellColors->
Reset();
858 this->CellPoints.clear();
862 cacheItem->PolygonsLoadingTime.Modified();
866 if (cacheItem->PolyTri.size() > 0)
869 static_cast<unsigned char*
>(cacheItem->PolyColors->GetVoidPointer(0)), 4);
884 std::vector<float> CellPoints;
888 PolyDataCache* cache;
890 #endif // VTKOPENGLCONTEXTDEVICE2DPRIVATE_H