首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Qt3D -两个方块的实例渲染。PrimitiveType?InstanceCount?

Qt3D -两个方块的实例渲染。PrimitiveType?InstanceCount?
EN

Stack Overflow用户
提问于 2020-07-05 15:44:42
回答 1查看 499关注 0票数 1

我想用实例渲染在屏幕上显示2个方块。

问题是:

当我画两个正方形时,它们是相连的。我不能画多个独立的正方形。

我知道这与PrimitiveType有关:我使用的是Qt3DRender::QGeometryRenderer::LineLoop或Qt3DRender::QGeometryRenderer::TriangleFan,但是有没有办法告诉Qt在多个实例中分离我的数据缓冲区?这样PrimitiveType就可以应用于每个实例,而不是所有的顶点。

我有一个创建形状的函数:

代码语言:javascript
复制
Qt3DCore::QEntity* SceneBuilder::createShapeInstancing(const QList<QVector3D> & listVertices, int nbPointGeometry, const QColor color, Qt3DCore::QEntity * rootEntity) 
{
    // Material
    Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial(rootEntity);
    material->setAmbient(color);

    // Custom entity
    Qt3DCore::QEntity *customMeshEntity = new Qt3DCore::QEntity(rootEntity);

    // Custom Mesh
    Qt3DRender::QGeometryRenderer *customMeshRenderer = new Qt3DRender::QGeometryRenderer(rootEntity);
    Qt3DRender::QGeometry *customGeometry = new Qt3DRender::QGeometry(customMeshRenderer);

    Qt3DRender::QBuffer *vertexDataBuffer = new Qt3DRender::QBuffer(Qt3DRender::QBuffer::VertexBuffer, customGeometry);

    int nbPointGeometry = nbPoint;

    QByteArray vertexBufferData;
    vertexBufferData.resize(listVertices.size() * nbPointGeometry * sizeof(QVector3D));
    QVector3D *posData = reinterpret_cast<QVector3D *>(vertexBufferData.data());

    QList<QVector3D>::const_iterator it;
    for (it = listVertices.begin(); it != listVertices.end(); it++)
    {
        *posData = *it;
        ++posData;
    }

    vertexDataBuffer->setData(vertexBufferData);

    //Attributes

    Qt3DRender::QAttribute *positionAttribute = new Qt3DRender::QAttribute();
    positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
    positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
    positionAttribute->setBuffer(vertexDataBuffer);
    positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
    positionAttribute->setVertexSize(3);
    positionAttribute->setByteOffset(0);
    positionAttribute->setByteStride((0 + 3) * sizeof(float));
    positionAttribute->setCount(listVertices.size());
    //positionAttribute->setDivisor(1);

    customGeometry->addAttribute(positionAttribute);

    customMeshRenderer->setGeometry(customGeometry);
    //customMeshRenderer->setInstanceCount(listVertices.size()/nbPoint);
    if (nbPointGeometry == 1)
        customMeshRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Points);
    else if (nbPointGeometry == 2)
        customMeshRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Lines);
    else if (nbPointGeometry == 3)
        customMeshRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles);
    else
        customMeshRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::LineLoop);

    customMeshEntity->addComponent(customMeshRenderer);
    customMeshEntity->addComponent(material);

    return customMeshEntity;
}

当我调用这个函数时,我把我的2个平方放在listVertices变量中。所以我所有的方块都在一次抽签中显示出来。

但这些形状是以某种方式连接起来的。有什么办法把它去掉吗?我不使用InstanceCount除数,但我不知道它是如何工作的。我用它做了一些测试,但都没有用。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-09 09:29:03

实际上,我刚刚找到了一个实例呈现这里的例子。

他们对QSphereGeometry进行子类化,并在着色器中执行实例渲染。您可以创建一个RectangleGeometry,它保存一个三角形的顶点,并为它配备与示例中的InstancedGeometry相同的功能。

结果:

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62742911

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档