Solved QObjectPicker gives me strange positions.
-
Hi,
I'm currently trying to write my first program using Qt3D. For the moment, I just have one object (a cuboid). And I try to click on it to obtain the position of the clicked point. The cuboid has size in X and in Z, but not in Y (I put a very small size). I have added a translation to the object, just to have it in the center of my view. Translation is (90, 0, 90). So:- I'm expecting to obtain a 0 value in the Y coordinate when I'm clicking on the object,
- I'm expecting to always obtain the same coordinates when I'm clicking on the same point, whatever the position of the camera.
Unfortunately, these two assumptions are wrong with my program. For the first one, I obtain a value between 110, and 130. And for the second one, the local and world coordinates I obtain differ them I move the position of the camera (by changing it initial position).
Conclusion: I missed something. But what?
My program is the following (cpp only):
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QMouseEvent> #include <QTimer> #include <QCamera> #include <QCuboidMesh> #include <QCylinderMesh> #include <QEntity> #include <QForwardRenderer> #include <QObjectPicker> #include <QOrbitCameraController> #include <QPickEvent> #include <QTexture> #include <QTextureMaterial> #include <QTransform> #include <Qt3DWindow> #include <QGoochMaterial> #include <iostream> 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; }
With the camera located at 150 in Y. When I click a well identified point, I obtain the following traces:
"Local Intersection: 3.34108 127.208 2.64624 World Intersection: 87.3538 127.208 93.3411"And when the camera is located at 225, the trace is:
"Local Intersection: 9.88376 126.661 7.69605 World Intersection: 82.304 126.661 99.8838"Difference is not huge, but clearly abnormal regarding the dimensions of my image. And I clearly do not understant why, in Y, I obtain, here, something around 126. Without the translation the result is around 110 in Y.
-
@Alain38-0
OK. It seems that the problems comes from the "bounding box". Configuring the picker on PrimitivePicking gives expected results