Home > Enterprise >  In QML, where is the data of vertex coordinates stored in Mesh?
In QML, where is the data of vertex coordinates stored in Mesh?

Time:11-05

I'm a Qt newbie. I managed to load ".obj" file and show the 3D model using Mesh in my "main.qml". In fact, all I did was just setting the source of the file. With the help of an "ObjectPicker", I can know the indices of the triangle vertices. However, I don't know how to reach the coordinates of those vertices.

The Mesh loads the data itself. Since I didn't write the geometry, attributes and buffers for it, where and how can I get the vertex coordinates?

Part of the code is shown below. I set the picking method as TrianglePicking. Thanks a lot!

    Entity{
        id: part0Entity;
        objectName: "part0Entity";
        enabled: true;
        Connections{
            target: visEnt0;
            onClicked:{
                part0Entity.enabled = !part0Entity.enabled;
            }
        }
        Mesh{
            id: part0Mesh;
            source: "file:/home/zhaoyiji/Desktop/SF6-C1400/part0.obj"
        }

        Transform{
            id: part0Transform;
            matrix: Qt.matrix4x4();
        }
        DiffuseSpecularMaterial{
            id: part0Diffuse;
            ambient: "#80DC143C";
        }
        ObjectPicker{
            id: part0Picker;
            dragEnabled: true;
            onClicked: {
                if (pick.button == PickEvent.MiddleButton){
                    console.log(pick.worldIntersection)
                }
            }
        }

        components: [part0Mesh, part0Transform, part0Diffuse, part0Picker]
    }

CodePudding user response:

It turns out that the raw data can be accessed as follows:

In main.cpp:

QQuickView view;
view.setSource(QUrl("qrc:/main.qml"));
auto part0Mesh = view.rootObject()->findChild<Qt3DRender::QMesh *>("part0Mesh");
qDebug()<<part0Mesh->geometryFactory()->operator()()->attributes().value(0)->buffer()->data();

In qml:

Mesh{
    id: part0Mesh;
    objectName: "part0Mesh";
    source: "file:/YourFilePath/YourFileName.obj"
}

This is for QMesh's auto loading. There will be a functor to load the data from the source we set. The geometryFactory() returns a QSharedPointer pointing to it, and then operator()() returns the geometry. If we build the buffers, the attributes and the geometry on our own, then we can access the data simply by:

part0Mesh->geometry()->attributes().value(0)->buffer()->data();

Figured it out by reading the source code of qmesh.cpp. Hard for a newbie ;)

  • Related