找回密码
 立即注册
首页 业界区 业界 VTK-8.2.0源码编译和初步使用(Cmake+VS2015+Qt5.14.2) ...

VTK-8.2.0源码编译和初步使用(Cmake+VS2015+Qt5.14.2)

吮槌圯 3 天前
一、准备数据
1、首先确保已安装VS5015和Qt5.14.2
2、下载Cmake并安装:Download CMake
3、下载VTK-8.2.0源码和数据并解压:Download | VTK

二、Cmake构建
1、在本地磁盘创建相关文件夹
1.png

2、进入源码根目录,找到CmakeList.txt,修改CmakeList.txt中的选项,使得Debug模式下生成的lib和dll文件能自带后缀_d,便于和Release的库文件进行区分,否则后面可能编译或链接有问题。
2.png

3、在Cmake中填入源码位置,编译后的位置,勾选Grouped方便看分组,点击Configure,选择VS2015,x64,点击Finish,等待配置完成。
3.png

4、按下图勾选,并设置库文件统一存放目录,再次点击Configure。(如果勾选BUILD_TESTING后期VS编译时间会比较长,默认不勾选)
4.png

 5、确认Qt的相关目录是否正确,不正确手动修改为正确的Qt的目录,VTK_QT_VERSION根据自己的Qt版本选择5或6,再次Configure,直至确认所有红色选项消失,点击Generate
5.png

6、进入VTK-8.2.0-Build目录,找到VTK.sln,用VS2015打开,先选择Debug, x64平台,解决方案管理器中,找到INSTALL项目,右键,生成,等待VS编译完成。再选择Release,x64平台,再次生成INSTALL项目。
6.png

 7、VS编译完成后,在VTK-8.2.0-Install文件夹中就会有我们想要的头文件、库文件(Debug和Release库都在里面),随后将bin文件夹加入系统环境变量,方便后续VS或Qt中使用
7.png

8.png

 三、在QCreator中创建工程VTKTest,以官方代码Hello VTK为例,
1、打开pro文件,添加VTK库文件
9.png

 
  1. INCLUDEPATH += E:\Code\VTK-8.2.0-Install\include\vtk-8.2
  2. win32:CONFIG(debug, debug|release): LIBS += -LE:\Code\VTK-8.2.0-Install\lib \
  3.                         -lvtkFiltersSources-8.2_d \
  4.                         -lvtkCommonColor-8.2_d \
  5.                         -lvtkCommonCore-8.2_d \
  6.                         -lvtkCommonExecutionModel-8.2_d \
  7.                         -lvtkFiltersSources-8.2_d \
  8.                         -lvtkInteractionStyle-8.2_d \
  9.                         -lvtkRenderingContextOpenGL2-8.2_d \
  10.                         -lvtkRenderingCore-8.2_d \
  11.                         -lvtkRenderingFreeType-8.2_d \
  12.                         -lvtkRenderingGL2PSOpenGL2-8.2_d \
  13.                         -lvtkRenderingOpenGL2-8.2_d \
  14.                         -lvtkGUISupportQt-8.2_d
  15. else:win32:CONFIG(release, debug|release): LIBS +=-LE:\Code\VTK-8.2.0-Install\lib \
  16.                         -lvtkFiltersSources-8.2 \
  17.                         -lvtkCommonColor-8.2 \
  18.                         -lvtkCommonCore-8.2 \
  19.                         -lvtkCommonExecutionModel-8.2 \
  20.                         -lvtkFiltersSources-8.2 \
  21.                         -lvtkInteractionStyle-8.2 \
  22.                         -lvtkRenderingContextOpenGL2-8.2 \
  23.                         -lvtkRenderingCore-8.2 \
  24.                         -lvtkRenderingFreeType-8.2 \
  25.                         -lvtkRenderingGL2PSOpenGL2-8.2 \
  26.                         -lvtkRenderingOpenGL2-8.2 \
  27.                         -lvtkGUISupportQt-8.2
复制代码
2、在main.cpp中添加初始化代码
10.png

 
  1. #include<vtkAutoInit.h>
  2. VTK_MODULE_INIT(vtkRenderingOpenGL2)
  3. VTK_MODULE_INIT(vtkInteractionStyle)
  4. VTK_MODULE_INIT(vtkRenderingFreeType)
