vtkValuePass.cxx 12.1 KB
Newer Older
Tim Thirion's avatar
Tim Thirion committed
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
/*=========================================================================

  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"
28 29 30 31 32 33
#include "vtkRenderbuffer.h"
#include "vtkRenderWindow.h"
#include "vtkFloatArray.h"
#include "vtkOpenGLError.h"
#include "vtkOpenGLRenderWindow.h"
#include "vtkFrameBufferObject2.h"
Tim Thirion's avatar
Tim Thirion committed
34 35

#include <cassert>
36

Tim Thirion's avatar
Tim Thirion committed
37 38 39 40 41 42 43 44 45 46

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);
47
vtkInformationKeyMacro(vtkValuePass, SCALAR_RANGE, DoubleVector);
Tim Thirion's avatar
Tim Thirion committed
48 49 50 51 52 53 54 55 56 57

class vtkValuePass::vtkInternals
{
public:
  int FieldAssociation;
  int FieldAttributeType;
  std::string FieldName;
  bool FieldNameSet;
  int Component;
  double ScalarRange[2];
58
  bool ScalarRangeSet;
59
  vtkSmartPointer<vtkClearRGBPass> ClearPass;
Tim Thirion's avatar
Tim Thirion committed
60 61

  vtkInternals()
62
  : ClearPass(vtkSmartPointer<vtkClearRGBPass>::New())
Tim Thirion's avatar
Tim Thirion committed
63 64 65 66 67 68
    {
    this->FieldAssociation = 0;
    this->FieldAttributeType = 0;
    this->FieldName = "";
    this->FieldNameSet = false;
    this->Component = 0;
69 70 71
    this->ScalarRange[0] = 0.0;
    this->ScalarRange[1] = -1.0;
    this->ScalarRangeSet = false;
Tim Thirion's avatar
Tim Thirion committed
72 73 74 75 76
    }
};

// ----------------------------------------------------------------------------
vtkValuePass::vtkValuePass()
77 78 79 80
: RenderingMode(1)
, ValueRenderBO(NULL)
, ValueFrameBO(NULL)
, ValuePassResourcesAllocated(false)
Tim Thirion's avatar
Tim Thirion committed
81 82
{
  this->Internals = new vtkInternals();
83 84 85

  this->Size[0] = 0;
  this->Size[1] = 0;
Tim Thirion's avatar
Tim Thirion committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
}

// ----------------------------------------------------------------------------
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 ||
106
      this->Internals->FieldName.compare(name) != 0)
Tim Thirion's avatar
Tim Thirion committed
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
    {
    this->Internals->FieldAssociation = fieldAssociation;
    this->Internals->FieldName = name;
    this->Internals->FieldNameSet = 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->Modified();
    }
}

// ----------------------------------------------------------------------------
void vtkValuePass::SetInputComponentToProcess(int component)
{
  if (this->Internals->Component != component)
    {
    this->Internals->Component = component;
    this->Modified();
    }
}

140 141 142 143 144 145 146 147 148 149 150 151 152 153
// ----------------------------------------------------------------------------
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();
    }
}


Tim Thirion's avatar
Tim Thirion committed
154 155 156 157 158 159 160 161
// ----------------------------------------------------------------------------
// 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);

162 163 164
  this->BeginPass(s->GetRenderer());

  this->Internals->ClearPass->Render(s);
Tim Thirion's avatar
Tim Thirion committed
165 166 167

  this->NumberOfRenderedProps=0;
  this->RenderOpaqueGeometry(s);
168 169 170 171 172

  // vtkFrameBufferObject2 is not supported
  //s->SetFrameBuffer(this->ValueFrameBO);

  this->EndPass();
Tim Thirion's avatar
Tim Thirion committed
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
}

// ----------------------------------------------------------------------------
// 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());
      }
204
    keys->Set(vtkValuePass::RENDER_VALUES(), this->RenderingMode);
Tim Thirion's avatar
Tim Thirion committed
205 206 207 208 209
    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);
210
    keys->Set(vtkValuePass::SCALAR_RANGE(), this->Internals->ScalarRange, 2);
Tim Thirion's avatar
Tim Thirion committed
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
    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());
239
    keys->Remove(vtkValuePass::SCALAR_RANGE());
Tim Thirion's avatar
Tim Thirion committed
240 241 242 243
    p->SetPropertyKeys(keys);
    ++i;
    }
}
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

//------------------------------------------------------------------------------
void vtkValuePass::BeginPass(vtkRenderer* ren)
{
  switch(this->RenderingMode)
  {
  case vtkValuePass::FLOATING_POINT:
    // Allocate if necessary and bind frame buffer.
    this->InitializeFloatingPointMode(ren);
    this->ValueFrameBO->Bind(GL_DRAW_FRAMEBUFFER);
    break;

  case vtkValuePass::INVERTIBLE_LUT:
  default:
    // Cleanup in case FLOATING_POINT was active.
    this->ReleaseFloatingPointMode(ren);
    break;
  }
}

//------------------------------------------------------------------------------
void vtkValuePass::EndPass()
{
  switch(this->RenderingMode)
  {
  case vtkValuePass::FLOATING_POINT:
    // Unbind the float FBO and glReadPixels to host side.
    this->ValueFrameBO->UnBind(GL_DRAW_FRAMEBUFFER);
    break;

  case vtkValuePass::INVERTIBLE_LUT:
  default:
    // Nothing to do in this mode.
    break;
  }
}

//------------------------------------------------------------------------------
void vtkValuePass::InitializeFloatingPointMode(vtkRenderer* ren)
{
  if (this->ValuePassResourcesAllocated)
    return;

  vtkRenderWindow* renWin = ren->GetRenderWindow();
  if (!this->IsFloatFBOSupported(renWin))
    return;

  // Allocate FBO's Color attachment target
  int* size = ren->GetSize();
  this->Size[0] = size[0];
  this->Size[1] = size[1];

  this->ValueRenderBO = vtkRenderbuffer::New();
  this->ValueRenderBO->SetContext(renWin);
  this->ValueRenderBO->CreateColorAttachment(this->Size[0], this->Size[1]);

  // Initialize the FBO into which the float value pass is rendered.
  this->ValueFrameBO = vtkFrameBufferObject2::New();
  this->ValueFrameBO->SetContext(renWin);
  this->ValueFrameBO->Bind(GL_FRAMEBUFFER);
  this->ValueFrameBO->InitializeViewport(this->Size[0], this->Size[1]);
  /* GL_COLOR_ATTACHMENT0 */
  this->ValueFrameBO->AddColorAttachment(GL_FRAMEBUFFER, 0, this->ValueRenderBO);

  // Verify FBO
  if(!this->ValueFrameBO->CheckFrameBufferStatus(GL_FRAMEBUFFER))
    {
    vtkErrorMacro("Failed to attach FBO.");
    this->ReleaseFloatingPointMode(ren);
    }

  this->ValueFrameBO->UnBind(GL_FRAMEBUFFER);
  this->ValuePassResourcesAllocated = true;
}

