OSGWidget.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 TrackPointRenderer; // Forward declaration
  22. class OSGWidget : public QOpenGLWidget {
  23. Q_OBJECT
  24. public:
  25. static void fixMaterialState(osg::ref_ptr<osg::Node> node, osg::Vec4* color = nullptr);
  26. OSGWidget(QWidget* parent = nullptr);
  27. virtual ~OSGWidget();
  28. void renderBaseMesh(const osg::ref_ptr<osg::Vec3Array> vertices, const osg::ref_ptr<osg::Vec3Array> normals);
  29. osg::ref_ptr<osg::Geode> getMesh();
  30. PickHandler* getPicker();
  31. TrackPointRenderer* getPointRenderer();
  32. void loadSteamvrThread();
  33. void clear();
  34. osg::ref_ptr<osg::Geometry> _steamvrThreadMesh;
  35. protected:
  36. virtual void paintEvent(QPaintEvent* paintEvent);
  37. virtual void paintGL();
  38. virtual void resizeGL(int width, int height);
  39. virtual void keyPressEvent(QKeyEvent* event);
  40. virtual void keyReleaseEvent(QKeyEvent* event);
  41. virtual void mouseMoveEvent(QMouseEvent* event);
  42. virtual void mousePressEvent(QMouseEvent* event);
  43. virtual void mouseReleaseEvent(QMouseEvent* event);
  44. virtual void wheelEvent(QWheelEvent* event);
  45. virtual bool event(QEvent* event);
  46. private:
  47. virtual void onHome();
  48. virtual void onResize(int width, int height);
  49. osg::ref_ptr<osg::Group> createAxesPreview();
  50. osgGA::EventQueue* getEventQueue() const;
  51. PickHandler* _picker;
  52. TrackPointRenderer* _pointRenderer;
  53. osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> graphicsWindow_;
  54. osg::ref_ptr<osgWidget::Viewer> _viewer;
  55. osgViewer::View* _view;
  56. osg::ref_ptr<osg::Group> _root;
  57. osg::ref_ptr<osg::Group> _pointRoot;
  58. osg::ref_ptr<osg::Group> _coordinateAxes;
  59. osg::ref_ptr<osg::Geode> _mesh;
  60. QPoint selectionStart_;
  61. QPoint selectionEnd_;
  62. bool selectionActive_;
  63. bool selectionFinished_;
  64. bool _steamvrLoaded = false;
  65. };