Search code examples
qtopenglscenegraph

How to use a QSGSimpleTextureNode?


I'm trying to understand how do I use a QSGSimpleTextureNode but Qt documentation is very vague. I want to render text on the scene graph, so basically what I want is to draw a texture with all the glyphs and then set that texture on a QSGSimpleTextureNode. My idea was to create the texture using standard OpenGL code and set the texture data to the data I have just created. I can't find an example to show me how to achieve this.


Solution

  • I would use QSGGeometryNode instead of QSGSimpleTextureNode. If I am not wrong it is not possible to set the texture coordinates in a QSGSimpleTextureNode. You could write a custom QQuickItem for the SpriteText and override the updatePaintNode:

    QSGNode* SpriteText::updatePaintNode(QSGNode *old, UpdatePaintNodeData *data)
    {
         QSGGeometryNode* node = static_cast<QSGGeometryNode*>(old);
         if (!node){
            node = new QSGGeometryNode();
         }
         QSGGeometry *geometry = NULL;
         if (!old){
            geometry = new QSGGeometry(QSGGeometry::defaultAttributes_TexturedPoint2D()
                          ,vertexCount);
            node->setFlag(QSGNode::OwnsGeometry);
            node->setMaterial(material);  // <-- Texture with your glyphs
            node->setFlag(QSGNode::OwnsMaterial);
            geometry->setDrawingMode(GL_TRIANGLES);
            node->setGeometry(geometry);
        } else {
            geometry = node->geometry();
            geometry->allocate(vertexCount);
        }
        if (textChanged){
            //For every Glyph in Text:
            //Calc x + y position for glyph in texture (between 0-1)
            //Create vertexes with calculated texture coordinates and calculated x coordinate
            geometry->vertexDataAsTexturedPoint2D()[index].set(...);
            ...
            node->markDirty(QSGNode::DirtyGeometry);
        }
        //you could start timer here which call update() methode
        return node;
    }