c++ - 用Qt3D画三角形

标签 c++ opengl qt3d

我需要用 Qt3D 绘制一个多边形,但我发现如何用三角形来做到这一点。所以。我找到了一个代码,它可以与 Qt3D 中的 OpenGL 更紧密地配合。它正在画线,但我改变了一条来画三角形。当我设置 setPrimitiveType(Qt3DRender::QGeometryRenderer::TriangleStrip); 时,它就起作用了。但我想用 setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles);,由于某种原因不起作用并且不绘制任何内容。也许我错误地配置了顶点和索引属性?或者也许我使用了错误的输入? (我的输入示例位于底部)

void MainWindow::drawTriangles(const QPolygonF polygon, QColor color, Qt3DCore::QEntity *_rootEntity)
{
    int numOfVertices = polygon.size();
    auto *geometry = new Qt3DRender::QGeometry(_rootEntity);

    // Create and fill vertex buffer
    QByteArray bufferBytes;
    bufferBytes.resize(3 * numOfVertices * static_cast<int>(sizeof(float)));
    float *positions = reinterpret_cast<float*>(bufferBytes.data());

    for(auto point : polygon){
        float fHalfMapWidth = (dFittedMaxMapX_ - dFittedMinMapX_) / 2;
        float fHalfMapHeight = (dFittedMaxMapY_ - dFittedMinMapY_) / 2;

        *positions++ = static_cast<float>(point.x() - (dFittedMinMapX_ + fHalfMapWidth));
        *positions++ = 0.0f; //We need to drow only on the surface
        *positions++ = static_cast<float>(point.y() - (dFittedMinMapY_ + fHalfMapHeight));
    }

    auto *buf = new Qt3DRender::QBuffer(geometry);
    buf->setData(bufferBytes);

    auto *positionAttribute = new Qt3DRender::QAttribute(geometry); // Create a vertex attribute
    positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
    positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float); // In vertex buffer we store floats
    positionAttribute->setVertexSize(3); // Size of a vertex
    positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute); // Type of an attribute
    positionAttribute->setBuffer(buf); // Set vertex buffer
    positionAttribute->setByteStride(3 * sizeof(float)); // Stride between vertices
    geometry->addAttribute(positionAttribute); // Add the attribute in our  Qt3DRender::QGeometry

    // Create and fill an index buffer
    QByteArray indexBytes;
    indexBytes.resize(numOfVertices * static_cast<int>(sizeof(unsigned int))); // start to end
    unsigned int *indices = reinterpret_cast<unsigned int*>(indexBytes.data());
    for(unsigned int i = 0; i < static_cast<unsigned int>(numOfVertices); ++i) {
        *indices++ = i;
    }

    auto *indexBuffer = new Qt3DRender::QBuffer(geometry);
    indexBuffer->setData(indexBytes);

    auto *indexAttribute = new Qt3DRender::QAttribute(geometry); // Create an index attribute
    indexAttribute->setVertexBaseType(Qt3DRender::QAttribute::UnsignedInt); // In the index buffer we will store Unsigned int
    indexAttribute->setAttributeType(Qt3DRender::QAttribute::IndexAttribute); // Type of an attribute
    indexAttribute->setBuffer(indexBuffer); // Set the index buffer
    indexAttribute->setCount(static_cast<unsigned int>(numOfVertices)); // As i understand, this is a number of vertices
    geometry->addAttribute(indexAttribute); // Add attribute in our Qt3DRender::QGeometry


    auto *poly = new Qt3DRender::QGeometryRenderer(_rootEntity);
    poly->setGeometry(geometry); 
    //poly->setPrimitiveType(Qt3DRender::QGeometryRenderer::TriangleStrip); - Working variant
    poly->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles); // - This is not working variant

    //Create a material
    auto *material = new Qt3DExtras::QPhongMaterial(_rootEntity);
    material->setAmbient(color);

    auto *trianglesEntity = new Qt3DCore::QEntity(_rootEntity);
    trianglesEntity->addComponent(poly); // Add our primitives
    trianglesEntity->addComponent(material); // Add material
}

我尝试使用三点和四点。当我使用 4 个点时,如果我将基本类型设置为“TriangleStrip”,它就会绘制。但对于“三角形”,这两种情况都不起作用。

mapBorder << QPointF(300,200) << QPointF(100,200) << QPointF(200,0) << QPointF(300,200) << QPointF(200,0) << QPointF(500,200) << QPointF(500,300); 
drawTriangles(mapBorder, QColor(Qt::black), mapEntity_);

或者你能给我另一个如何绘制多边形或三角形的建议吗?

最佳答案

您的积分按顺时针顺序设置,您可以通过此链接重新排序或设置不同的行为:doc.qt.io/qt-5.11/qt3drender-qcullface.html

如果您两者都需要,我会尝试Qt3DRender::QCullFace::NoCulling

关于c++ - 用Qt3D画三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54800529/

相关文章:

c++ - 在 Debian 8.3 ARM 上构建 Boost 单元测试框架

c++ - 使用迭代器进行二进制搜索,为什么我们使用 "(end - begin)/2"?

qt - 许多 QGLMaterial 项目的 openGL 性能不佳

qt5 - 如何在 QT5 上安装 QT3d?

C++虚拟模板函数

c++ - 为 Code::Blocks 安装 FLTK

opengl - 使用 GLSL 着色器 + 光照/法线

qt3d - 在 Qt3D 中使用 SSBO

c++ - glBlitFramebuffer 无效操作

c++ - OpenGL 按面着色