Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DPI scaling #326

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/demo-app/demo_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ void callback() {
static int loadedMat = 1;
static bool depthClick = false;

ImGui::PushItemWidth(100);
ImGui::PushItemWidth(100 * polyscope::state::globalContext.dpiScale);

ImGui::InputInt("num points", &numPoints);
ImGui::InputFloat("param value", &param);
Expand Down
1 change: 1 addition & 0 deletions include/polyscope/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ struct Context {
std::map<std::string, std::map<std::string, std::unique_ptr<Structure>>> structures;
std::map<std::string, std::unique_ptr<Group>> groups;
float lengthScale = 1.;
float dpiScale = 1.;
std::tuple<glm::vec3, glm::vec3> boundingBox =
std::tuple<glm::vec3, glm::vec3>{glm::vec3{-1., -1., -1.}, glm::vec3{1., 1., 1.}};
std::vector<std::unique_ptr<SlicePlane>> slicePlanes;
Expand Down
6 changes: 3 additions & 3 deletions include/polyscope/parameterization_quantity.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ ParameterizationQuantity<QuantityT>::ParameterizationQuantity(QuantityT& quantit
template <typename QuantityT>
void ParameterizationQuantity<QuantityT>::buildParameterizationUI() {

ImGui::PushItemWidth(100);
ImGui::PushItemWidth(100 * state::globalContext.dpiScale);

// Modulo stripey width
if (ImGui::DragFloat("period", &checkerSize.get(), .001, 0.0001, 1.0, "%.4f",
Expand All @@ -82,7 +82,7 @@ void ParameterizationQuantity<QuantityT>::buildParameterizationUI() {
setCheckerColors(getCheckerColors());
break;
case ParamVizStyle::CHECKER_ISLANDS:
ImGui::PushItemWidth(100);
ImGui::PushItemWidth(100 * state::globalContext.dpiScale);
if (ImGui::DragFloat("alt darkness", &altDarkness.get(), 0.01, 0., 1.)) {
altDarkness.manuallyChanged();
requestRedraw();
Expand All @@ -103,7 +103,7 @@ void ParameterizationQuantity<QuantityT>::buildParameterizationUI() {
case ParamVizStyle::LOCAL_CHECK:
case ParamVizStyle::LOCAL_RAD: {
// Angle slider
ImGui::PushItemWidth(100);
ImGui::PushItemWidth(100 * state::globalContext.dpiScale);
ImGui::SliderAngle("angle shift", &localRot, -180,
180); // displays in degrees, works in radians TODO refresh/update/persist
if (ImGui::DragFloat("alt darkness", &altDarkness.get(), 0.01, 0., 1.)) {
Expand Down
2 changes: 1 addition & 1 deletion include/polyscope/scalar_quantity.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void ScalarQuantity<QuantityT>::buildScalarUI() {
// Isolines
if (isolinesEnabled.get()) {

ImGui::PushItemWidth(100);
ImGui::PushItemWidth(100 * state::globalContext.dpiScale);


auto styleName = [](const IsolineStyle& m) -> std::string {
Expand Down
2 changes: 1 addition & 1 deletion src/camera_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ void CameraView::buildCustomUI() {

void CameraView::buildCustomOptionsUI() {

ImGui::PushItemWidth(150);
ImGui::PushItemWidth(150 * state::globalContext.dpiScale);

if (widgetFocalLengthUpper == -777) widgetFocalLengthUpper = 2. * (*widgetFocalLength.get().getValuePtr());
if (ImGui::SliderFloat("widget focal length", widgetFocalLength.get().getValuePtr(), 0, widgetFocalLengthUpper,
Expand Down
2 changes: 1 addition & 1 deletion src/curve_network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ void CurveNetwork::buildCustomUI() {
setColor(getColor());
}
ImGui::SameLine();
ImGui::PushItemWidth(100);
ImGui::PushItemWidth(100 * state::globalContext.dpiScale);
if (ImGui::SliderFloat("Radius", radius.get().getValuePtr(), 0.0, .1, "%.5f",
ImGuiSliderFlags_Logarithmic | ImGuiSliderFlags_NoRoundToFormat)) {
radius.manuallyChanged();
Expand Down
2 changes: 1 addition & 1 deletion src/file_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void filenamePromptCallback(char* buff, size_t len) {
static bool windowOpen = true;
ImGui::Begin("Enter filename", &windowOpen, ImGuiWindowFlags_AlwaysAutoResize);

ImGui::PushItemWidth(500);
ImGui::PushItemWidth(500 * state::globalContext.dpiScale);
ImGui::InputText("##filename", buff, len);

if (ImGui::Button("Ok")) {
Expand Down
2 changes: 1 addition & 1 deletion src/image_quantity_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void ImageQuantity::buildImageUI() {

if (getShowFullscreen()) {

ImGui::PushItemWidth(100);
ImGui::PushItemWidth(100 * state::globalContext.dpiScale);
if (ImGui::SliderFloat("transparency", &transparency.get(), 0.f, 1.f)) {
transparency.manuallyChanged();
requestRedraw();
Expand Down
5 changes: 3 additions & 2 deletions src/imgui_config.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2017-2023, Nicholas Sharp and the Polyscope contributors. https://polyscope.run

#include "polyscope/imgui_config.h"
#include <polyscope/polyscope.h>

namespace polyscope {

Expand Down Expand Up @@ -84,13 +85,13 @@ std::tuple<ImFontAtlas*, ImFont*, ImFont*> prepareImGuiFonts() {
{ // add regular font
ImFontConfig config;
regularFont = io.Fonts->AddFontFromMemoryCompressedTTF(render::getLatoRegularCompressedData(),
render::getLatoRegularCompressedSize(), 18.0f, &config);
render::getLatoRegularCompressedSize(), state::globalContext.dpiScale*18.0f, &config);
}

{ // add mono font
ImFontConfig config;
monoFont = io.Fonts->AddFontFromMemoryCompressedTTF(render::getCousineRegularCompressedData(),
render::getCousineRegularCompressedSize(), 16.0f, &config);
render::getCousineRegularCompressedSize(), state::globalContext.dpiScale*16.0f, &config);
}

// io.Fonts->AddFontFromFileTTF("test-font-name.ttf", 16);
Expand Down
2 changes: 1 addition & 1 deletion src/messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void buildWarningUI(std::string warningBaseString, std::string warningDetailStri
}

// Nice button sizing
float buttonWidth = 120;
float buttonWidth = 120 * state::globalContext.dpiScale;
float buttonOffset = (warningModalSize.x - buttonWidth) / 2.0;
buttonOffset = std::max(buttonOffset, 0.0f);
doIndent = buttonOffset > 0;
Expand Down
2 changes: 1 addition & 1 deletion src/point_cloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ void PointCloud::buildCustomUI() {
setPointColor(getPointColor());
}
ImGui::SameLine();
ImGui::PushItemWidth(70);
ImGui::PushItemWidth(70 * state::globalContext.dpiScale);
if (ImGui::SliderFloat("Radius", pointRadius.get().getValuePtr(), 0.0, .1, "%.5f",
ImGuiSliderFlags_Logarithmic | ImGuiSliderFlags_NoRoundToFormat)) {
pointRadius.manuallyChanged();
Expand Down
16 changes: 9 additions & 7 deletions src/polyscope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ bool unshowRequested = false;
float imguiStackMargin = 10;
float lastWindowHeightPolyscope = 200;
float lastWindowHeightUser = 200;
float leftWindowsWidth = 305;
float rightWindowsWidth = 500;
constexpr float LEFT_WINDOWS_WIDTH = 305;
constexpr float RIGHT_WINDOWS_WIDTH = 500;
float leftWindowsWidth = LEFT_WINDOWS_WIDTH;
float rightWindowsWidth = RIGHT_WINDOWS_WIDTH;

auto lastMainLoopIterTime = std::chrono::steady_clock::now();

Expand Down Expand Up @@ -554,7 +556,7 @@ void userGuiBegin() {
void userGuiEnd() {

if (options::userGuiIsOnRightSide) {
rightWindowsWidth = ImGui::GetWindowWidth();
rightWindowsWidth = RIGHT_WINDOWS_WIDTH * state::globalContext.dpiScale;
lastWindowHeightUser = imguiStackMargin + ImGui::GetWindowHeight();
} else {
lastWindowHeightUser = 0;
Expand Down Expand Up @@ -645,7 +647,7 @@ void buildPolyscopeGui() {
ImGui::Text("Rolling: %.1f ms/frame (%.1f fps)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::Text("Last: %.1f ms/frame (%.1f fps)", ImGui::GetIO().DeltaTime * 1000.f, 1.f / ImGui::GetIO().DeltaTime);

ImGui::PushItemWidth(40);
ImGui::PushItemWidth(40 * state::globalContext.dpiScale);
if (ImGui::InputInt("max fps", &options::maxFPS, 0)) {
if (options::maxFPS < 1 && options::maxFPS != -1) {
options::maxFPS = -1;
Expand Down Expand Up @@ -679,7 +681,7 @@ void buildPolyscopeGui() {


lastWindowHeightPolyscope = imguiStackMargin + ImGui::GetWindowHeight();
leftWindowsWidth = ImGui::GetWindowWidth();
leftWindowsWidth = LEFT_WINDOWS_WIDTH * state::globalContext.dpiScale;

ImGui::End();
}
Expand Down Expand Up @@ -748,7 +750,7 @@ void buildStructureGui() {
ImGui::PopID();
}

leftWindowsWidth = ImGui::GetWindowWidth();
leftWindowsWidth = LEFT_WINDOWS_WIDTH * state::globalContext.dpiScale;

ImGui::End();
}
Expand All @@ -767,7 +769,7 @@ void buildPickGui() {
ImGui::Separator();
selection.first->buildPickUI(selection.second);

rightWindowsWidth = ImGui::GetWindowWidth();
rightWindowsWidth = RIGHT_WINDOWS_WIDTH * state::globalContext.dpiScale;
ImGui::End();
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/render/color_maps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "polyscope/render/engine.h"

#include "imgui.h"
#include "polyscope/polyscope.h"

namespace polyscope {

Expand All @@ -17,7 +18,7 @@ namespace render {
bool buildColormapSelector(std::string& cm, std::string fieldName) {
bool changed = false;

ImGui::PushItemWidth(125);
ImGui::PushItemWidth(125 * state::globalContext.dpiScale);

if (ImGui::BeginCombo(fieldName.c_str(), cm.c_str())) {
for (auto& c : render::engine->colorMaps) {
Expand Down
2 changes: 1 addition & 1 deletion src/render/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ void Engine::buildEngineGui() {
if (ImGui::TreeNode("Appearance")) {

// == Display
ImGui::PushItemWidth(120);
ImGui::PushItemWidth(120 * state::globalContext.dpiScale);
// ImGui::Text("Background");
// ImGui::SameLine();
static std::string displayBackgroundName = "None";
Expand Down
6 changes: 3 additions & 3 deletions src/render/ground_plane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ void GroundPlane::buildGui() {
ImGui::SetNextItemOpen(false, ImGuiCond_FirstUseEver);
if (ImGui::TreeNode("Ground Plane")) {

ImGui::PushItemWidth(160);
ImGui::PushItemWidth(160*state::globalContext.dpiScale);
if (ImGui::BeginCombo("Mode", modeName(options::groundPlaneMode).c_str())) {
for (GroundPlaneMode m : {GroundPlaneMode::None, GroundPlaneMode::Tile, GroundPlaneMode::TileReflection,
GroundPlaneMode::ShadowOnly}) {
Expand All @@ -426,7 +426,7 @@ void GroundPlane::buildGui() {
ImGui::PopItemWidth();

// Height
ImGui::PushItemWidth(80);
ImGui::PushItemWidth(80 * state::globalContext.dpiScale);
switch (options::groundPlaneHeightMode) {
case GroundPlaneHeightMode::Automatic:
if (ImGui::SliderFloat("##HeightValue", options::groundPlaneHeightFactor.getValuePtr(), -1.0, 1.0))
Expand All @@ -446,7 +446,7 @@ void GroundPlane::buildGui() {
}
ImGui::PopItemWidth();
ImGui::SameLine();
ImGui::PushItemWidth(100);
ImGui::PushItemWidth(100*state::globalContext.dpiScale);
if (ImGui::BeginCombo("Height##Mode", heightModeName(options::groundPlaneHeightMode).c_str())) {
for (GroundPlaneHeightMode m : {GroundPlaneHeightMode::Automatic, GroundPlaneHeightMode::Manual}) {
std::string mName = heightModeName(m);
Expand Down
8 changes: 8 additions & 0 deletions src/render/opengl/gl_engine_glfw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ void GLEngineGLFW::initializeImGui() {
const char* glsl_version = "#version 150";
ImGui_ImplOpenGL3_Init(glsl_version);

float xScale, yScale;
glfwGetWindowContentScale(mainWindow, &xScale, &yScale);
state::globalContext.dpiScale = std::min(xScale, yScale);

configureImGui();
}

Expand All @@ -132,6 +136,10 @@ void GLEngineGLFW::shutdownImGui() {
}

void GLEngineGLFW::ImGuiNewFrame() {
float xScale, yScale;
glfwGetWindowContentScale(mainWindow, &xScale, &yScale);
state::globalContext.dpiScale = std::min(xScale, yScale);

ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
Expand Down
12 changes: 6 additions & 6 deletions src/surface_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,7 @@ void SurfaceMesh::buildCustomUI() {

{ // Flat shading or smooth shading?
ImGui::SameLine();
ImGui::PushItemWidth(85);
ImGui::PushItemWidth(85 * state::globalContext.dpiScale);

auto styleName = [](const MeshShadeStyle& m) -> std::string {
switch (m) {
Expand Down Expand Up @@ -1278,7 +1278,7 @@ void SurfaceMesh::buildCustomUI() {

{ // Edge options
ImGui::SameLine();
ImGui::PushItemWidth(100);
ImGui::PushItemWidth(100 * state::globalContext.dpiScale);
if (edgeWidth.get() == 0.) {
bool showEdges = false;
if (ImGui::Checkbox("Edges", &showEdges)) {
Expand All @@ -1291,14 +1291,14 @@ void SurfaceMesh::buildCustomUI() {
}

// Edge color
ImGui::PushItemWidth(100);
ImGui::PushItemWidth(100 * state::globalContext.dpiScale);
if (ImGui::ColorEdit3("Edge Color", &edgeColor.get()[0], ImGuiColorEditFlags_NoInputs))
setEdgeColor(edgeColor.get());
ImGui::PopItemWidth();

// Edge width
ImGui::SameLine();
ImGui::PushItemWidth(75);
ImGui::PushItemWidth(75 * state::globalContext.dpiScale);
if (ImGui::SliderFloat("Width", &edgeWidth.get(), 0.001, 2.)) {
// NOTE: this intentionally circumvents the setEdgeWidth() setter to avoid repopulating the buffer as the
// slider is dragged---otherwise we repopulate the buffer on every change, which mostly works fine. This is a
Expand Down Expand Up @@ -1419,12 +1419,12 @@ long long int SurfaceMesh::selectVertex() {
ImGui::SetNextWindowSize(ImVec2(300, 0), ImGuiCond_Once);
ImGui::Begin("Select vertex", &showWindow);

ImGui::PushItemWidth(300);
ImGui::PushItemWidth(300 * state::globalContext.dpiScale);
ImGui::TextUnformatted("Hold ctrl and left-click to select a vertex");
ImGui::Separator();

// Choose by number
ImGui::PushItemWidth(300);
ImGui::PushItemWidth(300 * state::globalContext.dpiScale);
static int iV = -1;
ImGui::InputInt("index", &iV);
if (ImGui::Button("Select by index")) {
Expand Down
10 changes: 5 additions & 5 deletions src/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ void buildViewGui() {

std::string viewStyleName = to_string(view::style);

ImGui::PushItemWidth(120);
ImGui::PushItemWidth(120 * state::globalContext.dpiScale);
std::array<NavigateStyle, 5> styles{NavigateStyle::Turntable, NavigateStyle::Free, NavigateStyle::Planar,
NavigateStyle::None, NavigateStyle::FirstPerson};
if (ImGui::BeginCombo("##View Style", viewStyleName.c_str())) {
Expand All @@ -747,7 +747,7 @@ void buildViewGui() {
ImGui::Text("Camera Style");

{ // == Up direction
ImGui::PushItemWidth(120);
ImGui::PushItemWidth(120 * state::globalContext.dpiScale);
std::string upStyleName;
switch (upDir) {
case UpDir::XUp:
Expand Down Expand Up @@ -802,7 +802,7 @@ void buildViewGui() {
}

{ // == Front direction
ImGui::PushItemWidth(120);
ImGui::PushItemWidth(120 * state::globalContext.dpiScale);
std::string frontStyleName;
switch (frontDir) {
case FrontDir::XFront:
Expand Down Expand Up @@ -894,7 +894,7 @@ void buildViewGui() {


ImGui::TextUnformatted("Bounding Box:");
ImGui::PushItemWidth(200);
ImGui::PushItemWidth(200 * state::globalContext.dpiScale);
glm::vec3& bboxMin = std::get<0>(state::boundingBox);
glm::vec3& bboxMax = std::get<1>(state::boundingBox);
if (ImGui::InputFloat3("min", &bboxMin[0])) updateStructureExtents();
Expand Down Expand Up @@ -958,7 +958,7 @@ void buildViewGui() {
{
ImGui::TextUnformatted("Dim:");
ImGui::SameLine();
ImGui::PushItemWidth(50);
ImGui::PushItemWidth(50 * state::globalContext.dpiScale);
bool changed = false;
int currWidth = view::windowWidth;
int currHeight = view::windowHeight;
Expand Down
6 changes: 3 additions & 3 deletions src/volume_grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void VolumeGrid::buildCustomUI() {

{ // Edge options
ImGui::SameLine();
ImGui::PushItemWidth(100);
ImGui::PushItemWidth(100 * state::globalContext.dpiScale);
if (edgeWidth.get() == 0.) {
bool showEdges = false;
if (ImGui::Checkbox("Edges", &showEdges)) {
Expand All @@ -65,14 +65,14 @@ void VolumeGrid::buildCustomUI() {
}

// Edge color
ImGui::PushItemWidth(100);
ImGui::PushItemWidth(100 * state::globalContext.dpiScale);
if (ImGui::ColorEdit3("Edge Color", &edgeColor.get()[0], ImGuiColorEditFlags_NoInputs))
setEdgeColor(edgeColor.get());
ImGui::PopItemWidth();

// Edge width
ImGui::SameLine();
ImGui::PushItemWidth(75);
ImGui::PushItemWidth(75 * state::globalContext.dpiScale);
if (ImGui::SliderFloat("Width", &edgeWidth.get(), 0.001, 2.)) {
// NOTE: this intentionally circumvents the setEdgeWidth() setter to avoid repopulating the buffer as the
// slider is dragged---otherwise we repopulate the buffer on every change, which mostly works fine. This is a
Expand Down
2 changes: 1 addition & 1 deletion src/volume_grid_scalar_quantity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void VolumeGridNodeScalarQuantity::buildCustomUI() {
ImGui::SameLine();

// Set isovalue
ImGui::PushItemWidth(120);
ImGui::PushItemWidth(120 * state::globalContext.dpiScale);
if (ImGui::SliderFloat("##Radius", &isosurfaceLevel.get(), vizRangeMin.get(), vizRangeMax.get(), "%.4e")) {
// Note: we intentionally do this rather than calling setIsosurfaceLevel(), because that function immediately
// recomputes the levelset mesh, which is too expensive during user interaction
Expand Down
Loading