Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QSGGeometry. Batch 2 triangles to draw a rectangle with QSGGeometry::DrawTriangleStrip.
Forum Updated to NodeBB v4.3 + New Features

QSGGeometry. Batch 2 triangles to draw a rectangle with QSGGeometry::DrawTriangleStrip.

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 341 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ALKMANA Offline
    ALKMANA Offline
    ALKMAN
    wrote on last edited by
    #1

    I am trying to separate the triangles that I draw with QSGGeometry::DrawTriangleStrip. into batches of 2 in order to draw seperate rectangles with a texture(eventually).

    I cannot figure out how I can do this.
    It seems that passing indices do not affect how OpenGL handles triangle strips.

    Am I missing something important here?

    My end result should be 2 rectangles but not joined together like in the pic below.
    Screenshot 2024-07-25 at 5.39.45 PM.jpg

    
    QSGNode *pianoroll_preview::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
    {
        pianoroll_preview_instance *n= static_cast<pianoroll_preview_instance *>(oldNode);
        QRectF rect = boundingRect();
    
        QSGGeometryNode *node = nullptr;
        QSGGeometry *geometry = nullptr;
    
        int vCount = 4;
        if (!n) {
            n = new pianoroll_preview_instance();
    
            node = new QSGGeometryNode;
            geometry = new QSGGeometry(QSGGeometry::defaultAttributes_ColoredPoint2D(), vCount);
            geometry->setDrawingMode(QSGGeometry::DrawTriangleStrip);
            //geometry = node->geometry();
            geometry->allocate( vCount * 2 );
            node->setGeometry(geometry);
            node->setFlag(QSGNode::OwnsGeometry);
            QSGVertexColorMaterial *material = new QSGVertexColorMaterial;
            node->setMaterial(material);
            node->setFlag(QSGNode::OwnsMaterial);
            n->appendChildNode( node );
        }
    
        QSGGeometry::ColoredPoint2D *vertex = geometry-> vertexDataAsColoredPoint2D();
    
        quint16* indices = geometry->indexDataAsUShort();
        indices[0] = 0; indices[1] = 1; indices[2] = 2;
        indices[3] = 0; indices[4] = 2; indices[5] = 3;
    
        indices[6] = 0; indices[7] = 1; indices[8] = 2;
        indices[9] = 0; indices[10] = 2; indices[11] = 3;
    
        int w = rect.width();
        int h = rect.height();
    
        vertex[0].set( 0, 0, RED );
        vertex[1].set( w/4, 0, GREEN );
        vertex[2].set( 0, w/4, YELLOW );
        vertex[3].set( w/4, w/4, 0,0,255, 0 );
    
    
        vertex[4].set( w/2+w/4, h/2+h/4, RED );
        vertex[5].set( w,       h/2+h/4, GREEN  );
        vertex[6].set( w/2+w/4, h, YELLOW );
        vertex[7].set( w, h, BLUE );
    
    
        return n;
    }
    

    I am on QT 5.15.2 and OpenGL ES 2.0

    johngodJ 1 Reply Last reply
    0
    • ALKMANA ALKMAN

      I am trying to separate the triangles that I draw with QSGGeometry::DrawTriangleStrip. into batches of 2 in order to draw seperate rectangles with a texture(eventually).

      I cannot figure out how I can do this.
      It seems that passing indices do not affect how OpenGL handles triangle strips.

      Am I missing something important here?

      My end result should be 2 rectangles but not joined together like in the pic below.
      Screenshot 2024-07-25 at 5.39.45 PM.jpg

      
      QSGNode *pianoroll_preview::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
      {
          pianoroll_preview_instance *n= static_cast<pianoroll_preview_instance *>(oldNode);
          QRectF rect = boundingRect();
      
          QSGGeometryNode *node = nullptr;
          QSGGeometry *geometry = nullptr;
      
          int vCount = 4;
          if (!n) {
              n = new pianoroll_preview_instance();
      
              node = new QSGGeometryNode;
              geometry = new QSGGeometry(QSGGeometry::defaultAttributes_ColoredPoint2D(), vCount);
              geometry->setDrawingMode(QSGGeometry::DrawTriangleStrip);
              //geometry = node->geometry();
              geometry->allocate( vCount * 2 );
              node->setGeometry(geometry);
              node->setFlag(QSGNode::OwnsGeometry);
              QSGVertexColorMaterial *material = new QSGVertexColorMaterial;
              node->setMaterial(material);
              node->setFlag(QSGNode::OwnsMaterial);
              n->appendChildNode( node );
          }
      
          QSGGeometry::ColoredPoint2D *vertex = geometry-> vertexDataAsColoredPoint2D();
      
          quint16* indices = geometry->indexDataAsUShort();
          indices[0] = 0; indices[1] = 1; indices[2] = 2;
          indices[3] = 0; indices[4] = 2; indices[5] = 3;
      
          indices[6] = 0; indices[7] = 1; indices[8] = 2;
          indices[9] = 0; indices[10] = 2; indices[11] = 3;
      
          int w = rect.width();
          int h = rect.height();
      
          vertex[0].set( 0, 0, RED );
          vertex[1].set( w/4, 0, GREEN );
          vertex[2].set( 0, w/4, YELLOW );
          vertex[3].set( w/4, w/4, 0,0,255, 0 );
      
      
          vertex[4].set( w/2+w/4, h/2+h/4, RED );
          vertex[5].set( w,       h/2+h/4, GREEN  );
          vertex[6].set( w/2+w/4, h, YELLOW );
          vertex[7].set( w, h, BLUE );
      
      
          return n;
      }
      

      I am on QT 5.15.2 and OpenGL ES 2.0

      johngodJ Offline
      johngodJ Offline
      johngod
      wrote on last edited by johngod
      #2

      Hi @ALKMAN

      Note that I never used QSGGeometry, but the primitive DrawTriangleStrip should be similar to OpenGL primitives, so you need to use degenerated triangles to break apart the 2 rectangles. You should only need a indice list of size 10 with the following vertices:
      0,1,2,3,3,4,45,6,7

      indices[0] = 0; indices[1] = 1; indices[2] = 2;
      indices[3] = 3; indices[4] = 3; indices[5] = 4;
      indices[6] = 4; indices[7] = 5; indices[8] = 6;
      indices[9] = 7;
      
      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved