JKac
JKac

Reputation: 76

Implementing RGB in QML using Qt3D

Hey The meshviewer shows a default color which I know how to change as you can see below. But how can i change that color into the rgb data I get from my 3D camera using a Intel RealSense Camera? Does anyone have experience with this. I was looking in to Qt3D.Render but I can't figure it out.

PhongMaterial {
    id: reconstructedMeshMaterial
    ambient: "#F6C6AF"
}

Output Meshviewer

Upvotes: 2

Views: 433

Answers (1)

iam_peter
iam_peter

Reputation: 3924

I've created an example project for you based on the Qt Quick 3D - Custom Geometry Example. It shows how to set a color per vertex using a vertex buffer. It uses QQuick3DGeometry to create a custom geometry that can be used in QML. The important part is to understand how to create the vertex buffer consisting of a position (x, y, z) and color (r, g, b, a). After setting the data the vertex buffer needs to be configured using the addAttribute() function. The documentation can be found here.

void ExampleTriangleGeometry::updateData()
{
    clear();

    // PositionSemantic: The attribute is a position. 3 components: x, y, and z
    // ColorSemantic: The attribute is a vertex color vector. 4 components: r, g, b, and a
    int stride = 7 * sizeof(float);

    // 3 vertices in total for the triangle
    QByteArray vertexData(3 * stride, Qt::Initialization::Uninitialized);
    float *p = reinterpret_cast<float *>(vertexData.data());

    // a triangle, front face = counter-clockwise
    *p++ = -1.0f; // x
    *p++ = -1.0f; // y
    *p++ = 0.0f;  // z

    *p++ = 1.0f; // r
    *p++ = 0.0f; // g
    *p++ = 0.0f; // b
    *p++ = 1.0f; // a

    *p++ = 1.0f;
    *p++ = -1.0f;
    *p++ = 0.0f;

    *p++ = 0.0f;
    *p++ = 1.0f;
    *p++ = 0.0f;
    *p++ = 1.0f;

    *p++ = 0.0f;
    *p++ = 1.0f;
    *p++ = 0.0f;

    *p++ = 0.0f;
    *p++ = 0.0f;
    *p++ = 1.0f;
    *p++ = 1.0f;

    setVertexData(vertexData);
    setStride(stride);
    setBounds(QVector3D(-1.0f, -1.0f, 0.0f), QVector3D(+1.0f, +1.0f, 0.0f));

    setPrimitiveType(QQuick3DGeometry::PrimitiveType::Triangles);

    addAttribute(QQuick3DGeometry::Attribute::PositionSemantic,
                 0,
                 QQuick3DGeometry::Attribute::F32Type);

    addAttribute(QQuick3DGeometry::Attribute::ColorSemantic,
                 3 * sizeof(float),
                 QQuick3DGeometry::Attribute::F32Type);
}

Upvotes: 1

Related Questions