Skip to content

OutlineGlowPass

vtk-examples/Cxx/Rendering/OutlineGlowPass

Description

Demonstrates how to render an object in a scene with a glowing outline.

The class vtkOutlineGlowPass is designed to highlight parts of a scene by applying the render pass to a layered renderer on top of the main scene. For optimal results, actors that form the outline should be brightly colored with lighting disabled. The outline will have the color of the actors. There is only one outline around all objects rendered by the delegate.

When combined with layered renderers, this creates a very visible highlight without altering the highlighted object.

Other languages

See (Python)

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

OutlineGlowPass.cxx

#include <vtkActor.h>
#include <vtkArrowSource.h>
#include <vtkCamera.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkOpenGLRenderer.h>
#include <vtkOutlineGlowPass.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderStepsPass.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
#include <vtkVersion.h>

#include <iomanip>
#include <iostream>
#include <string>

namespace {
/**
 * Check the VTK version.
 *
 * @param major: Major version.
 * @param major: Minor version.
 * @param major: Build version.
 *
 * @return True if the requested VTK version is greater or equal to the actual
 * VTK version.
 */
bool VTKVersionOk(unsigned long long const& major,
                  unsigned long long const& minor,
                  unsigned long long const& build);
} // namespace

int main(int, char*[])
{
  if (!VTKVersionOk(9, 0, 20200909))
  {
    std::cerr
        << "You need VTK version 9.0.20200909 or greater to run this program."
        << std::endl;
    return EXIT_FAILURE;
  }

  vtkNew<vtkNamedColors> colors;

  vtkNew<vtkRenderWindowInteractor> iren;
  vtkNew<vtkRenderWindow> renWin;
  renWin->SetMultiSamples(0);

  iren->SetRenderWindow(renWin);

  // Set up the renderers.
  // One for the object and the other for the outline.
  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderer> rendererOutline;
  rendererOutline->SetLayer(1);
  renWin->SetNumberOfLayers(2);
  renWin->AddRenderer(rendererOutline);
  renWin->AddRenderer(renderer);

  // Create an arrow.
  vtkNew<vtkArrowSource> arrowSource;
  // arrowSource->SetShaftRadius(1.0);
  // arrowSource->SetTipLength(1.0);
  arrowSource->Update();

  // Create mapper and actor for the main renderer.
  vtkNew<vtkPolyDataMapper> mapperMain;
  mapperMain->SetInputConnection(arrowSource->GetOutputPort());

  vtkNew<vtkActor> actorMain;
  actorMain->SetMapper(mapperMain);
  actorMain->GetProperty()->SetDiffuseColor(
      colors->GetColor3d("LimeGreen").GetData());

  renderer->AddActor(actorMain);

  // Lets make the outline glow!
  // Create the render pass.
  vtkNew<vtkRenderStepsPass> basicPasses;
  vtkNew<vtkOutlineGlowPass> glowPass;
  glowPass->SetDelegatePass(basicPasses);

  // Apply the render pass to the highlight renderer.
  rendererOutline->SetPass(glowPass);

  // Create mapper and actor for the outline.
  vtkNew<vtkPolyDataMapper> mapperOutline;
  mapperOutline->SetInputConnection(arrowSource->GetOutputPort());

  vtkNew<vtkActor> actorOutline;
  actorOutline->SetMapper(mapperOutline);
  actorOutline->GetProperty()->SetColor(
      colors->GetColor3d("Magenta").GetData());
  actorOutline->GetProperty()->LightingOff();

  rendererOutline->AddActor(actorOutline);

  renWin->SetSize(600, 600);

  renderer->GradientBackgroundOn();
  renderer->SetBackground(colors->GetColor3d("DarkSlateGray").GetData());
  renderer->SetBackground2(colors->GetColor3d("DarkSlateBlue").GetData());

  renderer->ResetCamera();
  vtkCamera* camera = renderer->GetActiveCamera();
  camera->Roll(45.0);
  camera->Azimuth(-30.0);
  camera->Elevation(-15.0);
  renderer->ResetCamera();
  // Now set the active camera for the outline.
  rendererOutline->SetActiveCamera(camera);

  renWin->SetWindowName("OutlineGlowPass");

  renWin->Render();

  iren->Start();
  return EXIT_SUCCESS;
}

namespace {
bool VTKVersionOk(unsigned long long const& major,
                  unsigned long long const& minor,
                  unsigned long long const& build)
{
  unsigned long long neededVersion =
      10000000000ULL * major + 100000000ULL * minor + build;
#ifndef VTK_VERSION_NUMBER
  auto ver = vtkSmartPointer<vtkVersion>();
  unsigned long long vtk_version_number =
      10000000000ULL * ver->GetVTKMajorVersion() +
      100000000ULL * ver->GetVTKMinorVersion() + ver->GetVTKBuildVersion();
  if (vtk_version_number >= neededVersion)
  {
    return true;
  }
  return false;
#else
  if (VTK_VERSION_NUMBER >= neededVersion)
  {
    return true;
  }
  return false;
#endif
}
} // namespace

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(OutlineGlowPass)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  FiltersSources
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "OutlineGlowPass: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(OutlineGlowPass MACOSX_BUNDLE OutlineGlowPass.cxx )
  target_link_libraries(OutlineGlowPass PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS OutlineGlowPass
  MODULES ${VTK_LIBRARIES}
)

Download and Build OutlineGlowPass

Click here to download OutlineGlowPass and its CMakeLists.txt file. Once the tarball OutlineGlowPass.tar has been downloaded and extracted,

cd OutlineGlowPass/build

If VTK is installed:

cmake ..

If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

make

and run it:

./OutlineGlowPass

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.