VTK配置所需工具:
编译Visual Studio版的VTK
step1.
Where is the source code: 选择vtk的源码路径
Where to build the binaries: 选择存放生成文件的目录
step2.
点击Configure
,选择Visual Studio
作为生成器(和你自己的版本对应就好),点击Finish
Step3.
待Configure
结束后,自定义配置。
指定CMAKE_INSTALL_PREFIX
,最后编译生成的bin
,include
,lib
文件夹等都将在指定的目录下。
修改完成后,再次Configure
,然后Generate
。
Step4.
在Step1中指定的文件夹下,将会生成一系列文件。用VisualStudio
打开VTK.sln
。
在右侧的解决方案栏中,选择ALL BUILD
,右键→生成。
待生成完成后,选择下方INSTALL
,右键→生成。
待安装结束后,在Step3中指定的路径下会生成bin
,include
,lib
,share
四个文件夹。
在Visual Studio中配置VTK
Step1.
新建项目→解决方案→右键属性→VC++目录→在包含目录和库目录中,分别新增上一步生成的include
文件夹和lib
文件夹的路径。
- 当缺失第三方库的
include
路径时,会找不到相关的函数、类的定义。
Step2.
链接器→输入→在附加依赖项中,输入lib
文件夹下所有的.lib
文件,此处可以使用正则表达式*
,
如D:\vtk-build\lib\*.lib
,代表D:\vtk-build\lib\
路径下所有的.lib
文件。
- 当缺失第三方库相关的
.lib
文件时,会出现****无法解析的外部符号****
一类的报错。
Step3.
将bin
文件夹的路径添加到系统环境变量PATH
中。
- 编译器会在系统的环境变量
PATH
中寻找运行程序所需要的.dll
文件
到此VTK的配置已经完成,接下来用一个例程测试一下
测试程序
读取并显示一张PNG
图片
#include <vtkImageViewer2.h>
#include <vtkNew.h>
#include <vtkPNGReader.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
// 网上的很多教程只有以上头文件,但用Visual Studio编译时一定要加上下面两句。
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
int main()
{
vtkNew<vtkPNGReader> reader;
reader->SetFileName("1.png");//要读取的图片路径
//显示
vtkNew<vtkImageViewer2> imageViewer;
imageViewer->SetInputConnection(reader->GetOutputPort());
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
imageViewer->SetupInteractor(renderWindowInteractor);
imageViewer->Render();
imageViewer->GetRenderer()->ResetCamera();
imageViewer->GetRenderWindow()->SetWindowName("PNGReader");
imageViewer->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
参考资料:VTK/Building/Windows