引言
该示例为官网上的例子。创建了一个每个面被设置相同颜色的立方体。
示例
开发环境
使用QtCreator4.11.2,Qt5.14.2。使用的vtk9.2的库及其头文件。创建空项目。
示例代码
其pro文件中的内容:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11 vtk9.2
# 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
SOUPDIR = $$PWD/../../SOUPdependency
vtk9.2 {
contains(QT_ARCH, x86_64) {
include($$SOUPDIR/vtk-9.2/vtk-9.2.pri)
} else {
include($$SOUPDIR/vtk-9.2-2017-omp-win32/vtk-9.2.pri)
}
DEFINES += vtkEventDataButton3D=vtkEventDataDevice3D
DEFINES += vtkEventDataMove3D=vtkEventDataDevice3D
}
include($$SOUPDIR/dcmtk-3.6.4/dcmtk-3.6.4.pri)
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
SOURCES += \
main.cpp
main.cpp
#include <vtkCubeSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkCamera.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkProperty.h>
#include <QDebug>
#include <QString>
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2)//渲染
VTK_MODULE_INIT(vtkInteractionStyle)//交互样式
VTK_MODULE_INIT(vtkRenderingFreeType)//文本图像
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2)//体素
int main(int argc,char*argv[])
{
vtkNew<vtkNamedColors> colors;
vtkNew<vtkCubeSource> cubeSource;
cubeSource->Update();
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputData(cubeSource->GetOutput());
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(colors->GetColor3d("Banana").GetData());
vtkNew<vtkRenderer> render;
render->AddActor(actor);
render->ResetCamera();
render->GetActiveCamera()->Azimuth(30);//设置相机的方位角,水平方向旋转,正数——顺时针,负数——逆时针
render->GetActiveCamera()->Elevation(30);//设置相机位置,上下方向,正数——相机向上移 负数——相机向下移
render->ResetCameraClippingRange();//重置相机前后剪切面范围
render->SetBackground(colors->GetColor3d("Sliver").GetData());
double dPostion[3];//获取并输出相机的位置
render->GetActiveCamera()->GetPosition(dPostion);
qDebug()<<QString::number(dPostion[0],'f',2)<<"1"<<QString::number(dPostion[1],'f',2)<<"2"<<QString::number(dPostion[2],'f',2);
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(render);
renderWindow->SetWindowName("Cube");
renderWindow->SetSize(300,300);
renderWindow->Render();
vtkNew<vtkRenderWindowInteractor> interactor;
interactor->SetRenderWindow(renderWindow);
interactor->Start();
return 0;
}