3

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.

Nuno Santos
  • 1,476
  • 3
  • 17
  • 34

1 Answers1

2

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;
}
riv333
  • 389
  • 2
  • 12
  • could you pls take a look at a very similar question from me [here](http://stackoverflow.com/questions/42538001/how-to-map-a-saved-texture-directly-on-a-quickitem-display-it?noredirect=1#comment72223805_42538001) ? – TheWaterProgrammer Mar 02 '17 at 06:06