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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkValuePass.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 "vtkValuePass.h"
#include "vtkClearRGBPass.h"
#include "vtkInformation.h"
#include "vtkInformationDoubleVectorKey.h"
#include "vtkInformationIntegerKey.h"
#include "vtkInformationStringKey.h"
#include "vtkMapper.h"
#include "vtkObjectFactory.h"
#include "vtkProp.h"
#include "vtkRenderer.h"
#include "vtkRenderState.h"
#include "vtkSmartPointer.h"
#include "vtkRenderbuffer.h"
#include "vtkRenderWindow.h"
#include "vtkFloatArray.h"
#include "vtkOpenGLError.h"
#include "vtkOpenGLRenderWindow.h"
#include "vtkFrameBufferObject2.h"
#include <cassert>
vtkStandardNewMacro(vtkValuePass);
vtkInformationKeyMacro(vtkValuePass, RENDER_VALUES, Integer);
vtkInformationKeyMacro(vtkValuePass, SCALAR_MODE, Integer);
vtkInformationKeyMacro(vtkValuePass, ARRAY_MODE, Integer);
vtkInformationKeyMacro(vtkValuePass, ARRAY_ID, Integer);
vtkInformationKeyMacro(vtkValuePass, ARRAY_NAME, String);
vtkInformationKeyMacro(vtkValuePass, ARRAY_COMPONENT, Integer);
vtkInformationKeyMacro(vtkValuePass, SCALAR_RANGE, DoubleVector);
vtkInformationKeyMacro(vtkValuePass, RELOAD_DATA, Integer);
class vtkValuePass::vtkInternals
{
public:
int FieldAssociation;
int FieldAttributeType;
std::string FieldName;
bool FieldNameSet;
int Component;
double ScalarRange[2];
bool ScalarRangeSet;
bool ReloadData;
// Description:
// Array holder for FLOATING_POINT mode. The result pixels are downloaded
// into this array.
vtkSmartPointer<vtkFloatArray> Values;
// Description:
// FLOATING_POINT mode resources. FBO, attachments and other control variables.
vtkFrameBufferObject2* ValueFrameBO;
vtkRenderbuffer* ValueRenderBO;
vtkRenderbuffer* DepthRenderBO;
bool ValuePassResourcesAllocated;
int FloatImageExt[6];
vtkInternals()
: Values(NULL)
, ValueFrameBO(NULL)
, ValueRenderBO(NULL)
, DepthRenderBO(NULL)
, ValuePassResourcesAllocated(false)
{
this->Values = vtkSmartPointer<vtkFloatArray>::New();
this->Values->SetNumberOfComponents(1); /* GL_RED */
this->FieldAssociation = 0;
this->FieldAttributeType = 0;
this->FieldName = "";
this->FieldNameSet = false;
this->Component = 0;
this->ScalarRange[0] = 0.0;
this->ScalarRange[1] = -1.0;
this->ScalarRangeSet = false;
this->ReloadData = true;
this->FloatImageExt[0] = 0; this->FloatImageExt[1] = 0;
this->FloatImageExt[2] = 0; this->FloatImageExt[3] = 0;
this->FloatImageExt[4] = 0; this->FloatImageExt[5] = 0;
}
~vtkInternals()
{
if (this->ValueFrameBO)
{
this->ValueFrameBO->Delete();
}
if (this->ValueRenderBO)
{
this->ValueRenderBO->Delete();
}
if (this->DepthRenderBO)
{
this->DepthRenderBO->Delete();
}
}
};
// ----------------------------------------------------------------------------
vtkValuePass::vtkValuePass()
: RenderingMode(INVERTIBLE_LUT)
{
this->Internals = new vtkInternals();
}
// ----------------------------------------------------------------------------
vtkValuePass::~vtkValuePass()
{
delete this->Internals;
}
// ----------------------------------------------------------------------------
void vtkValuePass::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
// ----------------------------------------------------------------------------
void vtkValuePass::SetInputArrayToProcess(int fieldAssociation,
const char *name)
{
if (!this->Internals->FieldNameSet ||
this->Internals->FieldAssociation != fieldAssociation ||
this->Internals->FieldName.compare(name) != 0)
{
this->Internals->FieldAssociation = fieldAssociation;
this->Internals->FieldName = name;
this->Internals->FieldNameSet = true;
this->Internals->ReloadData = true;
this->Modified();
}
}
// ----------------------------------------------------------------------------
void vtkValuePass::SetInputArrayToProcess(int fieldAssociation,
int fieldAttributeType)
{
if (this->Internals->FieldAssociation != fieldAssociation ||
this->Internals->FieldAttributeType != fieldAttributeType ||
this->Internals->FieldNameSet)
{
this->Internals->FieldAssociation = fieldAssociation;
this->Internals->FieldAttributeType = fieldAttributeType;
this->Internals->FieldNameSet = false;
this->Internals->ReloadData = true;
this->Modified();
}
}
// ----------------------------------------------------------------------------
void vtkValuePass::SetInputComponentToProcess(int component)
{
if (this->Internals->Component != component)
{
this->Internals->Component = component;
this->Internals->ReloadData = true;
this->Modified();
}
}
// ----------------------------------------------------------------------------
void vtkValuePass::SetScalarRange(double min, double max)
{
if (this->Internals->ScalarRange[0] != min ||
this->Internals->ScalarRange[1] != max)
{
this->Internals->ScalarRange[0] = min;
this->Internals->ScalarRange[1] = max;
this->Internals->ScalarRangeSet = (max > min);
this->Modified();
}
}
// ----------------------------------------------------------------------------
// Description:
// Perform rendering according to a render state \p s.
// \pre s_exists: s!=0
void vtkValuePass::Render(const vtkRenderState *s)
{
assert("pre: s_exists" && s!=0);
this->BeginPass(s->GetRenderer());
this->NumberOfRenderedProps = 0;
this->RenderOpaqueGeometry(s);
this->EndPass();
}
// ----------------------------------------------------------------------------
// Description:
// Opaque pass with key checking.
// \pre s_exists: s!=0
void vtkValuePass::RenderOpaqueGeometry(const vtkRenderState *s)
{
assert("pre: s_exists" && s!=0);
std::vector<int> scalarVisibilities;
int c = s->GetPropArrayCount();
int i = 0;
while (i < c)
{
vtkProp *p = s->GetPropArray()[i];
// Cache scalar visibility state and turn it on
vtkActor *a = vtkActor::SafeDownCast(p);
if (a)
{
scalarVisibilities.push_back(a->GetMapper()->GetScalarVisibility());
a->GetMapper()->ScalarVisibilityOn();
}
vtkSmartPointer<vtkInformation> keys = p->GetPropertyKeys();
if (!keys)
{
keys.TakeReference(vtkInformation::New());
}
keys->Set(vtkValuePass::RENDER_VALUES(), this->RenderingMode);
keys->Set(vtkValuePass::SCALAR_MODE(), this->Internals->FieldAssociation);
keys->Set(vtkValuePass::ARRAY_MODE(), this->Internals->FieldNameSet);
keys->Set(vtkValuePass::ARRAY_ID(), this->Internals->FieldAttributeType);
keys->Set(vtkValuePass::ARRAY_NAME(), this->Internals->FieldName.c_str());
keys->Set(vtkValuePass::ARRAY_COMPONENT(), this->Internals->Component);
keys->Set(vtkValuePass::SCALAR_RANGE(), this->Internals->ScalarRange, 2);
if (this->Internals->ReloadData)
{
keys->Set(vtkValuePass::RELOAD_DATA(), 1);
}
p->SetPropertyKeys(keys);
int rendered =
p->RenderOpaqueGeometry(s->GetRenderer());
this->NumberOfRenderedProps += rendered;
++i;
}
// Remove set keys
i = 0;
while (i < c)
{
vtkProp *p = s->GetPropArray()[i];
// Restore scalar visibility state
vtkActor *a = vtkActor::SafeDownCast(p);
if (a)
{
a->GetMapper()->SetScalarVisibility(scalarVisibilities[i]);
}
vtkInformation *keys = p->GetPropertyKeys();
keys->Remove(vtkValuePass::RENDER_VALUES());
keys->Remove(vtkValuePass::SCALAR_MODE());
keys->Remove(vtkValuePass::ARRAY_MODE());
keys->Remove(vtkValuePass::ARRAY_ID());
keys->Remove(vtkValuePass::ARRAY_NAME());
keys->Remove(vtkValuePass::ARRAY_COMPONENT());
keys->Remove(vtkValuePass::SCALAR_RANGE());
if (this->Internals->ReloadData)
{
keys->Remove(vtkValuePass::RELOAD_DATA());
this->Internals->ReloadData = false;
}
p->SetPropertyKeys(keys);
++i;
}
}
//------------------------------------------------------------------------------
void vtkValuePass::BeginPass(vtkRenderer* ren)
{
switch(this->RenderingMode)
{
case vtkValuePass::FLOATING_POINT:
// Allocate if necessary and bind frame buffer.
if (this->HasWindowSizeChanged(ren))
{
this->ReleaseFloatingPointMode(ren);
}
if (this->InitializeFloatingPointMode(ren))
{
this->Internals->ValueFrameBO->SaveCurrentBindings();
this->Internals->ValueFrameBO->Bind(GL_DRAW_FRAMEBUFFER);
}
break;
case vtkValuePass::INVERTIBLE_LUT:
default:
// Cleanup in case FLOATING_POINT was active.
this->ReleaseFloatingPointMode(ren);
break;
}
// Clear buffers
#if GL_ES_VERSION_2_0 != 1
glClearDepth(1.0);
#else
glClearDepthf(1.0f);
#endif
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
//------------------------------------------------------------------------------
void vtkValuePass::EndPass()
{
switch(this->RenderingMode)
{
case vtkValuePass::FLOATING_POINT:
// Unbind the float FBO and glReadPixels to host side.
this->Internals->ValueFrameBO->UnBind(GL_DRAW_FRAMEBUFFER);
break;
case vtkValuePass::INVERTIBLE_LUT:
default:
// Nothing to do in this mode.
break;
}
}
//------------------------------------------------------------------------------
bool vtkValuePass::HasWindowSizeChanged(vtkRenderer* ren)
{
if (!this->Internals->ValueFrameBO)
{
return true;
}
int* size = ren->GetSize();
int* fboSize = this->Internals->ValueFrameBO->GetLastSize(false);
return (fboSize[0] != size[0] || fboSize[1] != size[1]);
}
//------------------------------------------------------------------------------
bool vtkValuePass::InitializeFloatingPointMode(vtkRenderer* ren)
{
if (this->Internals->ValuePassResourcesAllocated)
{
return true;
}
vtkRenderWindow* renWin = ren->GetRenderWindow();
if (!this->IsFloatingPointModeSupported(renWin))
{
vtkWarningMacro("Switching to INVERTIBLE_LUT mode.");
this->RenderingMode = vtkValuePass::INVERTIBLE_LUT;
return false;
}
int* size = ren->GetSize();
// Allocate FBO's Color attachment target
this->Internals->ValueRenderBO = vtkRenderbuffer::New();
this->Internals->ValueRenderBO->SetContext(renWin);
// CreateColorAttachment formats the attachment RGBA32F by
// default, this is what vtkValuePass expects.
this->Internals->ValueRenderBO->CreateColorAttachment(size[0], size[1]);
// Allocate FBO's depth attachment target
this->Internals->DepthRenderBO = vtkRenderbuffer::New();
this->Internals->DepthRenderBO->SetContext(renWin);
this->Internals->DepthRenderBO->CreateDepthAttachment(size[0], size[1]);
// Initialize the FBO into which the float value pass is rendered.
this->Internals->ValueFrameBO = vtkFrameBufferObject2::New();
this->Internals->ValueFrameBO->SetContext(renWin);
this->Internals->ValueFrameBO->SaveCurrentBindings();
this->Internals->ValueFrameBO->Bind(GL_FRAMEBUFFER);
this->Internals->ValueFrameBO->InitializeViewport(size[0], size[1]);
this->Internals->ValueFrameBO->GetLastSize(true); /*force a cached size update*/
/* GL_COLOR_ATTACHMENT0 */
this->Internals->ValueFrameBO->AddColorAttachment(GL_FRAMEBUFFER, 0, this->Internals->ValueRenderBO);
this->Internals->ValueFrameBO->AddDepthAttachment(GL_FRAMEBUFFER, this->Internals->DepthRenderBO);
// Verify FBO
if(!this->Internals->ValueFrameBO->CheckFrameBufferStatus(GL_FRAMEBUFFER))
{
vtkErrorMacro("Failed to attach FBO.");
this->ReleaseFloatingPointMode(ren);
return false;
}
this->Internals->ValueFrameBO->UnBind(GL_FRAMEBUFFER);
this->Internals->ValuePassResourcesAllocated = true;
return true;
}
//-----------------------------------------------------------------------------
void vtkValuePass::ReleaseFloatingPointMode(vtkRenderer* ren)
{
if (!this->Internals->ValuePassResourcesAllocated)
{
return;
}
vtkRenderWindow* renWin = ren->GetRenderWindow();
renWin->MakeCurrent();
// Cleanup FBO (grahpics resources cleaned internally)
this->Internals->ValueFrameBO->Delete();
this->Internals->ValueFrameBO = NULL;
this->Internals->ValueRenderBO->Delete();
this->Internals->ValueRenderBO = NULL;
this->Internals->DepthRenderBO->Delete();
this->Internals->DepthRenderBO = NULL;
this->Internals->ValuePassResourcesAllocated = false;
}
//-----------------------------------------------------------------------------
bool vtkValuePass::IsFloatingPointModeSupported(vtkRenderWindow *renWin)
{
vtkOpenGLRenderWindow *context = vtkOpenGLRenderWindow::SafeDownCast(renWin);
if (!context)
{
vtkErrorMacro(<< "Support for " << renWin->GetClassName()
<< " not implemented");
return false;
}
#if GL_ES_VERSION_2_0 != 1
bool contextSupport = vtkOpenGLRenderWindow::GetContextSupportsOpenGL32();
if (!contextSupport)
{
vtkWarningMacro(<< "Context does not support OpenGL core profile 3.2. "
<< " Will check extension support.");
}
bool extSupport = glewIsSupported("GL_EXT_framebuffer_object") &&
glewIsSupported("GL_ARB_texture_float");
if (!extSupport)
{
vtkWarningMacro(<< "EXT_framebuffer_object or ARB_texture_float not"
<< " supported.");
}
return contextSupport && extSupport;
#else
return true;
#endif
}
//------------------------------------------------------------------------------
vtkFloatArray* vtkValuePass::GetFloatImageDataArray(vtkRenderer* ren)
{
if (!this->Internals->ValuePassResourcesAllocated)
{
return this->Internals->Values;
}
vtkRenderWindow* renWin = ren->GetRenderWindow();
renWin->MakeCurrent();
//Allocate output array.
int* size = this->Internals->ValueFrameBO->GetLastSize(false);
this->Internals->Values->SetNumberOfTuples(size[0] * size[1]);
// RGB channels are equivalent in the FBO (they all contain the rendered
// values).
this->GetFloatImageData(GL_RED, size[0], size[1],
this->Internals->Values->GetVoidPointer(0));
return this->Internals->Values;
}
//-------------------------------------------------------------------------------
void vtkValuePass::GetFloatImageData(int const format, int const width,
int const height, void* data)
{
// Prepare and bind value texture and FBO.
this->Internals->ValueFrameBO->SaveCurrentBindings();
this->Internals->ValueFrameBO->Bind(GL_READ_FRAMEBUFFER);
GLint originalReadBuff;
glGetIntegerv(GL_READ_BUFFER, &originalReadBuff);
glReadBuffer(GL_COLOR_ATTACHMENT0);
// Calling pack alignment ensures any window size can be grabbed.
glPixelStorei(GL_PACK_ALIGNMENT, 1);
#if GL_ES_VERSION_2_0 != 1
glClampColor(GL_CLAMP_READ_COLOR, GL_FALSE);
#endif
glReadPixels(0, 0, width, height, format, GL_FLOAT,
data);
glReadBuffer(originalReadBuff);
this->Internals->ValueFrameBO->UnBind(GL_READ_FRAMEBUFFER);
vtkOpenGLCheckErrorMacro("Failed to read pixels from OpenGL buffer!");
}
//-------------------------------------------------------------------------------
int* vtkValuePass::GetFloatImageExtents()
{
int* size = this->Internals->ValueFrameBO->GetLastSize(false);
this->Internals->FloatImageExt[0] = 0; this->Internals->FloatImageExt[1] = size[0] - 1;
this->Internals->FloatImageExt[2] = 0; this->Internals->FloatImageExt[3] = size[1] - 1;
this->Internals->FloatImageExt[4] = 0; this->Internals->FloatImageExt[5] = 0;
return this->Internals->FloatImageExt;
}
|