Viewing 3D Models Using Qt

In this post I’ll share an example project that describes how to load and view 3D models using Qt (such as Blender files and so on). The source codes here are tested with Qt 5.9.4 which was released a few days ago.

Here’s a  screenshot depicting the application at work:

This project is based on Qt 3D QML classes.  First things’s first you should make sure required modules for Qt 3D are added to your Qt Quick Application project, and your *.pro file, as seen here:

QT += quick qml 3dcore 3drender 3dinput 3dlogic 3dextras 3danimation

Next, you need to add the required import statements to your QML file:

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





Our QML file will include an ApplicationWindow, with a ToolBar (and a ToolButton) to open a 3D model file using a FileDialog, and a Scene3D that will include the loaded model:

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
{

}
}





Finally, the Scene3D will include a single Enity with all the codes necessary to load and view the model:

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, 0.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
}
]
}
}
}

That’s about it. You can download the source codes for this example project using this link. A full list of supported 3D model file formats can be found here. Qt 3D uses Open Asset Import Library (assimp), so ideally it should support the following formats as of the time of writing this post:

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





Leave a Reply

Your email address will not be published. Required fields are marked *