//-----------------------------------------------------------------------------
void vtkValuePass::ReleaseFloatingPointMode(vtkRenderer* ren)
{
  if (!this->ValuePassResourcesAllocated)
    return;

  vtkRenderWindow* renWin = ren->GetRenderWindow();
  renWin->MakeCurrent();

  // Cleanup FBO (grahpics resources cleaned internally)
  this->ValueFrameBO->Delete();
  this->ValueFrameBO = NULL;

  this->ValueRenderBO->Delete();
  this->ValueRenderBO = NULL;

  this->ValuePassResourcesAllocated = false;
}

//-----------------------------------------------------------------------------
bool vtkValuePass::IsFloatFBOSupported(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::GetFloatImageData(vtkRenderer* ren)
{
  vtkRenderWindow* renWin = ren->GetRenderWindow();
  renWin->MakeCurrent();

  //Allocate output array.
  vtkFloatArray* pixels = vtkFloatArray::New();
  pixels->SetNumberOfValues(this->Size[0] * this->Size[1] /* * numComponents */);

  // Prepare and bind value texture and FBO.
  this->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);
  glClampColor(GL_CLAMP_READ_COLOR, GL_FALSE);

  glReadPixels(0, 0, this->Size[0], this->Size[1], GL_RED, GL_FLOAT,
    pixels->GetVoidPointer(0));

  glReadBuffer(originalReadBuff);
  this->ValueFrameBO->UnBind(GL_READ_FRAMEBUFFER);

  vtkOpenGLCheckErrorMacro("Failed to read pixels from OpenGL buffer!");
  return pixels;
}

//-------------------------------------------------------------------------------
std::vector<int> vtkValuePass::GetFloatImageExtents(vtkRenderer* ren)
{
  vtkRenderWindow* renWin = ren->GetRenderWindow();
  renWin->MakeCurrent();
  int* size = ren->GetSize();

  std::vector<int> ext;
  ext.reserve(6);
  ext.push_back(0); ext.push_back(this->Size[0] - 1);
  ext.push_back(0); ext.push_back(this->Size[1] - 1);
  ext.push_back(0); ext.push_back(0);

  return ext;
}