本机环境:Windows10+QT5.14.2+CMake3.24
VTK8.2下载:https://www.vtk.org/files/release/8.2/VTK-8.2.0.zip
环境变量设置:调整系统环境变量先后顺序,将path中的C:\Qt\Qt5.14.2\5.14.2\msvc2017_64\bin调整到前面位置。
CMAKE编译VTK步骤:
1、解压缩VTK-8.2.0.zip文件到目录D:\VTK-8.2.0
2、打开桌面上的CMake (cmake-gui)快捷方式,选择编译方式为VS2017+x64
3、将默认安装路径
CMAKE_INSTALL_PREFIX = C:/Program Files (x86)/VTK
修改成自己的路径:
CMAKE_INSTALL_PREFIX = D:/VTK-8.2.0/msvc2017/build
6、VTK_Group_Qt 添加勾选
7、搜索QT5相关的路径,调整为本地msvc2017_64编译器所在路径C:\Qt\Qt5.14.2\5.14.2\msvc2017_64
8、在CMAKE界面点击两次Config按钮后,在点击Generate按钮,之后点击OpenProject按钮。
9、在VS2017中打开VTK解决方案,先用鼠标左键选择ALL_BUILD项目,然后点击鼠标右键选择生成解决方案,记得选择Release+x64模式来编译,取代默认的使用Debug+x86模式编译项目,半个小时后基本生成完成。
10、编译万BUIL_ALL项目后。再选择解决方案中的INSTALL项目,点击鼠标右键,选择生成项目,这样子就会把所有包含文件等等安装到D:\VTK-8.2.0\msvc2017\build目录下(也就是CMAKE中配置的CMAKE_INSTALL_PREFIX = D:/VTK-8.2.0/msvc2017/build路径)。
11、使用VS2017新建C++项目进行测试,项目包含目录、库目录、依赖项设置如下:
--------------------------------------------------------------------------------------------------------------------------------------
包含目录
D:\VTK-8.2.0\msvc2017\build\include\vtk-8.2
--------------------------------------------------------------------------------------------------------------------------------------
库目录
D:\VTK-8.2.0\msvc2017\build\lib
--------------------------------------------------------------------------------------------------------------------------------------
依赖项:
vtkChartsCore-8.2.lib
vtkCommonColor-8.2.lib
vtkCommonComputationalGeometry-8.2.lib
vtkCommonCore-8.2.lib
vtkCommonDataModel-8.2.lib
vtkCommonExecutionModel-8.2.lib
vtkCommonMath-8.2.lib
vtkCommonMisc-8.2.lib
vtkCommonSystem-8.2.lib
vtkCommonTransforms-8.2.lib
vtkDICOMParser-8.2.lib
vtkDomainsChemistry-8.2.lib
vtkDomainsChemistryOpenGL2-8.2.lib
vtkdoubleconversion-8.2.lib
vtkexodusII-8.2.lib
vtkexpat-8.2.lib
vtkFiltersAMR-8.2.lib
vtkFiltersCore-8.2.lib
vtkFiltersExtraction-8.2.lib
vtkFiltersFlowPaths-8.2.lib
vtkFiltersGeneral-8.2.lib
vtkFiltersGeneric-8.2.lib
vtkFiltersGeometry-8.2.lib
vtkFiltersHybrid-8.2.lib
vtkFiltersHyperTree-8.2.lib
vtkFiltersImaging-8.2.lib
vtkFiltersModeling-8.2.lib
vtkFiltersParallel-8.2.lib
vtkFiltersParallelImaging-8.2.lib
vtkFiltersPoints-8.2.lib
vtkFiltersProgrammable-8.2.lib
vtkFiltersSelection-8.2.lib
vtkFiltersSMP-8.2.lib
vtkFiltersSources-8.2.lib
vtkFiltersStatistics-8.2.lib
vtkFiltersTexture-8.2.lib
vtkFiltersTopology-8.2.lib
vtkFiltersVerdict-8.2.lib
vtkfreetype-8.2.lib
vtkGeovisCore-8.2.lib
vtkgl2ps-8.2.lib
vtkglew-8.2.lib
vtkhdf5-8.2.lib
vtkhdf5_hl-8.2.lib
vtkImagingColor-8.2.lib
vtkImagingCore-8.2.lib
vtkImagingFourier-8.2.lib
vtkImagingGeneral-8.2.lib
vtkImagingHybrid-8.2.lib
vtkImagingMath-8.2.lib
vtkImagingMorphological-8.2.lib
vtkImagingSources-8.2.lib
vtkImagingStatistics-8.2.lib
vtkImagingStencil-8.2.lib
vtkInfovisCore-8.2.lib
vtkInfovisLayout-8.2.lib
vtkInteractionImage-8.2.lib
vtkInteractionStyle-8.2.lib
vtkInteractionWidgets-8.2.lib
vtkIOAMR-8.2.lib
vtkIOAsynchronous-8.2.lib
vtkIOCityGML-8.2.lib
vtkIOCore-8.2.lib
vtkIOEnSight-8.2.lib
vtkIOExodus-8.2.lib
vtkIOExport-8.2.lib
vtkIOExportOpenGL2-8.2.lib
vtkIOExportPDF-8.2.lib
vtkIOGeometry-8.2.lib
vtkIOImage-8.2.lib
vtkIOImport-8.2.lib
vtkIOInfovis-8.2.lib
vtkIOLegacy-8.2.lib
vtkIOLSDyna-8.2.lib
vtkIOMINC-8.2.lib
vtkIOMovie-8.2.lib
vtkIONetCDF-8.2.lib
vtkIOParallel-8.2.lib
vtkIOParallelXML-8.2.lib
vtkIOPLY-8.2.lib
vtkIOSegY-8.2.lib
vtkIOSQL-8.2.lib
vtkIOTecplotTable-8.2.lib
vtkIOVeraOut-8.2.lib
vtkIOVideo-8.2.lib
vtkIOXML-8.2.lib
vtkIOXMLParser-8.2.lib
vtkjpeg-8.2.lib
vtkjsoncpp-8.2.lib
vtklibharu-8.2.lib
vtklibxml2-8.2.lib
vtklz4-8.2.lib
vtklzma-8.2.lib
vtkmetaio-8.2.lib
vtkNetCDF-8.2.lib
vtkogg-8.2.lib
vtkParallelCore-8.2.lib
vtkpng-8.2.lib
vtkproj-8.2.lib
vtkpugixml-8.2.lib
vtkRenderingAnnotation-8.2.lib
vtkRenderingContext2D-8.2.lib
vtkRenderingContextOpenGL2-8.2.lib
vtkRenderingCore-8.2.lib
vtkRenderingFreeType-8.2.lib
vtkRenderingGL2PSOpenGL2-8.2.lib
vtkRenderingImage-8.2.lib
vtkRenderingLabel-8.2.lib
vtkRenderingLOD-8.2.lib
vtkRenderingOpenGL2-8.2.lib
vtkRenderingVolume-8.2.lib
vtkRenderingVolumeOpenGL2-8.2.lib
vtksqlite-8.2.lib
vtksys-8.2.lib
vtktheora-8.2.lib
vtktiff-8.2.lib
vtkverdict-8.2.lib
vtkViewsContext2D-8.2.lib
vtkViewsCore-8.2.lib
vtkViewsInfovis-8.2.lib
vtkzlib-8.2.lib
--------------------------------------------------------------------------------------------------------------------------------------
12、编写测试CPP项目文件代码如下:
/*=========================================================================
Program: Visualization Toolkit
Module: Cube.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
// This example shows how to manually create vtkPolyData.
// For a python version, please see:
// [Cube](https://lorensen.github.io/VTKExamples/site/Python/DataManipulation/Cube/)
#include
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
vtkNew colors;
std::array, 8> pts = { {{{0, 0, 0}},
{{1, 0, 0}},
{{1, 1, 0}},
{{0, 1, 0}},
{{0, 0, 1}},
{{1, 0, 1}},
{{1, 1, 1}},
{{0, 1, 1}}} };
// The ordering of the corner points on each face.
std::array, 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}}} };
// We'll create the building blocks of polydata including data attributes.
vtkNew cube;
vtkNew points;
vtkNew polys;
vtkNew scalars;
// Load the point, cell, and data attributes.
for (auto i = 0ul; i < pts.size(); ++i)
{
points->InsertPoint(i, pts[i].data());
scalars->InsertTuple1(i, i);
}
for (auto&& i : ordering)
{
polys->InsertNextCell(vtkIdType(i.size()), i.data());
}
// We now assign the pieces to the vtkPolyData.
cube->SetPoints(points);
cube->SetPolys(polys);
cube->GetPointData()->SetScalars(scalars);
// Now we'll look at it.
vtkNew cubeMapper;
cubeMapper->SetInputData(cube);
cubeMapper->SetScalarRange(cube->GetScalarRange());
vtkNew cubeActor;
cubeActor->SetMapper(cubeMapper);
// The usual rendering stuff.
vtkNew camera;
camera->SetPosition(1, 1, 1);
camera->SetFocalPoint(0, 0, 0);
vtkNew renderer;
vtkNew renWin;
renWin->AddRenderer(renderer);
vtkNew iren;
iren->SetRenderWindow(renWin);
renderer->AddActor(cubeActor);
renderer->SetActiveCamera(camera);
renderer->ResetCamera();
renderer->SetBackground(colors->GetColor3d("Cornsilk").GetData());
renWin->SetSize(600, 600);
// interact with data
renWin->Render();
iren->Start();
return EXIT_SUCCESS;
}
13、编译运行解决方案前需要将D:\VTK-8.2.0\msvc2017\build\bin下的所有DLL文件拷贝到D:\C++Code\TestVTK82\x64\Release目录下,首先出现黑色屏幕,需要鼠标点一下,然后出现渲染的OPENGL效果如下:
至此,完成VTK8.2运行环境的搭建和测试,MINGW32的没有在本地测试成功,还是MSVC编译器相对来说简单点。