3

私の会社では Qt3D を使用して CAD モデルを表示しています。Wee は、この関数を使用してQCamera::viewEntity(Qt3DCore::QEntity *entity)、特定のエンティティの境界球を計算し、エンティティを画面に合わせようとしました。

QEntityここで、空のノードの場合に解決できない問題に出くわしました。結局、ノードに頂点/ポイントが含まれていない場合、ノードを空と呼びます。この場合、バウンディング ボリュームの計算では無視する必要があると予想しました。代わりに、中心 (0.,0.,0.) と半径 0 の境界球があるため、処理されるようです。

次のコードは、この問題を示しています。

main.cpp

#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QFrame>
#include <Qt3DRender/QRenderSettings>
#include <Qt3DCore/QTransform>
#include <Qt3DRender/QCamera>
#include <Qt3DExtras/QSphereMesh>
#include <Qt3DExtras/QDiffuseSpecularMaterial>
#include <Qt3DExtras/QForwardRenderer>
#include <Qt3DExtras/Qt3DWindow>
#include <Qt3DRender/QCamera>
#include <Qt3DRender/QCameraLens>
#include <QPushButton>

Qt3DCore::QEntity* createSphereMesh()
{
    auto sphereMat = new Qt3DExtras::QDiffuseSpecularMaterial;
    sphereMat->setDiffuse(QColor(Qt::blue));

    auto mesh = new Qt3DExtras::QSphereMesh();
    mesh->setRadius(0.5);

    auto meshEntity = new Qt3DCore::QEntity;
    meshEntity->addComponent(mesh);
    meshEntity->addComponent(sphereMat);

    return meshEntity;
}

int main(int argc, char* argv[])
{
    QApplication a(argc, argv);

    auto view = new Qt3DExtras::Qt3DWindow();
    view->defaultFrameGraph()->setClearColor(QColor(127, 127, 127));
    auto settings = view->renderSettings();
    settings->setActiveFrameGraph(view->activeFrameGraph());

    auto rootEntity = new Qt3DCore::QEntity();
    view->setRootEntity(rootEntity);

    auto sphere1 = createSphereMesh();
    sphere1->setParent(rootEntity);
    auto trafo1 = new Qt3DCore::QTransform;
    trafo1->setTranslation(QVector3D(20, 10, 0));
    sphere1->addComponent(trafo1);
    auto sphere2 = createSphereMesh();
    sphere2->setParent(rootEntity);
    auto trafo2 = new Qt3DCore::QTransform;
    trafo2->setTranslation(QVector3D(20, -10, 0));
    sphere2->addComponent(trafo2);

    QObject::connect(view->camera()->lens(), &Qt3DRender::QCameraLens::viewSphere, [&](const QVector3D& center, float radius) {
        qDebug() << "Bounding Sphere:" << center << radius;
        auto boundingSphereEntity = new Qt3DCore::QEntity;

        auto sphereMat = new Qt3DExtras::QDiffuseSpecularMaterial;
        sphereMat->setAlphaBlendingEnabled(true);
        sphereMat->setDiffuse(QColor(255,255,255,80));

        auto mesh = new Qt3DExtras::QSphereMesh();
        mesh->setRadius(radius);

        boundingSphereEntity->addComponent(sphereMat);
        boundingSphereEntity->addComponent(mesh);

        auto trafoAll = new Qt3DCore::QTransform;
        trafoAll->setTranslation(center);
        boundingSphereEntity->addComponent(trafoAll);
        boundingSphereEntity->addComponent(mesh);
        boundingSphereEntity->addComponent(sphereMat);
        boundingSphereEntity->setParent(rootEntity);
        });
    auto container = QWidget::createWindowContainer(view);


    auto viewAllBtn = new QPushButton("View All");
    QObject::connect(viewAllBtn, &QPushButton::clicked, [&]() {
        view->camera()->viewAll();
        });
    QFrame frame;
    frame.setFixedSize(500, 500);
    frame.setLayout(new QVBoxLayout);
    frame.layout()->addWidget(container);
    frame.layout()->addWidget(viewAllBtn);

    frame.show();

    return a.exec();
}

半径 0.5 で互いに距離 20 の 2 つの青い球体があります。中心 (20,0,0) と半径 (10.5) の境界球を期待しています。

代わりに、プログラムは次のように出力します。

Bounding Sphere: QVector3D(12.1432, 2.14455, -1.32699e-08) 14.9644

値 2.14455 は本当にどこからともなく来ているようで、私の追加バウンディング ボリューム球体はいくぶん予測不可能です。

予期しないバウンディング ボリューム球

QVector3D(20, 10, 0)翻訳を結果に置き換えると、QVector3D(0,10,0)期待どおりになります。

バウンディング ボリューム球を修正する

バウンディング ボリュームの計算からルート エンティティを除外するにはどうすればよいですか?

4

0 に答える 0