5.5 纹理贴图
5.5.1 对象接口说明
vtkTexture:
当纹理坐标超出[0,1]范围时,打开/关闭纹理贴图的重复。 | |
当纹理坐标超出[0,1]范围时,打开/关闭纹理贴图的夹紧。 | |
渲染时打开/关闭纹理贴图的线性插值。 | |
SetMipmap(bool) | 渲染时打开/关闭mipmap的使用。 |
强制纹理质量为16位或32位。 | |
如有必要,指定查找表以转换标量。 | |
SetTransform(vtkTransform * transform) | 在纹理上设置变换,允许人们缩放,旋转和平移纹理。 |
用于指定纹理如何将其RGB和Alpha值与其他纹理以及渲染纹理的片段混合。 | |
SetCubeMap(bool val) | 这个纹理是一个立方体贴图,如果是这样,它需要6个输入一个立方体的每一面。 您必须在连接输入之前设置此项。输入必须具有相同的大小,数据类型和深度 |
vtkTextureMapToPlane:
指定定义平面原点的点。 | |
指定定义平面第一轴的点 | |
指定定义平面第二轴的点。 | |
指定平面法线。 | |
指定纹理st坐标对的s坐标范围。 | |
指定纹理st坐标对的t坐标范围。 | |
开启/关闭自动生成平面。 |
vtkSphereSource: 生成以原点为中心的圆柱
设置圆柱的高度。 | |
设置圆柱的半径。 | |
设置圆柱中心。 | |
设置用于定义圆柱的面的数量。 | |
打开/关闭是否用多边形盖住圆柱体。 |
5.5.2 代码实现
创建纹理对象:
void ESTexture::createBMP()
{
std::string fileName = "D:\\project\\project\\applicationproject\\vtkES\\bin\\x64\\Debug\\timg.bmp";
vtkSmartPointer<vtkBMPReader> reader = vtkSmartPointer<vtkBMPReader>::New();
reader->SetFileName(fileName.c_str());
m_texture = vtkSmartPointer<vtkTexture>::New();
m_texture->SetInputConnection(reader->GetOutputPort());
m_texture->InterpolateOn();
}
创建vtkPlaneSource贴图:
void ESTexture::createPlane()
{
// Create a plane
vtkSmartPointer<vtkPlaneSource> plane = vtkSmartPointer<vtkPlaneSource>::New();
plane->SetCenter(0.0, 0.0, 0.0);
plane->SetNormal(0.0, 0.0, 1.0);
vtkSmartPointer<vtkTextureMapToPlane> texturePlane = vtkSmartPointer<vtkTextureMapToPlane>::New();
texturePlane->SetInputConnection(plane->GetOutputPort());
vtkSmartPointer<vtkPolyDataMapper> planeMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
planeMapper->SetInputConnection(texturePlane->GetOutputPort());
vtkSmartPointer<vtkActor> texturedPlane = vtkSmartPointer<vtkActor>::New();
texturedPlane->SetMapper(planeMapper);
texturedPlane->SetTexture(m_texture);
m_pRender->AddActor(texturedPlane);
}
创建vtkSphereSource纹理贴图:
void ESTexture::createSphere()
{
vtkSmartPointer<vtkSphereSource> sphereSource = vtkSmartPointer<vtkSphereSource>::New();
sphereSource->SetCenter(0,0,0);
sphereSource->SetRadius(1);
sphereSource->SetThetaResolution(40);
sphereSource->SetPhiResolution(40);
sphereSource->Update();
//
vtkSmartPointer<vtkTextureMapToSphere> textureMapToSphere = vtkSmartPointer<vtkTextureMapToSphere>::New();
textureMapToSphere->SetInputConnection(sphereSource->GetOutputPort());
//Create mapper and set the mapped texture as input
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(textureMapToSphere->GetOutputPort());
//Create actor and set the mapper and the texture
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
actor->SetTexture(m_texture);
m_pRender->AddActor(actor);
}
创建圆柱体的纹理贴图:
void ESTexture::createCylinder()
{
vtkSmartPointer<vtkCylinderSource> cylinder = vtkSmartPointer<vtkCylinderSource>::New();
cylinder->SetHeight(20.0);
cylinder->SetCenter(0, 0, 0);
cylinder->SetRadius(3.0);
cylinder->SetResolution(100);
vtkSmartPointer<vtkTextureMapToCylinder> texturemap = vtkSmartPointer<vtkTextureMapToCylinder>::New();
texturemap->SetInputConnection(cylinder->GetOutputPort());
texturemap->SetPreventSeam(0);
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(texturemap->GetOutputPort());
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
actor->SetTexture(m_texture);
m_pRender->AddActor(actor);
}
创建立方体的纹理贴图:
void ESTexture::createCube()
{
double p[6] = {0,1,0,1,0,1};
//获取当前坐标范围的中心点
//m_vtkView->getCoordRange(p);
double centerx = (p[0] + p[1]) / 2;
double centery = (p[2] + p[3]) / 2;
double centerz = (p[4] + p[5]) / 2;
double cube_size = 1.0;
//构造立方体
m_pts.clear();
m_pts.push_back(VTKPoint3D(centerx - cube_size / 2, centery - cube_size / 2, centerz - cube_size / 2));
m_pts.push_back(VTKPoint3D(centerx + cube_size / 2, centery - cube_size / 2, centerz - cube_size / 2));
m_pts.push_back(VTKPoint3D(centerx + cube_size / 2, centery + cube_size / 2, centerz - cube_size / 2));
m_pts.push_back(VTKPoint3D(centerx - cube_size / 2, centery + cube_size / 2, centerz - cube_size / 2));
m_pts.push_back(VTKPoint3D(centerx - cube_size / 2, centery - cube_size / 2, centerz + cube_size / 2));
m_pts.push_back(VTKPoint3D(centerx + cube_size / 2, centery - cube_size / 2, centerz + cube_size / 2));
m_pts.push_back(VTKPoint3D(centerx + cube_size / 2, centery + cube_size / 2, centerz + cube_size / 2));
m_pts.push_back(VTKPoint3D(centerx - cube_size / 2, centery + cube_size / 2, centerz + cube_size / 2));
// The ordering of the corner points on each face.
std::array<std::array<vtkIdType, 4>, 6> ordering = { {
{ { 0, 1, 2, 3 } },
{ { 4, 5, 6, 7 } },
{ { 0, 1, 5, 4 } },
{ { 1, 2, 6, 5 } },
{ { 2, 3, 7, 6 } },
{ { 3, 0, 4, 7 } } } };
//
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkCellArray> polys = vtkSmartPointer<vtkCellArray>::New();
VTKPoint3D pt;
for (int index = 0; index < 8; ++index)
{
pt = m_pts[index];
points->InsertPoint(index, pt.m_x, pt.m_y, pt.m_z);
}
for (auto&& i : ordering)
{
polys->InsertNextCell(vtkIdType(i.size()), i.data());
}
//将点和多边形加入polydata
vtkIdType* indices;
vtkIdType numberOfPoints;
VTKPoint3D pt0, pt1, pt2;
vtkSmartPointer<vtkAppendPolyData> polyDatas = vtkSmartPointer<vtkAppendPolyData>::New();
for (polys->InitTraversal();polys->GetNextCell(numberOfPoints, indices);)
{
vtkSmartPointer<vtkPolyData> profile = vtkSmartPointer<vtkPolyData>::New();
vtkSmartPointer<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New();
lines->InsertNextCell(numberOfPoints);
for (vtkIdType i = 0; i < numberOfPoints; i++)
{
vtkIdType id = indices[i];
lines->InsertCellPoint(id);
double point[3];
points->GetPoint(id, point);
if (i == 0)
{
pt0.m_x = point[0];pt0.m_y = point[1];pt0.m_z = point[2];
}
if (i == 1)
{
pt1.m_x = point[0];pt1.m_y = point[1];pt1.m_z = point[2];
}
if (i == 2)
{
pt2.m_x = point[0];pt2.m_y = point[1];pt2.m_z = point[2];
}
}
profile->SetPoints(points);
profile->SetPolys(lines);
计算多边形的法向量
vtkSmartPointer<vtkPolyDataNormals> normal = vtkSmartPointer<vtkPolyDataNormals>::New();
normal->SetInputData(profile);
设置多边形纹理映射的模式
设置为plane模式
vtkSmartPointer<vtkTextureMapToPlane> tmapper = vtkSmartPointer<vtkTextureMapToPlane>::New();
tmapper->SetInputConnection(normal->GetOutputPort());
//设置纹理st坐标系的顶点坐标 和两点坐标,定义了st坐标系
tmapper->SetOrigin(pt1.m_x, pt1.m_y, pt1.m_z);
tmapper->SetPoint1(pt0.m_x, pt0.m_y, pt0.m_z);
tmapper->SetPoint2(pt2.m_x, pt2.m_y, pt2.m_z);
vtkSmartPointer<vtkTransformTextureCoords> xform = vtkSmartPointer<vtkTransformTextureCoords>::New();
xform->SetInputConnection(tmapper->GetOutputPort());
xform->SetScale(1, 1, 1);
polyDatas->AddInputConnection(xform->GetOutputPort());
}
polyDatas->Update();
vtkSmartPointer<vtkPolyDataMapper> cubeMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
cubeMapper->SetInputConnection(polyDatas->GetOutputPort());
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(cubeMapper);
actor->SetTexture(m_texture);
m_pRender->AddActor(actor);
m_pRender->ResetCamera();
m_pRender->ResetCameraClippingRange();
m_pRenderWnd->Render();
}