Skip to content

TextureMapQuad

vtk-examples/Cxx/Visualization/TextureMapQuad


Description

This example defines 4 points (coplanar) and textures them using the input image.

Question

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

Code

TextureMapQuad.cxx

#include <string>
#include <vtkCellArray.h>
#include <vtkFloatArray.h>
#include <vtkImageData.h>
#include <vtkJPEGReader.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkPolygon.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkTexture.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<vtkPoints> points;
  points->InsertNextPoint(0.0, 0.0, 0.0);
  points->InsertNextPoint(1.0, 0.0, 0.0);
  points->InsertNextPoint(1.0, 1.0, 0.0);
  points->InsertNextPoint(0.0, 2.0, 0.0);

  vtkNew<vtkCellArray> polygons;
  vtkNew<vtkPolygon> polygon;
  polygon->GetPointIds()->SetNumberOfIds(4); // make a quad
  polygon->GetPointIds()->SetId(0, 0);
  polygon->GetPointIds()->SetId(1, 1);
  polygon->GetPointIds()->SetId(2, 2);
  polygon->GetPointIds()->SetId(3, 3);

  polygons->InsertNextCell(polygon);

  vtkNew<vtkPolyData> quad;
  quad->SetPoints(points);
  quad->SetPolys(polygons);

  vtkNew<vtkFloatArray> textureCoordinates;
  textureCoordinates->SetNumberOfComponents(2);
  textureCoordinates->SetName("TextureCoordinates");

  float tuple[2] = {0.0, 0.0};
  textureCoordinates->InsertNextTuple(tuple);
  tuple[0] = 1.0;
  tuple[1] = 0.0;
  textureCoordinates->InsertNextTuple(tuple);
  tuple[0] = 1.0;
  tuple[1] = 1.0;
  textureCoordinates->InsertNextTuple(tuple);
  tuple[0] = 0.0;
  tuple[1] = 2.0;
  textureCoordinates->InsertNextTuple(tuple);

  quad->GetPointData()->SetTCoords(textureCoordinates);

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

  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputData(quad);

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

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

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

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

  renderWindow->Render();

  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(TextureMapQuad)

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

if (NOT VTK_FOUND)
  message("Skipping TextureMapQuad: ${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(TextureMapQuad MACOSX_BUNDLE TextureMapQuad.cxx )
  target_link_libraries(TextureMapQuad PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(TextureMapQuad MACOSX_BUNDLE TextureMapQuad.cxx )
  target_link_libraries(TextureMapQuad PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS TextureMapQuad
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build TextureMapQuad

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

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

./TextureMapQuad

WINDOWS USERS

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