原文链接:http://amin-ahmadi.com/2018/01/28/viewing-3d-models-using-qt/
本文使用QT Quick中的Scene3D QML类型来查看3D模型文件,QT 3D使用assimp库来加载,支持如下3D文件格式:
3D, 3DS, 3MF, AC, AC3D, ACC, AMJ, ASE, ASK, B3D, BLEND (Blender), BVH, COB, CMS, DAE/Collada, DXF, ENFF, FBX, glTF 1.0 + GLB, glTF 2.0, HMB, IFC-STEP, IRR / IRRMESH, LWO, LWS, LXO, MD2, MD3, MD5, MDC, MDL, MESH / MESH.XML, MOT, MS3D, NDO, NFF, OBJ, OFF, OGEX, PLY, PMX, PRJ, Q3O, Q3S, RAW, SCN, SIB, SMD, STL, STP, TER, UC, VTA, X, X3D, XGL, ZGL等
我使用QT5.12+Qt Creator4.8.0测试,效果图如下:
QML文件将包含ApplicationWindow,其中ToolBar(包含ToolButton)使用FileDialog打开3D模型文件,Scene3D将包含加载的模型:
main.qml
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
import QtQuick.Controls 2 . 2 import QtQuick.Dialogs 1 . 2 import QtQuick.Scene3D 2 . 0 import Qt3D.Core 2 . 0 import Qt3D.Render 2 . 0 import Qt3D.Input 2 . 0 import Qt3D.Extras 2 . 0 ApplicationWindow { visible: true width: 640 height: 480 title: qsTr( "3D Viewer" ) header: ToolBar { ToolButton { text: "Open 3D Model" onPressed: { fileDialog.open() } } } FileDialog { id: fileDialog onAccepted: { sceneLoader.source = fileDialog.fileUrl } } Scene3D { anchors.fill: parent aspects: [ "input" , "logic" ] cameraAspectRatioMode: Scene3D.AutomaticAspectRatio Entity { id: sceneRoot Camera { id: camera projectionType: CameraLens.PerspectiveProjection fieldOfView: 30 aspectRatio: 16 / 9 nearPlane : 0 . 1 farPlane : 1000 . 0 position: Qt.vector3d( 10 . 0 , 0 . 0 , 100 . 0 ) upVector: Qt.vector3d( 0 . 0 , 1 . 0 , 0 . 0 ) viewCenter: Qt.vector3d( 0 . 0 , 0 . 0 , 0 . 0 ) } OrbitCameraController { camera: camera } components: [ RenderSettings { activeFrameGraph: ForwardRenderer { clearColor: Qt.rgba( 0 , 0 . 5 , 1 , 1 ) camera: camera } }, InputSettings { } ] Entity { id: monkeyEntity components: [ SceneLoader { id: sceneLoader } ] } } } } |
例子比较简单,主要使用了SceneLoader来加载3D模型文件,只能实现3D模型文件简单的加载,以及鼠标滚轮缩放等,更详细的操控可在InputSettings中完善!