簡體   English   中英

如何在Qt3D中畫一條簡單的線?

[英]How do I draw a simple line in Qt3D?

我覺得這應該很簡單,但是在我的生活中,我無法弄清楚如何使用Qt 3D繪制基本線條。 關於這個主題,我唯一能找到的指導就是這段晦澀的視頻 ,其中有令人討厭的原始字節緩沖區和通過很少記錄的類進行的內存操作。

有沒有更好的方法來使用我缺少的閃亮的新API?

從您鏈接的視頻中,我想到了下面的代碼(也發布在Qt論壇中: https : //forum.qt.io/topic/66808/qt3d-draw-grid-axis-lines/3 )。

首先,您需要創建QGeometry。 因為它是一條簡單的線,所以它僅由2個頂點(起點,終點)和2個索引(鏈接這些頂點)組成。 為此,您需要創建2個QByteArray並將其存儲到QBuffer中。 在第一個中,存儲2個頂點(每個頂點的x,y和z坐標)。 在第二個中,您只是說您想將第一個頂點鏈接到第二個。 當我們在渲染器上使用Qt3DRender::QGeometryRenderer::Lines時,僅需要2個索引。

完成后,只需將QGeometry放入QGeometryRenderer中即可擁有網格,然后將其放入QEntity中,使其出現在樹中並進行渲染。

#include <Qt3DCore/QEntity>
#include <Qt3DCore/QTransform>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DRender/QAttribute>
#include <Qt3DRender/QBuffer>
#include <Qt3DRender/QGeometry>

void drawLine(const QVector3D& start, const QVector3D& end, const QColor& color, Qt3DCore::QEntity *_rootEntity)
{
    auto *geometry = new Qt3DRender::QGeometry(_rootEntity);

    // position vertices (start and end)
    QByteArray bufferBytes;
    bufferBytes.resize(3 * 2 * sizeof(float)); // start.x, start.y, start.end + end.x, end.y, end.z
    float *positions = reinterpret_cast<float*>(bufferBytes.data());
    *positions++ = start.x();
    *positions++ = start.y();
    *positions++ = start.z();
    *positions++ = end.x();
    *positions++ = end.y();
    *positions++ = end.z();

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

    auto *positionAttribute = new Qt3DRender::QAttribute(geometry);
    positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
    positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
    positionAttribute->setVertexSize(3);
    positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
    positionAttribute->setBuffer(buf);
    positionAttribute->setByteStride(3 * sizeof(float));
    positionAttribute->setCount(2);
    geometry->addAttribute(positionAttribute); // We add the vertices in the geometry

    // connectivity between vertices
    QByteArray indexBytes;
    indexBytes.resize(2 * sizeof(unsigned int)); // start to end
    unsigned int *indices = reinterpret_cast<unsigned int*>(indexBytes.data());
    *indices++ = 0;
    *indices++ = 1;

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

    auto *indexAttribute = new Qt3DRender::QAttribute(geometry);
    indexAttribute->setVertexBaseType(Qt3DRender::QAttribute::UnsignedInt);
    indexAttribute->setAttributeType(Qt3DRender::QAttribute::IndexAttribute);
    indexAttribute->setBuffer(indexBuffer);
    indexAttribute->setCount(2);
    geometry->addAttribute(indexAttribute); // We add the indices linking the points in the geometry

    // mesh
    auto *line = new Qt3DRender::QGeometryRenderer(_rootEntity);
    line->setGeometry(geometry);
    line->setPrimitiveType(Qt3DRender::QGeometryRenderer::Lines);
    auto *material = new Qt3DExtras::QPhongMaterial(_rootEntity);
    material->setAmbient(color);

    // entity
    auto *lineEntity = new Qt3DCore::QEntity(_rootEntity);
    lineEntity->addComponent(line);
    lineEntity->addComponent(material);
}

這分別是我對緩沖區類型必須做的更正(我現在假定其已棄用):

auto *buf = new Qt3DRender::QBuffer(Qt3DRender::QBuffer::VertexBuffer, geometry);
auto *indexBuffer = new Qt3DRender::QBuffer(Qt3DRender::QBuffer::IndexBuffer, geometry);

我的是Win 10的Qt 5.9。

我建議看一下https://doc-snapshots.qt.io/qt5-5.9/qt3d-basicshapes-cpp-example.html ,不久前我問自己一個類似的問題,即如何畫圓。 好吧,3D圓是一個圓環,其半徑比例特殊:

// thin Torus = Circle in 3D
Qt3DCore::QEntity *torusEntity0 = new Qt3DCore::QEntity(rootEntity);
Qt3DExtras::QTorusMesh *torusMesh0 = new Qt3DExtras::QTorusMesh;
torusMesh0->setRadius(15);
torusMesh0->setMinorRadius(0.01f);
torusMesh0->setRings(100);
torusMesh0->setSlices(20);
torusEntity0->addComponent(torusMesh0);
torusEntity0->addComponent(material);

那么3D線是什么? 這將是具有非常小的外半徑的圓柱體。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM