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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkStandardPolyDataPainter.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.
=========================================================================*/
/*
* Copyright 2004 Sandia Corporation.
* Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
* license for use of this work by or on behalf of the
* U.S. Government. Redistribution and use in source and binary forms, with
* or without modification, are permitted provided that this Notice and any
* statement of authorship are reproduced on all copies.
*/
#include "vtkStandardPolyDataPainter.h"
#include "vtkActor.h"
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkConfigure.h"
#include "vtkDataArray.h"
#include "vtkGenericVertexAttributeMapping.h"
#include "vtkInformation.h"
#include "vtkObjectFactory.h"
#include "vtkPainterDeviceAdapter.h"
#include "vtkPointData.h"
#include "vtkPolyData.h"
#include "vtkPolygon.h"
#include "vtkProperty.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkShaderDeviceAdapter2.h"
#include "vtkSmartPointer.h"
#include "vtkTimerLog.h"
#include "vtkTriangle.h"
#include <vector>
class vtkStandardPolyDataPainter::vtkInternal
{
public:
struct vtkInfo
{
unsigned int MappingsIndex;
vtkDataArray* Array;
};
typedef std::vector<vtkInfo> InfoVector;
typedef std::vector<vtkDataArray *> DataArrayVector;
InfoVector CellAttributesCache;
InfoVector PointAttributesCache;
DataArrayVector MultiTextureCoords;
vtkSmartPointer<vtkGenericVertexAttributeMapping> Mappings;
};
//-----------------------------------------------------------------------------
vtkStandardNewMacro(vtkStandardPolyDataPainter);
//-----------------------------------------------------------------------------
static inline int vtkStandardPolyDataPainterGetTotalCells(vtkPolyData* pd,
unsigned long typeflags)
{
int total_cells = 0;
total_cells += (typeflags & vtkPainter::VERTS)?
pd->GetNumberOfVerts() : 0;
total_cells += (typeflags & vtkPainter::LINES)?
pd->GetNumberOfLines() : 0;
total_cells += (typeflags & vtkPainter::POLYS)?
pd->GetNumberOfPolys() : 0;
total_cells += (typeflags & vtkPainter::STRIPS)?
pd->GetNumberOfStrips() : 0;
return total_cells;
}
//-----------------------------------------------------------------------------
vtkStandardPolyDataPainter::vtkStandardPolyDataPainter()
{
this->Internal = new vtkInternal;
}
//-----------------------------------------------------------------------------
vtkStandardPolyDataPainter::~vtkStandardPolyDataPainter()
{
delete this->Internal;
}
//-----------------------------------------------------------------------------
void vtkStandardPolyDataPainter::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
void vtkStandardPolyDataPainter::AddMultiTextureCoordsArray(vtkDataArray * array)
{
this->Internal->MultiTextureCoords.push_back(array);
}
//-----------------------------------------------------------------------------
void vtkStandardPolyDataPainter::ProcessInformation(vtkInformation* info)
{
this->Internal->Mappings = 0;
if( info->Has(DATA_ARRAY_TO_VERTEX_ATTRIBUTE()) )
{
vtkGenericVertexAttributeMapping* collection =
vtkGenericVertexAttributeMapping::SafeDownCast(
info->Get(DATA_ARRAY_TO_VERTEX_ATTRIBUTE()));
this->Internal->Mappings = collection;
}
}
//-----------------------------------------------------------------------------
void vtkStandardPolyDataPainter::UpdateGenericAttributesCache(
vtkShaderDeviceAdapter2* shaderDevice2)
{
if (this->Internal->Mappings)
{
vtkPolyData* pd = this->GetInputAsPolyData();
unsigned int max = this->Internal->Mappings->GetNumberOfMappings();
for (unsigned int cc=0; cc < max; cc++)
{
int field = this->Internal->Mappings->GetFieldAssociation(cc);
const char *dataArrayName = this->Internal->Mappings->GetArrayName(cc);
const char *vertexAttributeName =
this->Internal->Mappings->GetAttributeName(cc);
if (dataArrayName == NULL)
{
continue;
}
if (vertexAttributeName == NULL)
{
continue;
}
vtkDataArray *inArray = NULL;
vtkInternal::InfoVector* dest=0;
if (field == vtkDataObject::FIELD_ASSOCIATION_POINTS)
{
inArray = pd->GetPointData()->GetArray(dataArrayName);
dest = &this->Internal->PointAttributesCache;
}
else if (field == vtkDataObject::FIELD_ASSOCIATION_CELLS)
{
inArray = pd->GetCellData()->GetArray(dataArrayName);
dest = &this->Internal->CellAttributesCache;
}
if (inArray && dest)
{
vtkInternal::vtkInfo info;
info.MappingsIndex = cc;
info.Array = inArray;
dest->push_back(info);
// This caches the attribute index.
// This is essential since we don't want to call GetAttributeLocation in
// glBegin()/glEnd().
if(shaderDevice2)
{
shaderDevice2->SendAttribute(vertexAttributeName, 0, 0, 0, 0);
}
}
}
}
}
//-----------------------------------------------------------------------------
void vtkStandardPolyDataPainter::RenderInternal(vtkRenderer* renderer,
vtkActor* actor,
unsigned long typeflags,
bool forceCompileOnly)
{
if (typeflags == 0)
{
// No primitive to render.
return;
}
if (!renderer->GetRenderWindow()->GetPainterDeviceAdapter())
{
vtkErrorMacro("Painter Device Adapter missing!");
return;
}
vtkPolyData* pd = this->GetInputAsPolyData();
this->TotalCells = vtkStandardPolyDataPainterGetTotalCells(pd, typeflags);
this->Timer->StartTimer();
vtkProperty* property = actor->GetProperty();
vtkIdType startCell = 0;
int interpolation = property->GetInterpolation();
vtkShaderDeviceAdapter2* shaderDevice2=0;
this->Internal->PointAttributesCache.clear();
this->Internal->CellAttributesCache.clear();
if(property->GetShading())
{
// Preprocess the generic vertex attributes that we need to pass to the
// shader.
shaderDevice2 = property->GetShaderDeviceAdapter2();
}
if(!shaderDevice2)
{
// load the shader device adapator from the information object
shaderDevice2 =
vtkShaderDeviceAdapter2::SafeDownCast(
this->GetInformation()->Get(SHADER_DEVICE_ADAPTOR()));
}
if(shaderDevice2)
{
shaderDevice2->PrepareForRender();
}
this->UpdateGenericAttributesCache(shaderDevice2);
if (typeflags & vtkPainter::VERTS)
{
this->DrawCells(VTK_POLY_VERTEX, pd->GetVerts(), startCell,
shaderDevice2, renderer, 0, interpolation);
}
startCell += pd->GetNumberOfVerts();
if (typeflags & vtkPainter::LINES)
{
this->DrawCells(VTK_POLY_LINE, pd->GetLines(), startCell,
shaderDevice2, renderer, 0, interpolation);
}
startCell += pd->GetNumberOfLines();
if (typeflags & vtkPainter::POLYS)
{
#if defined(__APPLE__) && (defined(VTK_USE_CARBON) || defined(VTK_USE_COCOA))
if (property->GetRepresentation() == VTK_WIREFRAME)
{
this->DrawCells(VTK_TETRA, pd->GetPolys(), startCell,
shaderDevice2, renderer,
this->BuildNormals, interpolation);
}
else
#endif
{
this->DrawCells(VTK_POLYGON, pd->GetPolys(), startCell,
shaderDevice2, renderer,
this->BuildNormals, interpolation);
}
}
startCell += pd->GetNumberOfPolys();
if (typeflags & vtkPainter::STRIPS)
{
this->DrawCells(VTK_TRIANGLE_STRIP, pd->GetStrips(), startCell,
shaderDevice2, renderer, this->BuildNormals,
interpolation);
}
this->Timer->StopTimer();
this->TimeToDraw = this->Timer->GetElapsedTime();
// let the superclass pass on the request to delegate painter.
// Ofcouse, more than likely, this call will never have a delegate,
// but anyways.
this->Superclass::RenderInternal(renderer, actor, typeflags,forceCompileOnly);
this->Internal->PointAttributesCache.clear();
this->Internal->CellAttributesCache.clear();
}
//-----------------------------------------------------------------------------
void vtkStandardPolyDataPainter::DrawCells(
int mode,
vtkCellArray *connectivity,
vtkIdType startCellId,
vtkShaderDeviceAdapter2 *shaderDevice2,
vtkRenderer *renderer,
int buildnormals,
int interpolation)
{
vtkPolyData* pd = this->GetInputAsPolyData();
vtkPainterDeviceAdapter* device = renderer->GetRenderWindow()->
GetPainterDeviceAdapter();
vtkCellData* cellData = pd->GetCellData();
vtkPointData* pointData = pd->GetPointData();
vtkUnsignedCharArray* fieldColors = vtkUnsignedCharArray::SafeDownCast(
pd->GetFieldData()->GetArray("Color"));
int disable_scalar_color = 0;
if (this->Information->Has(DISABLE_SCALAR_COLOR()) &&
this->Information->Get(DISABLE_SCALAR_COLOR())==1)
{
disable_scalar_color = 1;
}
if (disable_scalar_color)
{
fieldColors = 0;
}
vtkPoints* p = pd->GetPoints();
vtkIdType npts, *pts;
vtkIdType cellId = startCellId;
vtkIdType fielddata_cellId = startCellId;
int pointtype = p->GetDataType();
void* voidpoints = p->GetVoidPointer(0);
int count = 0;
double polyNorm[3];
vtkIdType normIdx[3];
if (buildnormals)
{
// check is normals are present in data. if so, no need to build them.
// Point normals can be used only when interpolation is not VTK_FLAT.
// When interpolation == VTK_FLAT and cell normals are absent,
// cell normals may be built (depending on this->BuildNormals).
buildnormals = ((pointData->GetNormals() && interpolation != VTK_FLAT) ||
cellData->GetNormals())? 0 : 1;
}
// skip scalars if disable_scalar_color is true.
int start_attribute = (disable_scalar_color? 1 : 0);
// get attribute mask
int cell_attribute_mask = 0;
for(int attribii = start_attribute; attribii < vtkCellData::NUM_ATTRIBUTES;
attribii++)
{
if(device->IsAttributesSupported(attribii))
{
cell_attribute_mask = cell_attribute_mask | (1 << attribii);
}
}
int point_attribute_mask = 0;
for(int attribii = start_attribute; attribii < vtkPointData::NUM_ATTRIBUTES;
attribii++)
{
if(device->IsAttributesSupported(attribii))
{
point_attribute_mask = point_attribute_mask | (1 << attribii);
}
}
// Note that cell attributes are overridden by point attributes.
for (connectivity->InitTraversal(); connectivity->GetNextCell(npts, pts); count++)
{
int attribii;
device->BeginPrimitive(mode);
// SEND CELL ATTRIBUTES
for (attribii = start_attribute; attribii < vtkCellData::NUM_ATTRIBUTES;
attribii++)
{
if (!((cell_attribute_mask >> attribii) & 1))
{
// skip non-renderable attributes.
continue;
}
vtkDataArray *a = cellData->GetAttribute(attribii);
if (a == NULL)
{
continue;
}
int numc = a->GetNumberOfComponents();
device->SendAttribute(attribii, numc, a->GetDataType(),
a->GetVoidPointer(numc*cellId));
}
if (buildnormals)
{
if (mode == VTK_POLYGON)
{
vtkPolygon::ComputeNormal(p, npts, pts, polyNorm);
}
else // VTK_TRIANGLE_STRIP
{
vtkTriangle::ComputeNormal(p, 3, pts, polyNorm);
}
device->SendAttribute(vtkDataSetAttributes::NORMALS, 3,
VTK_DOUBLE, polyNorm);
}
if (fieldColors)
{
// fieldColors are same as cell colors except when rendering
// VTK_TRIANGLE_STRIP, when they represent triangle colors.
int numc = fieldColors->GetNumberOfComponents();
device->SendAttribute(vtkCellData::SCALARS, numc, VTK_UNSIGNED_CHAR,
fieldColors->GetVoidPointer(numc * fielddata_cellId));
fielddata_cellId++;
}
// Send generic attributes associated with the cell. Shaders style 2.
vtkInternal::InfoVector::iterator gaIter =
this->Internal->CellAttributesCache.begin();
for (; shaderDevice2 && gaIter != this->Internal->CellAttributesCache.end(); ++gaIter)
{
vtkDataArray* a = gaIter->Array;
unsigned int mappingsIndex = gaIter->MappingsIndex;
int numc = a->GetNumberOfComponents();
int siComp = this->Internal->Mappings->GetComponent(mappingsIndex);
// if siComp==-1, then all components of the array are sent,
// otherwise only the chosen component is sent.
shaderDevice2->SendAttribute(
this->Internal->Mappings->GetAttributeName(mappingsIndex),
(siComp>=0)? 1: numc,
a->GetDataType(),
(siComp>=0) ? a->GetVoidPointer(numc*cellId+siComp) :
a->GetVoidPointer(numc*cellId));
}
for (vtkIdType cellpointi = 0; cellpointi < npts; cellpointi++)
{
vtkIdType pointId = pts[cellpointi];
// If using field colors, then we must send tringle colors,
// if rendering triangle strips.
if (fieldColors && mode == VTK_TRIANGLE_STRIP && cellpointi > 2)
{
int numc = fieldColors->GetNumberOfComponents();
device->SendAttribute(vtkCellData::SCALARS, numc, VTK_UNSIGNED_CHAR,
fieldColors->GetVoidPointer(numc * fielddata_cellId));
fielddata_cellId++;
}
// SEND POINT ATTRIBUTES.
// Send point centered attributes.
for (attribii = start_attribute; attribii < vtkPointData::NUM_ATTRIBUTES;
attribii++)
{
if (!((point_attribute_mask >> attribii) & 1))
{
// skip non-renderable attributes.
continue;
}
vtkDataArray *a = pointData->GetAttribute(attribii);
if (!a || attribii == vtkPointData::VECTORS ||
(interpolation == VTK_FLAT && attribii == vtkPointData::NORMALS))
{
// Point normals are skipped when interpolation is flat.
// We may want to add an interpolation painter that does this.
continue;
}
int numc = a->GetNumberOfComponents();
device->SendAttribute(attribii, numc, a->GetDataType(),
a->GetVoidPointer(numc*pointId));
}
if (buildnormals && mode == VTK_TRIANGLE_STRIP && cellpointi >=2)
{
// build the normal for each triangle in a tstrip.
if (cellpointi % 2)
{
normIdx[0] = pts[cellpointi-2]; normIdx[1] = pts[cellpointi];
normIdx[2] = pts[cellpointi-1];
vtkTriangle::ComputeNormal(p, 3, normIdx, polyNorm);
}
else
{
normIdx[0] = pts[cellpointi-2]; normIdx[1] = pts[cellpointi-1];
normIdx[2] = pts[cellpointi];
vtkTriangle::ComputeNormal(p, 3, normIdx, polyNorm);
}
device->SendAttribute(vtkDataSetAttributes::NORMALS, 3,
VTK_DOUBLE, polyNorm);
}
// Send generic attributes associated with the point. Shader style 2.
gaIter = this->Internal->PointAttributesCache.begin();
for (; shaderDevice2 && gaIter !=
this->Internal->PointAttributesCache.end(); ++gaIter)
{
vtkDataArray* a = gaIter->Array;
unsigned int mappingsIndex = gaIter->MappingsIndex;
int numc = a->GetNumberOfComponents();
int siComp = this->Internal->Mappings->GetComponent(mappingsIndex);
// if siComp==-1, then all components of the array are sent,
// otherwise only the chosen component is sent.
shaderDevice2->SendAttribute(
this->Internal->Mappings->GetAttributeName(mappingsIndex),
(siComp>=0)? 1: numc,
a->GetDataType(),
(siComp>=0) ? a->GetVoidPointer(numc*pointId+siComp) :
a->GetVoidPointer(numc*pointId));
}
// Check for any multitexture attributes and send them.
gaIter = this->Internal->PointAttributesCache.begin();
for (; gaIter != this->Internal->PointAttributesCache.end(); ++gaIter)
{
vtkDataArray* a = gaIter->Array;
unsigned int mappingsIndex = gaIter->MappingsIndex;
int numc = a->GetNumberOfComponents();
int siComp = this->Internal->Mappings->GetComponent(mappingsIndex);
int textureIndex = this->Internal->Mappings->GetTextureUnit(mappingsIndex);
if(textureIndex >= 0)
{
device->SendMultiTextureCoords(
(siComp>=0)? 1: numc,
a->GetDataType(),
a->GetVoidPointer(0),
textureIndex,
numc*pointId
);
}
}
//vtkInformationVector *inArrayVec =
// this->Information->Get(vtkAlgorithm::INPUT_ARRAYS_TO_PROCESS());
//int numArrays = inArrayVec->GetNumberOfInformationObjects();
//for(int i = 0; i < numArrays; i++)
// {
// //this->
// }
device->SendAttribute(vtkPointData::NUM_ATTRIBUTES, 3,
pointtype, voidpoints, 3*pointId);
}
device->EndPrimitive();
cellId++;
if (count == 10000)
{
count = 0;
// report progress
this->UpdateProgress(static_cast<double>(cellId - startCellId)/this->TotalCells);
// Abort render.
if (renderer->GetRenderWindow()->CheckAbortStatus())
{
return;
}
}
}
}
|