VTK学习之路——画画我的小苹果
1、首先确定苹果外轮廓的控制点,形成点集。
2、构建一个线单元。
3、构建多边形数据,把这些点集连接起来绘制一条线。
4、采用压制过滤器旋转轮廓线,构成苹果的表面实体。
5、绘制苹果
示例程序的代码如下:
#include "stdafx.h" #include <vtkPoints.h> #include <vtkSmartPointer.h> #include <vtkCellArray.h> #include <vtkPolyData.h> #include <vtkPolyDataMapper.h> #include <vtkRotationalExtrusionFilter.h> #include <vtkActor.h> #include <vtkProperty.h> #include <vtkRenderer.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> int _tmain(int argc, _TCHAR* argv[]) { //定义苹果轮廓线的点坐标 vtkSmartPointer<vtkPoints>pPoints=vtkPoints::New(); pPoints->InsertPoint(0,0.0,0.0,1.5); pPoints->InsertPoint(1,1.0,0.0,1); pPoints->InsertPoint(2,2.0,0.0,1.5); pPoints->InsertPoint(3,3.0,0.0,2.5); pPoints->InsertPoint(4,3.5,0.0,3.0); pPoints->InsertPoint(5,4.0,0.0,5.0); pPoints->InsertPoint(6,3.5,0.0,6.0); pPoints->InsertPoint(7,2.0,0.0,7.0); pPoints->InsertPoint(8,1.0,0.0,6.0); pPoints->InsertPoint(9,0.0,0.0,5.0); //构建线单元,绘制轮廓线 vtkSmartPointer<vtkCellArray>pLineCell=vtkCellArray::New(); //构建一个单元,由10个点构成 pLineCell->InsertNextCell(10); //构成单元点集的索引号 pLineCell->InsertCellPoint(0); pLineCell->InsertCellPoint(1); pLineCell->InsertCellPoint(2); pLineCell->InsertCellPoint(3); pLineCell->InsertCellPoint(4); pLineCell->InsertCellPoint(5); pLineCell->InsertCellPoint(6); pLineCell->InsertCellPoint(7); pLineCell->InsertCellPoint(8); pLineCell->InsertCellPoint(9); //定义多边形数据集 vtkSmartPointer<vtkPolyData>pPolyData=vtkPolyData::New(); //设置构成多边形的几何数据 pPolyData->SetPoints(pPoints); //设置单元类型,线单元 pPolyData->SetLines(pLineCell); //压制轮廓线,形成苹果实体 vtkSmartPointer<vtkRotationalExtrusionFilter>pExtruFilter=vtkRotationalExtrusionFilter::New(); pExtruFilter->SetInput(pPolyData); //设置生成苹果表面的光滑度参数,苹果的表面由多少个面构成 pExtruFilter->SetResolution(10); //将数据映射到图形系统进行绘制 vtkSmartPointer<vtkPolyDataMapper>pMap=vtkPolyDataMapper::New(); pMap->SetInput(pExtruFilter->GetOutput()); vtkSmartPointer<vtkActor>pActor=vtkActor::New(); pActor->SetMapper(pMap); pActor->GetProperty()->SetColor(0.0,1.0,0.0); //绘制 vtkSmartPointer<vtkRenderer>pRen=vtkRenderer::New(); pRen->AddActor(pActor); //创建绘制窗体 vtkSmartPointer<vtkRenderWindow>pRenWin=vtkRenderWindow::New(); pRenWin->AddRenderer(pRen); //创建交互器 vtkSmartPointer<vtkRenderWindowInteractor>pIren=vtkRenderWindowInteractor::New(); pIren->SetRenderWindow(pRenWin); //设置背景颜色 pRen->SetBackground(1,1,1); //设置窗口尺寸 pRenWin->SetSize(300,300); pRenWin->Render(); pRen->ResetCamera(); //开始绘制 pIren->Initialize(); pIren->Start(); return 0; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。