Skip to content

Commit 16ec766

Browse files
committed
Fix selection on high-DPI/retina displays
1 parent 755b120 commit 16ec766

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

GUI/OpenGL/MiniGL.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ MiniGL::DestroyFct MiniGL::destroyfunc = nullptr;
3434
void (*MiniGL::exitfunc)(void) = NULL;
3535
int MiniGL::m_width = 0;
3636
int MiniGL::m_height = 0;
37+
int MiniGL::m_windowWidth = 0;
38+
int MiniGL::m_windowHeight = 0;
39+
Real MiniGL::m_devicePixelRatio = 1.0f;
3740
Quaternionr MiniGL::m_rotation;
3841
Real MiniGL::m_zoom = 1.0;
3942
Vector3r MiniGL::m_translation;
@@ -686,7 +689,9 @@ void MiniGL::viewport ()
686689
{
687690
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
688691

689-
glfwGetFramebufferSize(m_glfw_window, &m_width, &m_height);
692+
glfwGetFramebufferSize(m_glfw_window, &m_width, &m_height);
693+
getWindowSize(m_windowWidth, m_windowHeight);
694+
m_devicePixelRatio = static_cast<Real>(m_width) / static_cast<Real>(m_windowWidth);
690695
glViewport (0, 0, m_width, m_height);
691696
setProjectionMatrix (m_width, m_height);
692697

@@ -787,8 +792,8 @@ void MiniGL::enableScreenShader(const Vector3r& color)
787792
{
788793
Shader& shader = m_shader_screen;
789794
shader.begin();
790-
glUniform1f(shader.getUniform("width"), static_cast<Real>(m_width));
791-
glUniform1f(shader.getUniform("height"), static_cast<Real>(m_height));
795+
glUniform1f(shader.getUniform("width"), static_cast<Real>(m_windowWidth));
796+
glUniform1f(shader.getUniform("height"), static_cast<Real>(m_windowHeight));
792797
glUniform3fv(shader.getUniform("color"), 1, &color(0));
793798
}
794799

@@ -956,7 +961,11 @@ void MiniGL::unproject(const int x, const int y, Vector3r &pos)
956961
glGetIntegerv(GL_VIEWPORT, viewport);
957962

958963
unproject(
959-
Vector3r(static_cast<Real>(x), static_cast<Real>(viewport[3] - y), static_cast<Real>(znear)),
964+
Vector3r(
965+
static_cast<Real>(x) * m_devicePixelRatio,
966+
static_cast<Real>(viewport[3]) - static_cast<Real>(y) * m_devicePixelRatio,
967+
static_cast<Real>(znear)
968+
),
960969
pos
961970
);
962971
}

GUI/OpenGL/MiniGL.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ namespace SPH
8080
static std::vector<MouseWheelFct> m_mouseWheelFct;
8181
static int m_width;
8282
static int m_height;
83+
static int m_windowWidth;
84+
static int m_windowHeight;
85+
static Real m_devicePixelRatio;
8386
static Vector3r m_translation;
8487
static Quaternionr m_rotation;
8588
static Real m_zoom;
@@ -199,6 +202,7 @@ namespace SPH
199202

200203
static int getWidth() { return m_width; }
201204
static int getHeight() { return m_height; }
205+
static Real getDevicePixelRatio() { return m_devicePixelRatio; }
202206

203207
static int getDrawMode() { return drawMode; }
204208
static void setDrawMode(int val) { drawMode = val; }

GUI/OpenGL/Selection.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ namespace SPH
4242
int itop = ip1y > ip2y ? ip1y : ip2y;
4343
int ibottom = ip1y < ip2y ? ip1y : ip2y;
4444

45-
float left = (float)ileft;
46-
float right = (float)iright;
47-
float top = (float)itop;
48-
float bottom = (float)ibottom;
45+
float left = (float)ileft * MiniGL::getDevicePixelRatio();
46+
float right = (float)iright * MiniGL::getDevicePixelRatio();
47+
float top = (float)itop * MiniGL::getDevicePixelRatio();
48+
float bottom = (float)ibottom * MiniGL::getDevicePixelRatio();
4949

5050
if (left != right && top != bottom)
5151
{

0 commit comments

Comments
 (0)