TextureMapImageData
Repository source: TextureMapImageData
Description¶
This example creates an image and texture maps it onto a plane.
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
TextureMapImageData.cxx
#include <vtkImageCanvasSource2D.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPlaneSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkTexture.h>
#include <array>
int main(int, char*[])
{
vtkNew<vtkNamedColors> colors;
std::array<double, 3> drawColor1{0, 0, 0};
std::array<double, 3> drawColor2{0, 0, 0};
auto color1 = colors->GetColor3ub("ForestGreen").GetData();
auto color2 = colors->GetColor3ub("DarkMagenta").GetData();
for (auto i = 0; i < 3; ++i)
{
drawColor1[i] = color1[i];
drawColor2[i] = color2[i];
}
// Create an image
vtkNew<vtkImageCanvasSource2D> imageSource;
imageSource->SetScalarTypeToUnsignedChar();
imageSource->SetExtent(0, 20, 0, 20, 0, 0);
imageSource->SetNumberOfScalarComponents(3);
imageSource->SetDrawColor(drawColor1.data());
imageSource->FillBox(0, 20, 0, 20);
imageSource->SetDrawColor(drawColor2.data());
imageSource->DrawSegment(0, 0, 19, 19);
imageSource->DrawSegment(19, 0, 0, 19);
imageSource->Update();
// 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(imageSource->GetOutputPort());
vtkNew<vtkPolyDataMapper> planeMapper;
planeMapper->SetInputConnection(plane->GetOutputPort());
vtkNew<vtkActor> texturedPlane;
texturedPlane->SetMapper(planeMapper);
texturedPlane->SetTexture(texture);
// Visualize the textured plane
vtkNew<vtkRenderer> renderer;
renderer->AddActor(texturedPlane);
renderer->SetBackground(colors->GetColor3d("Silver").GetData());
renderer->ResetCamera();
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("TextureMapImageData");
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(TextureMapImageData)
find_package(VTK COMPONENTS
CommonColor
CommonCore
FiltersSources
ImagingSources
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "TextureMapImageData: 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(TextureMapImageData MACOSX_BUNDLE TextureMapImageData.cxx )
target_link_libraries(TextureMapImageData PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS TextureMapImageData
MODULES ${VTK_LIBRARIES}
)
Download and Build TextureMapImageData¶
Click here to download TextureMapImageData and its CMakeLists.txt file. Once the tarball TextureMapImageData.tar has been downloaded and extracted,
cd TextureMapImageData/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:
./TextureMapImageData
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.