33 #ifndef vtkOpenGLContextDevice2DPrivate_h
34 #define vtkOpenGLContextDevice2DPrivate_h
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;
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);
181 return this->AddCacheData(
key, cacheData);
187 template <
class StringType>
204 vtkTypeUInt32 hash = static_cast<vtkTypeUInt32>(
id);
208 assert(
"Hash is really a uint32" && static_cast<size_t>(hash) ==
id);
234 static_cast<unsigned char>(
color[1] * 255),
235 static_cast<unsigned char>(
color[2] * 255),
236 static_cast<unsigned char>(textProperty->
GetOpacity() * 255));
341 float* texCoord =
new float[2*n];
342 float minX = f[0];
float minY = f[1];
343 float maxX = f[0];
float maxY = f[1];
345 for(
int i = 0; i < n; ++i)
347 minX = fptr[0] < minX ? fptr[0] : minX;
348 maxX = fptr[0] > maxX ? fptr[0] : maxX;
349 minY = fptr[1] < minY ? fptr[1] : minY;
350 maxY = fptr[1] > maxY ? fptr[1] : maxY;
357 float rangeX = (textureBounds[1] - textureBounds[0]) ?
358 textureBounds[1] - textureBounds[0] : 1.;
359 float rangeY = (textureBounds[3] - textureBounds[2]) ?
360 textureBounds[3] - textureBounds[2] : 1.;
361 for (
int i = 0; i < n; ++i)
363 texCoord[i*2] = (fptr[0]-minX) / rangeX;
364 texCoord[i*2+1] = (fptr[1]-minY) / rangeY;
370 float rangeX = (maxX - minX)? maxX - minX : 1.f;
371 float rangeY = (maxY - minY)? maxY - minY : 1.f;
372 for (
int i = 0; i < n; ++i)
374 texCoord[i*2] = (fptr[0]-minX)/rangeX;
375 texCoord[i*2+1] = (fptr[1]-minY)/rangeY;
385 for (
int i = 0; i < 2; ++i)
387 while (pow2[i] <
size[i])
399 vtkGenericWarningMacro(
"Invalid image format: expected unsigned char.");
402 int bytesPerPixel =
image->GetNumberOfScalarComponents();
407 for (
int i = 0; i < 2; ++i)
409 texCoords[i] =
size[i] /
float(newImg[i]);
412 unsigned char *dataPtr =
413 new unsigned char[newImg[0] * newImg[1] * bytesPerPixel];
414 unsigned char *origPtr =
415 static_cast<unsigned char*>(
image->GetScalarPointer());
417 for (
int i = 0; i < newImg[0]; ++i)
419 for (
int j = 0; j < newImg[1]; ++j)
421 for (
int k = 0; k < bytesPerPixel; ++k)
425 dataPtr[i * bytesPerPixel + j * newImg[0] * bytesPerPixel + k] =
426 origPtr[i * bytesPerPixel + j *
size[0] * bytesPerPixel + k];
430 dataPtr[i * bytesPerPixel + j * newImg[0] * bytesPerPixel + k] =
438 GLint glFormat = bytesPerPixel == 3 ? GL_RGB : GL_RGBA;
439 GLint glInternalFormat = bytesPerPixel == 3 ? GL_RGB8 : GL_RGBA8;
441 glGenTextures(1, &tmpIndex);
442 glBindTexture(GL_TEXTURE_2D, tmpIndex);
444 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
445 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
446 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
448 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
451 glTexImage2D(GL_TEXTURE_2D, 0 , glInternalFormat,
452 newImg[0], newImg[1], 0, glFormat,
453 GL_UNSIGNED_BYTE, static_cast<const GLvoid *>(dataPtr));
462 cout <<
"Error = not an unsigned char..." << endl;
465 int bytesPerPixel =
image->GetNumberOfScalarComponents();
469 unsigned char *dataPtr =
470 static_cast<unsigned char*>(
image->GetScalarPointer());
472 GLint glFormat = bytesPerPixel == 3 ? GL_RGB : GL_RGBA;
473 GLint glInternalFormat = bytesPerPixel == 3 ? GL_RGB8 : GL_RGBA8;
475 glGenTextures(1, &tmpIndex);
476 glBindTexture(GL_TEXTURE_2D, tmpIndex);
478 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
479 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
480 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
482 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
485 glTexImage2D(GL_TEXTURE_2D, 0 , glInternalFormat,
487 GL_UNSIGNED_BYTE, static_cast<const GLvoid *>(dataPtr));
565 this->Colors = colors;
571 this->DrawLines(cellArray, scalarMode, x, y,
scale);
575 this->DrawPolygons(cellArray, scalarMode, x, y,
scale);
587 void MapCurrentCell (
float const posX,
float const posY,
float const scale,
590 this->CellPoints.reserve(this->NumPointsCell * 2);
592 for (
int i = 0; i < this->NumPointsCell; i++)
598 float const x = static_cast<float>(
point[0]) + posX;
599 float const y = static_cast<float>(
point[1]) + posY;
600 this->CellPoints.push_back(x *
scale);
601 this->CellPoints.push_back(y *
scale);
608 mappedColorId = this->PointIds[i];
611 mappedColorId = cellId;
614 std::cerr <<
"Scalar mode not supported!" << std::endl;
618 this->CellColors->
SetTuple(i, mappedColorId, this->Colors);
627 void DrawLines(
vtkCellArray* cellArray,
int scalarMode,
float const x,
628 float const y,
float const scale)
630 if (cellArray->
GetMTime() > this->LinesLoadingTime)
633 this->LineColors->
Reset();
637 this->Lines.reserve(numVertices * 2);
644 this->PointIds); cellId++)
646 this->MapCurrentCell(x, y,
scale, cellId, scalarMode);
649 for (
int i = 0; i < this->NumPointsCell; i++)
651 this->Lines.push_back(this->CellPoints[2 * i]);
652 this->Lines.push_back(this->CellPoints[2 * i + 1]);
654 double* color4 = this->CellColors->
GetTuple(i);
655 this->LineColors->
InsertTuple4(vertOffset + i, color4[0], color4[1], color4[2],
659 vertOffset += this->NumPointsCell;
660 this->CellColors->
Reset();
661 this->CellPoints.clear();
667 this->Device->
DrawLines(&this->Lines[0], this->Lines.size() / 2,
681 this->PointIds); cellId++)
683 numTriVert += 3 * (this->NumPointsCell - 2);
694 void DrawPolygons(
vtkCellArray* cellArray,
int scalarMode,
float const x,
695 float const y,
float const scale)
697 if (cellArray->
GetMTime() > this->PolygonsLoadingTime)
699 this->PolyTri.clear();
700 this->PolyColors->
Reset();
703 vtkIdType const totalTriVert = this->GetCountTriangleVertices(cellArray);
704 this->PolyTri.reserve(totalTriVert * 2);
713 this->PointIds); cellId++)
715 this->MapCurrentCell(x, y,
scale, cellId, scalarMode);
718 for (
int i = 0; i < this->NumPointsCell - 2; i++)
720 this->PolyTri.push_back(this->CellPoints[0]);
721 this->PolyTri.push_back(this->CellPoints[1]);
722 this->PolyTri.push_back(this->CellPoints[i * 2 + 2]);
723 this->PolyTri.push_back(this->CellPoints[i * 2 + 3]);
724 this->PolyTri.push_back(this->CellPoints[i * 2 + 4]);
725 this->PolyTri.push_back(this->CellPoints[i * 2 + 5]);
728 vtkIdType const triangOffset = vertOffset + 3 * i;
729 double* color4 = this->CellColors->
GetTuple(0);
730 this->PolyColors->
InsertTuple4(triangOffset, color4[0], color4[1],
731 color4[2], color4[3]);
733 color4 = this->CellColors->
GetTuple(i + 1);
734 this->PolyColors->
InsertTuple4(triangOffset + 1, color4[0], color4[1],
735 color4[2], color4[3]);
737 color4 = this->CellColors->
GetTuple(i + 2);
738 this->PolyColors->
InsertTuple4(triangOffset + 2, color4[0], color4[1],
739 color4[2], color4[3]);
742 vertOffset += 3 * (this->NumPointsCell - 2);
743 this->CellColors->
Reset();
744 this->CellPoints.clear();
747 this->PolygonsLoadingTime.
Modified();
751 static_cast<unsigned char*>(this->PolyColors->
GetVoidPointer(0)), 4);
765 std::vector<float> CellPoints;
773 std::vector<float> PolyTri;
782 std::vector<float> Lines;
787 #endif // VTKOPENGLCONTEXTDEVICE2DPRIVATE_H