1818#include < CGAL/Qt/quaternion.h>
1919#include < CGAL/export/Qt.h>
2020#include < QOpenGLFunctions_2_1>
21+ #include < QOpenGLFunctions>
2122
2223namespace CGAL {
2324class QGLViewer ;
@@ -199,6 +200,7 @@ public Q_SLOTS:
199200 CGAL::QGLViewer's window dimensions when the Camera is attached to a CGAL::QGLViewer. See
200201 also QOpenGLWidget::height() */
201202 int screenHeight () const { return screenHeight_; }
203+ qreal devicePixelRatio () const { return devicePixelRatio_; }
202204 void getViewport (GLint viewport[4 ]) const ;
203205 qreal pixelGLRatio (const Vec &position) const ;
204206
@@ -279,7 +281,7 @@ public Q_SLOTS:
279281 setScreenWidthAndHeight (int (100.0 * aspect), 100 );
280282 }
281283
282- void setScreenWidthAndHeight (int width, int height);
284+ void setScreenWidthAndHeight (int width, int height, qreal devicePixelRatio = 1.0 );
283285 /* ! Sets the zNearCoefficient() value. */
284286 void setZNearCoefficient (qreal coef) {
285287 zNearCoef_ = coef;
@@ -444,6 +446,7 @@ private Q_SLOTS:
444446
445447 // C a m e r a p a r a m e t e r s
446448 int screenWidth_, screenHeight_; // size of the window, in pixels
449+ qreal devicePixelRatio_;
447450 qreal fieldOfView_; // in radians
448451 Vec sceneCenter_;
449452 qreal sceneRadius_; // OpenGL units
@@ -467,6 +470,36 @@ private Q_SLOTS:
467470 KeyFrameInterpolator *interpolationKfi_;
468471};
469472
473+ inline void read_pixel (const QPoint &pixel, QOpenGLFunctions *p,
474+ const Camera *camera, GLenum format, GLenum type,
475+ GLvoid *pixel_data) {
476+ const auto pixel_ratio = camera->devicePixelRatio ();
477+ p->glReadPixels (pixel.x () * pixel_ratio,
478+ (camera->screenHeight () - pixel.y ()) * pixel_ratio - 1 , 1 , 1 ,
479+ format, type, pixel_data);
480+ }
481+
482+ inline auto read_pixel_as_float_rgb (const QPoint &pixel, QOpenGLFunctions *p,
483+ const Camera *camera) {
484+ std::array<float , 3 > res;
485+ read_pixel (pixel, p, camera, GL_RGB, GL_FLOAT, res.data ());
486+ return res;
487+ }
488+
489+ inline auto read_pixel_as_ubyte_rgba (const QPoint &pixel, QOpenGLFunctions *p,
490+ const Camera *camera) {
491+ std::array<GLubyte, 4 > res;
492+ read_pixel (pixel, p, camera, GL_RGBA, GL_UNSIGNED_BYTE, res.data ());
493+ return res;
494+ }
495+
496+ inline float read_depth_under_pixel (const QPoint &pixel, QOpenGLFunctions *p,
497+ const Camera *camera) {
498+ float depth = 2 .0f ;
499+ read_pixel (pixel, p, camera, GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
500+ return depth;
501+ }
502+
470503} // namespace qglviewer
471504} // CGAL
472505#endif // QGLVIEWER_CAMERA_H
0 commit comments