[Interest] Accessing vertex data of primitive shapes (cuboid, sphere, …)

Megidd Git megiddgit at gmail.com
Mon Mar 9 11:45:58 CET 2020


You might want to do this:

       Qt3DRender::QGeometryRenderer *mesh = ...

       Qt3DRender::QGeometry *meshGeometry = mesh->geometry();

    for (Qt3DRender::QAttribute *attribute : meshGeometry->attributes()) {

        Qt3DRender::QBuffer *buf = attribute->buffer();

        if (buf) {

            if (buf->data().isEmpty())

                buf->setData(buf->dataGenerator()->operator()());

            buf->setSyncData(true);

            buf->setAccessType(Qt3DRender::QBuffer::AccessType::ReadWrite);

        }

    }


On Mon, Mar 9, 2020 at 12:54 PM Federico Ferri <federico.ferri.it at gmail.com>
wrote:

> I’m trying to access vertex data of QGeometry objects for computing the
> bounding box of the object.
> This works fine for QMesh, but for QCuboidMesh, QSphereMesh, etc… vertex
> data seems invalid.
> Is there some method to call to make vertex data actual?
>
> This is my code:
>
> QVector<qreal> entityBoundingBox(Qt3DCore::QEntity *entity)
> {
>     for(const auto &comp : entity->componentsOfType<Qt3DRender::QGeometryRenderer>())
>     {
>         auto geom = comp->geometry();
>         if(geom)
>             return geometryBoundingBox(geom);
>     }
>     return {};
> }
>
> QVector<qreal> geometryBoundingBox(Qt3DRender::QGeometry *geom)
> {
>     QVector<qreal> ret;
>     auto attrs = geom->attributes();
>     for(const auto &attr : attrs)
>     {
>         if(attr->name() != "vertexPosition") continue;
>         int n = attr->count();
>         auto buf = attr->buffer();
>         const QByteArray &data = buf->data();
>         float vmin[3], vmax[3];
>         for(int i = 0; i < n; i++)
>         {
>             int vertexOffset = i * attr->byteStride();
>             int offset = vertexOffset + attr->byteOffset();
>             const char *rawData = &(data.constData()[offset]);
>             auto value = reinterpret_cast<const float*>(rawData);
>             qDebug() << "geometryBoundingBox: scanning vertex" << value[0] << value[1] << value[2];
>             for(int j = 0; j < 3; j++)
>             {
>                 vmin[j] = i ? qMin(vmin[j], value[j]) : value[j];
>                 vmax[j] = i ? qMax(vmax[j], value[j]) : value[j];
>             }
>         }
>         ret.resize(6);
>         ret[0] = vmin[0]; ret[1] = vmin[1]; ret[2] = vmin[2];
>         ret[3] = vmax[0]; ret[4] = vmax[1]; ret[5] = vmax[2];
>         return ret;
>     }
>     return ret;
> }
>
>
> when working on the cuboid or sphere meshes I see vertex data such as:
>
> geometryBoundingBox: scanning vertex 1.2992e-08 5.50632e+11 1.29257e+19
>
> geometryBoundingBox: scanning vertex 0 0 nan
>
> geometryBoundingBox: scanning vertex 9.40053e-33 1.4013e-45 9.40053e-33
>
> geometryBoundingBox: scanning vertex 0 0 nan
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20200309/5c4dbfa9/attachment.html>


More information about the Interest mailing list