目的
本介绍如何显示VTK库中自带一些模型。
新建空白工程,添加QWidget控件并提升为QVTKOpenGLNativeWidget。
pro
1 2 3 4 5 6 7
| INCLUDEPATH += /usr/include/vtk-9.1
LIBS += -lvtkGUISupportQt-9.1 -lvtksys-9.1 -lvtkInteractionStyle-9.1 \ -lvtkRenderingOpenGL2-9.1 -lvtkRenderingCore-9.1 -lvtkCommonExecutionModel-9.1 \ -lvtkCommonCore-9.1 -lvtkIOGeometry-9.1 -lvtkIOImage-9.1 -lvtkRenderingImage-9.1 \ -lvtkViewsCore-9.1 -lvtkImagingCore-9.1 -lvtkInteractionImage-9.1 -lvtkFiltersSources-9.1 \ -lvtkCommonColor-9.1
|
头文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| #include <vtkAutoInit.h> #include <vtkBMPReader.h> #include <vtkFloatArray.h> #include <vtkGenericOpenGLRenderWindow.h> #include <vtkImageActor.h> #include <vtkImageChangeInformation.h> #include <vtkImageData.h> #include <vtkImageImport.h> #include <vtkImageViewer2.h> #include <vtkInteractorStyleImage.h> #include <vtkJPEGReader.h> #include <vtkLookupTable.h> #include <vtkMetaImageReader.h> #include <vtkNamedColors.h> #include <vtkNew.h> #include <vtkPointData.h> #include <vtkPolyDataMapper.h> #include <vtkProperty.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> #include <vtkRenderer.h> #include <vtkSTLReader.h> #include <vtkSmartPointer.h> #include <vtkXMLImageDataWriter.h> #include <vtkEventQtSlotConnect.h> #include <vtkImageBlend.h> #include <vtkImageCanvasSource2D.h> #include <vtkImageIterator.h> #include <vtkImageLuminance.h> #include <vtkImageStencil.h> #include <vtkImageStencilData.h> #include <vtkImageViewer.h> #include <vtkPNGReader.h> #include <vtkSphereSource.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2) VTK_MODULE_INIT(vtkInteractionStyle)
|
像素
在构造函数中添加
1
| QSurfaceFormat::setDefaultFormat(QVTKOpenGLNativeWidget::defaultFormat());
|
设置界面渲染的精度。
读取
从官方接口读取模型数据
1 2
| vtkNew<vtkSphereSource> sphereSource; sphereSource->GetOutputPort();
|
注意这里的new方式。
mapper
1 2
| vtkNew<vtkPolyDataMapper> sphereMapper; sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
|
actor
1 2 3 4
| vtkNew<vtkActor> sphereActor; vtkNew<vtkNamedColors> colors; sphereActor->SetMapper(sphereMapper); sphereActor->GetProperty()->SetColor(colors->GetColor4d("Tomato").GetData());
|
renderer
1 2 3
| vtkNew<vtkRenderer> renderer; renderer->AddActor(sphereActor); renderer->SetBackground(colors->GetColor3d("SteelBlue").GetData());
|
window
1 2 3 4
| vtkNew<vtkGenericOpenGLRenderWindow> renderWindow; renderWindow->AddRenderer(renderer); renderWindow->SetWindowName("RenderWindowNoUIFile"); ui->widget->setRenderWindow(renderWindow);
|
效果
编译运行程序
说明
要想在窗口显示(window),需要设置渲染器(renderer),渲染器需要输入(actor),输入需要数据(mapper),数据要先读取(reader)。
流程为
flowchart TB
A[reader] --> B[mapper]
B --> C[actor]
C --> D[renderer]
D --> E[window]