QSGGeometry. Batch 2 triangles to draw a rectangle with QSGGeometry::DrawTriangleStrip.
-
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.
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
-
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.
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
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,7indices[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;