Search code examples
qtqt3d

Change and update geometry mesh at runtime


This question and answer, in Oct 2015, implies it is possible to change Qt3D mesh and update it:

Question

I want to use Qt3d in a situation that involves dynamic runtime changes of geometric data on the application side.

What is the best way to dynamically change a mesh for an entity?

I'd rather do all this on the C++ side, but QMesh doesn't seem to provide an API for changing the mesh data.

I looked at some other examples of making a custom QAbstractMesh class and QAbstractMeshFunctor. It looks like I could possibly make a custom mesh type to do what I need but I have a question. If I keep a reference to the QMeshDataPtr that I make from the functor, can I simply modify the mesh data whenever I want and the entities that reference it will update automatically?

Answer

The API for this has changed a little in 5.6. The geometric data is now contained in one or more QBuffer objects and is referenced by one or more QAttributes that describe the data layout in the buffers. The QAttributes are rendered by adding them to a QGeometryRenderer component.

You can either update the above objects on the main thread and call update() or as before you can also use a functor to have the backend generate the dynamic data.

Now, my question is about calling update(). Exactly what section of Qt3D API is referred to?


Solution

  • There is a test available at Qt installation directory on my Linux machine:

    /home/{user}/Qt5.12.6/5.12.6/Src/qt3d/tests/manual/custom-mesh-update-data-cpp/
    
    

    which I discovered by following this link when searching Google for qt3d mesh update keywords.


    The above test is using Qt3DRender::QBuffer API to update mesh data:

    void QBuffer::updateData(int offset, const QByteArray &bytes)

    Updates the data by replacing it with bytes at offset.

    Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

    Code looks like this:

    Qt3DRender::QBuffer *vertexDataBuffer;
    
    // ...
    
    QByteArray updateData;
    
    // ...
    
    vertexDataBuffer->updateData(pos,updateData);
    
    // ...