OSGWidget.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #pragma once
  2. // Include dependencies
  3. #include <QPoint>
  4. #include <QOpenGLWidget>
  5. #include <osg/ref_ptr>
  6. #include <osgViewer/GraphicsWindow>
  7. #include <osgViewer/CompositeViewer>
  8. namespace osgWidget {
  9. //! The subclass of osgViewer::CompositeViewer we use
  10. /*!
  11. * This subclassing allows us to remove the annoying automatic
  12. * setting of the CPU affinity to core 0 by osgViewer::ViewerBase,
  13. * osgViewer::CompositeViewer's base class.
  14. */
  15. class Viewer: public osgViewer::CompositeViewer {
  16. public:
  17. virtual void setupThreading();
  18. };
  19. }
  20. class PickHandler; // Forward declaration
  21. class OSGWidget : public QOpenGLWidget {
  22. Q_OBJECT
  23. public:
  24. static void fixMaterialState(osg::ref_ptr<osg::Node> node);
  25. OSGWidget(QWidget* parent = nullptr);
  26. virtual ~OSGWidget();
  27. void renderBaseMesh(const osg::ref_ptr<osg::Vec3Array> vertices, const osg::ref_ptr<osg::Vec3Array> normals);
  28. osg::ref_ptr<osg::Geode> getMesh();
  29. PickHandler* getPicker();
  30. protected:
  31. virtual void paintEvent(QPaintEvent* paintEvent);
  32. virtual void paintGL();
  33. virtual void resizeGL(int width, int height);
  34. virtual void keyPressEvent(QKeyEvent* event);
  35. virtual void keyReleaseEvent(QKeyEvent* event);
  36. virtual void mouseMoveEvent(QMouseEvent* event);
  37. virtual void mousePressEvent(QMouseEvent* event);
  38. virtual void mouseReleaseEvent(QMouseEvent* event);
  39. virtual void wheelEvent(QWheelEvent* event);
  40. virtual bool event(QEvent* event);
  41. private:
  42. virtual void onHome();
  43. virtual void onResize(int width, int height);
  44. osgGA::EventQueue* getEventQueue() const;
  45. PickHandler* _picker;
  46. osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> graphicsWindow_;
  47. osg::ref_ptr<osgWidget::Viewer> _viewer;
  48. osgViewer::View* _view;
  49. osg::ref_ptr<osg::Group> _root;
  50. osg::ref_ptr<osg::Geode> _coordinateAxes;
  51. osg::ref_ptr<osg::Geode> _mesh;
  52. QPoint selectionStart_;
  53. QPoint selectionEnd_;
  54. bool selectionActive_;
  55. bool selectionFinished_;
  56. };