Skip to content

Commit

Permalink
Fix selection on high-DPI/retina displays
Browse files Browse the repository at this point in the history
  • Loading branch information
ruberith committed Mar 12, 2023
1 parent 755b120 commit 97e9638
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
17 changes: 13 additions & 4 deletions GUI/OpenGL/MiniGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ MiniGL::DestroyFct MiniGL::destroyfunc = nullptr;
void (*MiniGL::exitfunc)(void) = NULL;
int MiniGL::m_width = 0;
int MiniGL::m_height = 0;
int MiniGL::m_windowWidth = 0;
int MiniGL::m_windowHeight = 0;
Real MiniGL::m_devicePixelRatio = 1.0f;
Quaternionr MiniGL::m_rotation;
Real MiniGL::m_zoom = 1.0;
Vector3r MiniGL::m_translation;
Expand Down Expand Up @@ -686,7 +689,9 @@ void MiniGL::viewport ()
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glfwGetFramebufferSize(m_glfw_window, &m_width, &m_height);
getWindowSize(m_windowWidth, m_windowHeight);
glfwGetFramebufferSize(m_glfw_window, &m_width, &m_height);
m_devicePixelRatio = static_cast<Real>(m_width) / static_cast<Real>(m_windowWidth);
glViewport (0, 0, m_width, m_height);
setProjectionMatrix (m_width, m_height);

Expand Down Expand Up @@ -787,8 +792,8 @@ void MiniGL::enableScreenShader(const Vector3r& color)
{
Shader& shader = m_shader_screen;
shader.begin();
glUniform1f(shader.getUniform("width"), static_cast<Real>(m_width));
glUniform1f(shader.getUniform("height"), static_cast<Real>(m_height));
glUniform1f(shader.getUniform("width"), static_cast<Real>(m_windowWidth));
glUniform1f(shader.getUniform("height"), static_cast<Real>(m_windowHeight));
glUniform3fv(shader.getUniform("color"), 1, &color(0));
}

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

unproject(
Vector3r(static_cast<Real>(x), static_cast<Real>(viewport[3] - y), static_cast<Real>(znear)),
Vector3r(
static_cast<Real>(x) * m_devicePixelRatio,
static_cast<Real>(viewport[3]) - static_cast<Real>(y) * m_devicePixelRatio,
static_cast<Real>(znear)
),
pos
);
}
Expand Down
4 changes: 4 additions & 0 deletions GUI/OpenGL/MiniGL.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ namespace SPH
static std::vector<MouseWheelFct> m_mouseWheelFct;
static int m_width;
static int m_height;
static int m_windowWidth;
static int m_windowHeight;
static Real m_devicePixelRatio;
static Vector3r m_translation;
static Quaternionr m_rotation;
static Real m_zoom;
Expand Down Expand Up @@ -199,6 +202,7 @@ namespace SPH

static int getWidth() { return m_width; }
static int getHeight() { return m_height; }
static Real getDevicePixelRatio() { return m_devicePixelRatio; }

static int getDrawMode() { return drawMode; }
static void setDrawMode(int val) { drawMode = val; }
Expand Down
8 changes: 4 additions & 4 deletions GUI/OpenGL/Selection.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ namespace SPH
int itop = ip1y > ip2y ? ip1y : ip2y;
int ibottom = ip1y < ip2y ? ip1y : ip2y;

float left = (float)ileft;
float right = (float)iright;
float top = (float)itop;
float bottom = (float)ibottom;
float left = (float)ileft * MiniGL::getDevicePixelRatio();
float right = (float)iright * MiniGL::getDevicePixelRatio();
float top = (float)itop * MiniGL::getDevicePixelRatio();
float bottom = (float)ibottom * MiniGL::getDevicePixelRatio();

if (left != right && top != bottom)
{
Expand Down

0 comments on commit 97e9638

Please sign in to comment.