void StockShapeSelector::MousePressEvent(QMouseEvent* event) {
  // Convert the mouse coordinates into a world-space ray.
  Viewport* viewport = renderer_->GetViewport();
  CameraNode* camera = viewport->GetCamera();
  const QVector3D dir = camera->Unproject(event->x(), event->y()).normalized();
  const QVector3D start = camera->Translation();

  // Cast the ray into the scene to select an object. This query is performed
  // using axis-aligned bounding boxes, which are fast and generally
  // sufficient. The bounding boxes are automatically computed from the scene
  // graph geometry, so we get the tests "for free".
  //
  // If we wanted more precise ray casting, then we could cull the results of
  // the query with a more exact method (e.g., based on a custom collision
  // volume).
  const int64_t selection_mask = 0x1;
  SelectionQuery query(renderer_->GetScene());
  std::vector<QueryResult> qresult = query.CastRay(selection_mask, start, dir);

  if (qresult.empty()) {
    return;
  }

  // Found an object. Pass it on to StockShapeRenderer
  QueryResult& first_result = qresult.front();
  renderer_->NodeSelected(first_result.node);
}
Exemple #2
0
int main(int argc, char *argv[]) {
  QApplication app(argc, argv);

  Viewer viewer;

  Viewport* viewport = viewer.GetViewport();

  // Set initial camera position
  const QVector3D eye(5, 5, -10);
  const QVector3D look_at(0, 0, 0);
  const QVector3D up(0, 1, 0);
  viewport->GetCamera()->LookAt(eye, look_at, up);

  // Add renderers
  ModelRenderer* modeler = new ModelRenderer("model", viewport);
  viewport->AddRenderer(modeler);

  if (argc > 1) {
    modeler->LoadModel(argv[1]);
  }

  // Add input handlers
  viewport->AddInputHandler(new ViewHandlerHorizontal(viewport,
      QVector3D(0, 1, 0), viewport));

  viewer.show();

  return app.exec();
}