Нарисуйте треугольник с Qt3D - PullRequest
0 голосов
/ 21 февраля 2019

Мне нужно нарисовать многоугольник с помощью Qt3D, но я нашел, как я могу сделать это с помощью треугольников.Так.Я нашел код, который более тесно работает с OpenGL из Qt3D.Он рисовал линию, но я изменил один, чтобы нарисовать треугольники.Когда я устанавливаю 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_);

Или вы можете дать мне другое предложение, как нарисовать многоугольник или треугольник.

1 Ответ

0 голосов
/ 21 февраля 2019

Ваши очки установлены по часовой стрелке, вы можете изменить их порядок или установить другое поведение по этой ссылке: doc.qt.io / qt-5.11 / qt3drender-qcullface.html

Если вам нужны оба, я бы попробовал Qt3DRender::QCullFace::NoCulling

...