ccGenericGLDisplay.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //##########################################################################
  2. //# #
  3. //# CLOUDCOMPARE #
  4. //# #
  5. //# This program is free software; you can redistribute it and/or modify #
  6. //# it under the terms of the GNU General Public License as published by #
  7. //# the Free Software Foundation; version 2 or later of the License. #
  8. //# #
  9. //# This program is distributed in the hope that it will be useful, #
  10. //# but WITHOUT ANY WARRANTY; without even the implied warranty of #
  11. //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  12. //# GNU General Public License for more details. #
  13. //# #
  14. //# COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI) #
  15. //# #
  16. //##########################################################################
  17. #ifndef CC_GENERIC_GL_DISPLAY
  18. #define CC_GENERIC_GL_DISPLAY
  19. //Always first
  20. #include "ccIncludeGL.h"
  21. //Local
  22. #include "ccViewportParameters.h"
  23. #include "ccColorTypes.h"
  24. //Qt
  25. #include <QFont>
  26. class QWidget;
  27. class ccDrawableObject;
  28. //! OpenGL camera parameters
  29. struct ccGLCameraParameters
  30. {
  31. ccGLCameraParameters()
  32. : viewport{0, 0, 0, 0}
  33. , perspective(false)
  34. , fov_deg(0.0f)
  35. , pixelSize(0.0)
  36. , nearClippingDepth(std::numeric_limits<double>::quiet_NaN())
  37. , farClippingDepth(std::numeric_limits<double>::quiet_NaN())
  38. {
  39. }
  40. //! Projects a 3D point in 2D (+ normalized 'z' coordinate)
  41. inline bool project(const CCVector3d& input3D, CCVector3d& output2D, bool* inFrustum = nullptr) const
  42. {
  43. return ccGL::Project<double, double>( input3D,
  44. modelViewMat.data(),
  45. projectionMat.data(),
  46. viewport,
  47. output2D,
  48. inFrustum,
  49. (inFrustum && !std::isnan(nearClippingDepth)) ? &nearClippingDepth : nullptr,
  50. (inFrustum && !std::isnan(farClippingDepth)) ? &farClippingDepth : nullptr);
  51. }
  52. //! Projects a 3D point in 2D (+ normalized 'z' coordinate)
  53. inline bool project(const CCVector3& input3D, CCVector3d& output2D, bool* inFrustum = nullptr) const
  54. {
  55. return ccGL::Project<PointCoordinateType, double>( input3D,
  56. modelViewMat.data(),
  57. projectionMat.data(),
  58. viewport,
  59. output2D,
  60. inFrustum,
  61. (inFrustum && !std::isnan(nearClippingDepth)) ? &nearClippingDepth : nullptr,
  62. (inFrustum && !std::isnan(farClippingDepth)) ? &farClippingDepth : nullptr);
  63. }
  64. //! Unprojects a 2D point (+ normalized 'z' coordinate) in 3D
  65. inline bool unproject(const CCVector3d& input2D, CCVector3d& output3D) const
  66. {
  67. return ccGL::Unproject<double, double>(input2D, modelViewMat.data(), projectionMat.data(), viewport, output3D);
  68. }
  69. //! Unprojects a 2D point (+ normalized 'z' coordinate) in 3D
  70. inline bool unproject(const CCVector3& input2D, CCVector3d& output3D) const
  71. {
  72. return ccGL::Unproject<PointCoordinateType, double>(input2D, modelViewMat.data(), projectionMat.data(), viewport, output3D);
  73. }
  74. //! Model view matrix (GL_MODELVIEW)
  75. ccGLMatrixd modelViewMat;
  76. //! Projection matrix (GL_PROJECTION)
  77. ccGLMatrixd projectionMat;
  78. //! Viewport (GL_VIEWPORT)
  79. int viewport[4];
  80. //! Perspective mode
  81. bool perspective;
  82. //! F.O.V. (in degrees)
  83. float fov_deg;
  84. //! Pixel size (approximate if in perspective mode)
  85. double pixelSize;
  86. //! Near clipping depth
  87. double nearClippingDepth;
  88. //! Far clipping depth
  89. double farClippingDepth;
  90. };
  91. //! Generic interface for GL displays
  92. class ccGenericGLDisplay
  93. {
  94. public:
  95. virtual ~ccGenericGLDisplay() = default;
  96. //! Returns the screen size
  97. virtual QSize getScreenSize() const = 0;
  98. //! Redraws display immediately
  99. virtual void redraw(bool only2D = false, bool resetLOD = true) = 0;
  100. //! Flags display as 'to be refreshed'
  101. /** See ccGenericGLDisplay::refresh.
  102. **/
  103. virtual void toBeRefreshed() = 0;
  104. //! Redraws display only if flagged as 'to be refreshed'
  105. /** See ccGenericGLDisplay::toBeRefreshed. Flag is turned
  106. to false after a call to this method.
  107. \param only2D whether to redraw everything (false) or only the 2D layer (true)
  108. **/
  109. virtual void refresh(bool only2D = false) = 0;
  110. //! Invalidates current viewport setup
  111. /** On next redraw, viewport information will be recomputed.
  112. **/
  113. virtual void invalidateViewport() = 0;
  114. //! Invalidates the 3D layer (FBO)
  115. /** On next redraw, the 3D layer will be updated
  116. **/
  117. virtual void deprecate3DLayer() = 0;
  118. //! Returns default text display font
  119. /** Warning: already takes rendering zoom into account!
  120. **/
  121. virtual QFont getTextDisplayFont() const = 0;
  122. //! Returns default label display font
  123. /** Warning: already takes rendering zoom into account!
  124. **/
  125. virtual QFont getLabelDisplayFont() const = 0;
  126. //! Text alignment
  127. enum TextAlign { ALIGN_HLEFT = 1,
  128. ALIGN_HMIDDLE = 2,
  129. ALIGN_HRIGHT = 4,
  130. ALIGN_VTOP = 8,
  131. ALIGN_VMIDDLE = 16,
  132. ALIGN_VBOTTOM = 32,
  133. ALIGN_DEFAULT = 1 | 8};
  134. //! Displays a string at a given 2D position
  135. /** This method should be called solely during 2D pass rendering.
  136. The coordinates are expressed relatively to the current viewport (y = 0 at the top!).
  137. \param text string
  138. \param x horizontal position of string origin
  139. \param y vertical position of string origin
  140. \param align alignment position flags
  141. \param bkgAlpha background transparency (0 by default)
  142. \param color text color (optional)
  143. \param font optional font (otherwise default one will be used)
  144. **/
  145. virtual void displayText( QString text,
  146. int x,
  147. int y,
  148. unsigned char align = ALIGN_DEFAULT,
  149. float bkgAlpha = 0.0f,
  150. const ccColor::Rgba* color = nullptr,
  151. const QFont* font = nullptr) = 0;
  152. //! Displays a string at a given 3D position
  153. /** This method should be called solely during 3D pass rendering (see paintGL).
  154. \param str string
  155. \param pos3D 3D position of string origin
  156. \param color RGBA color (optional: if let to 0, default text rendering color is used)
  157. \param font font (optional)
  158. **/
  159. virtual void display3DLabel(const QString& str,
  160. const CCVector3& pos3D,
  161. const ccColor::Rgba* color = nullptr,
  162. const QFont& font=QFont()) = 0;
  163. //! Returns the current OpenGL camera parameters
  164. virtual void getGLCameraParameters(ccGLCameraParameters& params) = 0;
  165. //! Converts 2D screen coordinates to 'centered' 2D OpenGL context coordinates
  166. virtual QPointF toCenteredGLCoordinates(int x, int y) const = 0;
  167. //! Converts 2D screen coordinates to 'corner-based' 2D OpenGL context coordinates
  168. virtual QPointF toCornerGLCoordinates(int x, int y) const = 0;
  169. //! Returns viewport parameters (zoom, etc.)
  170. virtual const ccViewportParameters& getViewportParameters() const = 0;
  171. //! Setups a (projective) camera
  172. /** \param cameraMatrix orientation/position matrix of the camera
  173. \param fov_deg vertical field of view (in degrees). Optional (ignored if 0).
  174. \param viewerBasedPerspective whether the perspective view should be object-centered (false) or camera-centered (true)
  175. \param bubbleViewMode set whether bubble-view mode should be enabled or not (in which case viewerBasedPerspective is forced by default)
  176. **/
  177. virtual void setupProjectiveViewport( const ccGLMatrixd& cameraMatrix,
  178. float fov_deg = 0.0f,
  179. bool viewerBasedPerspective = true,
  180. bool bubbleViewMode = false) = 0;
  181. //! Warns the display that the enity is about to be removed
  182. virtual void aboutToBeRemoved(ccDrawableObject* entity) = 0;
  183. //! Returns this window as a proper Qt widget
  184. virtual QWidget* asWidget() { return nullptr; }
  185. };
  186. #endif //CC_GENERIC_GL_DISPLAY