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

标签 c++ visual-studio 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 改编了一个小例子一个(虽然在 python 中)可以帮助你的起点。在这种情况下,vtkTextureMapToPlane 不是理想的,因为它只覆盖立方体的 2 个面(查看下图)。但是,我认为 vtkTextureMapToBox,如 this链接,应该可以解决这个问题(我无法使用它,因为我使用的是 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()

结果:

enter image description here

关于c++ - 在立方体面上应用图像 - VTK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40308608/

相关文章:

Qt 创建者 + MITK (Linux)

c++ - 简单高效的C++容器,具有map和list容器的特点

c++ - 在 std::vector<std::unique_ptr<T>> 中迭代 const T&

c++ - "ODR-use"是什么意思?

visual-studio - 如何在 Visual Studio 2017 中启用 WiX 项目

c++ - 意外的 while 行为 C++(可能与 IDE 相关)

c++ - 在 VTK 中绘制球体数组

c++ - 不明确的构造函数调用(我假设)

visual-studio - 将搜索限制为 Visual Studio 中的指定文件夹或文件类型?

c++ - 查找基类/父类(super class)成员的实现