Added ImGuiWrapper

This commit is contained in:
Enrico Turri 2018-10-31 10:19:44 +01:00 committed by Vojtech Kral
parent 5c054d11ca
commit 57e9d28a84
9 changed files with 772 additions and 0 deletions

View file

@ -33,6 +33,8 @@
#define ENABLE_WORLD_ROTATIONS (1 && ENABLE_1_42_0)
// Enables shortcut keys for gizmos
#define ENABLE_GIZMOS_SHORTCUT (1 && ENABLE_1_42_0)
// Scene's GUI made using imgui library
#define ENABLE_IMGUI (1 && ENABLE_1_42_0)
#endif // _technologies_h_

View file

@ -82,6 +82,8 @@ add_library(libslic3r_gui STATIC
GUI/BonjourDialog.hpp
GUI/ButtonsDescription.cpp
GUI/ButtonsDescription.hpp
GUI/ImGuiWrapper.hpp
GUI/ImGuiWrapper.cpp
Config/Snapshot.cpp
Config/Snapshot.hpp
Config/Version.cpp

View file

@ -2932,6 +2932,9 @@ void GLCanvas3D::Gizmos::_render_overlay(const GLCanvas3D& canvas) const
return;
float cnv_w = (float)canvas.get_canvas_size().get_width();
#if ENABLE_IMGUI
float cnv_h = (float)canvas.get_canvas_size().get_height();
#endif // ENABLE_IMGUI
float zoom = canvas.get_camera_zoom();
float inv_zoom = (zoom != 0.0f) ? 1.0f / zoom : 0.0f;
@ -2946,6 +2949,10 @@ void GLCanvas3D::Gizmos::_render_overlay(const GLCanvas3D& canvas) const
float tex_size = (float)it->second->get_textures_size() * OverlayTexturesScale * inv_zoom;
GLTexture::render_texture(it->second->get_texture_id(), top_x, top_x + tex_size, top_y - tex_size, top_y);
#if ENABLE_IMGUI
if (it->second->get_state() == GLGizmoBase::On)
it->second->render_input_window(2.0f * OverlayOffsetX + tex_size * zoom, 0.5f * cnv_h - top_y * zoom, selection);
#endif // ENABLE_IMGUI
top_y -= (tex_size + scaled_gap_y);
}
}
@ -3745,6 +3752,10 @@ void GLCanvas3D::render()
set_tooltip("");
#if ENABLE_IMGUI
wxGetApp().get_imgui().new_frame();
#endif // ENABLE_IMGUI
// picking pass
_picking_pass();
@ -3787,6 +3798,10 @@ void GLCanvas3D::render()
_render_toolbar();
_render_layer_editing_overlay();
#if ENABLE_IMGUI
wxGetApp().get_imgui().render();
#endif // ENABLE_IMGUI
m_canvas->SwapBuffers();
}
@ -4446,6 +4461,10 @@ void GLCanvas3D::on_timer(wxTimerEvent& evt)
void GLCanvas3D::on_mouse(wxMouseEvent& evt)
{
#if ENABLE_IMGUI
wxGetApp().get_imgui().update_mouse_data(evt);
#endif // ENABLE_IMGUI
Point pos(evt.GetX(), evt.GetY());
int selected_object_idx = m_selection.get_object_idx();
@ -5303,6 +5322,10 @@ void GLCanvas3D::_resize(unsigned int w, unsigned int h)
if ((m_canvas == nullptr) && (m_context == nullptr))
return;
#if ENABLE_IMGUI
wxGetApp().get_imgui().set_display_size((float)w, (float)h);
#endif // ENABLE_IMGUI
// ensures that this canvas is current
#if ENABLE_USE_UNIQUE_GLCONTEXT
_set_current();

View file

@ -686,6 +686,20 @@ void GLGizmoRotate3D::on_render(const GLCanvas3D::Selection& selection) const
m_gizmos[Z].render(selection);
}
#if ENABLE_IMGUI
void GLGizmoRotate3D::on_render_input_window(float x, float y, const GLCanvas3D::Selection& selection) const
{
Vec3d rotation(Geometry::rad2deg(m_gizmos[0].get_angle()), Geometry::rad2deg(m_gizmos[1].get_angle()), Geometry::rad2deg(m_gizmos[2].get_angle()));
std::string label = _("Rotation (deg)");
wxGetApp().get_imgui().set_next_window_pos(x, y, ImGuiCond_Always);
wxGetApp().get_imgui().set_next_window_bg_alpha(0.5f);
wxGetApp().get_imgui().begin(label, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse);
wxGetApp().get_imgui().input_vec3("", rotation, 100.0f, "%.2f");
wxGetApp().get_imgui().end();
}
#endif // ENABLE_IMGUI
const float GLGizmoScale3D::Offset = 5.0f;
GLGizmoScale3D::GLGizmoScale3D(GLCanvas3D& parent)
@ -972,6 +986,21 @@ void GLGizmoScale3D::on_render_for_picking(const GLCanvas3D::Selection& selectio
render_grabbers_for_picking(selection.get_bounding_box());
}
#if ENABLE_IMGUI
void GLGizmoScale3D::on_render_input_window(float x, float y, const GLCanvas3D::Selection& selection) const
{
bool single_instance = selection.is_single_full_instance();
Vec3d scale = single_instance ? 100.0 * selection.get_volume(*selection.get_volume_idxs().begin())->get_scaling_factor() : 100.0 * m_scale;
std::string label = _("Scale (%)");
wxGetApp().get_imgui().set_next_window_pos(x, y, ImGuiCond_Always);
wxGetApp().get_imgui().set_next_window_bg_alpha(0.5f);
wxGetApp().get_imgui().begin(label, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse);
wxGetApp().get_imgui().input_vec3("", scale, 100.0f, "%.2f");
wxGetApp().get_imgui().end();
}
#endif // ENABLE_IMGUI
void GLGizmoScale3D::render_grabbers_connection(unsigned int id_1, unsigned int id_2) const
{
unsigned int grabbers_count = (unsigned int)m_grabbers.size();
@ -1185,6 +1214,30 @@ void GLGizmoMove3D::on_render_for_picking(const GLCanvas3D::Selection& selection
render_grabbers_for_picking(selection.get_bounding_box());
}
#if ENABLE_IMGUI
void GLGizmoMove3D::on_render_input_window(float x, float y, const GLCanvas3D::Selection& selection) const
{
bool show_position = selection.is_single_full_instance();
const Vec3d& position = selection.get_bounding_box().center();
Vec3d displacement = show_position ? position : m_displacement;
std::string label = show_position ? _("Position (mm)") : _("Displacement (mm)");
wxGetApp().get_imgui().set_next_window_pos(x, y, ImGuiCond_Always);
wxGetApp().get_imgui().set_next_window_bg_alpha(0.5f);
wxGetApp().get_imgui().begin(label, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse);
wxGetApp().get_imgui().input_vec3("", displacement, 100.0f, "%.2f");
if (ImGui::Button("Test"))
{
std::cout << "WOW" << std::endl;
}
wxGetApp().get_imgui().end();
}
#endif // ENABLE_IMGUI
double GLGizmoMove3D::calc_projection(const UpdateData& data) const
{
double projection = 0.0;

View file

@ -137,6 +137,10 @@ public:
virtual void create_external_gizmo_widgets(wxWindow *parent);
#if ENABLE_IMGUI
void render_input_window(float x, float y, const GLCanvas3D::Selection& selection) const { on_render_input_window(x, y, selection); }
#endif // ENABLE_IMGUI
protected:
virtual bool on_init() = 0;
virtual std::string on_get_name() const = 0;
@ -155,6 +159,10 @@ protected:
virtual void on_render(const GLCanvas3D::Selection& selection) const = 0;
virtual void on_render_for_picking(const GLCanvas3D::Selection& selection) const = 0;
#if ENABLE_IMGUI
virtual void on_render_input_window(float x, float y, const GLCanvas3D::Selection& selection) const {}
#endif // ENABLE_IMGUI
float picking_color_component(unsigned int id) const;
void render_grabbers(const BoundingBoxf3& box) const;
void render_grabbers_for_picking(const BoundingBoxf3& box) const;
@ -291,6 +299,10 @@ protected:
g.render_for_picking(selection);
}
}
#if ENABLE_IMGUI
virtual void on_render_input_window(float x, float y, const GLCanvas3D::Selection& selection) const;
#endif // ENABLE_IMGUI
};
class GLGizmoScale3D : public GLGizmoBase
@ -328,6 +340,10 @@ protected:
virtual void on_render(const GLCanvas3D::Selection& selection) const;
virtual void on_render_for_picking(const GLCanvas3D::Selection& selection) const;
#if ENABLE_IMGUI
virtual void on_render_input_window(float x, float y, const GLCanvas3D::Selection& selection) const;
#endif // ENABLE_IMGUI
private:
void render_grabbers_connection(unsigned int id_1, unsigned int id_2) const;
@ -368,6 +384,10 @@ protected:
virtual void on_render(const GLCanvas3D::Selection& selection) const;
virtual void on_render_for_picking(const GLCanvas3D::Selection& selection) const;
#if ENABLE_IMGUI
virtual void on_render_input_window(float x, float y, const GLCanvas3D::Selection& selection) const;
#endif // ENABLE_IMGUI
private:
double calc_projection(const UpdateData& data) const;
};

