有时候,我们为了使模型有更加逼真的效果,就需要用到纹理映射,或叫纹理贴图。
纹理映射是生成可视化逼真效果的强大工具,纹理映射的基本思想是在渲染过程中,图像可以贴到渲染对象的表面,因此可以创建出细节更加丰富的渲染效果。
纹理映射需要提供三个信息:待贴纹理图的表面、纹理映射和纹理坐标。
下面是一个对圆柱体进行纹理贴图的例子,把一张2D的图像“贴”到圆柱体的表面。
#include <vtkCylinderSource.h>
#include <vtkLineSource.h>
#include <vtkPolyData.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>
#include <vtkTubeFilter.h>
#include <vtkBMPReader.h>
#include <vtkJPEGReader.h>
#include <vtkTexture.h>
#include <vtkTextureMapToCylinder.h>
#include <vtkTransformTextureCoords.h>
int main(int argc, char* argv[])
{
if (argc < 2)
{
std::cout << "Usage: " << argv[0] << " texture(.jpg)" << std::endl;
return EXIT_FAILURE;
}
// Create a Cylinder
vtkSmartPointer<vtkCylinderSource> cylinderSource =
vtkSmartPointer<vtkCylinderSource>::New();
cylinderSource->SetHeight(10.0);
cylinderSource->SetCenter(0.0, 0.0, 0.0);
cylinderSource->SetRadius(2.0);
cylinderSource->SetResolution(50);
// Load a jpeg file
vtkSmartPointer<vtkJPEGReader> jpegReader =
vtkSmartPointer<vtkJPEGReader>::New();
jpegReader->SetFileName(argv[1]);
vtkSmartPointer<vtkTexture> texture =
vtkSmartPointer<vtkTexture>::New();
texture->SetInputConnection(jpegReader->GetOutputPort());
texture->InterpolateOn();
// map two-dimensional to three-dimensional, use cylinder
vtkSmartPointer<vtkTextureMapToCylinder> textureMap =
vtkSmartPointer<vtkTextureMapToCylinder>::New();
textureMap->SetInputConnection(cylinderSource->GetOutputPort());
vtkSmartPointer<vtkTransformTextureCoords> transformTextureCoords =
vtkSmartPointer<vtkTransformTextureCoords>::New();
transformTextureCoords->SetInputConnection(textureMap->GetOutputPort());
//transformTextureCoords->SetScale(1, 1, 0);
vtkSmartPointer<vtkPolyDataMapper> cylinderMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
cylinderMapper->SetInputConnection(/*textureMap*/transformTextureCoords->GetOutputPort());
vtkSmartPointer<vtkActor> cylinderActor =
vtkSmartPointer<vtkActor>::New();
cylinderActor->SetMapper(cylinderMapper);
cylinderActor->SetTexture(texture);
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetSize(600, 600);
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(cylinderActor);
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
圆柱体的创建可参考如何创建圆柱体
关键代码
vtkSmartPointer<vtkTexture> texture =
vtkSmartPointer<vtkTexture>::New();
texture->SetInputConnection(/*bmpReader*/jpegReader->GetOutputPort());
texture->InterpolateOn();
// map two-dimensional to three-dimensional, use cylinder
vtkSmartPointer<vtkTextureMapToCylinder> textureMap =
vtkSmartPointer<vtkTextureMapToCylinder>::New();
textureMap->SetInputConnection(cylinderSource->GetOutputPort());
vtkSmartPointer<vtkTransformTextureCoords> transformTextureCoords =
vtkSmartPointer<vtkTransformTextureCoords>::New();
transformTextureCoords->SetInputConnection(textureMap->GetOutputPort());
//transformTextureCoords->SetScale(1, 1, 0);
vtkSmartPointer<vtkPolyDataMapper> cylinderMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
cylinderMapper->SetInputConnection(/*textureMap*/transformTextureCoords->GetOutputPort());
vtkSmartPointer<vtkActor> cylinderActor =
vtkSmartPointer<vtkActor>::New();
cylinderActor->SetMapper(cylinderMapper);
cylinderActor->SetTexture(texture);
效果图
完整代码
Reference
VTK纹理映射之vtkTransformTextureCoods
VTK/Examples/Cxx/Texture/TexturedSphere