qt3DScene选择对象功能的实现

 

用C++实现的qt3DScene选点功能,QPickEvent.localIntersection

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include 
#include 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 

#include 
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , m_firstPedicle     (true)
{
    ui->setupUi(this);
    this->create3DScene();
}

void MainWindow::create3DScene()
{
    // Create the 3D Window and its container.
    Qt3DExtras::Qt3DWindow *view3D = new Qt3DExtras::Qt3DWindow();
    view3D->defaultFrameGraph()->setClearColor(QColor(QRgb(0x4d4d4f)));
    QWidget *container3D = QWidget::createWindowContainer(view3D);
    container3D->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    ui->ui_3dContainerLayout->addWidget(container3D);

    // Root entity
    m_rootEntity = new Qt3DCore::QEntity();

    // Camera
    Qt3DRender::QCamera *camera = view3D->camera();
    camera->lens()->setPerspectiveProjection(60.0f, 1.0f, 0.1f, 1000.0f); // 1.0 is used to have the same dimensions in the two directions
    camera->setPosition  (QVector3D(90.0f, 225.0f, 90.0f));
    camera->setViewCenter(QVector3D(90.0f,  90.0f, 90.0f));
    camera->setUpVector  (QVector3D(1.0f, 0.0f, 0.0f));

    // Camera controls
    Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(m_rootEntity);
    camController->setLinearSpeed( 50.0f );
    camController->setLookSpeed( 180.0f );
    camController->setCamera(camera);

    // Background object (rotate with the view)
    Qt3DCore::QEntity *      backgroundEntity = new Qt3DCore::QEntity(m_rootEntity);

    Qt3DExtras::QCuboidMesh * background2DView = new Qt3DExtras::QCuboidMesh;
    background2DView->setXExtent(180.0f);
    background2DView->setZExtent(180.0f);
    background2DView->setYExtent(0.00001f);

    Qt3DExtras::QTextureMaterial *backgroundMaterial = new Qt3DExtras::QTextureMaterial;
    Qt3DRender::QTexture2D *      backgroundTexture  = new Qt3DRender::QTexture2D(backgroundMaterial);
    Qt3DRender::QTextureImage *   backgroundImage    = new Qt3DRender::QTextureImage(backgroundMaterial);
    backgroundImage->setSource(QUrl("qrc:/Resources/Img/Contrast/contrast.jpg"));
    backgroundTexture->addTextureImage(backgroundImage);
    backgroundMaterial->setTexture(backgroundTexture);
    Qt3DCore::QTransform *backgroundTransform = new Qt3DCore::QTransform;
    backgroundTransform->setRotationY(-90.0f);
    backgroundTransform->setTranslation(QVector3D(90.0f, 0.0f, 90.0f));
    backgroundEntity->addComponent(background2DView);
    backgroundEntity->addComponent(backgroundTransform);
    backgroundEntity->addComponent(backgroundMaterial);

    view3D->setRootEntity(m_rootEntity);

    // Pour debug
    Qt3DRender::QObjectPicker * toto = new Qt3DRender::QObjectPicker;
    backgroundEntity->addComponent(toto);

    QObject::connect(toto, &Qt3DRender::QObjectPicker::clicked, this, &MainWindow::picked);
}

void MainWindow::picked(Qt3DRender::QPickEvent *pick)
{
    std::cout
            << " Local Intersection: " << pick->localIntersection().x()
              << " " << pick->localIntersection().y()
              << " " << pick->localIntersection().z()
    << " World Intersection: " << pick->worldIntersection().x()
                  << " " << pick->worldIntersection().y()
                  << " " << pick->worldIntersection().z() << std::endl;
}

 

qml

    ObjectPicker {
        id: pointxyziPicker
        property bool picking: true
        onPressed: {
            picking = true
            console.log("point: (" + pick.localIntersection.x + ", " + pick.localIntersection.y + ", " + pick.localIntersection.z + ")")
            console.log("point distance: " + Number(pick.localIntersection.length()))
        }
    }

 

 

MouseArea {
    id: selectArea;
    anchors.fill: parent;
    onPressed: {
        if (highlightItem !== null) {
            // if there is already a selection, delete it
            highlightItem.destroy (); 
        }
        // create a new rectangle at the wanted position
        highlightItem = highlightComponent.createObject (selectArea, {
            "x" : mouse.x 
        });
        // here you can add you zooming stuff if you want
    }
    onPositionChanged: {
        // on move, update the width of rectangle
        highlightItem.width = (Math.abs (mouse.x - highlightItem.x));  
    }
    onReleased: {
        // here you can add you zooming stuff if you want
    }

    property Rectangle highlightItem : null;

    Component {
        id: highlightComponent;

        Rectangle {
            color: "yellow";
            opacity; 0.35;
            anchors {
                top: parent.top;
                bottom: parent.bottom;
            }
        }
    }
}

 

你可能感兴趣的:(qt3DScene选择对象功能的实现)