Skip to content

Commit

Permalink
Mac: Implement Retina for the 3D scene, fix #97
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechkral committed Jan 24, 2019
1 parent 54bd2e5 commit 4081d13
Show file tree
Hide file tree
Showing 11 changed files with 421 additions and 89 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" STRE
endif ()
endif()

if (APPLE)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=partial-availability -Werror=unguarded-availability -Werror=unguarded-availability-new")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=partial-availability -Werror=unguarded-availability -Werror=unguarded-availability-new")
endif ()

# Where all the bundled libraries reside?
set(LIBDIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(LIBDIR_BIN ${CMAKE_CURRENT_BINARY_DIR}/src)
Expand Down
8 changes: 7 additions & 1 deletion src/slic3r/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 2.6)

include(PrecompiledHeader)

add_library(libslic3r_gui STATIC
set(SLIC3R_GUI_SOURCES
pchheader.cpp
pchheader.hpp
GUI/AboutDialog.cpp
Expand Down Expand Up @@ -127,6 +127,12 @@ add_library(libslic3r_gui STATIC
Utils/HexFile.hpp
)

if (APPLE)
list(APPEND SLIC3R_GUI_SOURCES Utils/RetinaHelperImpl.mm)
endif ()

add_library(libslic3r_gui STATIC ${SLIC3R_GUI_SOURCES})

target_link_libraries(libslic3r_gui libslic3r avrdude imgui)
if (SLIC3R_PCH AND NOT SLIC3R_SYNTAXONLY)
add_precompiled_header(libslic3r_gui pchheader.hpp FORCEINCLUDE)
Expand Down
5 changes: 5 additions & 0 deletions src/slic3r/GUI/AppConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ void AppConfig::set_defaults()
if (get("use_legacy_opengl").empty())
set("use_legacy_opengl", "0");

#if __APPLE__
if (get("use_retina_opengl").empty())
set("use_retina_opengl", "1");
#endif

if (get("remember_output_path").empty())
set("remember_output_path", "1");

Expand Down
242 changes: 173 additions & 69 deletions src/slic3r/GUI/GLCanvas3D.cpp

Large diffs are not rendered by default.

36 changes: 30 additions & 6 deletions src/slic3r/GUI/GLCanvas3D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#define slic3r_GLCanvas3D_hpp_

#include <stddef.h>
#include <memory>

#include "libslic3r/Technologies.hpp"
#include "3DScene.hpp"
#include "GLToolbar.hpp"
#include "Event.hpp"
Expand All @@ -20,6 +22,9 @@ class wxTimerEvent;
class wxPaintEvent;
class wxGLCanvas;

// Support for Retina OpenGL on Mac OS
#define ENABLE_RETINA_GL __APPLE__

class GLUquadric;
typedef class GLUquadric GLUquadricObj;

Expand All @@ -36,6 +41,10 @@ namespace GUI {

class GLGizmoBase;

#if ENABLE_RETINA_GL
class RetinaHelper;
#endif

class GeometryBuffer
{
std::vector<float> m_vertices;
Expand All @@ -55,16 +64,20 @@ class Size
{
int m_width;
int m_height;
float m_scale_factor;

public:
Size();
Size(int width, int height);
Size(int width, int height, float scale_factor = 1.0);

int get_width() const;
void set_width(int width);

int get_height() const;
void set_height(int height);

int get_scale_factor() const;
void set_scale_factor(int height);
};

class Rect
Expand Down Expand Up @@ -297,6 +310,9 @@ class GLCanvas3D
};

private:
static const float THICKNESS_BAR_WIDTH;
static const float THICKNESS_RESET_BUTTON_HEIGHT;

bool m_use_legacy_opengl;
bool m_enabled;
Shader m_shader;
Expand Down Expand Up @@ -380,6 +396,9 @@ class GLCanvas3D
void _render_active_object_annotations(const GLCanvas3D& canvas, const Rect& bar_rect) const;
void _render_profile(const Rect& bar_rect) const;
void update_slicing_parameters();

static float thickness_bar_width(const GLCanvas3D &canvas);
static float reset_button_height(const GLCanvas3D &canvas);
};

struct Mouse
Expand Down Expand Up @@ -690,10 +709,6 @@ class GLCanvas3D
private:
class Gizmos
{
static const float OverlayIconsScale;
static const float OverlayBorder;
static const float OverlayGapY;

public:
enum EType : unsigned char
{
Expand All @@ -714,6 +729,10 @@ class GLCanvas3D
BackgroundTexture m_background_texture;
EType m_current;

float m_overlay_icons_scale;
float m_overlay_border;
float m_overlay_gap_y;

public:
Gizmos();
~Gizmos();
Expand All @@ -723,6 +742,8 @@ class GLCanvas3D
bool is_enabled() const;
void set_enabled(bool enable);

void set_overlay_scale(float scale);

std::string update_hover_state(const GLCanvas3D& canvas, const Vec2d& mouse_pos, const Selection& selection);
void update_on_off_state(const GLCanvas3D& canvas, const Vec2d& mouse_pos, const Selection& selection);
void update_on_off_state(const Selection& selection);
Expand Down Expand Up @@ -812,7 +833,7 @@ class GLCanvas3D
public:
WarningTexture();

bool generate(const std::string& msg);
bool generate(const std::string& msg, const GLCanvas3D& canvas);

void render(const GLCanvas3D& canvas) const;
};
Expand Down Expand Up @@ -842,6 +863,9 @@ class GLCanvas3D

wxGLCanvas* m_canvas;
wxGLContext* m_context;
#if ENABLE_RETINA_GL
std::unique_ptr<RetinaHelper> m_retina_helper;
#endif
bool m_in_render;
LegendTexture m_legend_texture;
WarningTexture m_warning_texture;
Expand Down
40 changes: 29 additions & 11 deletions src/slic3r/GUI/ImGuiWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <cstdio>
#include <vector>
#include <cmath>
#include <stdexcept>

#include <boost/format.hpp>
#include <boost/log/trivial.hpp>
Expand All @@ -24,6 +26,7 @@ namespace GUI {

ImGuiWrapper::ImGuiWrapper()
: m_font_texture(0)
, m_style_scaling(1.0)
, m_mouse_buttons(0)
, m_disabled(false)
{
Expand All @@ -39,18 +42,9 @@ bool ImGuiWrapper::init()
{
ImGui::CreateContext();

ImGuiIO& io = ImGui::GetIO();
ImFont* font = io.Fonts->AddFontFromFileTTF((Slic3r::resources_dir() + "/fonts/NotoSans-Regular.ttf").c_str(), 18.0f);
if (font == nullptr) {
font = io.Fonts->AddFontDefault();
if (font == nullptr)
return false;
}
else {
m_fonts.insert(FontsMap::value_type("Noto Sans Regular 18", font));
}
init_default_font(m_style_scaling);

io.IniFilename = nullptr;
ImGui::GetIO().IniFilename = nullptr;

return true;
}
Expand All @@ -62,6 +56,15 @@ void ImGuiWrapper::set_display_size(float w, float h)
io.DisplayFramebufferScale = ImVec2(1.0f, 1.0f);
}

void ImGuiWrapper::set_style_scaling(float scaling)
{
if (!std::isnan(scaling) && !std::isinf(scaling) && scaling != m_style_scaling) {
ImGui::GetStyle().ScaleAllSizes(scaling / m_style_scaling);
init_default_font(scaling);
m_style_scaling = scaling;
}
}

bool ImGuiWrapper::update_mouse_data(wxMouseEvent& evt)
{
ImGuiIO& io = ImGui::GetIO();
Expand Down Expand Up @@ -198,6 +201,21 @@ bool ImGuiWrapper::want_any_input() const
return io.WantCaptureMouse || io.WantCaptureKeyboard || io.WantTextInput;
}

void ImGuiWrapper::init_default_font(float scaling)
{
static const float font_size = 18.0f;

ImGuiIO& io = ImGui::GetIO();
io.Fonts->Clear();
ImFont* font = io.Fonts->AddFontFromFileTTF((Slic3r::resources_dir() + "/fonts/NotoSans-Regular.ttf").c_str(), font_size * scaling);
if (font == nullptr) {
font = io.Fonts->AddFontDefault();
if (font == nullptr) {
throw std::runtime_error("ImGui: Could not load deafult font");
}
}
}

void ImGuiWrapper::create_device_objects()
{
create_fonts_texture();
Expand Down
3 changes: 3 additions & 0 deletions src/slic3r/GUI/ImGuiWrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ImGuiWrapper

FontsMap m_fonts;
unsigned m_font_texture;
float m_style_scaling;
unsigned m_mouse_buttons;
bool m_disabled;

Expand All @@ -32,6 +33,7 @@ class ImGuiWrapper
void read_glsl_version();

void set_display_size(float w, float h);
void set_style_scaling(float scaling);
bool update_mouse_data(wxMouseEvent &evt);

void new_frame();
Expand All @@ -58,6 +60,7 @@ class ImGuiWrapper
bool want_text_input() const;
bool want_any_input() const;
private:
void init_default_font(float scaling);
void create_device_objects();
void create_fonts_texture();
void render_draw_data(ImDrawData *draw_data);
Expand Down
16 changes: 14 additions & 2 deletions src/slic3r/GUI/Preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ void PreferencesDialog::build()
option = Option (def,"show_incompatible_presets");
m_optgroup->append_single_option_line(option);

// TODO: remove?
def.label = L("Use legacy OpenGL 1.1 rendering");
def.type = coBool;
def.tooltip = L("If you have rendering issues caused by a buggy OpenGL 2.0 driver, "
Expand All @@ -96,6 +97,16 @@ void PreferencesDialog::build()
option = Option (def,"use_legacy_opengl");
m_optgroup->append_single_option_line(option);

#if __APPLE__
def.label = L("Use Retina resolution for the 3D scene");
def.type = coBool;
def.tooltip = L("If enabled, the 3D scene will be rendered in Retina resolution. "
"If you are experiencing 3D performance problems, disabling this option may help.");
def.default_value = new ConfigOptionBool{ app_config->get("use_retina_opengl") == "1" };
option = Option (def, "use_retina_opengl");
m_optgroup->append_single_option_line(option);
#endif

auto sizer = new wxBoxSizer(wxVERTICAL);
sizer->Add(m_optgroup->sizer, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);

Expand All @@ -110,8 +121,9 @@ void PreferencesDialog::build()

void PreferencesDialog::accept()
{
if (m_values.find("no_defaults") != m_values.end()||
m_values.find("use_legacy_opengl")!= m_values.end()) {
if (m_values.find("no_defaults") != m_values.end() ||
m_values.find("use_legacy_opengl") != m_values.end() ||
m_values.find("use_retina_opengl") != m_values.end()) {
warning_catcher(this, _(L("You need to restart Slic3r to make the changes effective.")));
}

Expand Down
29 changes: 29 additions & 0 deletions src/slic3r/Utils/RetinaHelper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef slic3r_RetinaHelper_hpp_
#define slic3r_RetinaHelper_hpp_

class wxWindow;


namespace Slic3r {
namespace GUI {

class RetinaHelper
{
public:
RetinaHelper(wxWindow* window);
~RetinaHelper();

void set_use_retina(bool value);
bool get_use_retina();
float get_scale_factor();

private:
wxWindow* m_window;
void* m_self;
};


} // namespace GUI
} // namespace Slic3r

#endif // RetinaHelper_h
15 changes: 15 additions & 0 deletions src/slic3r/Utils/RetinaHelperImpl.hmm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#import <Cocoa/Cocoa.h>

class wxEvtHandler;

@interface RetinaHelperImpl : NSObject
{
NSView *view;
wxEvtHandler* handler;
}

-(id)initWithView:(NSView *)view handler:(wxEvtHandler *)handler;
-(void)setViewWantsBestResolutionOpenGLSurface:(BOOL)value;
-(BOOL)getViewWantsBestResolutionOpenGLSurface;
-(float)getBackingScaleFactor;
@end
Loading

0 comments on commit 4081d13

Please sign in to comment.