在立方体面上应用图像 - VTK

Apply an image on cube faces - VTK

我在 visual studio 2010 年使用 VTK,我想在立方体面上应用图像。

读取我的图像的代码:

// Read JPG image
vtkSmartPointer<vtkJPEGReader> JPEGReader = vtkSmartPointer<vtkJPEGReader>::New();
JPEGReader->SetFileName(argv[1]);
JPEGReader->Update();

// Image actor
vtkSmartPointer<vtkImageActor> imageActor = vtkSmartPointer<vtkImageActor>::New();
imageActor->GetMapper()->SetInputData(JPEGReader->GetOutput());

设置立方体代码:

// Setup cube
vtkSmartPointer<vtkCubeSource> cubeSource = vtkSmartPointer<vtkCubeSource>::New();
cubeSource->Update();
vtkSmartPointer<vtkPolyDataMapper> cubeMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
cubeMapper->SetInputConnection(cubeSource->GetOutputPort());
vtkSmartPointer<vtkActor> cubeActor = vtkSmartPointer<vtkActor>::New();
cubeActor->SetMapper(cubeMapper);
cubeActor->GetProperty()->SetDiffuseColor(.3, .6, .4);

我该怎么做?

你需要使用纹理和纹理贴图来实现你想要的。我从 this one (although in python) that can help you with a starting point. In this case, vtkTextureMapToPlane is not the ideal one, because it only covers 2 faces of the cube (check out the image below). However, I think vtkTextureMapToBox, as in this link 中改编了一个小例子,应该可以做到这一点(我不能使用它,因为我使用的是 VTK 5.8)。

代码:

import vtk

# Create a render window
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
renWin.SetSize(480,480)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

# Generate a cube 
cube = vtk.vtkCubeSource()

# Read the image data from a file
reader = vtk.vtkJPEGReader()
reader.SetFileName("yourimage.jpg")

# Create texture object
texture = vtk.vtkTexture()
texture.SetInputConnection(reader.GetOutputPort())

#Map texture coordinates
map_to_plane = vtk.vtkTextureMapToPlane()
map_to_plane.SetInputConnection(cube.GetOutputPort())

# Create mapper and set the mapped texture as input
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(map_to_plane.GetOutputPort())

# Create actor and set the mapper and the texture
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.SetTexture(texture)

ren.AddActor(actor)

iren.Initialize()
renWin.Render()
iren.Start()

结果: