ccGLWindowStereo.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #pragma once
  2. //##########################################################################
  3. //# #
  4. //# CLOUDCOMPARE #
  5. //# #
  6. //# This program is free software; you can redistribute it and/or modify #
  7. //# it under the terms of the GNU General Public License as published by #
  8. //# the Free Software Foundation; version 2 or later of the License. #
  9. //# #
  10. //# This program is distributed in the hope that it will be useful, #
  11. //# but WITHOUT ANY WARRANTY; without even the implied warranty of #
  12. //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  13. //# GNU General Public License for more details. #
  14. //# #
  15. //# COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI) #
  16. //# #
  17. //##########################################################################
  18. #include "qCC_glWindow.h"
  19. //local
  20. #include "ccGLWindowInterface.h"
  21. #include <QHBoxLayout>
  22. #include <QWidget>
  23. #include <QWindow>
  24. class QOpenGLPaintDevice;
  25. //! OpenGL 3D view
  26. class CCGLWINDOW_LIB_API ccGLWindowStereo : public QWindow, public ccGLWindowInterface
  27. {
  28. Q_OBJECT
  29. public:
  30. //! Default constructor
  31. ccGLWindowStereo(QSurfaceFormat* format = nullptr, QWindow* parent = nullptr, bool silentInitialization = false);
  32. //! Destructor
  33. ~ccGLWindowStereo() override;
  34. //inherited from ccGLWindowInterface
  35. inline qreal getDevicePixelRatio() const override { return devicePixelRatio(); }
  36. inline QFont getFont() const override { return font(); }
  37. inline QOpenGLContext* getOpenGLContext() const override { return context(); }
  38. inline void setWindowCursor(const QCursor& cursor) override { setCursor(cursor); }
  39. void doMakeCurrent() override;
  40. inline QObject* asQObject() override { return this; }
  41. inline const QObject* asQObject() const override { return this; }
  42. inline QString getWindowTitle() const override { return windowTitle(); }
  43. inline void doGrabMouse() override { grabMouse(); }
  44. inline void doReleaseMouse() override { releaseMouse(); }
  45. inline QPoint doMapFromGlobal(const QPoint& P) const override { return mapFromGlobal(P); }
  46. inline void doShowMaximized() override { showMaximized(); }
  47. inline void doResize(int w, int h) override { resize(w, h); }
  48. inline void doResize(const QSize& size) override { resize(size); }
  49. inline QImage doGrabFramebuffer() override { return {}; }
  50. inline bool isStereo() const override { return true; }
  51. //! Returns the parent widget
  52. QWidget* parentWidget() const { return m_parentWidget; }
  53. //! Sets 'parent' widget
  54. void setParentWidget(QWidget* widget);
  55. //! Returns the font
  56. inline const QFont& font() const { return m_font; }
  57. //shortcuts
  58. void setWindowTitle(QString title) { setTitle(title); }
  59. QString windowTitle() const { return title(); }
  60. inline QWidget* asWidget() override { return m_parentWidget; }
  61. inline QSize getScreenSize() const override { return size(); }
  62. //inherited from ccGLWindowInterface
  63. inline int qtWidth() const override { return QWindow::width(); }
  64. inline int qtHeight() const override { return QWindow::height(); }
  65. inline QSize qtSize() const override { return QWindow::size(); }
  66. bool enableStereoMode(const StereoParams& params) override;
  67. void disableStereoMode() override;
  68. //inherited from ccGenericGLDisplay
  69. void requestUpdate() override;
  70. //! For compatibility with the QOpenGLWidget version
  71. inline void update() { doPaintGL(); }
  72. void grabMouse();
  73. void releaseMouse();
  74. //! Creates an instance
  75. static void Create(ccGLWindowStereo*& window, QWidget*& widget, bool silentInitialization = false);
  76. //! Casts a widget to a ccGLWindowStereo instance (if possible)
  77. static ccGLWindowStereo* FromWidget(QWidget* widget);
  78. protected: //rendering
  79. //inherited from ccGLWindowInterface
  80. inline ccQOpenGLFunctions* functions() const override { return context() ? context()->versionFunctions<ccQOpenGLFunctions>() : nullptr; }
  81. inline QSurfaceFormat getSurfaceFormat() const override { return format(); }
  82. void doSetMouseTracking(bool enable) override;
  83. void doShowFullScreen() override { showFullScreen(); }
  84. void doShowNormal() override { showNormal(); }
  85. bool prepareOtherStereoGlassType(CC_DRAW_CONTEXT& context, RenderingParams& params, ccFrameBufferObject*& currentFBO) override;
  86. void processOtherStereoGlassType(RenderingParams& renderingParams) override;
  87. bool setCustomCameraProjection(RenderingParams& params, ccGLMatrixd& modelViewMat, ccGLMatrixd& projectionMat) override;
  88. bool initPaintGL() override;
  89. void swapGLBuffers() override;
  90. //! Returns the context (if any)
  91. inline QOpenGLContext* context() const { return m_context; }
  92. //! Don't call makeCurrent() on this instance, as the FBO would not properly be managed
  93. /** Use doMakeCurrent() instaad.
  94. **/
  95. void makeCurrent() = delete;
  96. protected: //other methods
  97. //! Reacts to the itemPickedFast signal (shortcut)
  98. Q_SLOT void onItemPickedFastSlot(ccHObject* pickedEntity, int pickedItemIndex, int x, int y) { onItemPickedFast(pickedEntity, pickedItemIndex, x, y); }
  99. //inherited from ccGLWindowInterface
  100. int width() const override { return QWindow::width(); }
  101. int height() const override { return QWindow::height(); }
  102. QSize size() const override { return QWindow::size(); }
  103. GLuint defaultQtFBO() const override;
  104. //events handling
  105. void mousePressEvent(QMouseEvent* event) override { processMousePressEvent(event); }
  106. void mouseMoveEvent(QMouseEvent* event) override { processMouseMoveEvent(event); }
  107. void mouseDoubleClickEvent(QMouseEvent* event) override { processMouseDoubleClickEvent(event); }
  108. void mouseReleaseEvent(QMouseEvent* event) override { processMouseReleaseEvent(event); }
  109. void wheelEvent(QWheelEvent* event) override { processWheelEvent(event); }
  110. bool event(QEvent* evt) override;
  111. bool preInitialize(bool &firstTime) override;
  112. bool postInitialize(bool firstTime) override;
  113. void resizeGL(int w, int h);
  114. virtual void dragEnterEvent(QDragEnterEvent* event) { doDragEnterEvent(event); }
  115. virtual void dropEvent(QDropEvent* event) { doDropEvent(event); }
  116. protected: //members
  117. //! Associated OpenGL context
  118. QOpenGLContext* m_context;
  119. //! Format
  120. QSurfaceFormat m_format;
  121. //! Associated widget (we use the WidgetContainer mechanism)
  122. QWidget* m_parentWidget;
  123. };
  124. //! Container widget for ccGLWindow
  125. class CCGLWINDOW_LIB_API ccGLStereoWidget : public QWidget
  126. {
  127. Q_OBJECT
  128. public:
  129. ccGLStereoWidget(ccGLWindowStereo* window, QWidget* parent = nullptr)
  130. : QWidget(parent)
  131. , m_associatedWindow(nullptr)
  132. {
  133. setLayout(new QHBoxLayout);
  134. layout()->setContentsMargins(0, 0, 0, 0);
  135. if (window)
  136. {
  137. setAssociatedWindow(window);
  138. }
  139. }
  140. virtual ~ccGLStereoWidget()
  141. {
  142. if (m_associatedWindow)
  143. {
  144. m_associatedWindow->setParent(nullptr);
  145. m_associatedWindow->close();
  146. }
  147. }
  148. inline ccGLWindowStereo* associatedWindow() const { return m_associatedWindow; }
  149. void setAssociatedWindow(ccGLWindowStereo* window)
  150. {
  151. if (window)
  152. {
  153. assert(layout() && layout()->count() == 0);
  154. QWidget* container = QWidget::createWindowContainer(window, this);
  155. layout()->addWidget(container);
  156. m_associatedWindow = window;
  157. m_associatedWindow->setParentWidget(container);
  158. }
  159. else
  160. {
  161. assert(false);
  162. }
  163. }
  164. protected:
  165. ccGLWindowStereo* m_associatedWindow;
  166. };