本文主要是介绍VTK练习实例(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
环境:win7_64 + VS2012 + VTK6.0 + CMake2.8
说明:实例使用CMake搭建工程
准备:新建目录E:\vtk_Exercise,在该目录下新建3个文件,分别是CMakeLists.txt,RenderCylinder.h,RenderCylinder.cpp
代码引用:https://blog.csdn.net/www_doling_net/article/details/8536376
步骤
step1:编写CMakeLists.txt,其内容如下
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.11)
MARK_AS_ADVANCED(CMAKE_BACKWARDS_COMPATIBILITY)
SET(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS TRUE)
SET(VTK_DIR E:/Thirdparty/x64/VTK)project(RenderCylinder)INCLUDE_DIRECTORIES(${VTK_DIR}/include/vtk-6.0
)
LINK_DIRECTORIES(${VTK_DIR}/lib/${ConfigurationName}
)
add_executable(${PROJECT_NAME} RenderCylinder.cpp RenderCylinder.h)target_link_libraries(${PROJECT_NAME}vtkFiltersGeometry-6.0vtkIOGeometry-6.0vtkIOCore-6.0vtkCommonCore-6.0vtkInteractionStyle-6.0vtkRenderingOpenGL-6.0vtkCommonDataModel-6.0vtkfilterscore-6.0vtkRenderingCore-6.0vtkCommonExecutionModel-6.0vtkFiltersSources-6.0
)
step2:编写RenderCylinder.h
#ifndef _RENDERCYLINDER_H_
#define _RENDERCYLINDER_H_//这两行代码必须有,否则编译成功,但运行时报错“no override found for。。。”
//适用VTK6.0及之前的版本,关于这点解释,参见https://www.cnblogs.com/cqu-qxl/p/6234763.html
#define vtkRenderingCore_AUTOINIT 2(vtkInteractionStyle,vtkRenderingOpenGL)
#define vtkRenderingVolume_AUTOINIT 1(vtkRenderingVolumeOpenGL)#include "vtkSmartPointer.h"#include "vtkRenderWindow.h"#include "vtkRenderer.h"#include "vtkRenderWindowInteractor.h"#include "vtkInteractorStyleTrackballCamera.h"#include "vtkCylinderSource.h"#include "vtkPolyDataMapper.h"#include "vtkActor.h"#endif
step3:编写RenderCylinder.cpp
#include "RenderCylinder.h"int main()
{vtkSmartPointer<vtkCylinderSource> cylinder = vtkSmartPointer<vtkCylinderSource>::New();cylinder->SetHeight( 3.0 );cylinder->SetRadius( 1.0 );cylinder->SetResolution( 10 );vtkSmartPointer<vtkPolyDataMapper> cylinderMapper = vtkSmartPointer<vtkPolyDataMapper>::New();cylinderMapper->SetInputConnection(cylinder->GetOutputPort()); vtkSmartPointer<vtkActor> cylinderActor = vtkSmartPointer<vtkActor>::New();cylinderActor->SetMapper( cylinderMapper );vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();renderer->AddActor( cylinderActor );renderer->SetBackground( 0.1, 0.2, 0.4 );vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();renWin->AddRenderer( renderer );renWin->SetSize( 300, 300 );vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();iren->SetRenderWindow(renWin);vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();iren->SetInteractorStyle(style);iren->Initialize();iren->Start();return 0;}
step4:打开cmake-gui.exe,在where is the source code栏填入路径E:\vtk_Exercise,在where to build the binaries栏填入E:\vtk_Exercise\bin(新建bin文件夹,存放*.sln,CMAKE临时文件等),单击左下角Configure,选择Visual Studio 11 Win64,单击Finish
step5:单击Generate
step6:构建工程结束。查看目录E:\vtk_Exercise\bin下,发现很多文件,打开RenderCylinder.sln,将工程RenderCylinder设为Set as Start Project,按下快捷键F7,开始Build工程,等待结束即可。
step7:我build的Debug版,所以再目录E:\vtk_Exercise\bin\Debug,生成RenderCylinder.exe。双击打开即可。实例运行。
这篇关于VTK练习实例(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!