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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPrimitivePainter.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 "vtkPrimitivePainter.h"
#include "vtkActor.h"
#include "vtkCellData.h"
#include "vtkDataSetAttributes.h"
#include "vtkGarbageCollector.h"
#include "vtkGenericVertexAttributeMapping.h"
#include "vtkInformation.h"
#include "vtkInformationIntegerKey.h"
#include "vtkInformationObjectBaseKey.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkProperty.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkTimerLog.h"
#include "vtkUnsignedCharArray.h"
#include "vtkShaderDeviceAdapter2.h"
//---------------------------------------------------------------------------
vtkPrimitivePainter::vtkPrimitivePainter()
{
this->SupportedPrimitive = 0x0; // must be set by subclasses. No primitive
// supported by default.
this->DisableScalarColor = 0;
this->OutputData = vtkPolyData::New();
this->GenericVertexAttributes = false;
this->MultiTextureAttributes = false;
}
//---------------------------------------------------------------------------
vtkPrimitivePainter::~vtkPrimitivePainter()
{
if( this->OutputData )
{
this->OutputData->Delete();
this->OutputData = 0;
}
}
//---------------------------------------------------------------------------
void vtkPrimitivePainter::ReportReferences(vtkGarbageCollector *collector)
{
this->Superclass::ReportReferences(collector);
vtkGarbageCollectorReport(collector, this->OutputData, "Output Data");
}
//---------------------------------------------------------------------------
vtkDataObject* vtkPrimitivePainter::GetOutput()
{
return this->OutputData;
}
//---------------------------------------------------------------------------
void vtkPrimitivePainter::ProcessInformation(vtkInformation *info)
{
this->GenericVertexAttributes = false;
if (info->Has(DATA_ARRAY_TO_VERTEX_ATTRIBUTE()))
{
vtkGenericVertexAttributeMapping *mappings =
vtkGenericVertexAttributeMapping::SafeDownCast(
info->Get(DATA_ARRAY_TO_VERTEX_ATTRIBUTE()));
this->GenericVertexAttributes = mappings &&
(mappings->GetNumberOfMappings() > 0);
this->MultiTextureAttributes = false;
if (mappings)
{
for (unsigned int i = 0; i < mappings->GetNumberOfMappings(); ++i)
{
if (mappings->GetTextureUnit(i) >= 0)
{
this->MultiTextureAttributes = true;
break;
}
}
}
}
if (info->Has(DISABLE_SCALAR_COLOR()) &&
info->Get(DISABLE_SCALAR_COLOR()) == 1)
{
this->DisableScalarColor = 1;
}
else
{
this->DisableScalarColor = 0;
}
}
//---------------------------------------------------------------------------
void vtkPrimitivePainter::PrepareForRendering(vtkRenderer* renderer,
vtkActor* actor)
{
// Here, we don't use the this->StaticData flag to mean that the input
// can never change, since the input may be the output of
// some filtering painter that filter on actor/renderer properties
// and not on the input polydata. Hence the input polydata
// may get modified even if the input to the PolyDataMapper is
// immutable.
// If the input has changed update the output.
if (this->OutputUpdateTime < this->MTime ||
this->OutputUpdateTime < this->GetInput()->GetMTime())
{
this->OutputData->ShallowCopy(this->GetInputAsPolyData());
this->OutputUpdateTime.Modified();
}
this->Superclass::PrepareForRendering(renderer, actor);
}
//---------------------------------------------------------------------------
void vtkPrimitivePainter::RenderInternal(vtkRenderer* renderer,
vtkActor* act,
unsigned long typeflags,
bool forceCompileOnly)
{
unsigned long supported_typeflags = this->SupportedPrimitive & typeflags;
if (!supported_typeflags)
{
// no supported primitive requested to be rendered.
this->Superclass::RenderInternal(renderer, act, typeflags,
forceCompileOnly);
return;
}
if (!renderer->GetRenderWindow()->GetPainterDeviceAdapter())
{
vtkErrorMacro("Painter Device Adapter is missing!");
return;
}
this->Timer->StartTimer();
int interpolation;
float tran;
vtkProperty *prop;
vtkUnsignedCharArray *c=NULL;
vtkDataArray *n;
vtkDataArray *t;
vtkDataArray *ef;
int tDim;
vtkPolyData *input = this->GetInputAsPolyData();
int cellNormals;
int cellScalars = 0;
int fieldScalars = 0;
// get the property
prop = act->GetProperty();
// get the transparency
tran = prop->GetOpacity();
// if the primitives are invisible then get out of here
if (tran <= 0.0)
{
return;
}
// get the shading interpolation
interpolation = prop->GetInterpolation();
if (!this->DisableScalarColor)
{
// are they cell or point scalars
c = vtkUnsignedCharArray::SafeDownCast(input->GetPointData()->GetScalars());
if (!c)
{
c = vtkUnsignedCharArray::SafeDownCast(input->GetCellData()->GetScalars());
cellScalars = 1;
}
if (!c)
{
c = vtkUnsignedCharArray::SafeDownCast(input->GetFieldData()->
GetArray("Color"));
fieldScalars = 1; // note when fieldScalars == 1, also cellScalars == 1.
// this ensures that primitive painters that do not distinguish between
// fieldScalars and cellScalars (eg. Verts/Lines/Polys painters) can ignore
// fieldScalars flag.
}
}
n = input->GetPointData()->GetNormals();
if (interpolation == VTK_FLAT)
{
// shunt point normals.
n = 0;
if (this->OutputData->GetPointData()->GetNormals())
{
this->OutputData->GetPointData()->SetNormals(0);
}
}
cellNormals = 0;
if (n == 0 && input->GetCellData()->GetNormals())
{
cellNormals = 1;
n = input->GetCellData()->GetNormals();
}
unsigned long idx = 0;
if (n && !cellNormals)
{
idx |= VTK_PDM_NORMALS;
}
if (c)
{
idx |= VTK_PDM_COLORS;
if (
/* RGBA */
(c->GetNumberOfComponents() == 4 && c->GetValueRange(3)[0] == 255) ||
/* LuminanceAlpha */
(c->GetNumberOfComponents() == 2 && c->GetValueRange(1)[0] == 255))
{
// If the opacity is 255, don't bother send the opacity values to OpenGL.
// Treat the colors are opaque colors (which they are).
idx |= VTK_PDM_OPAQUE_COLORS;
}
if (cellScalars)
{
idx |= VTK_PDM_CELL_COLORS;
}
if (fieldScalars)
{
idx |= VTK_PDM_FIELD_COLORS;
}
}
if (cellNormals)
{
idx |= VTK_PDM_CELL_NORMALS;
}
// Texture and color by texture
t = input->GetPointData()->GetTCoords();
if ( t )
{
tDim = t->GetNumberOfComponents();
if (tDim > 3)
{
vtkDebugMacro(<< "Currently only 1d, 2d and 3d texture coordinates are supported.\n");
t = NULL;
}
}
// Set the flags
if (t)
{
idx |= VTK_PDM_TCOORDS;
}
// Edge flag
ef = input->GetPointData()->GetAttribute(vtkDataSetAttributes::EDGEFLAG);
if (ef && ef->GetNumberOfComponents() != 1)
{
vtkDebugMacro(<< "Currently only 1d edge flags are supported.");
ef = NULL;
}
if (ef && !ef->IsA("vtkUnsignedCharArray"))
{
vtkDebugMacro(<< "Currently only unsigned char edge flags are suported.");
ef = NULL;
}
// Set the flags
if (ef)
{
idx |= VTK_PDM_EDGEFLAGS;
}
if (!act)
{
vtkErrorMacro("No actor");
}
vtkShaderDeviceAdapter2 *shaderDevice2 = NULL;
if (prop->GetShading())
{
shaderDevice2 = prop->GetShaderDeviceAdapter2();
}
if(!shaderDevice2)
{
// load the shader device adapator from the information object
shaderDevice2 =
vtkShaderDeviceAdapter2::SafeDownCast(
this->GetInformation()->Get(SHADER_DEVICE_ADAPTOR()));
}
if (shaderDevice2 && this->GenericVertexAttributes)
{
idx |= VTK_PDM_GENERIC_VERTEX_ATTRIBUTES;
}
if (this->MultiTextureAttributes)
{
idx |= VTK_PDM_GENERIC_VERTEX_ATTRIBUTES;
}
if (this->RenderPrimitive(idx, n, c, t, renderer))
{
// subclass rendered the supported primitive successfully.
// The delegate need not render it.
typeflags &= (~this->SupportedPrimitive);
}
this->Timer->StopTimer();
this->TimeToDraw = this->Timer->GetElapsedTime();
this->Superclass::RenderInternal(renderer, act, typeflags,forceCompileOnly);
}
//---------------------------------------------------------------------------
void vtkPrimitivePainter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "SupportedPrimitive: " << this->SupportedPrimitive << endl;
}
|