前言
VTK网站有大量的程序示例,可以通过手敲代码的形式进行学习。下面就开始使用Qt来完成所有示例。
环境搭建
1)准备VTK库、QtCreator开发环境、Qt库
VTK使用8.2版本(64),Qt使用5.14.0(mingw73_64)
2)添加库到系统环境变量
VTK路径:C:\VTK\bin Qt路径:C:\Qt\Qt5.14.0\5.14.0\mingw73_64\bin
3)创建项目
打开Vtk示例,网站地址:
https://examples.vtk.org/site/Cxx/
参考Hello world示例,
将该项目Cmakelist文件中的库文件名复制出来。同时创建一个Qt项目,将项目依赖的头文件路径和库文件添加到Qt工程文件中。
项目编译
1)编译问题
这里报错可以看出是库的引用出了问题,追踪到该函数所在类,
返回值出现问题,再追踪到返回值类型的类的头文件中
VTKCOMMONEXECUTIONMODEL_EXPORT可以看出依赖的是vtkcommonexecutionmodel相关的库,此时只需要去VTK库的lib目录下找
将该库名添加到工程的pro文件中。再次编译就没有问题了。
示例源码
1)工程文件
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
INCLUDEPATH += C:\VTK\include\vtk-8.2
LIBS += -L"C:\VTK\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
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
2)主要代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <array>
#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>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
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);
ui->scene->SetRenderWindow(window);
ui->scene->GetRenderWindow()->Render();
ui->scene->GetRenderWindow()->Start();
}
MainWindow::~MainWindow()
{
delete ui;
}
#include "mainwindow.h"
#include <QApplication>
#include<vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType);
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
3)运行效果