1

I am experiencing issues loading and displaying a 3D model (.obj) in QML (Qt 5.15/6.0.0). I have tried the following 3 techniques but they all seem to have unique issues.

  1. Using QQuickItem and OpenGL with my own .obj loader. This can work but I'd like to take advantage of the power Qt provides as far as quality rendering, assimp model loading, and the potential to move away from OpenGL-specific draw code.

  2. A Scene3D from the Qt3D module. This works but it crashes on exit. I've tried suggested workarounds but they all fall short of preventing the crash, or if I do prevent the crash then my model doesn't load or display properly.

  3. A View3D from Quick 3D. This seems like the best option going forward but the drawback seems to be that I need to preprocess the obj file using balsam in order to convert it into a qt .mesh format. I'd rather avoid this step as I'd like users to be able to load their own models (obj, step, etc.)

Being able to set the mesh and texture source is required. This is possible using a Scene3D with a Mesh, but I don't see the same option using a View3D with a Model object.

Ideally, I could use the qml Qt3D.Render::Mesh item or Qt3DRender::QMesh to load object files dynamically but then use that mesh to render in the View3D. Or use a custom QQuick3DGeometry that loads the model and can be added as the QtQuick3D::Mode.geometry.

Am I missing something here or am I trying to blend two incompatible modules: Qt3D and Quick3D? Can a model be dynamically loaded into a View3D without converting it into a .mesh file?

SajadBlog
  • 514
  • 2
  • 12
  • Could you provide code for option number 2? Scened3D has already been successfully used with QML to display .obj models: https://github.com/tripolskypetr/simpleqml3d. – Florian Blume Feb 22 '21 at 11:36
  • This Iron Man model project is a good example of the problem. The model displays but the application crashes on exit. [Crash](https://imgur.com/a/dMLwEOx). This is possibly a variation of: [Bug Report](https://bugreports.qt.io/browse/QTBUG-64974). I've manage to display the models using View3D without issue but dynamic obj loading is still a future goal. – TheShiftingInt Feb 22 '21 at 16:11
  • That's weird, it doesn't crash for me. Neither with Qt 5.14.2 nor with 5.15.2. And neither when running or debugging the application. – Florian Blume Feb 23 '21 at 08:25
  • It occurs on the included "Qt 3D: Scene3D QML Example" as well, or any example using Scene3D. View3D is working with .mesh files and appears stable for my purpose. I could always use balsam to convert model files at runtime once I'm required to load them dynamically. Or hopefully something presents itself in Quick 3D. – TheShiftingInt Feb 23 '21 at 16:44

1 Answers1

1

Use SceneLoader.

This code is an example that you can Import .obj File in Scene3d:

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
import QtQuick.Controls.Material 2.12

ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("3D Viewer")

header: ToolBar
{
    Material.theme: Material.Dark

    ToolButton
    {
        text: "Open 3D Model"
        onPressed:
        {
            fileDialog.open()
        }
    }
}

FileDialog
{
    id: fileDialog
    onAccepted:
    {
        sceneLoader.source = fileDialog.fileUrl
        print(fileDialog.fileUrl)
        mainScene3d.forceActiveFocus()


    }
}

Scene3D
{
    id:mainScene3d
    anchors.fill: parent
    focus: true
    hoverEnabled: true

    aspects: ["input", "logic","render"]
    cameraAspectRatioMode: Scene3D.AutomaticAspectRatio

    Entity
    {
        id: sceneRoot

        Camera
        {
            id: camera
            projectionType: CameraLens.PerspectiveProjection
            fieldOfView: 30
            aspectRatio: 16/9
            nearPlane : 0.1
            farPlane : 100.0
            position: Qt.vector3d( 10.0, 0.0, 10.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:"#333333"
                    camera: camera
                }
            },
            InputSettings
            {
            }
        ]

        Entity
        {
            id: monkeyEntity
            components: [
                SceneLoader
                {
                    id: sceneLoader
                }
            ]
        }
    }
}
}

Output screenshot

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Parisa.H.R
  • 3,303
  • 3
  • 19
  • 38