View file

@ -32,6 +32,9 @@
#include <I18N.hpp>
#include <wx/wupdlock.h>
#include "SysInfoDialog.hpp"
#if ENABLE_IMGUI
#include <imgui\imgui.h>
#endif // ENABLE_IMGUI
namespace Slic3r {
namespace GUI {
@ -57,6 +60,11 @@ static std::string libslic3r_translate_callback(const char *s) { return wxGetTra
IMPLEMENT_APP(GUI_App)
bool GUI_App::OnInit()
{
#if ENABLE_IMGUI
if (!m_imgui.init())
return false;
#endif // ENABLE_IMGUI
SetAppName("Slic3rPE-alpha");
SetAppDisplayName("Slic3r Prusa Edition");
@ -177,6 +185,14 @@ bool GUI_App::OnInit()
return true;
}
#if ENABLE_IMGUI
int GUI_App::OnExit()
{
m_imgui.shutdown();
return 0;
}
#endif // ENABLE_IMGUI
unsigned GUI_App::get_colour_approx_luma(const wxColour &colour)
{
double r = colour.Red();

View file

@ -4,6 +4,9 @@
#include <string>
#include "PrintConfig.hpp"
#include "MainFrame.hpp"
#if ENABLE_IMGUI
#include "ImGuiWrapper.hpp"
#endif // ENABLE_IMGUI
#include <wx/app.h>
#include <wx/colour.h>
@ -82,8 +85,15 @@ class GUI_App : public wxApp
wxLocale* m_wxLocale{ nullptr };
#if ENABLE_IMGUI
ImGuiWrapper m_imgui;
#endif // ENABLE_IMGUI
public:
bool OnInit() override;
#if ENABLE_IMGUI
int OnExit() override;
#endif // ENABLE_IMGUI
GUI_App() : wxApp() {}
unsigned get_colour_approx_luma(const wxColour &colour);
@ -151,6 +161,10 @@ public:
std::vector<Tab *> tabs_list;
#if ENABLE_IMGUI
ImGuiWrapper& get_imgui() { return m_imgui; }
#endif // ENABLE_IMGUI
};
DECLARE_APP(GUI_App)

View file

@ -0,0 +1,574 @@
#include "../../libslic3r/libslic3r.h"
#include "ImGuiWrapper.hpp"
#include "Utils.hpp"
#include <wx/event.h>
#include <GL/glew.h>
namespace Slic3r {
namespace GUI {
ImGuiWrapper::ImGuiWrapper()
: m_glsl_version_string("")
, m_shader_handle(0)
, m_vert_handle(0)
, m_frag_handle(0)
, m_font_texture(0)
, m_vbo_handle(0)
, m_elements_handle(0)
, m_attrib_location_tex(0)
, m_attrib_location_proj_mtx(0)
, m_attrib_location_position(0)
, m_attrib_location_uv(0)
, m_attrib_location_color(0)
{
}
bool ImGuiWrapper::init()
{
// Store GLSL version string so we can refer to it later in case we recreate shaders. Note: GLSL version is NOT the same as GL version. Leave this to NULL if unsure.
std::string glsl_version;
#ifdef USE_GL_ES3
glsl_version = "#version 300 es";
#else
glsl_version = "#version 130";
#endif
m_glsl_version_string = glsl_version + "\n";
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));
io.IniFilename = nullptr;
return true;
}
void ImGuiWrapper::shutdown()
{
destroy_device_objects();
ImGui::DestroyContext();
}
void ImGuiWrapper::set_display_size(float w, float h)
{
ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = ImVec2(w, h);
io.DisplayFramebufferScale = ImVec2(1.0f, 1.0f);
}
void ImGuiWrapper::update_mouse_data(wxMouseEvent& evt)
{
ImGuiIO& io = ImGui::GetIO();
io.MousePos = ImVec2((float)evt.GetX(), (float)evt.GetY());
io.MouseDown[0] = evt.LeftDown();
io.MouseDown[1] = evt.RightDown();
io.MouseDown[2] = evt.MiddleDown();
if (io.MouseDown[0])
{
int a = 0;
}
}
void ImGuiWrapper::new_frame()
{
if (m_font_texture == 0)
create_device_objects();
ImGui::NewFrame();
}
void ImGuiWrapper::render()
{
ImGuiIO& io = ImGui::GetIO();
ImGui::Render();
render_draw_data(ImGui::GetDrawData());
}
void ImGuiWrapper::set_next_window_pos(float x, float y, int flag)
{
ImGui::SetNextWindowPos(ImVec2(x, y), (ImGuiCond)flag);
}
void ImGuiWrapper::set_next_window_bg_alpha(float alpha)
{
ImGui::SetNextWindowBgAlpha(alpha);
}
bool ImGuiWrapper::begin(const std::string& name, int flags)
{
return ImGui::Begin(name.c_str(), nullptr, (ImGuiWindowFlags)flags);
}
void ImGuiWrapper::end()
{
ImGui::End();
}
bool ImGuiWrapper::input_double(const std::string& label, double& value, const std::string& format)
{
return ImGui::InputDouble(label.c_str(), &value, 0.0f, 0.0f, format.c_str());
}
bool ImGuiWrapper::input_vec3(const std::string& label, Vec3d& value, float width, const std::string& format)
{
bool value_changed = false;
ImGui::BeginGroup();
for (int i = 0; i < 3; ++i)
{
std::string item_label = (i == 0) ? "X" : ((i == 1) ? "Y" : "Z");
ImGui::PushID(i);
ImGui::PushItemWidth(width);
value_changed |= ImGui::InputDouble(item_label.c_str(), &value(i), 0.0f, 0.0f, format.c_str());
ImGui::PopID();
}
ImGui::EndGroup();
return value_changed;
}
void ImGuiWrapper::create_device_objects()
{
// Backup GL state
GLint last_texture, last_array_buffer, last_vertex_array;
glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array);
// Parse GLSL version string
int glsl_version = 130;
::sscanf(m_glsl_version_string.c_str(), "#version %d", &glsl_version);
const GLchar* vertex_shader_glsl_120 =
"uniform mat4 ProjMtx;\n"
"attribute vec2 Position;\n"
"attribute vec2 UV;\n"
"attribute vec4 Color;\n"
"varying vec2 Frag_UV;\n"
"varying vec4 Frag_Color;\n"
"void main()\n"
"{\n"
" Frag_UV = UV;\n"
" Frag_Color = Color;\n"
" gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
"}\n";
const GLchar* vertex_shader_glsl_130 =
"uniform mat4 ProjMtx;\n"
"in vec2 Position;\n"
"in vec2 UV;\n"
"in vec4 Color;\n"
"out vec2 Frag_UV;\n"
"out vec4 Frag_Color;\n"
"void main()\n"
"{\n"
" Frag_UV = UV;\n"
" Frag_Color = Color;\n"
" gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
"}\n";
const GLchar* vertex_shader_glsl_300_es =
"precision mediump float;\n"
"layout (location = 0) in vec2 Position;\n"
"layout (location = 1) in vec2 UV;\n"
"layout (location = 2) in vec4 Color;\n"
"uniform mat4 ProjMtx;\n"
"out vec2 Frag_UV;\n"
"out vec4 Frag_Color;\n"
"void main()\n"
"{\n"
" Frag_UV = UV;\n"
" Frag_Color = Color;\n"
" gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
"}\n";
const GLchar* vertex_shader_glsl_410_core =
"layout (location = 0) in vec2 Position;\n"
"layout (location = 1) in vec2 UV;\n"
"layout (location = 2) in vec4 Color;\n"
"uniform mat4 ProjMtx;\n"
"out vec2 Frag_UV;\n"
"out vec4 Frag_Color;\n"
"void main()\n"
"{\n"
" Frag_UV = UV;\n"
" Frag_Color = Color;\n"
" gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
"}\n";
const GLchar* fragment_shader_glsl_120 =
"#ifdef GL_ES\n"
" precision mediump float;\n"
"#endif\n"
"uniform sampler2D Texture;\n"
"varying vec2 Frag_UV;\n"
"varying vec4 Frag_Color;\n"
"void main()\n"
"{\n"
" gl_FragColor = Frag_Color * texture2D(Texture, Frag_UV.st);\n"
"}\n";
const GLchar* fragment_shader_glsl_130 =
"uniform sampler2D Texture;\n"
"in vec2 Frag_UV;\n"
"in vec4 Frag_Color;\n"
"out vec4 Out_Color;\n"
"void main()\n"
"{\n"
" Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
"}\n";
const GLchar* fragment_shader_glsl_300_es =
"precision mediump float;\n"
"uniform sampler2D Texture;\n"
"in vec2 Frag_UV;\n"
"in vec4 Frag_Color;\n"
"layout (location = 0) out vec4 Out_Color;\n"
"void main()\n"
"{\n"
" Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
"}\n";
const GLchar* fragment_shader_glsl_410_core =
"in vec2 Frag_UV;\n"
"in vec4 Frag_Color;\n"
"uniform sampler2D Texture;\n"
"layout (location = 0) out vec4 Out_Color;\n"
"void main()\n"
"{\n"
" Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
"}\n";
// Select shaders matching our GLSL versions
const GLchar* vertex_shader = nullptr;
const GLchar* fragment_shader = nullptr;
if (glsl_version < 130)
{
vertex_shader = vertex_shader_glsl_120;
fragment_shader = fragment_shader_glsl_120;
}
else if (glsl_version == 410)
{
vertex_shader = vertex_shader_glsl_410_core;
fragment_shader = fragment_shader_glsl_410_core;
}
else if (glsl_version == 300)
{
vertex_shader = vertex_shader_glsl_300_es;
fragment_shader = fragment_shader_glsl_300_es;
}
else
{
vertex_shader = vertex_shader_glsl_130;
fragment_shader = fragment_shader_glsl_130;
}
// Create shaders
const GLchar* vertex_shader_with_version[2] = { m_glsl_version_string.c_str(), vertex_shader };
m_vert_handle = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(m_vert_handle, 2, vertex_shader_with_version, nullptr);
glCompileShader(m_vert_handle);
check_shader(m_vert_handle, "vertex shader");
const GLchar* fragment_shader_with_version[2] = { m_glsl_version_string.c_str(), fragment_shader };
m_frag_handle = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(m_frag_handle, 2, fragment_shader_with_version, nullptr);
glCompileShader(m_frag_handle);
check_shader(m_frag_handle, "fragment shader");
m_shader_handle = glCreateProgram();
glAttachShader(m_shader_handle, m_vert_handle);
glAttachShader(m_shader_handle, m_frag_handle);
glLinkProgram(m_shader_handle);
check_program(m_shader_handle, "shader program");
m_attrib_location_tex = glGetUniformLocation(m_shader_handle, "Texture");
m_attrib_location_proj_mtx = glGetUniformLocation(m_shader_handle, "ProjMtx");
m_attrib_location_position = glGetAttribLocation(m_shader_handle, "Position");
m_attrib_location_uv = glGetAttribLocation(m_shader_handle, "UV");
m_attrib_location_color = glGetAttribLocation(m_shader_handle, "Color");
// Create buffers
glGenBuffers(1, &m_vbo_handle);
glGenBuffers(1, &m_elements_handle);
create_fonts_texture();
// Restore modified GL state
glBindTexture(GL_TEXTURE_2D, last_texture);
glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer);
glBindVertexArray(last_vertex_array);
}
void ImGuiWrapper::create_fonts_texture()
{
// Build texture atlas
ImGuiIO& io = ImGui::GetIO();
unsigned char* pixels;
int width, height;
io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bits (75% of the memory is wasted, but default font is so small) because it is more likely to be compatible with user's existing shaders. If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory.
// Upload texture to graphics system
GLint last_texture;
glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
glGenTextures(1, &m_font_texture);
glBindTexture(GL_TEXTURE_2D, m_font_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
// Store our identifier
io.Fonts->TexID = (ImTextureID)(intptr_t)m_font_texture;
// Restore state
glBindTexture(GL_TEXTURE_2D, last_texture);
}
bool ImGuiWrapper::check_program(unsigned int handle, const char* desc)
{
GLint status = 0, log_length = 0;
glGetProgramiv(handle, GL_LINK_STATUS, &status);
glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &log_length);
if ((GLboolean)status == GL_FALSE)
fprintf(stderr, "ERROR: ImGuiWrapper::check_program(): failed to link %s! (with GLSL '%s')\n", desc, m_glsl_version_string);
if (log_length > 0)
{
ImVector<char> buf;
buf.resize((int)(log_length + 1));
glGetProgramInfoLog(handle, log_length, NULL, (GLchar*)buf.begin());
fprintf(stderr, "%s\n", buf.begin());
}
return (GLboolean)status == GL_TRUE;
}
bool ImGuiWrapper::check_shader(unsigned int handle, const char* desc)
{
GLint status = 0, log_length = 0;
glGetShaderiv(handle, GL_COMPILE_STATUS, &status);
glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &log_length);
if ((GLboolean)status == GL_FALSE)
fprintf(stderr, "ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to compile %s!\n", desc);
if (log_length > 0)
{
ImVector<char> buf;
buf.resize((int)(log_length + 1));
glGetShaderInfoLog(handle, log_length, NULL, (GLchar*)buf.begin());
fprintf(stderr, "%s\n", buf.begin());
}
return (GLboolean)status == GL_TRUE;
}
void ImGuiWrapper::render_draw_data(ImDrawData* draw_data)
{
// Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
ImGuiIO& io = ImGui::GetIO();
int fb_width = (int)(draw_data->DisplaySize.x * io.DisplayFramebufferScale.x);
int fb_height = (int)(draw_data->DisplaySize.y * io.DisplayFramebufferScale.y);
if (fb_width <= 0 || fb_height <= 0)
return;
draw_data->ScaleClipRects(io.DisplayFramebufferScale);
// Backup GL state
GLenum last_active_texture; glGetIntegerv(GL_ACTIVE_TEXTURE, (GLint*)&last_active_texture);
glActiveTexture(GL_TEXTURE0);
GLint last_program; glGetIntegerv(GL_CURRENT_PROGRAM, &last_program);
GLint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
#ifdef GL_SAMPLER_BINDING
GLint last_sampler; glGetIntegerv(GL_SAMPLER_BINDING, &last_sampler);
#endif
GLint last_array_buffer; glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
GLint last_vertex_array; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array);
#ifdef GL_POLYGON_MODE
GLint last_polygon_mode[2]; glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode);
#endif
GLint last_viewport[4]; glGetIntegerv(GL_VIEWPORT, last_viewport);
GLint last_scissor_box[4]; glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box);
GLenum last_blend_src_rgb; glGetIntegerv(GL_BLEND_SRC_RGB, (GLint*)&last_blend_src_rgb);
GLenum last_blend_dst_rgb; glGetIntegerv(GL_BLEND_DST_RGB, (GLint*)&last_blend_dst_rgb);
GLenum last_blend_src_alpha; glGetIntegerv(GL_BLEND_SRC_ALPHA, (GLint*)&last_blend_src_alpha);
GLenum last_blend_dst_alpha; glGetIntegerv(GL_BLEND_DST_ALPHA, (GLint*)&last_blend_dst_alpha);
GLenum last_blend_equation_rgb; glGetIntegerv(GL_BLEND_EQUATION_RGB, (GLint*)&last_blend_equation_rgb);
GLenum last_blend_equation_alpha; glGetIntegerv(GL_BLEND_EQUATION_ALPHA, (GLint*)&last_blend_equation_alpha);
GLboolean last_enable_blend = glIsEnabled(GL_BLEND);
GLboolean last_enable_cull_face = glIsEnabled(GL_CULL_FACE);
GLboolean last_enable_depth_test = glIsEnabled(GL_DEPTH_TEST);
GLboolean last_enable_scissor_test = glIsEnabled(GL_SCISSOR_TEST);
// Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, polygon fill
glEnable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glEnable(GL_SCISSOR_TEST);
#ifdef GL_POLYGON_MODE
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
#endif
// Setup viewport, orthographic projection matrix
// Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayMin is typically (0,0) for single viewport apps.
glViewport(0, 0, (GLsizei)fb_width, (GLsizei)fb_height);
float L = draw_data->DisplayPos.x;
float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
float T = draw_data->DisplayPos.y;
float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
const float ortho_projection[4][4] =
{
{ 2.0f / (R - L), 0.0f, 0.0f, 0.0f },
{ 0.0f, 2.0f / (T - B), 0.0f, 0.0f },
{ 0.0f, 0.0f, -1.0f, 0.0f },
{ (R + L) / (L - R), (T + B) / (B - T), 0.0f, 1.0f },
};
glUseProgram(m_shader_handle);
glUniform1i(m_attrib_location_tex, 0);
glUniformMatrix4fv(m_attrib_location_proj_mtx, 1, GL_FALSE, &ortho_projection[0][0]);
#ifdef GL_SAMPLER_BINDING
glBindSampler(0, 0); // We use combined texture/sampler state. Applications using GL 3.3 may set that otherwise.
#endif
// Recreate the VAO every time
// (This is to easily allow multiple GL contexts. VAO are not shared among GL contexts, and we don't track creation/deletion of windows so we don't have an obvious key to use to cache them.)
GLuint vao_handle = 0;
glGenVertexArrays(1, &vao_handle);
glBindVertexArray(vao_handle);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo_handle);
glEnableVertexAttribArray(m_attrib_location_position);
glEnableVertexAttribArray(m_attrib_location_uv);
glEnableVertexAttribArray(m_attrib_location_color);
glVertexAttribPointer(m_attrib_location_position, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, pos));
glVertexAttribPointer(m_attrib_location_uv, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, uv));
glVertexAttribPointer(m_attrib_location_color, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, col));
// Draw
ImVec2 pos = draw_data->DisplayPos;
for (int n = 0; n < draw_data->CmdListsCount; n++)
{
const ImDrawList* cmd_list = draw_data->CmdLists[n];
const ImDrawIdx* idx_buffer_offset = 0;
glBindBuffer(GL_ARRAY_BUFFER, m_vbo_handle);
glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)cmd_list->VtxBuffer.Size * sizeof(ImDrawVert), (const GLvoid*)cmd_list->VtxBuffer.Data, GL_STREAM_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_elements_handle);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, (GLsizeiptr)cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx), (const GLvoid*)cmd_list->IdxBuffer.Data, GL_STREAM_DRAW);
for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
{
const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
if (pcmd->UserCallback)
{
// User callback (registered via ImDrawList::AddCallback)
pcmd->UserCallback(cmd_list, pcmd);
}
else
{
ImVec4 clip_rect = ImVec4(pcmd->ClipRect.x - pos.x, pcmd->ClipRect.y - pos.y, pcmd->ClipRect.z - pos.x, pcmd->ClipRect.w - pos.y);
if (clip_rect.x < fb_width && clip_rect.y < fb_height && clip_rect.z >= 0.0f && clip_rect.w >= 0.0f)
{
// Apply scissor/clipping rectangle
glScissor((int)clip_rect.x, (int)(fb_height - clip_rect.w), (int)(clip_rect.z - clip_rect.x), (int)(clip_rect.w - clip_rect.y));
// Bind texture, Draw
glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->TextureId);
glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer_offset);
}
}
idx_buffer_offset += pcmd->ElemCount;
}
}
glDeleteVertexArrays(1, &vao_handle);
// Restore modified GL state
glUseProgram(last_program);
glBindTexture(GL_TEXTURE_2D, last_texture);
#ifdef GL_SAMPLER_BINDING
glBindSampler(0, last_sampler);
#endif
glActiveTexture(last_active_texture);
glBindVertexArray(last_vertex_array);
glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer);
glBlendEquationSeparate(last_blend_equation_rgb, last_blend_equation_alpha);
glBlendFuncSeparate(last_blend_src_rgb, last_blend_dst_rgb, last_blend_src_alpha, last_blend_dst_alpha);
if (last_enable_blend) glEnable(GL_BLEND); else glDisable(GL_BLEND);
if (last_enable_cull_face) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE);
if (last_enable_depth_test) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
if (last_enable_scissor_test) glEnable(GL_SCISSOR_TEST); else glDisable(GL_SCISSOR_TEST);
#ifdef GL_POLYGON_MODE
glPolygonMode(GL_FRONT_AND_BACK, (GLenum)last_polygon_mode[0]);
#endif
glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]);
glScissor(last_scissor_box[0], last_scissor_box[1], (GLsizei)last_scissor_box[2], (GLsizei)last_scissor_box[3]);
}
void ImGuiWrapper::destroy_device_objects()
{
if (m_vbo_handle != 0)
{
glDeleteBuffers(1, &m_vbo_handle);
m_vbo_handle = 0;
}
if (m_elements_handle != 0)
{
glDeleteBuffers(1, &m_elements_handle);
m_elements_handle = 0;
}
if ((m_shader_handle != 0) && (m_vert_handle != 0))
glDetachShader(m_shader_handle, m_vert_handle);
if (m_vert_handle != 0)
{
glDeleteShader(m_vert_handle);
m_vert_handle = 0;
}
if ((m_shader_handle != 0) && (m_frag_handle != 0))
glDetachShader(m_shader_handle, m_frag_handle);
if (m_frag_handle != 0)
{
glDeleteShader(m_frag_handle);
m_frag_handle = 0;
}
if (m_shader_handle != 0)
{
glDeleteProgram(m_shader_handle);
m_shader_handle = 0;
}
destroy_fonts_texture();
}
void ImGuiWrapper::destroy_fonts_texture()
{
if (m_font_texture)
{
ImGuiIO& io = ImGui::GetIO();
io.Fonts->TexID = 0;
glDeleteTextures(1, &m_font_texture);
m_font_texture = 0;
}
}
} // namespace GUI
} // namespace Slic3r

