Skip to content

Commit

Permalink
Merge pull request #6576 from lrineau/QGLViewer-fix_picking_with_HiDP…
Browse files Browse the repository at this point in the history
…I_screen-GF

Fix 3D demo with HiDPI screen
  • Loading branch information
sloriot authored May 26, 2022
2 parents e6f6120 + b32619f commit 7753120
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 41 deletions.
35 changes: 34 additions & 1 deletion GraphicsView/include/CGAL/Qt/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <CGAL/Qt/quaternion.h>
#include <CGAL/export/Qt.h>
#include <QOpenGLFunctions_2_1>
#include <QOpenGLFunctions>

namespace CGAL{
class QGLViewer;
Expand Down Expand Up @@ -199,6 +200,7 @@ public Q_SLOTS:
CGAL::QGLViewer's window dimensions when the Camera is attached to a CGAL::QGLViewer. See
also QOpenGLWidget::height() */
int screenHeight() const { return screenHeight_; }
qreal devicePixelRatio() const { return devicePixelRatio_; }
void getViewport(GLint viewport[4]) const;
qreal pixelGLRatio(const Vec &position) const;

Expand Down Expand Up @@ -279,7 +281,7 @@ public Q_SLOTS:
setScreenWidthAndHeight(int(100.0 * aspect), 100);
}

void setScreenWidthAndHeight(int width, int height);
void setScreenWidthAndHeight(int width, int height, qreal devicePixelRatio = 1.0);
/*! Sets the zNearCoefficient() value. */
void setZNearCoefficient(qreal coef) {
zNearCoef_ = coef;
Expand Down Expand Up @@ -444,6 +446,7 @@ private Q_SLOTS:

// C a m e r a p a r a m e t e r s
int screenWidth_, screenHeight_; // size of the window, in pixels
qreal devicePixelRatio_;
qreal fieldOfView_; // in radians
Vec sceneCenter_;
qreal sceneRadius_; // OpenGL units
Expand All @@ -467,6 +470,36 @@ private Q_SLOTS:
KeyFrameInterpolator *interpolationKfi_;
};

inline void read_pixel(const QPoint &pixel, QOpenGLFunctions *p,
const Camera *camera, GLenum format, GLenum type,
GLvoid *pixel_data) {
const auto pixel_ratio = camera->devicePixelRatio();
p->glReadPixels(pixel.x() * pixel_ratio,
(camera->screenHeight() - pixel.y()) * pixel_ratio - 1, 1, 1,
format, type, pixel_data);
}

inline auto read_pixel_as_float_rgb(const QPoint &pixel, QOpenGLFunctions *p,
const Camera *camera) {
std::array<float, 3> res;
read_pixel(pixel, p, camera, GL_RGB, GL_FLOAT, res.data());
return res;
}

inline auto read_pixel_as_ubyte_rgba(const QPoint &pixel, QOpenGLFunctions *p,
const Camera *camera) {
std::array<GLubyte, 4> res;
read_pixel(pixel, p, camera, GL_RGBA, GL_UNSIGNED_BYTE, res.data());
return res;
}

