- 默认前提:已安装好VS2017
- Cmake下载安装(版本无所谓):https://blog.csdn.net/u011231598/article/details/80338941
- vtk7.1.1官网下载地址(别的版本配置方式一样):https://vtk.org/files/release/7.1/VTK-7.1.1.zip
- VTK解压缩后得到一个VTK-7.1.1的文件夹,我在这个文件夹里面新建了两个文件夹build和VTK-PREFIX。(我的分别是是D:/VTK-7.1.1/build和D:/VTK-7.1.1/VTK-PREFIX)
- 用cmake编译VTK:
打开cmake,在 where is the source code 后面填写VTK解压缩后的文件夹地址,在where to build the binaries 后面填写我们刚刚创建的build文件夹地址。 - 然后点击configure,选择Visual Studio 15 2017 Win64,因为我是在64位的vs2017下编译的。等待一段时间,出现红字,在BUILD_EXAMPLES后面打勾,在CMAKE_INSTALL_PREFIX后面填写我们刚刚建立的VTK-PREFIX文件夹地址。
然后再次点击configure,红色消失。
点击generate,出现Generate Done。
-
打开我们之前创建的build文件夹,用vs2017打开VTK.sln文件。选中ALL_BUILD项目,右键选择生成,静静等待一段时间直到编译完成。
-
看看我们的VTK是否安装成功:选择Cube项目,右键选择设为启动项目,然后CTRL+F5运行,一个彩色的多边形就出来啦
以上,VTK就安装成功了,下面我们来看一下新建一个vs工程,VTK该如何配置。
- 在VTK.sln的工程中,选中INASTALL项目,右键选择生成,这样在我们前面创建的VTK-PREFIX文件夹中就生成了VTK的头文件、dll文件和lib文件
- 在环境变量,系统变量的PATH中,把bin文件夹的地址加进去。我的是D:\VTK-7.1.1\VTK-PREFIX\bin
- 在VS2017中创建一个新的工程,选择项目->属性,在VC++目录的包含目录中,把VTK-PREFIX文件夹中的include文件夹的地址加进去,在VC++目录的库目录中,把VTK-PREFIX文件夹中的lib文件夹的地址加进去。
- 在VTK-PREFIX的
lib
文件夹内新建一个txt
文件,将DIR *.lib /B >LIBLIST.TXT
写入txt并保存,改后缀.txt
为.bat
并双击运行,这时在目录生成了一个新的.txt
文件,文件存有所有lib
文件的名字。编辑项目->属性->配置属性->链接器->输入 附加依赖项,将刚生成的所有lib
文件的名字粘贴进去。 - .dll 配置:配置完上边,项目应该可以生成了,把 build 文件夹中的 bin 文件夹中的所有 dll 文件放到VS生成出的 exe 所在文件夹下。找不到的可以编译后仔细查看目录,在Debug下。或者在项目->属性->配置属性->调试 环境 写入
PATH=
VTK-PREFIX的bin的完整目录,也可以将dll
文件全部复制在项目中。 - 可惜的是每次新建一个项目都要重新配置(貌似可以写配置文件,方便一点,我自己没有试过)
测试:
将以下代码复制到cpp中,配置成功了就能成功显示。
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCylinderSource.h>
#include <vtkNamedColors.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <array>
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);//和官方的例子有些不同,下面解释
VTK_MODULE_INIT(vtkInteractionStyle);
int main(int, char *[]) {
vtkSmartPointer<vtkNamedColors> colors =
vtkSmartPointer<vtkNamedColors>::New();
// Set the background color.
std::array<unsigned char, 4> bkg{{26, 51, 102, 255}};
colors->SetColor("BkgColor", bkg.data());
// This creates a polygonal cylinder model with eight circumferential facets
// (i.e, in practice an octagonal prism).
vtkSmartPointer<vtkCylinderSource> cylinder =
vtkSmartPointer<vtkCylinderSource>::New();
cylinder->SetResolution(8);
// The mapper is responsible for pushing the geometry into the graphics
// library. It may also do color mapping, if scalars or other attributes are
// defined.
vtkSmartPointer<vtkPolyDataMapper> cylinderMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
cylinderMapper->SetInputConnection(cylinder->GetOutputPort());
// The actor is a grouping mechanism: besides the geometry (mapper), it
// also has a property, transformation matrix, and/or texture map.
// Here we set its color and rotate it around the X and Y axes.
vtkSmartPointer<vtkActor> cylinderActor = vtkSmartPointer<vtkActor>::New();
cylinderActor->SetMapper(cylinderMapper);
cylinderActor->GetProperty()->SetColor(
colors->GetColor4d("Tomato").GetData());
cylinderActor->RotateX(30.0);
cylinderActor->RotateY(-45.0);
// The renderer generates the image
// which is then displayed on the render window.
// It can be thought of as a scene to which the actor is added
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(cylinderActor);
renderer->SetBackground(colors->GetColor3d("BkgColor").GetData());
// Zoom in a little by accessing the camera and invoking its "Zoom" method.
renderer->ResetCamera();
renderer->GetActiveCamera()->Zoom(1.5);
// The render window is the actual GUI window
// that appears on the computer screen
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetSize(300, 300);
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("Cylinder");
// The render window interactor captures mouse events
// and will perform appropriate camera or actor manipulation
// depending on the nature of the events.
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// This starts the event loop and as a side effect causes an initial render.
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
报过的错:
模块计算机类型“x64”与目标计算机类型“X86”冲突 注意所有的都改成64和WIN64(WIN32也不行)