View file

@ -0,0 +1,68 @@
#ifndef slic3r_ImGuiWrapper_hpp_
#define slic3r_ImGuiWrapper_hpp_
#include <imgui\imgui.h>
#include <string>
#include <map>
class wxMouseEvent;
namespace Slic3r {
namespace GUI {
class ImGuiWrapper
{
std::string m_glsl_version_string;
unsigned int m_shader_handle;
unsigned int m_vert_handle;
unsigned int m_frag_handle;
unsigned int m_vbo_handle;
unsigned int m_elements_handle;
int m_attrib_location_tex;
int m_attrib_location_proj_mtx;
int m_attrib_location_position;
int m_attrib_location_uv;
int m_attrib_location_color;
typedef std::map<std::string, ImFont*> FontsMap;
FontsMap m_fonts;
unsigned int m_font_texture;
public:
ImGuiWrapper();
bool init();
void shutdown();
void set_display_size(float w, float h);
void update_mouse_data(wxMouseEvent& evt);
void new_frame();
void render();
void set_next_window_pos(float x, float y, int flag);
void set_next_window_bg_alpha(float alpha);
bool begin(const std::string& name, int flags = 0);
void end();
bool input_double(const std::string& label, double& value, const std::string& format = "%.3f");
bool input_vec3(const std::string& label, Vec3d& value, float width, const std::string& format = "%.3f");
private:
void create_device_objects();
void create_fonts_texture();
bool check_program(unsigned int handle, const char* desc);
bool check_shader(unsigned int handle, const char* desc);
void render_draw_data(ImDrawData* draw_data);
void destroy_device_objects();
void destroy_fonts_texture();
};
} // namespace GUI
} // namespace Slic3r
#endif // slic3r_ImGuiWrapper_hpp_