2016-10-28 14 views
0

我在visual studio 2010上使用VTK,我想要在立方體面上應用圖像。在立方體面上應用圖像 - 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); 

我怎麼做到的?

回答

1

你需要使用紋理和紋理貼圖來實現你想要的。我改編了一個來自this的小例子(雖然在python中),可以幫助你從一個起點出發。在這種情況下,vtkTextureMapToPlane並不理想,因爲它只覆蓋立方體的兩個面(請查看下面的圖像)。不過,我認爲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