VTK-8.2.0源碼編譯和初步使用(Cmake+VS2015+Qt5.14.2)
一、準(zhǔn)備數(shù)據(jù)
1、首先確保已安裝VS5015和Qt5.14.2
2、下載Cmake并安裝:Download CMake
3、下載VTK-8.2.0源碼和數(shù)據(jù)并解壓:Download | VTK
二、Cmake構(gòu)建
1、在本地磁盤創(chuàng)建相關(guān)文件夾

2、進入源碼根目錄,找到CmakeList.txt,修改CmakeList.txt中的選項,使得Debug模式下生成的lib和dll文件能自帶后綴_d,便于和Release的庫文件進行區(qū)分,否則后面可能編譯或鏈接有問題。

3、在Cmake中填入源碼位置,編譯后的位置,勾選Grouped方便看分組,點擊Configure,選擇VS2015,x64,點擊Finish,等待配置完成。

4、按下圖勾選,并設(shè)置庫文件統(tǒng)一存放目錄,再次點擊Configure。(如果勾選BUILD_TESTING后期VS編譯時間會比較長,默認(rèn)不勾選)

5、確認(rèn)Qt的相關(guān)目錄是否正確,不正確手動修改為正確的Qt的目錄,VTK_QT_VERSION根據(jù)自己的Qt版本選擇5或6,再次Configure,直至確認(rèn)所有紅色選項消失,點擊Generate

6、進入VTK-8.2.0-Build目錄,找到VTK.sln,用VS2015打開,先選擇Debug, x64平臺,解決方案管理器中,找到INSTALL項目,右鍵,生成,等待VS編譯完成。再選擇Release,x64平臺,再次生成INSTALL項目。

7、VS編譯完成后,在VTK-8.2.0-Install文件夾中就會有我們想要的頭文件、庫文件(Debug和Release庫都在里面),隨后將bin文件夾加入系統(tǒng)環(huán)境變量,方便后續(xù)VS或Qt中使用


三、在QCreator中創(chuàng)建工程VTKTest,以官方代碼Hello VTK為例,
1、打開pro文件,添加VTK庫文件

INCLUDEPATH += E:\Code\VTK-8.2.0-Install\include\vtk-8.2 win32:CONFIG(debug, debug|release): LIBS += -LE:\Code\VTK-8.2.0-Install\lib \ -lvtkFiltersSources-8.2_d \ -lvtkCommonColor-8.2_d \ -lvtkCommonCore-8.2_d \ -lvtkCommonExecutionModel-8.2_d \ -lvtkFiltersSources-8.2_d \ -lvtkInteractionStyle-8.2_d \ -lvtkRenderingContextOpenGL2-8.2_d \ -lvtkRenderingCore-8.2_d \ -lvtkRenderingFreeType-8.2_d \ -lvtkRenderingGL2PSOpenGL2-8.2_d \ -lvtkRenderingOpenGL2-8.2_d \ -lvtkGUISupportQt-8.2_d else:win32:CONFIG(release, debug|release): LIBS +=-LE:\Code\VTK-8.2.0-Install\lib \ -lvtkFiltersSources-8.2 \ -lvtkCommonColor-8.2 \ -lvtkCommonCore-8.2 \ -lvtkCommonExecutionModel-8.2 \ -lvtkFiltersSources-8.2 \ -lvtkInteractionStyle-8.2 \ -lvtkRenderingContextOpenGL2-8.2 \ -lvtkRenderingCore-8.2 \ -lvtkRenderingFreeType-8.2 \ -lvtkRenderingGL2PSOpenGL2-8.2 \ -lvtkRenderingOpenGL2-8.2 \ -lvtkGUISupportQt-8.2
2、在main.cpp中添加初始化代碼

#include<vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
VTK_MODULE_INIT(vtkRenderingFreeType)
3、MainWindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QVTKOpenGLWidget.h> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: Ui::MainWindow *ui; QVTKOpenGLWidget *m_pScene; }; #endif // MAINWINDOW_H
4、MainWindow.cpp
#include "MainWindow.h" #include "ui_MainWindow.h" #include <vtkActor.h> #include <vtkCamera.h> #include <vtkCylinderSource.h> #include <vtkNamedColors.h> #include <vtkNew.h> #include <vtkPolyDataMapper.h> #include <vtkProperty.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> #include <vtkRenderer.h> #include <vtkGenericOpenGLRenderWindow.h> #include <array> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) , m_pScene(nullptr) { ui->setupUi(this); m_pScene = new QVTKOpenGLWidget(); this->setCentralWidget(m_pScene); vtkNew<vtkNamedColors> colors; // 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). vtkNew<vtkCylinderSource> cylinder; 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. vtkNew<vtkPolyDataMapper> cylinderMapper; 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. vtkNew<vtkActor> cylinderActor; 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 vtkNew<vtkRenderer> renderer; 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); vtkSmartPointer<vtkGenericOpenGLRenderWindow> window = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New(); window->AddRenderer(renderer); m_pScene->SetRenderWindow(window); m_pScene->GetRenderWindow()->Render(); m_pScene->GetRenderWindow()->Start(); } MainWindow::~MainWindow() { delete ui; }
5、結(jié)果。

總結(jié):
最好事先在Debug模式下加入后綴_d,否則容易混淆庫文件,按上述步驟,在Debug模式和Release模式下都可以運行!

浙公網(wǎng)安備 33010602011771號