这是第一次写博客,也是为了记录和分享一些过程,描述的不对的地方,请不吝斧正,谢谢各位了!!
这个教程综合了网上一些博客的资料和自己实践的过程,有问题可以直接留言。
1.下载vtk-5.8.0zip & vtkdata-5.8.0.zip 分别解压至 ./VTKsourcecode & ./VTKdata 中
./VTKsourcecode(包含CMakeLists.txt文件)
./VTKdata
./VTKbin(新建)
2.安装cmake2.8,打开cmake:
camke之前最好检查一下自己电脑是否安装了高于4.0版本的.Net Framework,否则cmake可能会报错
打开控制面板,选程序和功能,在里面找一下,如果有最好全部卸载,保证只有.Net Framework 4.0存在于电脑中
这个过程也可以通过其他软件管理去完成(例如360软件管家)
1>
Where is the source code: 选择 ./VTKsourcecode
Where to build the binaries: 选择 ./VTKbin
2>
点击Configure(选择VS10 64bits)
vtk_use_guisupport:GUI勾选
vtk_use_mfc:MFC勾选
vtk_data_root: 一般可能会找不到,指定一下目录,如“E:\I_VTK\VTK\VTKdata”
build_examples:勾选上则编译例子(我一般不选,浪费时间)
build_testing: 勾选上则编译测试代码,也是例子(同上)
build_shared_libs: 设置为on。
3> 所有项目会变成白色后后可以点击 Generate,完成时会提示 Generating done,此时关闭cmake。
3.用VS 2010打开 E:\I_VTK\VTK\VTKbin 目录下的 VTK.sln 文件,右键点击All_Build选择重新生成。
错误1:This file requires _WIN32_WINNT to be #defined at least to 0x0403. Value 0x0501 or higher is recommended. (link:http://blog.csdn.net/cgcoder/article/details/8262232)
解决办法为:
找到五个工程:vtkDLG、vtkMDI、vtkSDI、Win32SampleMFC、vtkMFC
a.打开前四个工程源文件中的stdAfx.h,如果找不到,则打开<资源文件>中的stdAfx.cpp,打开找到其中#include "stdAfx.h" ,右击:<打开"stdAfx.h">
b.打开vtkMFC工程的vtkMFCWindow.cpp,按如下方法修改(注:a.4个stdafx.h 3个都是在.\VTKsourcecode\Examples\GUI\Win32\vtkMFC\下面,你可以搜索一下,共4个stdafx.h,另外1个在.\VTKsourcecode\Examples\GUI\Win32下面)
c.一个 vtkMFCWindow.cpp, 在.\VTKsourcecode\GUISupport\MFC
// Modify the following defines if you have to target a platform prior to the ones specified below.
// Refer to MSDN for the latest info on corresponding values for different platforms.
#ifndef WINVER // Allow use of features specific to Windows 95 and Windows NT 4 or later.
#define WINVER
0x0601 // Change this to the appropriate value to target Windows 98 and Windows 2000 or later.
#endif
#ifndef _WIN32_WINNT // Allow use of features specific to Windows NT 4 or later.
#define _WIN32_WINNT
0x0601 // Change this to the appropriate value to target Windows 98 and Windows 2000 or later.
#endif
#if _MSC_VER >= 1300
#ifndef _WIN32_WINDOWS // Allow use of features specific to Windows 98 or later.
#define _WIN32_WINDOWS
0x0601 // Change this to the appropriate value to target Windows Me or later.
#endif
#endif
#ifndef _WIN32_IE // Allow use of features specific to IE 4.0 or later.
#define _WIN32_IE
0x0800 // Change this to the appropriate value to target IE 5.0 or later.
#endif
红字对应的是win7版本。
另外还要改动文件vtkMFCWindow.cpp中的部分代码:
// Allow use of features specific to Windows 95 and Windows NT 4 or later.
#ifndef WINVER
#define WINVER
0x0601
#endif
// Define _WIN32_WINNT and _WIN32_IE to avoid the following error with Visual
// Studio 2008 SP1:
// "C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\sdkddkver.h(217) :
// fatal error C1189: #error : _WIN32_WINNT settings conflicts with _WIN32_IE
// setting"
#ifndef _WIN32_WINNT
#define _WIN32_WINNT
0x0601 // =_WIN32_WINNT_NT4
#endif
#ifndef _WIN32_IE
#define _WIN32_IE
0x0800 //=_WIN32_IE_IE60SP1
#endif
此时,应该没有问题了。
若出现cannot find vtkMFC.lib则是由于cmake阶段未勾选vtk_use_mfc
添加环境变量path = ‘E:\I_VTK\VTK\VTKbin\bin\Debug’;
4.新建测试工程
新建文件夹 ./VTKtest
./src
./bin
在src中创建cpp和CMakeLists.txt文件(用于cmake创建工程)
main.cpp
#include
#include
#include
#include
#include
#include
#include
#include
int main(int, char *[])
{
// Create a sphere
vtkSmartPointer sphereSource =
vtkSmartPointer::New();
sphereSource->SetCenter(0.0, 0.0, 0.0);
sphereSource->SetRadius(5.0);
//mapper
vtkSmartPointer mapper =
vtkSmartPointer::New();
mapper->SetInputConnection(sphereSource->GetOutputPort());
//actor
vtkSmartPointer actor =
vtkSmartPointer::New();
actor->SetMapper(mapper);
//renderer ,renderWindow, renderWindowInteractor.
vtkSmartPointer renderer =
vtkSmartPointer::New();
vtkSmartPointer renderWindow =
vtkSmartPointer::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer renderWindowInteractor =
vtkSmartPointer::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(actor);
renderer->SetBackground(.3, .6, .3); // Background color green
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
CMakeList.txt
#----------------------------------------------------------------------------------
cmake_minimum_required( VERSION 2.8 )
project( VTKtest )
#----------------------------------------------------------------------------------
find_package( VTK REQUIRED )
include( ${VTK_USE_FILE} )
#----------------------------------------------------------------------------------
SET( PROJECT_SRCS
main.cpp
)
#----------------------------------------------------------------------------------
INCLUDE_DIRECTORIES( ${PROJECT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
${VTK_DIR}
)
ADD_EXECUTABLE( VTKtest
${PROJECT_SRCS}
)
TARGET_LINK_LIBRARIES ( VTKtest
${VTK_LIBRARIES}
)
打开cmake,选择src和bin之后configure和generate,在bin文件夹中生成sln。
用VS2010打开工程,编译,运行之!