VTK 学习----VTK对象绘制-纹理贴图(立方体、平面(vtkPlaneSource)、球(vtkSphereSource)、圆柱体(vtkCylinderSource))

5.5 纹理贴图

5.5.1 对象接口说明

vtkTexture

SetRepeatvtkTypeBool

当纹理坐标超出[0,1]范围时,打开/关闭纹理贴图的重复。

SetEdgeClampvtkTypeBool

当纹理坐标超出[0,1]范围时,打开/关闭纹理贴图的夹紧。

SetInterpolatevtkTypeBool

渲染时打开/关闭纹理贴图的线性插值。

SetMipmapbool

渲染时打开/关闭mipmap的使用。

SetQualityint

强制纹理质量为16位或32位。

SetLookupTablevtkScalarsToColors *

如有必要,指定查找表以转换标量。

SetTransformvtkTransform * transform

在纹理上设置变换,允许人们缩放,旋转和平移纹理。

SetBlendingModeint

用于指定纹理如何将其RGB和Alpha值与其他纹理以及渲染纹理的片段混合。

SetCubeMapbool val

这个纹理是一个立方体贴图,如果是这样,它需要6个输入一个立方体的每一面。

您必须在连接输入之前设置此项。输入必须具有相同的大小,数据类型和深度

vtkTextureMapToPlane:

SetOrigin (doubledoubledouble)

指定定义平面原点的点。

SetPoint1 (doubledoubledouble)

指定定义平面第一轴的点

SetPoint2 (doubledoubledouble)

指定定义平面第二轴的点。

SetNormal (doubledoubledouble)

指定平面法线。

SetSRange (doubledouble)

指定纹理st坐标对的s坐标范围。

SetTRange (doubledouble)

指定纹理st坐标对的t坐标范围。

SetAutomaticPlaneGeneration (vtkTypeBool)

开启/关闭自动生成平面。

vtkSphereSource: 生成以原点为中心的圆柱

SetHeight (double)

设置圆柱的高度。

SetRadius (double)

设置圆柱的半径。

SetCenter (doubledoubledouble)

设置圆柱中心。

SetResolution (int)

设置用于定义圆柱的面的数量。

SetCapping (vtkTypeBool)

打开/关闭是否用多边形盖住圆柱体。

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();
}

  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dylan55_you

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值