复制代码
3、MainWindow.h
  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QMainWindow>
  4. #include <QVTKOpenGLWidget.h>
  5. QT_BEGIN_NAMESPACE
  6. namespace Ui
  7. {
  8.     class MainWindow;
  9. }
  10. QT_END_NAMESPACE
  11. class MainWindow : public QMainWindow
  12. {
  13.     Q_OBJECT
  14. public:
  15.     MainWindow(QWidget *parent = nullptr);
  16.     ~MainWindow();
  17. private:
  18.     Ui::MainWindow *ui;
  19.     QVTKOpenGLWidget *m_pScene;
  20. };
  21. #endif // MAINWINDOW_H
复制代码
4、MainWindow.cpp
  1. #include "MainWindow.h"
  2. #include "ui_MainWindow.h"
  3. #include <vtkActor.h>
  4. #include <vtkCamera.h>
  5. #include <vtkCylinderSource.h>
  6. #include <vtkNamedColors.h>
  7. #include <vtkNew.h>
  8. #include <vtkPolyDataMapper.h>
  9. #include <vtkProperty.h>
  10. #include <vtkRenderWindow.h>
  11. #include <vtkRenderWindowInteractor.h>
  12. #include <vtkRenderer.h>
  13. #include <vtkGenericOpenGLRenderWindow.h>
  14. #include
  15. MainWindow::MainWindow(QWidget *parent)
  16.     : QMainWindow(parent)
  17.     , ui(new Ui::MainWindow)
  18.     , m_pScene(nullptr)
  19. {
  20.     ui->setupUi(this);
  21.     m_pScene = new QVTKOpenGLWidget();
  22.     this->setCentralWidget(m_pScene);
  23.     vtkNew<vtkNamedColors> colors;
  24.     // Set the background color.
  25.     std::array<unsigned char, 4> bkg{{26, 51, 102, 255}};
  26.     colors->SetColor("BkgColor", bkg.data());
  27.     // This creates a polygonal cylinder model with eight circumferential facets
  28.     // (i.e, in practice an octagonal prism).
  29.     vtkNew<vtkCylinderSource> cylinder;
  30.     cylinder->SetResolution(8);
  31.     // The mapper is responsible for pushing the geometry into the graphics
  32.     // library. It may also do color mapping, if scalars or other attributes are
  33.     // defined.
  34.     vtkNew<vtkPolyDataMapper> cylinderMapper;
  35.     cylinderMapper->SetInputConnection(cylinder->GetOutputPort());
  36.     // The actor is a grouping mechanism: besides the geometry (mapper), it
  37.     // also has a property, transformation matrix, and/or texture map.
  38.     // Here we set its color and rotate it around the X and Y axes.
  39.     vtkNew<vtkActor> cylinderActor;
  40.     cylinderActor->SetMapper(cylinderMapper);
  41.     cylinderActor->GetProperty()->SetColor(
  42.         colors->GetColor4d("Tomato").GetData());
  43.     cylinderActor->RotateX(30.0);
  44.     cylinderActor->RotateY(-45.0);
  45.     // The renderer generates the image
  46.     // which is then displayed on the render window.
  47.     // It can be thought of as a scene to which the actor is added
  48.     vtkNew<vtkRenderer> renderer;
  49.     renderer->AddActor(cylinderActor);
  50.     renderer->SetBackground(colors->GetColor3d("BkgColor").GetData());
  51.     // Zoom in a little by accessing the camera and invoking its "Zoom" method.
  52.     renderer->ResetCamera();
  53.     renderer->GetActiveCamera()->Zoom(1.5);
  54.     vtkSmartPointer<vtkGenericOpenGLRenderWindow> window = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
  55.     window->AddRenderer(renderer);
  56.     m_pScene->SetRenderWindow(window);
  57.     m_pScene->GetRenderWindow()->Render();
  58.     m_pScene->GetRenderWindow()->Start();
  59. }
  60. MainWindow::~MainWindow()
  61. {
  62.     delete ui;
  63. }
复制代码
5、结果。
11.png

总结:
最好事先在Debug模式下加入后缀_d,否则容易混淆库文件,按上述步骤,在Debug模式和Release模式下都可以运行!
 

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册