Mike
Mike

Reputation: 582

QObjectPicker not working with custom geometry mesh and renderer

I have set up a simple 3d scene with a custom QGeometryRenderer and QGeometry. The custom QGeometry is loaded from a ply file.

class ColorMeshGeometry : public Qt3DRender::QGeometry
{
  Q_OBJECT
public:
  ColorMeshGeometry(QString meshFile, ColorMeshRenderer *parent);
  ~ColorMeshGeometry();
  void GeometryCenter(QVector3D* center);
};

class ColorMeshRenderer : public Qt3DRender::QGeometryRenderer
{
  ColorMeshGeometry* m_geometry;
  Q_OBJECT
public:
  explicit ColorMeshRenderer(QString meshFile, Qt3DCore::QNode *parent = 0)
  {
    m_geometry = new ColorMeshGeometry(meshFile, this);
    setGeometry(m_geometry);
  }

  ~ColorMeshRenderer();
  void ViewCenter(QVector3D* center);
};

And here is the code to set it all up:

uto view = new Qt3DExtras::Qt3DWindow();
auto container = createWindowContainer(view, this);
auto rootEntity = new Qt3DCore::QEntity();

ColorMeshRenderer* mesh = new ColorMeshRenderer(filename, rootEntity);
view->defaultFrameGraph()->setClearColor(QColor(QRgb(0x111111)));
view->camera()->lens()->setPerspectiveProjection(45.0f, view->width()/view->height(), 0.01f, 100000.0f);
view->camera()->setPosition(QVector3D(0.f, -512.f, 500.0f));
view->camera()->setViewCenter(QVector3D(0, 0, 0));

auto material = new Qt3DExtras::QPerVertexColorMaterial(rootEntity);

auto picker = new Qt3DRender::QObjectPicker(rootEntity);
picker->setHoverEnabled(false);
picker->setDragEnabled(false);

auto plyEntity = new Qt3DCore::QEntity(rootEntity);
plyEntity->addComponent(mesh);
plyEntity->addComponent(material);
plyEntity->addComponent(picker);

connect(picker, &Qt3DRender::QObjectPicker::pressed, this, &ViewerWidget::picker_Clicked);

Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(rootEntity);
camController->setCamera(view->camera());

view->setRootEntity(rootEntity);

The QObjectPicker::pressed event is never fired. If I use the Qt3DExtras::QTorusMesh instead, the pressed event is fired. What else needs to be implemented for the QObjectPicker to work with a custom mesh?

Edit:

Full sample code can be found here.

Upvotes: 4

Views: 820

Answers (1)

Mike
Mike

Reputation: 582

The fix for this ended up being the Perspective Projection. The back pane of the project ended up being way to far way, the fix is:

view->camera()->lens()->setPerspectiveProjection(45.0f, view->width()/view->height(), 0.01f, 5000.0f);

Upvotes: 2

Related Questions