Skip to content

TextureMapPlane

vtk-examples/Cxx/Visualization/TextureMapPlane


Description

This example reads a jpg image file and uses it to texture map a plane.

Question

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

Code

TextureMapPlane.cxx

#include <vtkImageData.h>
#include <vtkJPEGReader.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPlaneSource.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkTexture.h>
#include <vtkTextureMapToPlane.h>

int main(int argc, char* argv[])
{
  // Parse command line arguments
  if (argc != 2)
  {
    std::cerr << "Usage: " << argv[0] << " Filename e.g. Yinyang.jpg"
              << std::endl;
    return EXIT_FAILURE;
  }

  vtkNew<vtkNamedColors> colors;

  std::string inputFilename = argv[1];

  // Read the image which will be the texture
  vtkNew<vtkJPEGReader> jPEGReader;
  jPEGReader->SetFileName(inputFilename.c_str());

  // Create a plane
  vtkNew<vtkPlaneSource> plane;
  plane->SetCenter(0.0, 0.0, 0.0);
  plane->SetNormal(0.0, 0.0, 1.0);

  // Apply the texture
  vtkNew<vtkTexture> texture;
  texture->SetInputConnection(jPEGReader->GetOutputPort());

  vtkNew<vtkTextureMapToPlane> texturePlane;
  texturePlane->SetInputConnection(plane->GetOutputPort());

  vtkNew<vtkPolyDataMapper> planeMapper;
  planeMapper->SetInputConnection(texturePlane->GetOutputPort());

  vtkNew<vtkActor> texturedPlane;
  texturedPlane->SetMapper(planeMapper);
  texturedPlane->SetTexture(texture);
  texturedPlane->GetProperty()->SetColor(
      colors->GetColor3d("MistyRose").GetData());

  // Visualize the textured plane
  vtkNew<vtkRenderer> renderer;
  renderer->AddActor(texturedPlane);
  renderer->SetBackground(colors->GetColor3d("MidnightBlue").GetData());
  renderer->ResetCamera();

  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("TextureMapPlane");

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

  renderWindow->Render();

  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(TextureMapPlane)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonDataModel
  vtkFiltersSources
  vtkFiltersTexture
  vtkIOImage
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping TextureMapPlane: ${VTK_NOT_FOUND_MESSAGE}")
  return ()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
  # old system
  include(${VTK_USE_FILE})
  add_executable(TextureMapPlane MACOSX_BUNDLE TextureMapPlane.cxx )
  target_link_libraries(TextureMapPlane PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(TextureMapPlane MACOSX_BUNDLE TextureMapPlane.cxx )
  target_link_libraries(TextureMapPlane PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS TextureMapPlane
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build TextureMapPlane

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

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

./TextureMapPlane

WINDOWS USERS

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