inline float read_depth_under_pixel(const QPoint &pixel, QOpenGLFunctions *p,
const Camera *camera) {
float depth = 2.0f;
read_pixel(pixel, p, camera, GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
return depth;
}

} // namespace qglviewer
} //CGAL
#endif // QGLVIEWER_CAMERA_H
6 changes: 3 additions & 3 deletions GraphicsView/include/CGAL/Qt/camera_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,11 @@ frustrum coherence.
If your Camera is used without a CGAL::QGLViewer (offscreen rendering, shadow maps),
use setAspectRatio() instead to define the projection matrix. */
CGAL_INLINE_FUNCTION
void Camera::setScreenWidthAndHeight(int width, int height) {
void Camera::setScreenWidthAndHeight(int width, int height, qreal devicePixelRatio) {
// Prevent negative and zero dimensions that would cause divisions by zero.
screenWidth_ = width > 0 ? width : 1;
screenHeight_ = height > 0 ? height : 1;
devicePixelRatio_ = devicePixelRatio;
projectionMatrixIsUpToDate_ = false;
}

Expand Down Expand Up @@ -884,8 +885,7 @@ Vec Camera::pointUnderPixel(const QPoint &pixel, bool &found) const {
// Qt uses upper corner for its origin while GL uses the lower corner.
if(auto p = dynamic_cast<QOpenGLFunctions*>(parent()))
{
p->glReadPixels(pixel.x(), screenHeight() - 1 - pixel.y(), 1, 1,
GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
depth = read_depth_under_pixel(pixel, p, this);
}
found = depth < 1.0;
Vec point(pixel.x(), pixel.y(), depth);
Expand Down
17 changes: 10 additions & 7 deletions GraphicsView/include/CGAL/Qt/qglviewer_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ void CGAL::QGLViewer::setCamera(qglviewer::Camera *const camera) {

camera->setSceneRadius(sceneRadius());
camera->setSceneCenter(sceneCenter());
camera->setScreenWidthAndHeight(width(), height());
camera->setScreenWidthAndHeight(width(), height(), devicePixelRatio());

// Disconnect current camera from this viewer.
disconnect(this->camera()->frame(), SIGNAL(manipulated()), this,
Expand Down Expand Up @@ -1147,7 +1147,9 @@ void CGAL::QGLViewer::beginSelection(const QPoint &point)
{
makeCurrent();
glEnable(GL_SCISSOR_TEST);
glScissor(point.x(), camera()->screenHeight()-1-point.y(), 1, 1);
glScissor(point.x() * devicePixelRatio(),
(camera()->screenHeight() - point.y()) * devicePixelRatio() - 1, 1,
1);
}

/*! This method is called by select() after scene elements were drawn by
Expand Down Expand Up @@ -2369,7 +2371,7 @@ CGAL_INLINE_FUNCTION
void CGAL::QGLViewer::resizeGL(int width, int height) {
QOpenGLWidget::resizeGL(width, height);
glViewport(0, 0, GLint(width), GLint(height));
camera()->setScreenWidthAndHeight(this->width(), this->height());
camera()->setScreenWidthAndHeight(this->width(), this->height(), this->devicePixelRatio());
}

//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -3187,10 +3189,11 @@ void CGAL::QGLViewer::drawVisualHints() {
mvpMatrix.ortho(-1,1,-1,1,-1,1);
size=30*devicePixelRatio();
rendering_program.setUniformValue("mvp_matrix", mvpMatrix);
glViewport(GLint((camera()->projectedCoordinatesOf(camera()->pivotPoint()).x-size/2)*devicePixelRatio()),
GLint((height() - camera()->projectedCoordinatesOf(camera()->pivotPoint()).y-size/2)*devicePixelRatio()), size, size);
glScissor (GLint((camera()->projectedCoordinatesOf(camera()->pivotPoint()).x-size/2)*devicePixelRatio()),
GLint((height() - camera()->projectedCoordinatesOf(camera()->pivotPoint()).y-size/2)*devicePixelRatio()), size, size);
const auto point_2d = camera()->projectedCoordinatesOf(camera()->pivotPoint());
glViewport(GLint(point_2d.x*devicePixelRatio()-size/2),
GLint((height() - point_2d.y)*devicePixelRatio()-size/2), size, size);
glScissor (GLint(point_2d.x*devicePixelRatio()-size/2),
GLint((height() - point_2d.y)*devicePixelRatio()-size/2), size, size);
rendering_program.setUniformValue("color", QColor(::Qt::black));
glDisable(GL_DEPTH_TEST);
glDrawArrays(GL_LINES, 0, static_cast<GLsizei>(4));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,7 @@ public Q_SLOTS:
boost::optional<DoubleConverter> fc;
Viewer_interface* viewer;
void getPixel(const QPoint& e) {
float data[3];
int vp[4];
viewer->glGetIntegerv(GL_VIEWPORT, vp);
viewer->glReadPixels(e.x(), vp[3] - e.y(), 1, 1, GL_RGB, GL_FLOAT, data);

const auto data = read_pixel_as_float_rgb(e, viewer, viewer->camera());
if(fc) {
Q_EMIT x(QString::number((*fc)(data[0]), 'f', 6 ));
} else if(ic) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1049,11 +1049,7 @@ void Scene_edit_box_item_priv::picking(int& type, int& id, Viewer_interface *vie
viewer->setBackgroundColor(::Qt::white);
draw_picking(viewer);

int rowLength = deviceWidth * 4; // data asked in RGBA,so 4 bytes.
const static int dataLength = rowLength * deviceHeight;
GLubyte* buffer = new GLubyte[dataLength];
// Qt uses upper corner for its origin while GL uses the lower corner.
viewer->glReadPixels(picked_pixel.x(), deviceHeight-1-picked_pixel.y(), 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
const auto buffer = read_pixel_as_ubyte_rgba(picked_pixel, viewer, viewer->camera());
//decode ID and pick (don't forget the case nothing is picked
if(!(buffer[0]==buffer[1] && buffer[1]==buffer[2]))
{
Expand All @@ -1080,7 +1076,6 @@ void Scene_edit_box_item_priv::picking(int& type, int& id, Viewer_interface *vie
}
}
}
delete[] buffer;
viewer->setBackgroundColor(bgColor);
fbo->release();
delete fbo;
Expand Down
9 changes: 4 additions & 5 deletions Polyhedron/demo/Polyhedron/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,9 +563,8 @@ void Scene::renderScene(const QList<Scene_interface::Item_id> &items,
if(with_names) {

// read depth buffer at pick location;
float depth = 1.0;
viewer->glReadPixels(picked_pixel.x(),viewer->camera()->screenHeight()-1-picked_pixel.y(),1,1,GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
if (depth != 1.0)
float depth = read_depth_under_pixel(picked_pixel, viewer, viewer->camera());
if (depth < 2.0)
{
//add object to list of picked objects;
picked_item_IDs[depth] = index;
Expand Down Expand Up @@ -634,7 +633,7 @@ void Scene::renderWireScene(const QList<Scene_interface::Item_id> &items,

// read depth buffer at pick location;
float depth = 1.0;
viewer->glReadPixels(picked_pixel.x(),viewer->camera()->screenHeight()-1-picked_pixel.y(),1,1,GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
depth = read_depth_under_pixel(picked_pixel, viewer, viewer->camera());
if (depth != 1.0)
{
//add object to list of picked objects;
Expand Down Expand Up @@ -676,7 +675,7 @@ void Scene::renderPointScene(const QList<Scene_interface::Item_id> &items,
if(item.renderingMode() == Points && with_names) {
// read depth buffer at pick location;
float depth = 1.0;
viewer->glReadPixels(picked_pixel.x(),viewer->camera()->screenHeight()-1-picked_pixel.y(),1,1,GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
depth = read_depth_under_pixel(picked_pixel, viewer, viewer->camera());
if (depth != 1.0)
{
//add object to list of picked objects;
Expand Down
2 changes: 1 addition & 1 deletion Polyhedron/demo/Polyhedron/Scene_group_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ void Scene_group_item::renderChildren(Viewer_interface *viewer,
if(with_names) {
// read depth buffer at pick location;
float depth = 1.0;
viewer->glReadPixels(picked_pixel.x(),viewer->camera()->screenHeight()-1-picked_pixel.y(),1,1,GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
depth = read_depth_under_pixel(picked_pixel, viewer, viewer->camera());
if (depth != 1.0)
{
//add object to list of picked objects;
Expand Down
8 changes: 1 addition & 7 deletions Polyhedron/demo/Polyhedron/Scene_spheres_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,6 @@ void Scene_spheres_item::draw(Viewer_interface *viewer) const
setBuffersFilled(true);
setBuffersInit(viewer, true);
}
int deviceWidth = viewer->camera()->screenWidth();
int deviceHeight = viewer->camera()->screenHeight();
if(d->has_plane)
{
QVector4D cp = cgal_plane_to_vector4d(d->plane);
Expand All @@ -207,12 +205,8 @@ void Scene_spheres_item::draw(Viewer_interface *viewer) const
}
if(d->pickable && (d->spheres.size() > 1 && viewer->inDrawWithNames()))
{
int rowLength = deviceWidth * 4; // data asked in RGBA,so 4 bytes.
const static int dataLength = rowLength * deviceHeight;
GLubyte* buffer = new GLubyte[dataLength];
// Qt uses upper corner for its origin while GL uses the lower corner.
QPoint picking_target = viewer->mapFromGlobal(QCursor::pos());
viewer->glReadPixels(picking_target.x(), deviceHeight-1-picking_target.y(), 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
const auto buffer = read_pixel_as_ubyte_rgba(picking_target, viewer, viewer->camera());
int ID = (buffer[0] + buffer[1] * 256 +buffer[2] * 256*256) ;
if(buffer[0]*buffer[1]*buffer[2] < 255*255*255)
{
Expand Down
12 changes: 6 additions & 6 deletions Triangulation_3/demo/Triangulation_3/Viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1696,9 +1696,9 @@ void Viewer::drawWithNames()
rendering_program.release();

//read depth and store in map
GLfloat depth = 1.0f;
glReadPixels(picking_pos.x(),camera()->screenHeight()-1-picking_pos.y(),1,1,GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
if (depth != 1.0)
GLfloat depth = 2.0f;
depth = read_depth_under_pixel(picking_pos, this, this->camera());
if (depth < 2.0f)
{
picked_IDs[depth] = i;
}
Expand Down Expand Up @@ -1740,9 +1740,9 @@ void Viewer::drawWithNames()
rendering_program.release();

//read depth and store in map
GLfloat depth = 1.0f;
glReadPixels(picking_pos.x(),camera()->screenHeight()-1-picking_pos.y(),1,1,GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
if (depth != 1.0)
GLfloat depth = 2.0f;
depth = read_depth_under_pixel(picking_pos, this, this->camera());
if (depth < 2.0f)
{
picked_IDs[depth] = -1;
}
Expand Down

0 comments on commit 7753120

Please sign in to comment.