1.代码
#pragma once
#include "InitInclude.h"
vtkSmartPointer<vtkActor> CreatPlaneActor()
{
vtkSmartPointer<vtkPlaneSource>planeSource = vtkSmartPointer<vtkPlaneSource>::New();//下面是根据点法式创建的平面
planeSource->SetCenter(0, 0, 0);
planeSource->SetNormal(0, 0, 1);
planeSource->Update();
// Apply linear extrusion
vtkNew<vtkLinearExtrusionFilter> extrude;
extrude->SetInputConnection(planeSource->GetOutputPort());
extrude->SetExtrusionTypeToNormalExtrusion();
extrude->SetVector(1, 0, 0);
extrude->SetScaleFactor(0.1);
vtkNew<vtkTriangleFilter> triangleFilter;
triangleFilter->SetInputConnection(extrude->GetOutputPort());
//读取纹理图像
vtkBMPReader *bmpReader = vtkBMPReader::New();
bmpReader->SetFileName("D:\\DKL\\work\\project\\001\\code\\QtProjTest\\up256.bmp");
vtkTexture *atext = vtkTexture::New();
atext->SetInputConnection(bmpReader->GetOutputPort());
atext->InterpolateOn();
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(triangleFilter->GetOutputPort());
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
if (1)//使用颜色贴图还是、壁纸贴图
{
actor->SetTexture(atext);
}
else
{
actor->GetProperty()->SetColor(1, 0.2, 0.1);
}
return actor;
}
//创建一个二维平面,作为底板的支撑面
//使用vtkPlaneSource方式建立平面
void creat2DBaseboard()
{
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->SetBackground(1, 1, 1);
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("LinearExtrusion");
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor = CreatPlaneActor();
renderer->AddActor(actor );
renderer->ResetCamera();
// Generate an interesting view
renderer->ResetCamera();
renderer->ResetCameraClippingRange();
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindow->Render();
renderWindowInteractor->Start();
}
2.效果