如何在VS中使用vtk库,本文教你全套详细流程。
(一)git安装
参考以下博客,非常详细,总之就是在官网下载最新的安装版版本,安装过程中一直勾选默认选项,点next即可
最新Windows安装git及配置超详细教程 (适合零基础、手把手教) - Googleblog - 博客园开始安装 Git 访问 Git 官网地址(https://git-scm.com/download/),直接访问即可,这里下载根据你的系统选择,我这里选择 Windows系统,然后点击该 "https://www.cnblogs.com/52xiaobu/p/14083995.html可以通过命令行查看git版本、设置用户名和邮箱、进行初始化
(二)vcpkg安装
1.在git命令行中输入以下命令,下载vcpkg包,因要访问github,所以需要连下vpn才能下载
git clone "https://github.com/microsoft/vcpkg"
2.添加环境变量
3.进行编译
双击运行bootstrap-vcpkg.bat,等待跑完生成vcpkg应用程序文件即可,建议在每次下载所需的库之前,都进行此项操作以更新vcpkg
详细内容见:
(三)vtk安装
shift+右键点击vcpkg文件夹,打开powershell窗口,输入命令“vcpkg install vtk:x64-windows”,用“vcpkg install name(包名)”默认下载的是x86包,在x64系统上不能运行
仍需要连接VPN,安装成功后,最后显示如下,在vcpkg的packages文件夹下生成了许多新文件,竟然用了1个多小时...
若安装出错或速度过慢,可用国内镜像修改vcpkg(因已安装成功,没再尝试):
- 镜像地址: http://106.15.181.5
- 环境变量名:X_VCPKG_ASSET_SOURCES
- 环境变量值:x-azurl,http://106.15.181.5/
(四)在vs中使用
在VS中要保证安装英文包,防止显示出错
在powerShell中输入vcpkg integrate install 命令
在vs中添加项目属性表,在c/c++的附加包含目录中添加路径D:\library\vcpkg\packages\vtk_x64-windows\include\vtk-9.0
在链接器——常规中添加库目录D:\library\vcpkg\packages\vtk_x64-windows\lib
(五)代码测试
#include <vtkActor.h>
#include <vtkCylinderSource.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
int main(int, char*[])
{
vtkNew<vtkNamedColors> colors;
// Create a sphere
vtkNew<vtkCylinderSource> cylinderSource;
cylinderSource->SetCenter(0.0, 0.0, 0.0);
cylinderSource->SetRadius(5.0);
cylinderSource->SetHeight(7.0);
cylinderSource->SetResolution(100);
// Create a mapper and actor
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputConnection(cylinderSource->GetOutputPort());
vtkNew<vtkActor> actor;
actor->GetProperty()->SetColor(colors->GetColor3d("Cornsilk").GetData());
actor->SetMapper(mapper);
// Create a renderer, render window, and interactor
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->SetWindowName("Cylinder");
renderWindow->AddRenderer(renderer);
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
// Add the actor to the scene
renderer->AddActor(actor);
renderer->SetBackground(colors->GetColor3d("DarkGreen").GetData());
// Render and interact
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}