QGeometryRenderer的setVertexCount API及其对光线投射结果的影响

setVertexCount API of QGeometryRenderer and its effect on ray casting results

我在三点之间创建了两条线的线框网格:

通过这些函数:

Qt3DRender::QGeometryRenderer *Utils::createWireframeMesh()
{
    Qt3DRender::QGeometryRenderer *mesh = new Qt3DRender::QGeometryRenderer();
    Qt3DRender::QGeometry *geometry = new Qt3DRender::QGeometry(mesh);
    Qt3DRender::QBuffer *vertexDataBuffer = new Qt3DRender::QBuffer(Qt3DRender::QBuffer::VertexBuffer,
                                                                 geometry);
    Qt3DRender::QBuffer *indexDataBuffer = new Qt3DRender::QBuffer(Qt3DRender::QBuffer::IndexBuffer,
                                                                   geometry);
    QByteArray vertexBufferData;
    QByteArray indexBufferData;

    int vertexCount = 3; // Three vertices at (0, -1, 0) and (1, 0, 0) and (0, 1, 0)
    int lineCount = 2; // Two lines between three vertices

    vertexBufferData.resize(vertexCount * 3 * sizeof(float));
    indexBufferData.resize(lineCount * 2 * sizeof(ushort));

    // Arrow triangle is 2D and is inside XY plane
    float *vPtr = reinterpret_cast<float *>(vertexBufferData.data());
    vPtr[0]  = 0.0f; vPtr[1]  = -1.0f; vPtr[2]  = 0.0f; // First vertex at (0, -1, 0)
    vPtr[3]  = 1.0f; vPtr[4]  = 0.0f;  vPtr[5]  = 0.0f; // Second vertex at (1, 0, 0)
    vPtr[6]  = 0.0f; vPtr[7]  = +1.0f; vPtr[8]  = 0.0f; // Third vertex at (0, 1, 0)

    ushort *iPtr = reinterpret_cast<ushort *>(indexBufferData.data());
    iPtr[0]  = 0; iPtr[1]  = 1; // First line from index 0 to index 1
    iPtr[2]  = 1; iPtr[3]  = 2; // Second line from index 1 to index 2


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

    addPositionAttributeToGeometry(geometry, vertexDataBuffer, vertexCount);
    addIndexAttributeToGeometry(geometry, indexDataBuffer, lineCount * 2);

    mesh->setInstanceCount(1);
    mesh->setIndexOffset(0);
    mesh->setFirstInstance(0);
    // How to set vertex count here?
    mesh->setVertexCount(vertexCount);
    mesh->setPrimitiveType(Qt3DRender::QGeometryRenderer::Lines);
    mesh->setGeometry(geometry);

    return mesh;
}

void Utils::addPositionAttributeToGeometry(Qt3DRender::QGeometry *geometry,
                                                 Qt3DRender::QBuffer *buffer, int count)
{
    Qt3DRender::QAttribute *posAttribute = new Qt3DRender::QAttribute();
    posAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
    posAttribute->setBuffer(buffer);
    posAttribute->setDataType(Qt3DRender::QAttribute::Float);
    posAttribute->setDataSize(3);
    posAttribute->setByteOffset(0);
    posAttribute->setByteStride(0);
    posAttribute->setCount(count);
    posAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());

    geometry->addAttribute(posAttribute);
}

void Utils::addIndexAttributeToGeometry(Qt3DRender::QGeometry *geometry,
                                              Qt3DRender::QBuffer *buffer, int count)
{
    Qt3DRender::QAttribute *indexAttribute = new Qt3DRender::QAttribute();
    indexAttribute->setAttributeType(Qt3DRender::QAttribute::IndexAttribute);
    indexAttribute->setBuffer(buffer);
    indexAttribute->setDataType(Qt3DRender::QAttribute::UnsignedShort);
    indexAttribute->setDataSize(1);
    indexAttribute->setByteOffset(0);
    indexAttribute->setByteStride(0);
    indexAttribute->setCount(count);

    geometry->addAttribute(indexAttribute);
}

在上面的代码中,我在这一行尝试了三种不同的语句:

    // How to set vertex count here?
    mesh->setVertexCount(vertexCount);
    mesh->setVertexCount(vertexCount * 2);
    mesh->setVertexCount(vertexCount * 3);

有了这些结果 - 我在我的 3D 场景中做了一些光线投射,这也受到了令人惊讶的影响:

DocumentationQt3DRender::QGeometryRenderervertexCount 属性 解释为:

vertexCount : int

Holds the primitive count.

在我的例子中,图元数是行数,所以我试了但只画了一条线:

我对 setVertexCount API 感到困惑。有人可以给我提示吗?

vertexCount 与您传递给 glDrawArraysglDrawElements 的值相同,即它是绘图中涉及的顶点数。由于您使用的是索引渲染,因此通常是索引的数量(假设您正在绘制索引数组中的所有数据)。所以在上面的例子中,它应该是 4.

请注意,我们最近修复了使用原始重启时的选行错误,但这不会影响您在上面包含的代码。