diff options
author | Enrico Turri <enricoturri@seznam.cz> | 2018-06-04 13:26:39 +0300 |
---|---|---|
committer | Enrico Turri <enricoturri@seznam.cz> | 2018-06-04 13:26:39 +0300 |
commit | 95e7d96f522886002bfc515797a91ad9e9758c91 (patch) | |
tree | 97954e434a17fbb0d61a5a8c424d4d19e78ecacf /xs | |
parent | 109dde00b24b349addbfdfbf0599372d74b7dc38 (diff) |
3DScene paint event handler moved to c++
Diffstat (limited to 'xs')
-rw-r--r-- | xs/src/slic3r/GUI/GLCanvas3D.cpp | 106 | ||||
-rw-r--r-- | xs/src/slic3r/GUI/GLCanvas3D.hpp | 14 | ||||
-rw-r--r-- | xs/src/slic3r/GUI/GLCanvas3DManager.cpp | 16 |
3 files changed, 98 insertions, 38 deletions
diff --git a/xs/src/slic3r/GUI/GLCanvas3D.cpp b/xs/src/slic3r/GUI/GLCanvas3D.cpp index 50913ae3a..008324a62 100644 --- a/xs/src/slic3r/GUI/GLCanvas3D.cpp +++ b/xs/src/slic3r/GUI/GLCanvas3D.cpp @@ -185,7 +185,7 @@ GLCanvas3D::Camera::Camera() : type(Ortho) , zoom(1.0f) , phi(45.0f) - , distance(0.0f) +// , distance(0.0f) , target(0.0, 0.0, 0.0) , m_theta(45.0f) { @@ -198,8 +198,8 @@ std::string GLCanvas3D::Camera::get_type_as_string() const default: case Unknown: return "unknown"; - case Perspective: - return "perspective"; +// case Perspective: +// return "perspective"; case Ortho: return "ortho"; }; @@ -942,6 +942,8 @@ GLCanvas3D::GLCanvas3D(wxGLCanvas* canvas, wxGLContext* context) , m_config(nullptr) , m_print(nullptr) , m_dirty(true) + , m_use_VBOs(false) + , m_late_init(false) , m_apply_zoom_to_volumes_filter(false) , m_hover_volume_id(-1) , m_warning_texture_enabled(false) @@ -968,6 +970,9 @@ GLCanvas3D::~GLCanvas3D() bool GLCanvas3D::init(bool useVBOs, bool use_legacy_opengl) { + if (!set_current()) + return false; + ::glClearColor(1.0f, 1.0f, 1.0f, 1.0f); ::glClearDepth(1.0f); @@ -990,16 +995,12 @@ bool GLCanvas3D::init(bool useVBOs, bool use_legacy_opengl) ::glEnable(GL_LIGHT1); // light from camera - GLfloat position[4] = { 1.0f, 0.0f, 1.0f, 0.0f }; - ::glLightfv(GL_LIGHT1, GL_POSITION, position); GLfloat specular[4] = { 0.3f, 0.3f, 0.3f, 1.0f }; ::glLightfv(GL_LIGHT1, GL_SPECULAR, specular); GLfloat diffuse[4] = { 0.2f, 0.2f, 0.2f, 1.0f }; ::glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse); // light from above - GLfloat position1[4] = { -0.5f, -0.5f, 1.0f, 0.0f }; - ::glLightfv(GL_LIGHT0, GL_POSITION, position1); GLfloat specular1[4] = { 0.2f, 0.2f, 0.2f, 1.0f }; ::glLightfv(GL_LIGHT0, GL_SPECULAR, specular1); GLfloat diffuse1[4] = { 0.5f, 0.5f, 0.5f, 1.0f }; @@ -1021,6 +1022,7 @@ bool GLCanvas3D::init(bool useVBOs, bool use_legacy_opengl) if (useVBOs && !m_layers_editing.init("variable_layer_height.vs", "variable_layer_height.fs")) return false; + m_use_VBOs = useVBOs; m_layers_editing.set_use_legacy_opengl(!use_legacy_opengl); return true; @@ -1251,17 +1253,32 @@ void GLCanvas3D::update_volumes_colors_by_extruder() m_volumes->update_colors_by_extruder(m_config); } -void GLCanvas3D::render(bool useVBOs) const +void GLCanvas3D::render() { if (m_canvas == nullptr) return; + if (!is_shown_on_screen()) + return; + + if (!set_current()) + return; + + if (!m_late_init) + _late_init(); + _camera_tranform(); + + GLfloat position[4] = { 1.0f, 0.0f, 1.0f, 0.0f }; + ::glLightfv(GL_LIGHT1, GL_POSITION, position); + GLfloat position1[4] = { -0.5f, -0.5f, 1.0f, 0.0f }; + ::glLightfv(GL_LIGHT0, GL_POSITION, position1); + _picking_pass(); _render_background(); _render_bed(); _render_axes(); - _render_objects(useVBOs); + _render_objects(); _render_cutting_plane(); _render_warning_texture(); _render_legend_texture(); @@ -1677,6 +1694,11 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt) evt.Skip(); } +void GLCanvas3D::on_paint(wxPaintEvent& evt) +{ + render(); +} + Size GLCanvas3D::get_canvas_size() const { int w = 0; @@ -1697,6 +1719,24 @@ Point GLCanvas3D::get_local_mouse_position() const return Point(mouse_pos.x, mouse_pos.y); } +void GLCanvas3D::_late_init() +{ + // This is a special path for wxWidgets on GTK, where an OpenGL context is initialized + // first when an OpenGL widget is shown for the first time.How ugly. + // In that case the volumes are wainting to be moved to Vertex Buffer Objects + // after the OpenGL context is being initialized. +#if defined(__LINUX__) + if (() && m_use_VBOs && (m_volumes != nullptr) + { + m_volumes->finalize_geometry(m_use_VBOs); + if ($^O eq 'linux' && $self->UseVBOs); + } +#endif // __LINUX__ + + zoom_to_bed(); + m_late_init = true; +} + void GLCanvas3D::_resize(unsigned int w, unsigned int h) { if (m_context == nullptr) @@ -1730,28 +1770,28 @@ void GLCanvas3D::_resize(unsigned int w, unsigned int h) break; } - case Camera::Perspective: - { - float bbox_r = (float)bbox.radius(); - float fov = PI * 45.0f / 180.0f; - float fov_tan = tan(0.5f * fov); - float cam_distance = 0.5f * bbox_r / fov_tan; - m_camera.distance = cam_distance; - - float nr = cam_distance - bbox_r * 1.1f; - float fr = cam_distance + bbox_r * 1.1f; - if (nr < 1.0f) - nr = 1.0f; - - if (fr < nr + 1.0f) - fr = nr + 1.0f; - - float h2 = fov_tan * nr; - float w2 = h2 * w / h; - ::glFrustum(-w2, w2, -h2, h2, nr, fr); - - break; - } +// case Camera::Perspective: +// { +// float bbox_r = (float)bbox.radius(); +// float fov = PI * 45.0f / 180.0f; +// float fov_tan = tan(0.5f * fov); +// float cam_distance = 0.5f * bbox_r / fov_tan; +// m_camera.distance = cam_distance; +// +// float nr = cam_distance - bbox_r * 1.1f; +// float fr = cam_distance + bbox_r * 1.1f; +// if (nr < 1.0f) +// nr = 1.0f; +// +// if (fr < nr + 1.0f) +// fr = nr + 1.0f; +// +// float h2 = fov_tan * nr; +// float w2 = h2 * w / h; +// ::glFrustum(-w2, w2, -h2, h2, nr, fr); +// +// break; +// } default: { throw std::runtime_error("Invalid camera type."); @@ -2008,7 +2048,7 @@ void GLCanvas3D::_render_axes() const m_axes.render(); } -void GLCanvas3D::_render_objects(bool useVBOs) const +void GLCanvas3D::_render_objects() const { if ((m_volumes == nullptr) || m_volumes->empty()) return; @@ -2017,7 +2057,7 @@ void GLCanvas3D::_render_objects(bool useVBOs) const if (!m_shader_enabled) _render_volumes(false); - else if (useVBOs) + else if (m_use_VBOs) { if (m_picking_enabled) { diff --git a/xs/src/slic3r/GUI/GLCanvas3D.hpp b/xs/src/slic3r/GUI/GLCanvas3D.hpp index 1caf69c0c..8e2cc1d0a 100644 --- a/xs/src/slic3r/GUI/GLCanvas3D.hpp +++ b/xs/src/slic3r/GUI/GLCanvas3D.hpp @@ -13,6 +13,7 @@ class wxIdleEvent; class wxKeyEvent; class wxMouseEvent; class wxTimerEvent; +class wxPaintEvent; namespace Slic3r { @@ -86,7 +87,7 @@ public: enum EType : unsigned char { Unknown, - Perspective, +// Perspective, Ortho, Num_types }; @@ -94,7 +95,7 @@ public: EType type; float zoom; float phi; - float distance; +// float distance; Pointf3 target; private: @@ -301,6 +302,8 @@ private: Print* m_print; bool m_dirty; + bool m_use_VBOs; + bool m_late_init; bool m_apply_zoom_to_volumes_filter; mutable int m_hover_volume_id; bool m_warning_texture_enabled; @@ -369,7 +372,7 @@ public: void update_volumes_colors_by_extruder(); - void render(bool useVBOs) const; + void render(); void render_texture(unsigned int tex_id, float left, float right, float bottom, float top) const; void register_on_viewport_changed_callback(void* callback); @@ -385,11 +388,14 @@ public: void on_mouse_wheel(wxMouseEvent& evt); void on_timer(wxTimerEvent& evt); void on_mouse(wxMouseEvent& evt); + void on_paint(wxPaintEvent& evt); Size get_canvas_size() const; Point get_local_mouse_position() const; private: + void _late_init(); + void _resize(unsigned int w, unsigned int h); BoundingBoxf3 _max_bounding_box() const; @@ -407,7 +413,7 @@ private: void _render_background() const; void _render_bed() const; void _render_axes() const; - void _render_objects(bool useVBOs) const; + void _render_objects() const; void _render_cutting_plane() const; void _render_warning_texture() const; void _render_legend_texture() const; diff --git a/xs/src/slic3r/GUI/GLCanvas3DManager.cpp b/xs/src/slic3r/GUI/GLCanvas3DManager.cpp index 10f825201..4a35d61fd 100644 --- a/xs/src/slic3r/GUI/GLCanvas3DManager.cpp +++ b/xs/src/slic3r/GUI/GLCanvas3DManager.cpp @@ -138,6 +138,19 @@ bool GLCanvas3DManager::add(wxGLCanvas* canvas, wxGLContext* context) if (canvas3D == nullptr) return false; + if (!m_gl_initialized) + { + canvas3D->set_current(); + init_gl(); + } + + if (!canvas3D->init(m_use_VBOs, m_use_legacy_opengl)) + { + delete canvas3D; + canvas3D = nullptr; + return false; + } + canvas->Bind(wxEVT_SIZE, [canvas3D](wxSizeEvent& evt) { canvas3D->on_size(evt); }); canvas->Bind(wxEVT_IDLE, [canvas3D](wxIdleEvent& evt) { canvas3D->on_idle(evt); }); canvas->Bind(wxEVT_CHAR, [canvas3D](wxKeyEvent& evt) { canvas3D->on_char(evt); }); @@ -155,6 +168,7 @@ bool GLCanvas3DManager::add(wxGLCanvas* canvas, wxGLContext* context) canvas->Bind(wxEVT_LEFT_DCLICK, [canvas3D](wxMouseEvent& evt) { canvas3D->on_mouse(evt); }); canvas->Bind(wxEVT_MIDDLE_DCLICK, [canvas3D](wxMouseEvent& evt) { canvas3D->on_mouse(evt); }); canvas->Bind(wxEVT_RIGHT_DCLICK, [canvas3D](wxMouseEvent& evt) { canvas3D->on_mouse(evt); }); + canvas->Bind(wxEVT_PAINT, [canvas3D](wxPaintEvent& evt) { canvas3D->on_paint(evt); }); m_canvases.insert(CanvasesMap::value_type(canvas, canvas3D)); @@ -417,7 +431,7 @@ void GLCanvas3DManager::render(wxGLCanvas* canvas) const { CanvasesMap::const_iterator it = _get_canvas(canvas); if (it != m_canvases.end()) - it->second->render(m_use_VBOs); + it->second->render(); } void GLCanvas3DManager::register_on_viewport_changed_callback(wxGLCanvas* canvas, void* callback) |