33 #ifndef vtkOpenGLContextDevice2DPrivate_h
34 #define vtkOpenGLContextDevice2DPrivate_h
94 return this->first == other.first;
131 typename std::list<CacheElement >::iterator it;
132 for (it = this->
Cache.begin(); it != this->
Cache.end(); ++it)
134 it->second.Texture->ReleaseGraphicsResources(window);
150 this->
Cache.pop_back();
152 this->
Cache.push_front(CacheElement(
key, cacheData));
153 return this->
Cache.begin()->second;
173 typename std::list<CacheElement>::iterator it =
174 std::find(this->Cache.begin(), this->Cache.end(), CacheElement(
key));
175 if (it != this->Cache.end())
182 cacheData.Texture->SetInputData(cacheData.ImageData);
183 return this->AddCacheData(
key, cacheData);
189 template <
class StringType>
206 vtkTypeUInt32 hash = static_cast<vtkTypeUInt32>(
id);
210 assert(
"Hash is really a uint32" && static_cast<size_t>(hash) ==
id);
236 static_cast<unsigned char>(
color[1] * 255),
237 static_cast<unsigned char>(
color[2] * 255),
238 static_cast<unsigned char>(textProperty->
GetOpacity() * 255));
343 float* texCoord =
new float[2*n];
344 float minX = f[0];
float minY = f[1];
345 float maxX = f[0];
float maxY = f[1];
347 for(
int i = 0; i < n; ++i)
349 minX = fptr[0] < minX ? fptr[0] : minX;
350 maxX = fptr[0] > maxX ? fptr[0] : maxX;
351 minY = fptr[1] < minY ? fptr[1] : minY;
352 maxY = fptr[1] > maxY ? fptr[1] : maxY;
359 float rangeX = (textureBounds[1] - textureBounds[0]) ?
360 textureBounds[1] - textureBounds[0] : 1.;
361 float rangeY = (textureBounds[3] - textureBounds[2]) ?
362 textureBounds[3] - textureBounds[2] : 1.;
363 for (
int i = 0; i < n; ++i)
365 texCoord[i*2] = (fptr[0]-minX) / rangeX;
366 texCoord[i*2+1] = (fptr[1]-minY) / rangeY;
372 float rangeX = (maxX - minX)? maxX - minX : 1.f;
373 float rangeY = (maxY - minY)? maxY - minY : 1.f;
374 for (
int i = 0; i < n; ++i)
376 texCoord[i*2] = (fptr[0]-minX)/rangeX;
377 texCoord[i*2+1] = (fptr[1]-minY)/rangeY;
387 for (
int i = 0; i < 2; ++i)
389 while (pow2[i] <
size[i])
401 vtkGenericWarningMacro(
"Invalid image format: expected unsigned char.");
404 int bytesPerPixel =
image->GetNumberOfScalarComponents();
409 for (
int i = 0; i < 2; ++i)
411 texCoords[i] =
size[i] /
float(newImg[i]);
414 unsigned char *dataPtr =
415 new unsigned char[newImg[0] * newImg[1] * bytesPerPixel];
416 unsigned char *origPtr =
417 static_cast<unsigned char*>(
image->GetScalarPointer());
419 for (
int i = 0; i < newImg[0]; ++i)
421 for (
int j = 0; j < newImg[1]; ++j)
423 for (
int k = 0; k < bytesPerPixel; ++k)
427 dataPtr[i * bytesPerPixel + j * newImg[0] * bytesPerPixel + k] =
428 origPtr[i * bytesPerPixel + j *
size[0] * bytesPerPixel + k];
432 dataPtr[i * bytesPerPixel + j * newImg[0] * bytesPerPixel + k] =
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 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
447 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
448 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
450 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
453 glTexImage2D(GL_TEXTURE_2D, 0 , glInternalFormat,
454 newImg[0], newImg[1], 0, glFormat,
455 GL_UNSIGNED_BYTE, static_cast<const GLvoid *>(dataPtr));
464 cout <<
"Error = not an unsigned char..." << endl;
467 int bytesPerPixel =
image->GetNumberOfScalarComponents();
471 unsigned char *dataPtr =
472 static_cast<unsigned char*>(
image->GetScalarPointer());
474 GLint glFormat = bytesPerPixel == 3 ? GL_RGB : GL_RGBA;
475 GLint glInternalFormat = bytesPerPixel == 3 ? GL_RGB8 : GL_RGBA8;
477 glGenTextures(1, &tmpIndex);
478 glBindTexture(GL_TEXTURE_2D, tmpIndex);
480 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
481 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
482 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
484 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
487 glTexImage2D(GL_TEXTURE_2D, 0 , glInternalFormat,
489 GL_UNSIGNED_BYTE, static_cast<const GLvoid *>(dataPtr));
558 this->cache =
new PolyDataCache();
573 this->Colors = colors;
579 this->DrawLines(polyData, scalarMode, x, y,
scale);
583 this->DrawPolygons(polyData, scalarMode, x, y,
scale);
590 this->cache->SwapCaches();
597 struct PolyDataCacheItem
601 std::vector<float> PolyTri;
605 std::vector<float> Lines;
614 std::map<vtkPolyData*, PolyDataCacheItem*>::iterator itPrev =
615 this->PrevFrameCache.begin();
616 for (; itPrev != this->PrevFrameCache.end(); ++itPrev)
618 delete itPrev->second;
621 std::map<vtkPolyData*, PolyDataCacheItem*>::iterator it =
622 this->CurrentFrameCache.begin();
623 for (; it != this->CurrentFrameCache.end(); ++it)
631 PolyDataCacheItem* cacheItem = this->CurrentFrameCache[
key];
632 if (cacheItem ==
nullptr)
634 cacheItem = this->PrevFrameCache[
key];
635 if (cacheItem ==
nullptr)
637 cacheItem =
new PolyDataCacheItem();
644 this->PrevFrameCache.erase(
key);
648 this->CurrentFrameCache[
key] = cacheItem;
659 std::map<vtkPolyData*, PolyDataCacheItem*>::iterator itPrev =
660 this->PrevFrameCache.begin();
661 for (; itPrev != this->PrevFrameCache.end(); ++itPrev)
663 delete itPrev->second;
667 this->PrevFrameCache.clear();
670 std::swap(this->PrevFrameCache, this->CurrentFrameCache);
675 std::map<vtkPolyData*, PolyDataCacheItem*> PrevFrameCache;
676 std::map<vtkPolyData*, PolyDataCacheItem*> CurrentFrameCache;
682 void MapCurrentCell (
float const posX,
float const posY,
float const scale,
685 this->CellPoints.reserve(this->NumPointsCell * 2);
687 for (
int i = 0; i < this->NumPointsCell; i++)
693 float const x = static_cast<float>(
point[0]) + posX;
694 float const y = static_cast<float>(
point[1]) + posY;
695 this->CellPoints.push_back(x *
scale);
696 this->CellPoints.push_back(y *
scale);
703 mappedColorId = this->PointIds[i];
706 mappedColorId = cellId;
709 std::cerr <<
"Scalar mode not supported!" << std::endl;
713 this->CellColors->
SetTuple(i, mappedColorId, this->Colors);
722 void DrawLines(
vtkPolyData* polyData,
int scalarMode,
float const x,
723 float const y,
float const scale)
725 PolyDataCacheItem* cacheItem = this->cache->GetCacheEntry(polyData);
727 if (polyData->
GetMTime() > cacheItem->LinesLoadingTime)
730 cacheItem->Lines.clear();
731 cacheItem->LineColors->Reset();
735 cacheItem->Lines.reserve(numVertices * 2);
737 cacheItem->LineColors->SetNumberOfTuples(numVertices);
753 for (
int i = 0; i < actualNumPointsCell - 1; ++i)
755 this->NumPointsCell = 2;
758 this->MapCurrentCell(x, y,
scale, cellId, scalarMode);
761 for (
int j = 0; j < this->NumPointsCell; j++)
763 cacheItem->Lines.push_back(this->CellPoints[2 * j]);
764 cacheItem->Lines.push_back(this->CellPoints[2 * j + 1]);
766 double* color4 = this->CellColors->
GetTuple(j);
767 cacheItem->LineColors->InsertTuple4(vertOffset + j, color4[0], color4[1], color4[2],
771 vertOffset += this->NumPointsCell;
772 this->CellColors->
Reset();
773 this->CellPoints.clear();
778 cacheItem->LinesLoadingTime.Modified();
782 if (cacheItem->Lines.size() > 0)
784 this->Device->
DrawLines(&cacheItem->Lines[0], cacheItem->Lines.size() / 2,
785 static_cast<unsigned char*>(cacheItem->LineColors->GetVoidPointer(0)),
786 cacheItem->LineColors->GetNumberOfComponents());
808 numTriVert += 3 * (this->NumPointsCell - 2);
820 void DrawPolygons(
vtkPolyData* polyData,
int scalarMode,
float const x,
821 float const y,
float const scale)
823 PolyDataCacheItem* cacheItem = this->cache->GetCacheEntry(polyData);
825 if (polyData->
GetMTime() > cacheItem->PolygonsLoadingTime)
827 cacheItem->PolyTri.clear();
828 cacheItem->PolyColors->Reset();
831 vtkIdType const totalTriVert = this->GetCountTriangleVertices(polyData);
832 cacheItem->PolyTri.reserve(totalTriVert * 2);
834 cacheItem->PolyColors->SetNumberOfTuples(totalTriVert);
856 this->MapCurrentCell(x, y,
scale, cellId, scalarMode);
859 for (
int i = 0; i < this->NumPointsCell - 2; i++)
861 cacheItem->PolyTri.push_back(this->CellPoints[0]);
862 cacheItem->PolyTri.push_back(this->CellPoints[1]);
863 cacheItem->PolyTri.push_back(this->CellPoints[i * 2 + 2]);
864 cacheItem->PolyTri.push_back(this->CellPoints[i * 2 + 3]);
865 cacheItem->PolyTri.push_back(this->CellPoints[i * 2 + 4]);
866 cacheItem->PolyTri.push_back(this->CellPoints[i * 2 + 5]);
869 vtkIdType const triangOffset = vertOffset + 3 * i;
870 double* color4 = this->CellColors->
GetTuple(0);
871 cacheItem->PolyColors->InsertTuple4(triangOffset, color4[0], color4[1],
872 color4[2], color4[3]);
874 color4 = this->CellColors->
GetTuple(i + 1);
875 cacheItem->PolyColors->InsertTuple4(triangOffset + 1, color4[0], color4[1],
876 color4[2], color4[3]);
878 color4 = this->CellColors->
GetTuple(i + 2);
879 cacheItem->PolyColors->InsertTuple4(triangOffset + 2, color4[0], color4[1],
880 color4[2], color4[3]);
883 vertOffset += 3 * (this->NumPointsCell - 2);
884 this->CellColors->
Reset();
885 this->CellPoints.clear();
889 cacheItem->PolygonsLoadingTime.Modified();
893 if (cacheItem->PolyTri.size() > 0)
896 static_cast<unsigned char*>(cacheItem->PolyColors->GetVoidPointer(0)), 4);
911 std::vector<float> CellPoints;
915 PolyDataCache* cache;
917 #endif // VTKOPENGLCONTEXTDEVICE2DPRIVATE_H