1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkOpenGLImageSliceMapper.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkOpenGLImageSliceMapper.h"
#include "vtk_glew.h"
#include "vtkObjectFactory.h"
#include "vtkImageData.h"
#include "vtkImageSlice.h"
#include "vtkImageProperty.h"
#include "vtkDataArray.h"
#include "vtkLookupTable.h"
#include "vtkPoints.h"
#include "vtkMatrix4x4.h"
#include "vtkMath.h"
#include "vtkMapper.h"
#include "vtkOpenGLCamera.h"
#include "vtkOpenGLRenderer.h"
#include "vtkOpenGLRenderWindow.h"
#include "vtkTimerLog.h"
#include "vtkGarbageCollector.h"
#include "vtkTemplateAliasMacro.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkTexture.h"
#include "vtkPointData.h"
#include "vtkOpenGLPolyDataMapper.h"
#include "vtkNew.h"
#include "vtkPolyData.h"
#include "vtkCellArray.h"
#include "vtkFloatArray.h"
#include "vtkTrivialProducer.h"
#include "vtkActor.h"
#include "vtkProperty.h"
#include <cmath>
#include "vtkOpenGLError.h"
vtkStandardNewMacro(vtkOpenGLImageSliceMapper);
//----------------------------------------------------------------------------
// Initializes an instance, generates a unique index.
vtkOpenGLImageSliceMapper::vtkOpenGLImageSliceMapper()
{
// setup the polygon mapper
{
vtkNew<vtkPolyData> polydata;
vtkNew<vtkPoints> points;
points->SetNumberOfPoints(4);
polydata->SetPoints(points.Get());
vtkNew<vtkCellArray> tris;
polydata->SetPolys(tris.Get());
vtkNew<vtkFloatArray> tcoords;
tcoords->SetNumberOfComponents(2);
tcoords->SetNumberOfTuples(4);
polydata->GetPointData()->SetTCoords(tcoords.Get());
vtkNew<vtkTrivialProducer> prod;
prod->SetOutput(polydata.Get());
vtkNew<vtkOpenGLPolyDataMapper> polyDataMapper;
polyDataMapper->SetInputConnection(prod->GetOutputPort());
this->PolyDataActor = vtkActor::New();
this->PolyDataActor->SetMapper(polyDataMapper.Get());
vtkNew<vtkTexture> texture;
texture->RepeatOff();
this->PolyDataActor->SetTexture(texture.Get());
}
// setup the backing polygon mapper
{
vtkNew<vtkPolyData> polydata;
vtkNew<vtkPoints> points;
points->SetNumberOfPoints(4);
polydata->SetPoints(points.Get());
vtkNew<vtkCellArray> tris;
polydata->SetPolys(tris.Get());
vtkNew<vtkTrivialProducer> prod;
prod->SetOutput(polydata.Get());
vtkNew<vtkOpenGLPolyDataMapper> polyDataMapper;
polyDataMapper->SetInputConnection(prod->GetOutputPort());
this->BackingPolyDataActor = vtkActor::New();
this->BackingPolyDataActor->SetMapper(polyDataMapper.Get());
}
// setup the background polygon mapper
{
vtkNew<vtkPolyData> polydata;
vtkNew<vtkPoints> points;
points->SetNumberOfPoints(10);
polydata->SetPoints(points.Get());
vtkNew<vtkCellArray> tris;
polydata->SetPolys(tris.Get());
vtkNew<vtkTrivialProducer> prod;
prod->SetOutput(polydata.Get());
vtkNew<vtkOpenGLPolyDataMapper> polyDataMapper;
polyDataMapper->SetInputConnection(prod->GetOutputPort());
this->BackgroundPolyDataActor = vtkActor::New();
this->BackgroundPolyDataActor->SetMapper(polyDataMapper.Get());
}
this->FragmentShaderIndex = 0;
this->RenderWindow = 0;
this->TextureSize[0] = 0;
this->TextureSize[1] = 0;
this->TextureBytesPerPixel = 1;
this->LastOrientation = -1;
this->LastSliceNumber = VTK_INT_MAX;
this->LoadCount = 0;
this->UseClampToEdge = false;
// Use GL_ARB_fragment_program, which is an extension to OpenGL 1.3
// that is compatible with very old drivers and hardware, and is still
// fully supported on modern hardware. The only caveat is that it is
// automatically disabled if any modern shaders (e.g. depth peeling)
// are simultaneously loaded, so it will not interfere with them.
this->UseFragmentProgram = false;
}
//----------------------------------------------------------------------------
vtkOpenGLImageSliceMapper::~vtkOpenGLImageSliceMapper()
{
this->RenderWindow = NULL;
this->BackgroundPolyDataActor->UnRegister(this);
this->BackingPolyDataActor->UnRegister(this);
this->PolyDataActor->UnRegister(this);
}
//----------------------------------------------------------------------------
// Release the graphics resources used by this texture.
void vtkOpenGLImageSliceMapper::ReleaseGraphicsResources(vtkWindow *renWin)
{
this->BackgroundPolyDataActor->ReleaseGraphicsResources(renWin);
this->BackingPolyDataActor->ReleaseGraphicsResources(renWin);
this->PolyDataActor->ReleaseGraphicsResources(renWin);
this->FragmentShaderIndex = 0;
this->RenderWindow = NULL;
this->Modified();
}
//----------------------------------------------------------------------------
// Subdivide the image until the pieces fit into texture memory
void vtkOpenGLImageSliceMapper::RecursiveRenderTexturedPolygon(
vtkRenderer *ren, vtkImageProperty *property,
vtkImageData *input, int extent[6], bool recursive)
{
int xdim, ydim;
int imageSize[2];
int textureSize[2];
// compute image size and texture size from extent
this->ComputeTextureSize(
extent, xdim, ydim, imageSize, textureSize);
// Check if we can fit this texture in memory
if (this->TextureSizeOK(textureSize))
{
// We can fit it - render
this->RenderTexturedPolygon(
ren, property, input, extent, recursive);
}
// If the texture does not fit, then subdivide and render
// each half. Unless the graphics card couldn't handle
// a texture a small as 256x256, because if it can't handle
// that, then something has gone horribly wrong.
else if (textureSize[0] > 256 || textureSize[1] > 256)
{
int subExtent[6];
subExtent[0] = extent[0]; subExtent[1] = extent[1];
subExtent[2] = extent[2]; subExtent[3] = extent[3];
subExtent[4] = extent[4]; subExtent[5] = extent[5];
// Which is larger, x or y?
int idx = ydim;
int tsize = textureSize[1];
if (textureSize[0] > textureSize[1])
{
idx = xdim;
tsize = textureSize[0];
}
// Divide size by two
tsize /= 2;
// Render each half recursively
subExtent[idx*2] = extent[idx*2];
subExtent[idx*2 + 1] = extent[idx*2] + tsize - 1;
this->RecursiveRenderTexturedPolygon(
ren, property, input, subExtent, true);
subExtent[idx*2] = subExtent[idx*2] + tsize;
subExtent[idx*2 + 1] = extent[idx*2 + 1];
this->RecursiveRenderTexturedPolygon(
ren, property, input, subExtent, true);
}
}
//----------------------------------------------------------------------------
// Load the given image extent into a texture and render it
void vtkOpenGLImageSliceMapper::RenderTexturedPolygon(
vtkRenderer *ren, vtkImageProperty *property,
vtkImageData *input, int extent[6], bool recursive)
{
// get the previous texture load time
vtkMTimeType loadTime = this->LoadTime.GetMTime();
// the render window, needed for state information
vtkOpenGLRenderWindow *renWin =
static_cast<vtkOpenGLRenderWindow *>(ren->GetRenderWindow());
bool reuseTexture = true;
// if context has changed, verify context capabilities
if (renWin != this->RenderWindow ||
renWin->GetContextCreationTime() > loadTime)
{
// force two initial loads for each new context
this->RenderWindow = renWin;
this->LoadCount = 0;
this->CheckOpenGLCapabilities(renWin);
reuseTexture = false;
}
vtkOpenGLClearErrorMacro();
// get information about the image
int xdim, ydim; // orientation of texture wrt input image
vtkImageSliceMapper::GetDimensionIndices(this->Orientation, xdim, ydim);
// check whether to use a shader for bicubic interpolation
//bool checkerboard = (property && property->GetCheckerboard());
//bool cubicInterpolation = (property &&
// property->GetInterpolationType() == VTK_CUBIC_INTERPOLATION);
//bool useFragmentProgram = false;
// (this->UseFragmentProgram &&
// (!this->ExactPixelMatch || !this->SliceFacesCamera) &&
// (cubicInterpolation || checkerboard));
// verify that the orientation and slice has not changed
bool orientationChanged = (this->Orientation != this->LastOrientation);
this->LastOrientation = this->Orientation;
bool sliceChanged = (this->SliceNumber != this->LastSliceNumber);
this->LastSliceNumber = this->SliceNumber;
// get the mtime of the property, including the lookup table
vtkMTimeType propertyMTime = 0;
if (property)
{
propertyMTime = property->GetMTime();
if (!this->PassColorData)
{
vtkScalarsToColors *table = property->GetLookupTable();
if (table)
{
vtkMTimeType mtime = table->GetMTime();
if (mtime > propertyMTime)
{
propertyMTime = mtime;
}
}
}
}
// need to reload the texture
if (this->vtkImageMapper3D::GetMTime() > loadTime ||
propertyMTime > loadTime ||
input->GetMTime() > loadTime ||
orientationChanged || sliceChanged ||
this->LoadCount < 2 || recursive)
{
this->LoadCount++;
// get the data to load as a texture
int xsize;
int ysize;
int bytesPerPixel;
// whether to try to use the input data directly as the texture
bool reuseData = true;
// generate the data to be used as a texture
unsigned char *data = this->MakeTextureData(
(this->PassColorData ? 0 : property), input, extent, xsize, ysize,
bytesPerPixel, reuseTexture, reuseData);
this->TextureSize[0] = xsize;
this->TextureSize[1] = ysize;
this->TextureBytesPerPixel = bytesPerPixel;
vtkImageData *id = vtkImageData::New();
id->SetExtent(0,xsize-1,0,ysize-1,0,0);
vtkUnsignedCharArray *uca = vtkUnsignedCharArray::New();
uca->SetNumberOfComponents(bytesPerPixel);
uca->SetArray(data,xsize*ysize*bytesPerPixel,reuseData,
vtkAbstractArray::VTK_DATA_ARRAY_DELETE);
id->GetPointData()->SetScalars(uca);
uca->Delete();
this->PolyDataActor->GetTexture()->SetInputData(id);
id->Delete();
if (property->GetInterpolationType() == VTK_NEAREST_INTERPOLATION &&
!this->ExactPixelMatch)
{
this->PolyDataActor->GetTexture()->InterpolateOff();
}
else
{
this->PolyDataActor->GetTexture()->InterpolateOn();
}
if (this->UseClampToEdge)
{
this->PolyDataActor->GetTexture()->EdgeClampOn();
}
else
{
this->PolyDataActor->GetTexture()->EdgeClampOff();
}
// modify the load time to the current time
this->LoadTime.Modified();
}
vtkPoints *points = this->Points;
if (this->ExactPixelMatch && this->SliceFacesCamera)
{
points = 0;
}
this->RenderPolygon(this->PolyDataActor, points, extent, ren);
if (this->Background)
{
double ambient = property->GetAmbient();
double diffuse = property->GetDiffuse();
double bkcolor[4];
this->GetBackgroundColor(property, bkcolor);
vtkProperty *pdProp = this->BackgroundPolyDataActor->GetProperty();
pdProp->SetAmbient(ambient);
pdProp->SetDiffuse(diffuse);
pdProp->SetColor(bkcolor[0], bkcolor[1], bkcolor[2]);
this->RenderBackground(this->BackgroundPolyDataActor, points, extent, ren);
}
vtkOpenGLCheckErrorMacro("failed after RenderTexturedPolygon");
}
//----------------------------------------------------------------------------
// Render the polygon that displays the image data
void vtkOpenGLImageSliceMapper::RenderPolygon(
vtkActor *actor, vtkPoints *points, const int extent[6], vtkRenderer *ren)
{
vtkOpenGLClearErrorMacro();
bool textured = (actor->GetTexture() != NULL);
vtkPolyData *poly = vtkPolyDataMapper::SafeDownCast(actor->GetMapper())->GetInput();
vtkPoints *polyPoints = poly->GetPoints();
vtkCellArray *tris = poly->GetPolys();
vtkDataArray *polyTCoords = poly->GetPointData()->GetTCoords();
if (!points)
{
double coords[12], tcoords[8];
this->MakeTextureGeometry(extent, coords, tcoords);
tris->Initialize();
tris->InsertNextCell(3);
tris->InsertCellPoint(0);
tris->InsertCellPoint(1);
tris->InsertCellPoint(2);
tris->InsertNextCell(3);
tris->InsertCellPoint(0);
tris->InsertCellPoint(2);
tris->InsertCellPoint(3);
tris->Modified();
polyPoints->SetNumberOfPoints(4);
if (textured)
{
polyTCoords->SetNumberOfTuples(4);
}
for (int i = 0; i < 4; i++)
{
polyPoints->SetPoint(i, coords[3*i], coords[3*i+1], coords[3*i+2]);
if (textured)
{
polyTCoords->SetTuple(i,&tcoords[2*i]);
}
}
}
else if (points->GetNumberOfPoints())
{
int xdim, ydim;
vtkImageSliceMapper::GetDimensionIndices(this->Orientation, xdim, ydim);
double *origin = this->DataOrigin;
double *spacing = this->DataSpacing;
double xshift = origin[xdim] - (0.5 - extent[2*xdim])*spacing[xdim];
double xscale = this->TextureSize[xdim]*spacing[xdim];
double yshift = origin[ydim] - (0.5 - extent[2*ydim])*spacing[ydim];
double yscale = this->TextureSize[ydim]*spacing[ydim];
vtkIdType ncoords = points->GetNumberOfPoints();
double coord[3];
double tcoord[2];
polyPoints->DeepCopy(points);
if (textured)
{
polyTCoords->SetNumberOfTuples(ncoords);
}
tris->Initialize();
tris->Allocate(4*(ncoords-2));
for (vtkIdType i = 0; i < ncoords; i++)
{
if (textured)
{
points->GetPoint(i, coord);
tcoord[0] = (coord[0] - xshift)/xscale;
tcoord[1] = (coord[1] - yshift)/yscale;
polyTCoords->SetTuple(i,tcoord);
}
if (i >= 2)
{
tris->InsertNextCell(3);
tris->InsertCellPoint(ncoords - (i+1)/2);
tris->InsertCellPoint(i/2 - 1);
tris->InsertCellPoint((i % 2 == 0) ? ncoords - 1 - i/2 : i/2);
}
}
tris->Modified();
}
if (textured)
{
actor->GetTexture()->Render(ren);
}
actor->GetMapper()->SetClippingPlanes(this->GetClippingPlanes());
actor->GetMapper()->Render(ren, actor);
if (textured)
{
actor->GetTexture()->PostRender(ren);
}
vtkOpenGLCheckErrorMacro("failed after RenderPolygon");
}
//----------------------------------------------------------------------------
// Render a wide black border around the polygon, wide enough to fill
// the entire viewport.
void vtkOpenGLImageSliceMapper::RenderBackground(
vtkActor *actor, vtkPoints *points, const int extent[6], vtkRenderer *ren)
{
vtkOpenGLClearErrorMacro();
vtkPolyData *poly = vtkPolyDataMapper::SafeDownCast(actor->GetMapper())->GetInput();
vtkPoints *polyPoints = poly->GetPoints();
vtkCellArray *tris = poly->GetPolys();
static double borderThickness = 1e6;
int xdim, ydim;
vtkImageSliceMapper::GetDimensionIndices(this->Orientation, xdim, ydim);
if (!points)
{
double coords[15], tcoords[10], center[3];
this->MakeTextureGeometry(extent, coords, tcoords);
coords[12] = coords[0];
coords[13] = coords[1];
coords[14] = coords[2];
center[0] = 0.25*(coords[0] + coords[3] + coords[6] + coords[9]);
center[1] = 0.25*(coords[1] + coords[4] + coords[7] + coords[10]);
center[2] = 0.25*(coords[2] + coords[5] + coords[8] + coords[11]);
// render 4 sides
tris->Initialize();
polyPoints->SetNumberOfPoints(10);
for (int side = 0; side < 4; side++)
{
tris->InsertNextCell(3);
tris->InsertCellPoint(side);
tris->InsertCellPoint(side+5);
tris->InsertCellPoint(side+1);
tris->InsertNextCell(3);
tris->InsertCellPoint(side+1);
tris->InsertCellPoint(side+5);
tris->InsertCellPoint(side+6);
}
for (int side = 0; side < 5; side++)
{
polyPoints->SetPoint(side, coords[3*side], coords[3*side+1], coords[3*side+2]);
double dx = coords[3*side+xdim] - center[xdim];
double sx = (dx >= 0 ? 1 : -1);
double dy = coords[3*side+ydim] - center[ydim];
double sy = (dy >= 0 ? 1 : -1);
coords[3*side+xdim] += borderThickness*sx;
coords[3*side+ydim] += borderThickness*sy;
polyPoints->SetPoint(side+5, coords[3*side], coords[3*side+1], coords[3*side+2]);
}
}
else if (points->GetNumberOfPoints())
{
vtkIdType ncoords = points->GetNumberOfPoints();
double coord[3], coord1[3];
points->GetPoint(ncoords-1, coord1);
points->GetPoint(0, coord);
double dx0 = coord[0] - coord1[0];
double dy0 = coord[1] - coord1[1];
double r = sqrt(dx0*dx0 + dy0*dy0);
dx0 /= r;
dy0 /= r;
tris->Initialize();
polyPoints->SetNumberOfPoints(ncoords*2+2);
for (vtkIdType i = 0; i < ncoords; i++)
{
tris->InsertNextCell(3);
tris->InsertCellPoint(i*2);
tris->InsertCellPoint(i*2+1);
tris->InsertCellPoint(i*2+2);
tris->InsertNextCell(3);
tris->InsertCellPoint(i*2+2);
tris->InsertCellPoint(i*2+1);
tris->InsertCellPoint(i*2+3);
}
for (vtkIdType i = 0; i <= ncoords; i++)
{
polyPoints->SetPoint(i*2,coord);
points->GetPoint(((i + 1) % ncoords), coord1);
double dx1 = coord1[0] - coord[0];
double dy1 = coord1[1] - coord[1];
r = sqrt(dx1*dx1 + dy1*dy1);
dx1 /= r;
dy1 /= r;
double t;
if (fabs(dx0 + dx1) > fabs(dy0 + dy1))
{
t = (dy1 - dy0)/(dx0 + dx1);
}
else
{
t = (dx0 - dx1)/(dy0 + dy1);
}
coord[0] += (t*dx0 + dy0)*borderThickness;
coord[1] += (t*dy0 - dx0)*borderThickness;
polyPoints->SetPoint(i*2+1,coord);
coord[0] = coord1[0];
coord[1] = coord1[1];
dx0 = dx1;
dy0 = dy1;
}
}
actor->GetMapper()->SetClippingPlanes(this->GetClippingPlanes());
actor->GetMapper()->Render(ren, actor);
vtkOpenGLCheckErrorMacro("failed after RenderBackground");
}
//----------------------------------------------------------------------------
void vtkOpenGLImageSliceMapper::BindFragmentProgram(
vtkRenderer *vtkNotUsed(ren), vtkImageProperty *vtkNotUsed(property))
{
vtkOpenGLClearErrorMacro();
// TODO: change all this
vtkOpenGLCheckErrorMacro("failed after BindFragmentProgram");
}
//----------------------------------------------------------------------------
vtkStdString vtkOpenGLImageSliceMapper::BuildFragmentProgram(
vtkImageProperty *vtkNotUsed(property))
{
// TODO change all this
vtkStdString prog =
"!!ARBfp1.0\n"
"\n";
return prog;
}
//----------------------------------------------------------------------------
void vtkOpenGLImageSliceMapper::ComputeTextureSize(
const int extent[6], int &xdim, int &ydim,
int imageSize[2], int textureSize[2])
{
// find dimension indices that will correspond to the
// columns and rows of the 2D texture
vtkImageSliceMapper::GetDimensionIndices(this->Orientation, xdim, ydim);
// compute the image dimensions
imageSize[0] = (extent[xdim*2+1] - extent[xdim*2] + 1);
imageSize[1] = (extent[ydim*2+1] - extent[ydim*2] + 1);
textureSize[0] = imageSize[0];
textureSize[1] = imageSize[1];
}
//----------------------------------------------------------------------------
// Determine if a given texture size is supported by the video card
bool vtkOpenGLImageSliceMapper::TextureSizeOK(const int size[2])
{
vtkOpenGLClearErrorMacro();
// First ask OpenGL what the max texture size is
GLint maxSize;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxSize);
if (size[0] > maxSize || size[1] > maxSize)
{
return 0;
}
vtkOpenGLCheckErrorMacro("failed after TextureSizeOK");
// if it does fit, we will render it later
return 1;
}
//----------------------------------------------------------------------------
// Set the modelview transform and load the texture
void vtkOpenGLImageSliceMapper::Render(vtkRenderer *ren, vtkImageSlice *prop)
{
vtkOpenGLClearErrorMacro();
vtkOpenGLRenderWindow *renWin =
vtkOpenGLRenderWindow::SafeDownCast(ren->GetRenderWindow());
if (renWin && (renWin != this->RenderWindow ||
renWin->GetContextCreationTime() > this->LoadTime.GetMTime()))
{
this->CheckOpenGLCapabilities(renWin);
}
// time the render
this->Timer->StartTimer();
// update the input information
vtkImageData *input = this->GetInput();
input->GetSpacing(this->DataSpacing);
input->GetOrigin(this->DataOrigin);
vtkInformation *inputInfo = this->GetInputInformation(0, 0);
inputInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),
this->DataWholeExtent);
vtkMatrix4x4 *matrix = this->GetDataToWorldMatrix();
this->PolyDataActor->SetUserMatrix(matrix);
this->BackingPolyDataActor->SetUserMatrix(matrix);
this->BackgroundPolyDataActor->SetUserMatrix(matrix);
if (prop->GetPropertyKeys())
{
this->PolyDataActor->SetPropertyKeys(prop->GetPropertyKeys());
this->BackingPolyDataActor->SetPropertyKeys(prop->GetPropertyKeys());
this->BackgroundPolyDataActor->SetPropertyKeys(prop->GetPropertyKeys());
}
// and now enable/disable as needed for our render
// glDisable(GL_CULL_FACE);
// glDisable(GL_COLOR_MATERIAL);
// do an offset to avoid depth buffer issues
// this->PolyDataActor->GetMapper()->
// SetResolveCoincidentTopology(VTK_RESOLVE_POLYGON_OFFSET);
// this->PolyDataActor->GetMapper()->
// SetRelativeCoincidentTopologyPolygonOffsetParameters(1.0,100);
// Add all the clipping planes TODO: really in the mapper
//int numClipPlanes = this->GetNumberOfClippingPlanes();
// Whether to write to the depth buffer and color buffer
glDepthMask(this->DepthEnable ? GL_TRUE : GL_FALSE); // supported in all
if (!this->ColorEnable && !this->MatteEnable)
{
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); // supported in all
}
// color and lighting related items
vtkImageProperty *property = prop->GetProperty();
double opacity = property->GetOpacity();
double ambient = property->GetAmbient();
double diffuse = property->GetDiffuse();
vtkProperty *pdProp = this->PolyDataActor->GetProperty();
pdProp->SetOpacity(opacity);
pdProp->SetAmbient(ambient);
pdProp->SetDiffuse(diffuse);
// render the backing polygon
int backing = property->GetBacking();
double *bcolor = property->GetBackingColor();
if (backing &&
(this->MatteEnable || (this->DepthEnable && !this->ColorEnable)))
{
// the backing polygon is always opaque
pdProp = this->BackingPolyDataActor->GetProperty();
pdProp->SetOpacity(1.0);
pdProp->SetAmbient(ambient);
pdProp->SetDiffuse(diffuse);
pdProp->SetColor(bcolor[0], bcolor[1], bcolor[2]);
this->RenderPolygon(this->BackingPolyDataActor, this->Points, this->DisplayExtent, ren);
if (this->Background)
{
double bkcolor[4];
this->GetBackgroundColor(property, bkcolor);
pdProp = this->BackgroundPolyDataActor->GetProperty();
pdProp->SetOpacity(1.0);
pdProp->SetAmbient(ambient);
pdProp->SetDiffuse(diffuse);
pdProp->SetColor(bkcolor[0], bkcolor[1], bkcolor[2]);
this->RenderBackground(this->BackgroundPolyDataActor, this->Points, this->DisplayExtent, ren);
}
}
// render the texture
if (this->ColorEnable || (!backing && this->DepthEnable))
{
this->RecursiveRenderTexturedPolygon(
ren, property, this->GetInput(), this->DisplayExtent, false);
}
// Set the masks back again
glDepthMask(GL_TRUE);
if (!this->ColorEnable && !this->MatteEnable)
{
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
}
this->Timer->StopTimer();
this->TimeToDraw = this->Timer->GetElapsedTime();
if (this->TimeToDraw == 0)
{
this->TimeToDraw = 0.0001;
}
vtkOpenGLCheckErrorMacro("failed after Render");
}
//----------------------------------------------------------------------------
void vtkOpenGLImageSliceMapper::CheckOpenGLCapabilities(vtkOpenGLRenderWindow*)
{
this->UseClampToEdge = true;
this->UseFragmentProgram = false;
}
//----------------------------------------------------------------------------
void vtkOpenGLImageSliceMapper::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
|