[Interest] Qt3D custom geometry

Igor Mironchik igor.mironchik at gmail.com
Fri Apr 7 21:41:58 CEST 2017


Hello,

What is wrong in this geometry? I expect vertical triangle, but I got a 
mess.

// 3 distinct vertices
     QByteArray vertexBufferData;
     vertexBufferData.resize(3 * (3 + 3 + 3) * sizeof(float));

     // Vertices
     QVector3D v0( 0.0f, 0.0f, 0.0f );
     QVector3D v1( -1.0f, 1.0f, 0.0f );
     QVector3D v2( 1.0f, 1.0f, 0.0f );

     QVector3D n( 0.0f, 0.0f, 1.0f );

     // Colors
     QVector3D red(1.0f, 0.0f, 0.0f);
     QVector3D green(0.0f, 1.0f, 0.0f);
     QVector3D blue(0.0f, 0.0f, 1.0f);

     QVector<QVector3D> vertices = QVector<QVector3D>()
             << v0 << n << red
             << v1 << n << blue
             << v2 << n << green;

     float *rawVertexArray = reinterpret_cast<float 
*>(vertexBufferData.data());
     int idx = 0;

     Q_FOREACH (const QVector3D &v, vertices) {
         rawVertexArray[idx++] = v.x();
         rawVertexArray[idx++] = v.y();
         rawVertexArray[idx++] = v.z();
     }

     // Indices (3)
     QByteArray indexBufferData;
     indexBufferData.resize(3 * sizeof(ushort));
     ushort *rawIndexArray = reinterpret_cast<ushort 
*>(indexBufferData.data());

     rawIndexArray[0] = 0;
     rawIndexArray[1] = 1;
     rawIndexArray[2] = 2;

     vertexDataBuffer->setData(vertexBufferData);
     indexDataBuffer->setData(indexBufferData);

     // Attributes
     Qt3DRender::QAttribute *positionAttribute = new 
Qt3DRender::QAttribute();
positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
     positionAttribute->setBuffer(vertexDataBuffer);
positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
     positionAttribute->setVertexSize(3);
     positionAttribute->setByteOffset(0);
     positionAttribute->setByteStride(9 * sizeof(float));
     positionAttribute->setCount(3);
positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());

     Qt3DRender::QAttribute *normalAttribute = new Qt3DRender::QAttribute();
normalAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
     normalAttribute->setBuffer(vertexDataBuffer);
normalAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
     normalAttribute->setVertexSize(3);
     normalAttribute->setByteOffset(3 * sizeof(float));
     normalAttribute->setByteStride(9 * sizeof(float));
     normalAttribute->setCount(3);
normalAttribute->setName(Qt3DRender::QAttribute::defaultNormalAttributeName());

     Qt3DRender::QAttribute *colorAttribute = new Qt3DRender::QAttribute();
colorAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
     colorAttribute->setBuffer(vertexDataBuffer);
colorAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
     colorAttribute->setVertexSize(3);
     colorAttribute->setByteOffset(6 * sizeof(float));
     colorAttribute->setByteStride(9 * sizeof(float));
     colorAttribute->setCount(3);
colorAttribute->setName(Qt3DRender::QAttribute::defaultColorAttributeName());

     Qt3DRender::QAttribute *indexAttribute = new Qt3DRender::QAttribute();
indexAttribute->setAttributeType(Qt3DRender::QAttribute::IndexAttribute);
     indexAttribute->setBuffer(indexDataBuffer);
indexAttribute->setVertexBaseType(Qt3DRender::QAttribute::UnsignedShort);
     indexAttribute->setVertexSize(1);
     indexAttribute->setByteOffset(0);
     indexAttribute->setByteStride(0);
     indexAttribute->setCount(3);

     customGeometry->addAttribute(positionAttribute);
     customGeometry->addAttribute(normalAttribute);
     customGeometry->addAttribute(colorAttribute);
     customGeometry->addAttribute(indexAttribute);




More information about the Interest mailing list