Merge branch 'tm_opencsg' into lm_tm_hollowing

This commit is contained in:
tamasmeszaros 2020-01-08 08:58:21 +01:00
commit 6b482be74e
7 changed files with 439 additions and 207 deletions

View File

@ -2,7 +2,10 @@ cmake_minimum_required(VERSION 3.0)
project(OpenCSG-example) project(OpenCSG-example)
add_executable(opencsg_example WIN32 main.cpp Engine.hpp Engine.cpp add_executable(opencsg_example WIN32
main.cpp
Engine.hpp Engine.cpp
ShaderCSGDisplay.hpp ShaderCSGDisplay.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../../src/slic3r/GUI/ProgressStatusBar.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../src/slic3r/GUI/ProgressStatusBar.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../../src/slic3r/GUI/I18N.hpp ${CMAKE_CURRENT_SOURCE_DIR}/../../src/slic3r/GUI/I18N.hpp
${CMAKE_CURRENT_SOURCE_DIR}/../../src/slic3r/GUI/I18N.cpp) ${CMAKE_CURRENT_SOURCE_DIR}/../../src/slic3r/GUI/I18N.cpp)

View File

@ -1,7 +1,6 @@
#include "Engine.hpp" #include "Engine.hpp"
#include <libslic3r/Utils.hpp> #include <libslic3r/Utils.hpp>
#include <libslic3r/SLAPrint.hpp> #include <libslic3r/SLAPrint.hpp>
#include <libslic3r/MTUtils.hpp>
#include <GL/glew.h> #include <GL/glew.h>
@ -66,22 +65,6 @@ void CSGDisplay::render_scene()
glFlush(); glFlush();
} }
template<class It,
class Trafo,
class GetPt,
class V = typename std::iterator_traits<It>::value_type>
std::vector<V> transform_pts(
It from, It to, Trafo &&tr, GetPt &&point)
{
auto ret = reserve_vector<V>(to - from);
for(auto it = from; it != to; ++it) {
V v = *it;
v.pos = tr * point(*it);
ret.emplace_back(std::move(v));
}
return ret;
}
void Scene::set_print(uqptr<SLAPrint> &&print) void Scene::set_print(uqptr<SLAPrint> &&print)
{ {
m_print = std::move(print); m_print = std::move(print);
@ -287,7 +270,7 @@ void IndexedVertexArray::shrink_to_fit() {
this->quad_indices.shrink_to_fit(); this->quad_indices.shrink_to_fit();
} }
void Primitive::render() void Volume::render()
{ {
glsafe(::glPushMatrix()); glsafe(::glPushMatrix());
glsafe(::glMultMatrixd(m_trafo.get_matrix().data())); glsafe(::glMultMatrixd(m_trafo.get_matrix().data()));

View File

@ -1,5 +1,5 @@
#ifndef SLIC3R_OCSG_EXMP_ENGINE_HPP #ifndef SLIC3R_OCSG_EXMP_ENGINE_HPP
#define SLIC3R_OCSG_EXMP_ENGINE_HPP_HPP #define SLIC3R_OCSG_EXMP_ENGINE_HPP
#include <vector> #include <vector>
#include <memory> #include <memory>
@ -25,7 +25,7 @@ template<class T> using wkptr = std::weak_ptr<T>;
template<class T, class A = std::allocator<T>> using vector = std::vector<T, A>; template<class T, class A = std::allocator<T>> using vector = std::vector<T, A>;
// remove empty weak pointers from a vector // remove empty weak pointers from a vector
template<class L> void cleanup(vector<std::weak_ptr<L>> &listeners) { template<class L> inline void cleanup(vector<std::weak_ptr<L>> &listeners) {
auto it = std::remove_if(listeners.begin(), listeners.end(), auto it = std::remove_if(listeners.begin(), listeners.end(),
[](auto &l) { return !l.lock(); }); [](auto &l) { return !l.lock(); });
listeners.erase(it, listeners.end()); listeners.erase(it, listeners.end());
@ -34,7 +34,7 @@ template<class L> void cleanup(vector<std::weak_ptr<L>> &listeners) {
// Call a class method on each element of a vector of objects (weak pointers) // Call a class method on each element of a vector of objects (weak pointers)
// of the same type. // of the same type.
template<class F, class L, class...Args> template<class F, class L, class...Args>
void call(F &&f, vector<std::weak_ptr<L>> &listeners, Args&&... args) { inline void call(F &&f, vector<std::weak_ptr<L>> &listeners, Args&&... args) {
for (auto &l : listeners) for (auto &l : listeners)
if (auto p = l.lock()) ((p.get())->*f)(std::forward<Args>(args)...); if (auto p = l.lock()) ((p.get())->*f)(std::forward<Args>(args)...);
} }
@ -171,19 +171,30 @@ public:
// Try to enable or disable multisampling. // Try to enable or disable multisampling.
bool enable_multisampling(bool e = true); bool enable_multisampling(bool e = true);
// A primitive that can be used with OpenCSG rendering algorithms. template<class It,
// Does a similar job to GLVolume. class Trafo,
class Primitive : public OpenCSG::Primitive class GetPt,
class V = typename std::iterator_traits<It>::value_type>
inline std::vector<V> transform_pts(
It from, It to, Trafo &&tr, GetPt &&point)
{ {
vector<V> ret;
ret.reserve(to - from);
for(auto it = from; it != to; ++it) {
V v = *it;
v.pos = tr * point(*it);
ret.emplace_back(std::move(v));
}
return ret;
}
class Volume {
IndexedVertexArray m_geom; IndexedVertexArray m_geom;
Geometry::Transformation m_trafo; Geometry::Transformation m_trafo;
public: public:
using OpenCSG::Primitive::Primitive; void render();
Primitive() : OpenCSG::Primitive(OpenCSG::Intersection, 1) {}
void render() override;
void translation(const Vec3d &offset) { m_trafo.set_offset(offset); } void translation(const Vec3d &offset) { m_trafo.set_offset(offset); }
void rotation(const Vec3d &rot) { m_trafo.set_rotation(rot); } void rotation(const Vec3d &rot) { m_trafo.set_rotation(rot); }
@ -197,6 +208,18 @@ public:
} }
}; };
// A primitive that can be used with OpenCSG rendering algorithms.
// Does a similar job to GLVolume.
class Primitive : public Volume, public OpenCSG::Primitive
{
public:
using OpenCSG::Primitive::Primitive;
Primitive() : OpenCSG::Primitive(OpenCSG::Intersection, 1) {}
void render() override { Volume::render(); }
};
// A simple representation of a camera in a 3D scene // A simple representation of a camera in a 3D scene
class Camera { class Camera {
protected: protected:
@ -286,13 +309,25 @@ private:
public: public:
int get_algo() const { return int(m_csgalg); } int get_algo() const { return int(m_csgalg); }
void set_algo(OpenCSG::Algorithm alg) { m_csgalg = alg; } void set_algo(int alg)
{
if (alg < OpenCSG::Algorithm::AlgorithmUnused)
m_csgalg = OpenCSG::Algorithm(alg);
}
int get_depth_algo() const { return int(m_depth_algo); } int get_depth_algo() const { return int(m_depth_algo); }
void set_depth_algo(OpenCSG::DepthComplexityAlgorithm alg) { m_depth_algo = alg; } void set_depth_algo(int alg)
{
if (alg < OpenCSG::DepthComplexityAlgorithmUnused)
m_depth_algo = OpenCSG::DepthComplexityAlgorithm(alg);
}
int get_optimization() const { return int(m_optim); } int get_optimization() const { return int(m_optim); }
void set_optimization(OpenCSG::Optimization o) { m_optim = o; } void set_optimization(int o)
{
if (o < OpenCSG::Optimization::OptimizationUnused)
m_optim = OpenCSG::Optimization(o);
}
void enable_csg(bool en = true) { m_enable = en; } void enable_csg(bool en = true) { m_enable = en; }
bool is_enabled() const { return m_enable; } bool is_enabled() const { return m_enable; }
@ -359,7 +394,9 @@ public:
~Display() override; ~Display() override;
Camera * camera() { return m_camera.get(); } shptr<const Camera> get_camera() const { return m_camera; }
shptr<Camera> get_camera() { return m_camera; }
void set_camera(shptr<Camera> cam) { m_camera = cam; }
virtual void swap_buffers() = 0; virtual void swap_buffers() = 0;
virtual void set_active(long width, long height); virtual void set_active(long width, long height);
@ -433,8 +470,8 @@ class Controller : public std::enable_shared_from_this<Controller>,
template<class F, class...Args> template<class F, class...Args>
void call_cameras(F &&f, Args&&... args) { void call_cameras(F &&f, Args&&... args) {
for (wkptr<Display> &l : m_displays) for (wkptr<Display> &l : m_displays)
if (auto disp = l.lock()) if (disp->camera()) if (auto disp = l.lock()) if (auto cam = disp->get_camera())
(disp->camera()->*f)(std::forward<Args>(args)...); (cam.get()->*f)(std::forward<Args>(args)...);
} }
public: public:
@ -454,6 +491,8 @@ public:
cleanup(m_displays); cleanup(m_displays);
} }
void remove_displays() { m_displays = {}; }
void on_scene_updated(const Scene &scene) override; void on_scene_updated(const Scene &scene) override;
void on_left_click_down() override { m_left_btn = true; } void on_left_click_down() override { m_left_btn = true; }

View File

@ -0,0 +1,81 @@
#include "ShaderCSGDisplay.hpp"
#include "libslic3r/SLAPrint.hpp"
#include <GL/glew.h>
namespace Slic3r { namespace GL {
void ShaderCSGDisplay::add_mesh(const TriangleMesh &mesh)
{
auto v = std::make_shared<CSGVolume>();
v->load_mesh(mesh);
m_volumes.emplace_back(v);
}
void ShaderCSGDisplay::render_scene()
{
GLfloat color[] = {1.f, 1.f, 0.f, 0.f};
glColor4fv(color);
glDepthFunc(GL_LESS);
for (auto &v : m_volumes) v->render();
glFlush();
}
void ShaderCSGDisplay::on_scene_updated(const Scene &scene)
{
// TriangleMesh mesh = print->objects().front()->hollowed_interior_mesh();
// Look at CSGDisplay::on_scene_updated to see how its done there.
const SLAPrint *print = scene.get_print();
if (!print) return;
m_volumes.clear();
for (const SLAPrintObject *po : print->objects()) {
const ModelObject *mo = po->model_object();
TriangleMesh msh = mo->raw_mesh();
sla::DrainHoles holedata = mo->sla_drain_holes;
for (const ModelInstance *mi : mo->instances) {
TriangleMesh mshinst = msh;
auto interior = po->hollowed_interior_mesh();
interior.transform(po->trafo().inverse());
mshinst.merge(interior);
mshinst.require_shared_vertices();
mi->transform_mesh(&mshinst);
auto bb = mshinst.bounding_box();
auto center = bb.center().cast<float>();
mshinst.translate(-center);
mshinst.require_shared_vertices();
add_mesh(mshinst);
auto tr = Transform3f::Identity();
tr.translate(-center);
transform_pts(holedata.begin(), holedata.end(), tr,
[](const sla::DrainHole &dh) {
return dh.pos;
});
transform_pts(holedata.begin(), holedata.end(), tr,
[](const sla::DrainHole &dh) {
return dh.normal;
});
}
for (const sla::DrainHole &holept : holedata) {
TriangleMesh holemesh = sla::to_triangle_mesh(holept.to_mesh());
holemesh.require_shared_vertices();
add_mesh(holemesh);
}
}
repaint();
}
}} // namespace Slic3r::GL

View File

@ -0,0 +1,27 @@
#ifndef SHADERCSGDISPLAY_HPP
#define SHADERCSGDISPLAY_HPP
#include "Engine.hpp"
namespace Slic3r { namespace GL {
class CSGVolume: public Volume
{
// Extend...
};
class ShaderCSGDisplay: public Display {
protected:
vector<shptr<CSGVolume>> m_volumes;
void add_mesh(const TriangleMesh &mesh);
public:
void render_scene() override;
void on_scene_updated(const Scene &scene) override;
};
}}
#endif // SHADERCSGDISPLAY_HPP

View File

@ -3,6 +3,7 @@
#include <memory> #include <memory>
#include "Engine.hpp" #include "Engine.hpp"
#include "ShaderCSGDisplay.hpp"
#include <GL/glew.h> #include <GL/glew.h>
@ -30,46 +31,62 @@
using namespace Slic3r::GL; using namespace Slic3r::GL;
class Renderer {
protected:
wxGLCanvas *m_canvas;
shptr<wxGLContext> m_context;
public:
Renderer(wxGLCanvas *c): m_canvas{c} {
auto ctx = new wxGLContext(m_canvas);
if (!ctx || !ctx->IsOK()) {
wxMessageBox("Could not create OpenGL context.", "Error",
wxOK | wxICON_ERROR);
return;
}
m_context.reset(ctx);
}
wxGLContext * context() { return m_context.get(); }
const wxGLContext * context() const { return m_context.get(); }
};
// Tell the CSGDisplay how to swap buffers and set the gl context.
class OCSGRenderer: public Renderer, public Slic3r::GL::CSGDisplay {
public:
OCSGRenderer(wxGLCanvas *c): Renderer{c} {}
void set_active(long w, long h) override
{
m_canvas->SetCurrent(*m_context);
Slic3r::GL::Display::set_active(w, h);
}
void swap_buffers() override { m_canvas->SwapBuffers(); }
};
// Tell the CSGDisplay how to swap buffers and set the gl context.
class ShaderCSGRenderer : public Renderer, public Slic3r::GL::ShaderCSGDisplay {
public:
ShaderCSGRenderer(wxGLCanvas *c): Renderer{c} {}
void set_active(long w, long h) override
{
m_canvas->SetCurrent(*m_context);
Slic3r::GL::Display::set_active(w, h);
}
void swap_buffers() override { m_canvas->SwapBuffers(); }
};
// The opengl rendering facility. Here we implement the rendering objects. // The opengl rendering facility. Here we implement the rendering objects.
class Canvas: public wxGLCanvas class Canvas: public wxGLCanvas
{ {
// Tell the CSGDisplay how to swap buffers and set the gl context.
class OCSGRenderer: public Slic3r::GL::CSGDisplay {
Canvas *m_canvas;
shptr<wxGLContext> m_context;
public:
OCSGRenderer(Canvas *c): m_canvas{c} {
auto ctx = new wxGLContext(m_canvas);
if (!ctx || !ctx->IsOK()) {
wxMessageBox("Could not create OpenGL context.", "Error",
wxOK | wxICON_ERROR);
return;
}
m_context.reset(ctx);
}
void set_active(long w, long h) override
{
m_canvas->SetCurrent(*m_context);
Slic3r::GL::Display::set_active(w, h);
}
wxGLContext * context() { return m_context.get(); }
const wxGLContext * context() const { return m_context.get(); }
void swap_buffers() override { m_canvas->SwapBuffers(); }
~OCSGRenderer() override { m_scene_cache.clear(); }
};
// Create the OCSGDisplay for rendering with OpenCSG algorithms
shptr<OCSGRenderer> m_ocsgdisplay = std::make_shared<OCSGRenderer>(this);
// One display is active at a time, the OCSGRenderer by default. // One display is active at a time, the OCSGRenderer by default.
shptr<Slic3r::GL::Display> m_display = m_ocsgdisplay; shptr<Slic3r::GL::Display> m_display;
public: public:
@ -90,13 +107,18 @@ public:
const wxSize ClientSize = GetClientSize(); const wxSize ClientSize = GetClientSize();
m_display->set_screen_size(ClientSize.x, ClientSize.y); m_display->set_screen_size(ClientSize.x, ClientSize.y);
m_display->repaint();
});
Bind(wxEVT_SIZE, [this](wxSizeEvent &) {
const wxSize ClientSize = GetClientSize();
m_display->set_screen_size(ClientSize.x, ClientSize.y);
}); });
} }
shptr<Slic3r::GL::Display> get_display() const { return m_display; } shptr<Slic3r::GL::Display> get_display() const { return m_display; }
void set_display(shptr<Slic3r::GL::Display> d) { m_display = d; } void set_display(shptr<Slic3r::GL::Display> d) { m_display = d; }
shptr<Slic3r::GL::CSGDisplay> get_ocsg_display() const { return m_ocsgdisplay; }
}; };
// Enumerate possible mouse events, we will record them. // Enumerate possible mouse events, we will record them.
@ -183,20 +205,27 @@ public:
case MV: MouseInput::move_to(evt.a, evt.b); break; case MV: MouseInput::move_to(evt.a, evt.b); break;
} }
wxSafeYield(); wxTheApp->Yield();
if (!m_playing)
break;
} }
m_playing = false; m_playing = false;
} }
void stop() { m_playing = false; }
bool is_playing() const { return m_playing; }
}; };
// The top level frame of the application. // The top level frame of the application.
class MyFrame: public wxFrame class MyFrame: public wxFrame
{ {
// Instantiate the 3D engine. // Instantiate the 3D engine.
shptr<Scene> m_scene; // Model shptr<Scene> m_scene; // Model
shptr<Canvas> m_canvas; // View shptr<Canvas> m_canvas; // Views store
shptr<Controller> m_ctl; // Controller shptr<OCSGRenderer> m_ocsgdisplay; // View
shptr<ShaderCSGRenderer> m_shadercsg_display; // Another view
shptr<Controller> m_ctl; // Controller
// Add a status bar with progress indication. // Add a status bar with progress indication.
shptr<Slic3r::GUI::ProgressStatusBar> m_stbar; shptr<Slic3r::GUI::ProgressStatusBar> m_stbar;
@ -236,12 +265,30 @@ class MyFrame: public wxFrame
// To keep track of the running average of measured fps values. // To keep track of the running average of measured fps values.
double m_fps_avg = 0.; double m_fps_avg = 0.;
public: // We need the record button across methods
MyFrame(const wxString & title, wxToggleButton *m_record_btn;
const wxPoint & pos, wxComboBox * m_alg_select;
const wxSize & size, wxComboBox * m_depth_select;
const Slic3r::GL::CSGSettings &settings); wxComboBox * m_optimization_select;
wxSpinCtrl * m_convexity_spin;
wxToggleButton *m_csg_toggle;
wxToggleButton *m_ms_toggle;
wxStaticText *m_fpstext;
CSGSettings m_csg_settings;
void read_csg_settings(const wxCmdLineParser &parser);
void set_renderer_algorithm(const wxString &alg);
void activate_canvas_display();
public:
MyFrame(const wxString & title,
const wxPoint & pos,
const wxSize & size,
const wxCmdLineParser &parser);
// Grab a 3mf and load (hollow it out) within the UI job. // Grab a 3mf and load (hollow it out) within the UI job.
void load_model(const std::string &fname) { void load_model(const std::string &fname) {
m_ui_job = std::make_unique<SLAJob>(this, fname); m_ui_job = std::make_unique<SLAJob>(this, fname);
@ -258,11 +305,15 @@ public:
std::getline(stream, model_name); std::getline(stream, model_name);
load_model(model_name); load_model(model_name);
while (!m_ui_job->is_finalized())
wxTheApp->Yield();;
int w, h; int w, h;
stream >> w >> h; stream >> w >> h;
SetSize(w, h); SetSize(w, h);
m_mouse.load(stream); m_mouse.load(stream);
if (m_record_btn) m_record_btn->Disable();
m_mouse.play(); m_mouse.play();
} }
} }
@ -278,13 +329,19 @@ public:
// Possible OpenCSG configuration values. Will be used on the command line and // Possible OpenCSG configuration values. Will be used on the command line and
// on the UI widgets. // on the UI widgets.
static const std::vector<wxString> CSG_ALGS = {"Auto", "Goldfeather", "SCS"}; static const std::vector<wxString> CSG_ALGS = {"Auto", "Goldfeather", "SCS", "EnricoShader"};
static const std::vector<wxString> CSG_DEPTH = {"Off", "OcclusionQuery", "On"}; static const std::vector<wxString> CSG_DEPTH = {"Off", "OcclusionQuery", "On"};
static const std::vector<wxString> CSG_OPT = { "Default", "ForceOn", "On", "Off" }; static const std::vector<wxString> CSG_OPT = { "Default", "ForceOn", "On", "Off" };
inline long get_idx(const wxString &a, const std::vector<wxString> &v)
{
auto it = std::find(v.begin(), v.end(), a.ToStdString());
return it - v.begin();
};
class App : public wxApp { class App : public wxApp {
MyFrame *m_frame = nullptr; MyFrame *m_frame = nullptr;
wxString m_fname;
public: public:
bool OnInit() override { bool OnInit() override {
@ -299,60 +356,127 @@ public:
parser.Parse(); parser.Parse();
wxString fname; bool is_play = parser.Found("play", &m_fname);
bool is_play = parser.Found("play", &fname);
wxString alg;
parser.Found("algorithm", &alg);
wxString depth;
parser.Found("depth", &depth);
wxString opt;
parser.Found("optimization", &opt);
long convexity = 1;
parser.Found("convexity", &convexity);
bool csg_off = parser.Found("disable-csg");
auto get_idx = [](const wxString &a, const std::vector<wxString> &v) {
auto it = std::find(v.begin(), v.end(), a.ToStdString());
return it - v.begin();
};
Slic3r::GL::CSGSettings settings;
if (auto a = get_idx(alg, CSG_ALGS) < OpenCSG::AlgorithmUnused)
settings.set_algo(OpenCSG::Algorithm(a));
if (auto a = get_idx(depth, CSG_DEPTH) < OpenCSG::DepthComplexityAlgorithmUnused)
settings.set_depth_algo(OpenCSG::DepthComplexityAlgorithm(a));
if (auto a = get_idx(opt, CSG_OPT) < OpenCSG::OptimizationUnused)
settings.set_optimization(OpenCSG::Optimization(a));
settings.set_convexity(unsigned(convexity));
settings.enable_csg(!csg_off);
m_frame = new MyFrame("PrusaSlicer OpenCSG Demo", wxDefaultPosition, wxSize(1024, 768), settings);
m_frame = new MyFrame("PrusaSlicer OpenCSG Demo", wxDefaultPosition, wxSize(1024, 768), parser);
if (is_play) { if (is_play) {
Bind(wxEVT_IDLE, &App::Play, this);
m_frame->Show( true ); m_frame->Show( true );
m_frame->play_back_mouse(fname.ToStdString());
m_frame->Close( true );
std::cout << m_frame->get_fps_average() << std::endl;
} else m_frame->Show( true ); } else m_frame->Show( true );
return true; return true;
} }
void Play(wxIdleEvent &) {
Unbind(wxEVT_IDLE, &App::Play, this);
m_frame->play_back_mouse(m_fname.ToStdString());
m_frame->Destroy();
}
}; };
wxIMPLEMENT_APP(App); wxIMPLEMENT_APP(App);
void MyFrame::read_csg_settings(const wxCmdLineParser &parser)
{
wxString alg;
parser.Found("algorithm", &alg);
wxString depth;
parser.Found("depth", &depth);
wxString opt;
parser.Found("optimization", &opt);
long convexity = 1;
parser.Found("convexity", &convexity);
bool csg_off = parser.Found("disable-csg");
if (auto a = get_idx(alg, CSG_ALGS) < OpenCSG::AlgorithmUnused)
m_csg_settings.set_algo(OpenCSG::Algorithm(a));
if (auto a = get_idx(depth, CSG_DEPTH) < OpenCSG::DepthComplexityAlgorithmUnused)
m_csg_settings.set_depth_algo(OpenCSG::DepthComplexityAlgorithm(a));
if (auto a = get_idx(opt, CSG_OPT) < OpenCSG::OptimizationUnused)
m_csg_settings.set_optimization(OpenCSG::Optimization(a));
m_csg_settings.set_convexity(unsigned(convexity));
m_csg_settings.enable_csg(!csg_off);
if (m_ocsgdisplay) m_ocsgdisplay->apply_csgsettings(m_csg_settings);
}
void MyFrame::set_renderer_algorithm(const wxString &alg)
{
long alg_idx = get_idx("EnricoShader", CSG_ALGS);
if (alg_idx < 0 || alg_idx >= CSG_ALGS.size()) return;
// If there is a valid display in place, save its camera.
auto cam = m_canvas->get_display() ?
m_canvas->get_display()->get_camera() : nullptr;
if (alg == "EnricoShader") {
m_alg_select->SetSelection(int(alg_idx));
m_depth_select->Disable();
m_optimization_select->Disable();
m_csg_toggle->Disable();
m_ocsgdisplay.reset();
canvas()->set_display(nullptr);
m_shadercsg_display = std::make_shared<ShaderCSGRenderer>(canvas());
canvas()->set_display(m_shadercsg_display);
} else {
if (m_csg_settings.get_algo() > 0) m_depth_select->Enable(true);
m_alg_select->SetSelection(m_csg_settings.get_algo());
m_depth_select->SetSelection(m_csg_settings.get_depth_algo());
m_optimization_select->SetSelection(m_csg_settings.get_optimization());
m_convexity_spin->SetValue(int(m_csg_settings.get_convexity()));
m_csg_toggle->SetValue(m_csg_settings.is_enabled());
m_optimization_select->Enable();
m_csg_toggle->Enable();
m_shadercsg_display.reset();
canvas()->set_display(nullptr);
m_ocsgdisplay = std::make_shared<OCSGRenderer>(canvas());
m_ocsgdisplay->apply_csgsettings(m_csg_settings);
canvas()->set_display(m_ocsgdisplay);
}
if (cam)
m_canvas->get_display()->set_camera(cam);
m_ctl->remove_displays();
m_ctl->add_display(m_canvas->get_display());
m_canvas->get_display()->get_fps_counter().add_listener([this](double fps) {
m_fpstext->SetLabel(wxString::Format("fps: %.2f", fps));
m_fps_avg = 0.9 * m_fps_avg + 0.1 * fps;
});
if (IsShown()) {
activate_canvas_display();
m_canvas->get_display()->on_scene_updated(*m_scene);
}
}
void MyFrame::activate_canvas_display()
{
const wxSize ClientSize = m_canvas->GetClientSize();
m_canvas->get_display()->set_active(ClientSize.x, ClientSize.y);
enable_multisampling(m_ms_toggle->GetValue());
// Do the repaint continuously
m_canvas->Bind(wxEVT_IDLE, [this](wxIdleEvent &evt) {
m_canvas->get_display()->repaint();
evt.RequestMore();
});
bind_canvas_events(m_mouse);
}
MyFrame::MyFrame(const wxString &title, const wxPoint &pos, const wxSize &size, MyFrame::MyFrame(const wxString &title, const wxPoint &pos, const wxSize &size,
const Slic3r::GL::CSGSettings &settings): const wxCmdLineParser &parser):
wxFrame(nullptr, wxID_ANY, title, pos, size) wxFrame(nullptr, wxID_ANY, title, pos, size)
{ {
wxMenu *menuFile = new wxMenu; wxMenu *menuFile = new wxMenu;
@ -377,7 +501,7 @@ MyFrame::MyFrame(const wxString &title, const wxPoint &pos, const wxSize &size,
// glReadPixels would not return the alpha channel on NVIDIA if // glReadPixels would not return the alpha channel on NVIDIA if
// not requested when the GL context is created. // not requested when the GL context is created.
WX_GL_MIN_ALPHA, 8, WX_GL_DEPTH_SIZE, 8, WX_GL_STENCIL_SIZE, 8, WX_GL_MIN_ALPHA, 8, WX_GL_DEPTH_SIZE, 8, WX_GL_STENCIL_SIZE, 8,
/*WX_GL_SAMPLE_BUFFERS, GL_TRUE, WX_GL_SAMPLES, 4,*/ 0}; WX_GL_SAMPLE_BUFFERS, GL_TRUE, WX_GL_SAMPLES, 4, 0};
m_scene = std::make_shared<Scene>(); m_scene = std::make_shared<Scene>();
m_ctl = std::make_shared<Controller>(); m_ctl = std::make_shared<Controller>();
@ -387,9 +511,7 @@ MyFrame::MyFrame(const wxString &title, const wxPoint &pos, const wxSize &size,
wxDefaultPosition, wxDefaultSize, wxDefaultPosition, wxDefaultSize,
wxWANTS_CHARS | wxFULL_REPAINT_ON_RESIZE); wxWANTS_CHARS | wxFULL_REPAINT_ON_RESIZE);
m_canvas->get_ocsg_display()->apply_csgsettings(settings); read_csg_settings(parser);
m_ctl->add_display(m_canvas->get_display());
wxPanel *control_panel = new wxPanel(this); wxPanel *control_panel = new wxPanel(this);
@ -402,12 +524,12 @@ MyFrame::MyFrame(const wxString &title, const wxPoint &pos, const wxSize &size,
wxSL_VERTICAL); wxSL_VERTICAL);
slider_sizer->Add(slider, 1, wxEXPAND); slider_sizer->Add(slider, 1, wxEXPAND);
auto ms_toggle = new wxToggleButton(control_panel, wxID_ANY, "Multisampling"); m_ms_toggle = new wxToggleButton(control_panel, wxID_ANY, "Multisampling");
console_sizer->Add(ms_toggle, 0, wxALL | wxEXPAND, 5); console_sizer->Add(m_ms_toggle, 0, wxALL | wxEXPAND, 5);
auto csg_toggle = new wxToggleButton(control_panel, wxID_ANY, "CSG"); m_csg_toggle = new wxToggleButton(control_panel, wxID_ANY, "CSG");
csg_toggle->SetValue(true); m_csg_toggle->SetValue(true);
console_sizer->Add(csg_toggle, 0, wxALL | wxEXPAND, 5); console_sizer->Add(m_csg_toggle, 0, wxALL | wxEXPAND, 5);
auto add_combobox = [control_panel, console_sizer] auto add_combobox = [control_panel, console_sizer]
(const wxString &label, const std::vector<wxString> &list) (const wxString &label, const std::vector<wxString> &list)
@ -441,22 +563,17 @@ MyFrame::MyFrame(const wxString &title, const wxPoint &pos, const wxSize &size,
return widget; return widget;
}; };
auto convexity_spin = add_spinctl("Convexity", CSGSettings::DEFAULT_CONVEXITY, 0, 100); m_convexity_spin = add_spinctl("Convexity", CSGSettings::DEFAULT_CONVEXITY, 0, 100);
auto alg_select = add_combobox("Algorithm", CSG_ALGS); m_alg_select = add_combobox("Algorithm", CSG_ALGS);
auto depth_select = add_combobox("Depth Complexity", CSG_DEPTH); m_depth_select = add_combobox("Depth Complexity", CSG_DEPTH);
auto optimization_select = add_combobox("Optimization", CSG_OPT); m_optimization_select = add_combobox("Optimization", CSG_OPT);
depth_select->Disable();
auto fpstext = new wxStaticText(control_panel, wxID_ANY, ""); m_fpstext = new wxStaticText(control_panel, wxID_ANY, "");
console_sizer->Add(fpstext, 0, wxALL, 5); console_sizer->Add(m_fpstext, 0, wxALL, 5);
m_canvas->get_ocsg_display()->get_fps_counter().add_listener([this, fpstext](double fps) {
fpstext->SetLabel(wxString::Format("fps: %.2f", fps) );
m_fps_avg = 0.9 * m_fps_avg + 0.1 * fps;
});
auto record_btn = new wxToggleButton(control_panel, wxID_ANY, "Record"); m_record_btn = new wxToggleButton(control_panel, wxID_ANY, "Record");
console_sizer->Add(record_btn, 0, wxALL | wxEXPAND, 5); console_sizer->Add(m_record_btn, 0, wxALL | wxEXPAND, 5);
controlsizer->Add(slider_sizer, 0, wxEXPAND); controlsizer->Add(slider_sizer, 0, wxEXPAND);
controlsizer->Add(console_sizer, 1, wxEXPAND); controlsizer->Add(console_sizer, 1, wxEXPAND);
@ -468,17 +585,16 @@ MyFrame::MyFrame(const wxString &title, const wxPoint &pos, const wxSize &size,
sizer->Add(control_panel, 0, wxEXPAND); sizer->Add(control_panel, 0, wxEXPAND);
SetSizer(sizer); SetSizer(sizer);
if (settings.get_algo() > 0) depth_select->Enable(true); wxString alg;
alg_select->SetSelection(settings.get_algo()); if (!parser.Found("algorithm", &alg)) alg = "Auto";
depth_select->SetSelection(settings.get_depth_algo());
optimization_select->SetSelection(settings.get_optimization());
convexity_spin->SetValue(int(settings.get_convexity()));
csg_toggle->SetValue(settings.is_enabled());
Bind(wxEVT_CLOSE_WINDOW, [this](wxCloseEvent &){ set_renderer_algorithm(alg);
RemoveChild(m_canvas.get());
Bind(wxEVT_CLOSE_WINDOW, [this](wxCloseEvent &evt){
if (m_canvas) RemoveChild(m_canvas.get());
m_canvas.reset(); m_canvas.reset();
Destroy(); if (!m_mouse.is_playing()) evt.Skip();
else m_mouse.stop();
}); });
Bind(wxEVT_MENU, [this](wxCommandEvent &) { Bind(wxEVT_MENU, [this](wxCommandEvent &) {
@ -490,70 +606,60 @@ MyFrame::MyFrame(const wxString &title, const wxPoint &pos, const wxSize &size,
Bind(wxEVT_MENU, [this](wxCommandEvent &) { Close(true); }, wxID_EXIT); Bind(wxEVT_MENU, [this](wxCommandEvent &) { Close(true); }, wxID_EXIT);
Bind(wxEVT_SHOW, [this, ms_toggle](wxShowEvent &) { Bind(wxEVT_SHOW, [this](wxShowEvent &) {
const wxSize ClientSize = GetClientSize(); activate_canvas_display();
m_canvas->get_display()->set_active(ClientSize.x, ClientSize.y);
enable_multisampling(ms_toggle->GetValue());
}); });
Bind(wxEVT_SLIDER, [this, slider](wxCommandEvent &) { Bind(wxEVT_SLIDER, [this, slider](wxCommandEvent &) {
m_ctl->move_clip_plane(double(slider->GetValue())); m_ctl->move_clip_plane(double(slider->GetValue()));
}); });
ms_toggle->Bind(wxEVT_TOGGLEBUTTON, [this, ms_toggle](wxCommandEvent &){ m_ms_toggle->Bind(wxEVT_TOGGLEBUTTON, [this](wxCommandEvent &){
enable_multisampling(ms_toggle->GetValue()); enable_multisampling(m_ms_toggle->GetValue());
m_canvas->get_display()->repaint(); m_canvas->get_display()->repaint();
}); });
csg_toggle->Bind(wxEVT_TOGGLEBUTTON, [this, csg_toggle](wxCommandEvent &){ m_csg_toggle->Bind(wxEVT_TOGGLEBUTTON, [this](wxCommandEvent &){
CSGSettings stt = m_canvas->get_ocsg_display()->get_csgsettings(); CSGSettings stt = m_ocsgdisplay->get_csgsettings();
stt.enable_csg(csg_toggle->GetValue()); stt.enable_csg(m_csg_toggle->GetValue());
m_canvas->get_ocsg_display()->apply_csgsettings(stt); m_ocsgdisplay->apply_csgsettings(stt);
}); });
alg_select->Bind(wxEVT_COMBOBOX, m_alg_select->Bind(wxEVT_COMBOBOX, [this](wxCommandEvent &) {
[this, alg_select, depth_select](wxCommandEvent &) wxString alg = m_alg_select->GetValue();
{ int sel = m_alg_select->GetSelection();
int sel = alg_select->GetSelection(); m_csg_settings.set_algo(sel);
depth_select->Enable(sel > 0); set_renderer_algorithm(alg);
CSGSettings stt = m_canvas->get_ocsg_display()->get_csgsettings();
stt.set_algo(OpenCSG::Algorithm(sel));
m_canvas->get_ocsg_display()->apply_csgsettings(stt);
}); });
depth_select->Bind(wxEVT_COMBOBOX, [this, depth_select](wxCommandEvent &) { m_depth_select->Bind(wxEVT_COMBOBOX, [this](wxCommandEvent &) {
int sel = depth_select->GetSelection(); int sel = m_depth_select->GetSelection();
CSGSettings stt = m_canvas->get_ocsg_display()->get_csgsettings(); m_csg_settings.set_depth_algo(sel);
stt.set_depth_algo(OpenCSG::DepthComplexityAlgorithm(sel)); if (m_ocsgdisplay) m_ocsgdisplay->apply_csgsettings(m_csg_settings);
m_canvas->get_ocsg_display()->apply_csgsettings(stt);
}); });
optimization_select->Bind(wxEVT_COMBOBOX, m_optimization_select->Bind(wxEVT_COMBOBOX, [this](wxCommandEvent &) {
[this, optimization_select](wxCommandEvent &) { int sel = m_optimization_select->GetSelection();
int sel = optimization_select->GetSelection(); m_csg_settings.set_optimization(sel);
CSGSettings stt = m_canvas->get_ocsg_display()->get_csgsettings(); if (m_ocsgdisplay) m_ocsgdisplay->apply_csgsettings(m_csg_settings);
stt.set_optimization(OpenCSG::Optimization(sel));
m_canvas->get_ocsg_display()->apply_csgsettings(stt);
}); });
convexity_spin->Bind(wxEVT_SPINCTRL, [this, convexity_spin](wxSpinEvent &) { m_convexity_spin->Bind(wxEVT_SPINCTRL, [this](wxSpinEvent &) {
CSGSettings stt = m_canvas->get_ocsg_display()->get_csgsettings(); int c = m_convexity_spin->GetValue();
int c = convexity_spin->GetValue();
if (c > 0) { if (c > 0) {
stt.set_convexity(unsigned(c)); m_csg_settings.set_convexity(unsigned(c));
m_canvas->get_ocsg_display()->apply_csgsettings(stt); if (m_ocsgdisplay) m_ocsgdisplay->apply_csgsettings(m_csg_settings);
} }
}); });
record_btn->Bind(wxEVT_TOGGLEBUTTON, [this, record_btn](wxCommandEvent &) { m_record_btn->Bind(wxEVT_TOGGLEBUTTON, [this](wxCommandEvent &) {
if (!m_ui_job) { if (!m_ui_job) {
m_stbar->set_status_text("No project loaded!"); m_stbar->set_status_text("No project loaded!");
return; return;
} }
if (record_btn->GetValue()) { if (m_record_btn->GetValue()) {
if (auto c = m_canvas->get_display()->camera()) reset(*c); if (auto c = m_canvas->get_display()->get_camera()) reset(*c);
m_ctl->on_scene_updated(*m_scene); m_ctl->on_scene_updated(*m_scene);
m_mouse.record(true); m_mouse.record(true);
} else { } else {
@ -575,14 +681,6 @@ MyFrame::MyFrame(const wxString &title, const wxPoint &pos, const wxSize &size,
} }
} }
}); });
// Do the repaint continuously
m_canvas->Bind(wxEVT_IDLE, [this](wxIdleEvent &evt) {
if (m_canvas->IsShown()) m_canvas->get_display()->repaint();
evt.RequestMore();
});
bind_canvas_events(m_mouse);
} }
void MyFrame::bind_canvas_events(MouseInput &ms) void MyFrame::bind_canvas_events(MouseInput &ms)

View File

@ -62,7 +62,6 @@ protected:
// Launched when the job is finished. It refreshes the 3Dscene by def. // Launched when the job is finished. It refreshes the 3Dscene by def.
virtual void finalize() { m_finalized = true; } virtual void finalize() { m_finalized = true; }
bool is_finalized() const { return m_finalized; }
public: public:
Job(std::shared_ptr<ProgressIndicator> pri) : m_progress(pri) Job(std::shared_ptr<ProgressIndicator> pri) : m_progress(pri)
@ -89,6 +88,8 @@ public:
}); });
} }
bool is_finalized() const { return m_finalized; }
Job(const Job &) = delete; Job(const Job &) = delete;
Job(Job &&) = delete; Job(Job &&) = delete;
Job &operator=(const Job &) = delete; Job &operator=(const Job &) = delete;