gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 01:58:45 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com