TexturePlane
VTKEx/Cxx/Texture/TexturePlane
Description¶
Simple example of texture mapping.
Info
See Figure 7-33 in Chapter 7 in the VTK Textbook.
Question
If you have a simple question about this example contact us at VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum
Code¶
TexturePlane.cxx
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkImageReader2.h>
#include <vtkImageReader2Factory.h>
#include <vtkNamedColors.h>
#include <vtkPlaneSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkTexture.h>
//
// This simple example shows how to do basic texture mapping.
//
//
// Load in the texture map. A texture is any unsigned char image. If it
// is not of this type, you will have to map it through a lookup table
// or by using vtkImageShiftScale.
//
int main (int argc, char *argv[])
{
// Verify input arguments
if ( argc != 2 )
{
std::cout << "Usage: " << argv[0]
<< " Filename" << std::endl;
return EXIT_FAILURE;
}
vtkSmartPointer<vtkImageReader2Factory> readerFactory =
vtkSmartPointer<vtkImageReader2Factory>::New();
vtkSmartPointer<vtkImageReader2> textureFile;
textureFile.TakeReference(
readerFactory->CreateImageReader2(argv[1]));
textureFile->SetFileName(argv[1]);
textureFile->Update();
vtkSmartPointer<vtkTexture> atext =
vtkSmartPointer<vtkTexture>::New();
atext->SetInputConnection(textureFile->GetOutputPort());
atext->InterpolateOn();
// Create a plane source and actor. The vtkPlanesSource generates
// texture coordinates.
//
vtkSmartPointer<vtkPlaneSource> plane =
vtkSmartPointer<vtkPlaneSource>::New();
vtkSmartPointer<vtkPolyDataMapper> planeMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
planeMapper->SetInputConnection(plane->GetOutputPort());
vtkSmartPointer<vtkActor> planeActor =
vtkSmartPointer<vtkActor>::New();
planeActor->SetMapper(planeMapper);
planeActor->SetTexture(atext);
// Create the RenderWindow, Renderer and both Actors
vtkSmartPointer<vtkNamedColors> colors =
vtkSmartPointer<vtkNamedColors>::New();
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renWin =
vtkSmartPointer<vtkRenderWindow>::New();
renWin->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> iren =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow(renWin);
// Add the actors to the renderer, set the background and size
renderer->AddActor(planeActor);
renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
renWin->SetSize(640, 480);
// render the image
renWin->Render();
renderer->ResetCamera();
renderer->GetActiveCamera()->Elevation(-30);
renderer->GetActiveCamera()->Roll(-20);
renderer->ResetCameraClippingRange();
renWin->Render();
iren->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(TexturePlane)
find_package(VTK COMPONENTS
vtkvtkCommonColor
vtkvtkFiltersSources
vtkvtkIOImage
vtkvtkInteractionStyle
vtkvtkRenderingContextOpenGL2
vtkvtkRenderingCore
vtkvtkRenderingFreeType
vtkvtkRenderingGL2PSOpenGL2
vtkvtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
message("Skipping TexturePlane: ${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(TexturePlane MACOSX_BUNDLE TexturePlane.cxx )
target_link_libraries(TexturePlane PRIVATE ${VTK_LIBRARIES})
else ()
# include all components
add_executable(TexturePlane MACOSX_BUNDLE TexturePlane.cxx )
target_link_libraries(TexturePlane PRIVATE ${VTK_LIBRARIES})
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS TexturePlane
MODULES ${VTK_LIBRARIES}
)
endif ()
Download and Build TexturePlane¶
Click here to download TexturePlane and its CMakeLists.txt file. Once the tarball TexturePlane.tar has been downloaded and extracted,
cd TexturePlane/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:
./TexturePlane
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.