Skip to content

MotionBlur

Repository source: MotionBlur


Description

Example of motin blur.

Info

See Figure 7-36 in Chapter 7 in the VTK Textbook.

Other languages

See (Python), (PythonicAPI)

Question

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

Code

MotionBlur.cxx

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkOpenGLRenderer.h>
#include <vtkPLYReader.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderStepsPass.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSimpleMotionBlurPass.h>

#include <array>
#include <iostream>
#include <string>

//----------------------------------------------------------------------------
int main(int argc, char* argv[])
{

  if (argc < 2)
  {
    cout << "Usage: " << argv[0] << " file e.g. Armadillo.ply" << endl;
    return EXIT_FAILURE;
  }

  vtkNew<vtkNamedColors> colors;

  // Set the colors.
  std::array<unsigned char, 4> a1Diff{{255, 204, 77, 255}};
  colors->SetColor("A1Diff", a1Diff.data());
  std::array<unsigned char, 4> a2Amb{{51, 51, 255, 255}};
  colors->SetColor("A2Amb", a2Amb.data());
  std::array<unsigned char, 4> a2Diff{{51, 255, 204, 255}};
  colors->SetColor("A2Diff", a2Diff.data());
  std::array<unsigned char, 4> a3Amb{{128, 166, 255, 255}};
  colors->SetColor("A3Amb", a3Amb.data());
  std::array<unsigned char, 4> bkg{{77, 102, 153, 255}};
  colors->SetColor("Bkg", bkg.data());

  vtkNew<vtkRenderer> renderer;
  renderer->SetBackground(colors->GetColor3d("Bkg").GetData());

  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->SetSize(500, 500);
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("MotionBlur");

  vtkNew<vtkRenderWindowInteractor> iren;
  iren->SetRenderWindow(renderWindow);

  vtkNew<vtkPLYReader> reader;
  reader->SetFileName(argv[1]);
  reader->Update();

  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputConnection(reader->GetOutputPort());

  // Create three models.
  {
    vtkNew<vtkActor> actor;
    actor->SetMapper(mapper);
    actor->GetProperty()->SetAmbientColor(colors->GetColor3d("Red").GetData());
    actor->GetProperty()->SetDiffuseColor(
        colors->GetColor3d("A1Diff").GetData());
    actor->GetProperty()->SetSpecular(0.0);
    actor->GetProperty()->SetDiffuse(0.5);
    actor->GetProperty()->SetAmbient(0.3);
    actor->SetPosition(-0.1, 0.0, -0.1);
    renderer->AddActor(actor);
  }

  {
    vtkNew<vtkActor> actor;
    actor->SetMapper(mapper);
    actor->GetProperty()->SetAmbientColor(
        colors->GetColor3d("A2Amb").GetData());
    actor->GetProperty()->SetDiffuseColor(
        colors->GetColor3d("A2Diff").GetData());
    actor->GetProperty()->SetSpecularColor(
        colors->GetColor3d("Black").GetData());
    actor->GetProperty()->SetSpecular(0.2);
    actor->GetProperty()->SetDiffuse(0.9);
    actor->GetProperty()->SetAmbient(0.1);
    actor->GetProperty()->SetSpecularPower(10.0);
    renderer->AddActor(actor);
  }

  {
    vtkNew<vtkActor> actor;
    actor->SetMapper(mapper);
    actor->GetProperty()->SetDiffuseColor(
        colors->GetColor3d("A3Amb").GetData());
    actor->GetProperty()->SetSpecularColor(
        colors->GetColor3d("White").GetData());
    actor->GetProperty()->SetSpecular(0.7);
    actor->GetProperty()->SetDiffuse(0.4);
    actor->GetProperty()->SetSpecularPower(60.0);
    actor->SetPosition(0.1, 0.0, 0.1);
    renderer->AddActor(actor);
  }

  renderWindow->SetMultiSamples(0);

  // Create the basic VTK render steps.
  vtkNew<vtkRenderStepsPass> basicPasses;

  vtkNew<vtkSimpleMotionBlurPass> motion;
  motion->SetDelegatePass(basicPasses);

  // Tell the renderer to use our render pass pipeline.
  vtkOpenGLRenderer* glrenderer =
      dynamic_cast<vtkOpenGLRenderer*>(renderer.GetPointer());
  glrenderer->SetPass(motion);

  int numRenders = 30;

  renderer->GetActiveCamera()->SetPosition(0, 0, -1);
  renderer->GetActiveCamera()->SetFocalPoint(0, 0, 0);
  renderer->GetActiveCamera()->SetViewUp(0, 1, 0);
  renderer->ResetCamera();
  renderer->GetActiveCamera()->Azimuth(15.0);
  renderer->GetActiveCamera()->Zoom(1.2);

  renderWindow->Render();

  for (int i = 0; i < numRenders; ++i)
  {
    renderer->GetActiveCamera()->Azimuth(10.0 / numRenders);
    renderer->GetActiveCamera()->Elevation(10.0 / numRenders);
    renderWindow->Render();
  }
  iren->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(MotionBlur)

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

if (NOT VTK_FOUND)
  message(FATAL_ERROR "MotionBlur: 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(MotionBlur MACOSX_BUNDLE MotionBlur.cxx )
  target_link_libraries(MotionBlur PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS MotionBlur
  MODULES ${VTK_LIBRARIES}
)

Download and Build MotionBlur

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

cd MotionBlur/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:

./MotionBlur

WINDOWS USERS

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