QT QML 3D模型查看器

时间:2022-02-26 17:16:43

原文链接: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测试,效果图如下:

QT QML 3D模型查看器

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
 

ApplicationWindow
{
    visible: true
    width: 
    height: 
    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: 
                aspectRatio: 
                nearPlane : 
                farPlane : 
                position: Qt.vector3d(  )
                upVector: Qt.vector3d(  )
                viewCenter: Qt.vector3d(  )
            }
            
            OrbitCameraController
            {
                camera: camera
            }
            
            components: [
                RenderSettings
                {
                    activeFrameGraph: ForwardRenderer
                    {
                        clearColor: Qt.rgba()
                        camera: camera
                    }
                },
                InputSettings
                {
                }
            ]
            
            Entity
            {
                id: monkeyEntity
                components: [
                    SceneLoader
                    {
                        id: sceneLoader
                    }
                ]
            }
        }
    }
}

例子比较简单,主要使用了SceneLoader来加载3D模型文件,只能实现3D模型文件简单的加载,以及鼠标滚轮缩放等,更详细的操控可在InputSettings中完善!