main.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. #include <iostream>
  2. #include <utility>
  3. #include <memory>
  4. #include "Engine.hpp"
  5. #include "ShaderCSGDisplay.hpp"
  6. #include <GL/glew.h>
  7. #include <opencsg/opencsg.h>
  8. // For compilers that support precompilation, includes "wx/wx.h".
  9. #include <wx/wxprec.h>
  10. #ifndef WX_PRECOMP
  11. #include <wx/wx.h>
  12. #endif
  13. #include <wx/slider.h>
  14. #include <wx/tglbtn.h>
  15. #include <wx/combobox.h>
  16. #include <wx/spinctrl.h>
  17. #include <wx/msgdlg.h>
  18. #include <wx/glcanvas.h>
  19. #include <wx/cmdline.h>
  20. #include "libslic3r/Model.hpp"
  21. #include "libslic3r/Format/3mf.hpp"
  22. #include "libslic3r/SLAPrint.hpp"
  23. #include "slic3r/GUI/Jobs/Job.hpp"
  24. #include "slic3r/GUI/ProgressStatusBar.hpp"
  25. using namespace Slic3r::GL;
  26. class Renderer {
  27. protected:
  28. wxGLCanvas *m_canvas;
  29. std::shared_ptr<wxGLContext> m_context;
  30. public:
  31. Renderer(wxGLCanvas *c): m_canvas{c} {
  32. auto ctx = new wxGLContext(m_canvas);
  33. if (!ctx || !ctx->IsOK()) {
  34. wxMessageBox("Could not create OpenGL context.", "Error",
  35. wxOK | wxICON_ERROR);
  36. return;
  37. }
  38. m_context.reset(ctx);
  39. }
  40. wxGLContext * context() { return m_context.get(); }
  41. const wxGLContext * context() const { return m_context.get(); }
  42. };
  43. // Tell the CSGDisplay how to swap buffers and set the gl context.
  44. class OCSGRenderer: public Renderer, public Slic3r::GL::CSGDisplay {
  45. public:
  46. OCSGRenderer(wxGLCanvas *c): Renderer{c} {}
  47. void set_active(long w, long h) override
  48. {
  49. m_canvas->SetCurrent(*m_context);
  50. Slic3r::GL::Display::set_active(w, h);
  51. }
  52. void swap_buffers() override { m_canvas->SwapBuffers(); }
  53. };
  54. // Tell the CSGDisplay how to swap buffers and set the gl context.
  55. class ShaderCSGRenderer : public Renderer, public Slic3r::GL::ShaderCSGDisplay {
  56. public:
  57. ShaderCSGRenderer(wxGLCanvas *c): Renderer{c} {}
  58. void set_active(long w, long h) override
  59. {
  60. m_canvas->SetCurrent(*m_context);
  61. Slic3r::GL::Display::set_active(w, h);
  62. }
  63. void swap_buffers() override { m_canvas->SwapBuffers(); }
  64. };
  65. // The opengl rendering facility. Here we implement the rendering objects.
  66. class Canvas: public wxGLCanvas
  67. {
  68. // One display is active at a time, the OCSGRenderer by default.
  69. std::shared_ptr<Slic3r::GL::Display> m_display;
  70. public:
  71. template<class...Args>
  72. Canvas(Args &&...args): wxGLCanvas(std::forward<Args>(args)...) {}
  73. std::shared_ptr<Slic3r::GL::Display> get_display() const { return m_display; }
  74. void set_display(std::shared_ptr<Slic3r::GL::Display> d) { m_display = d; }
  75. };
  76. // Enumerate possible mouse events, we will record them.
  77. enum EEvents { LCLK_U, RCLK_U, LCLK_D, RCLK_D, DDCLK, SCRL, MV };
  78. struct Event
  79. {
  80. EEvents type;
  81. long a, b;
  82. Event(EEvents t, long x = 0, long y = 0) : type{t}, a{x}, b{y} {}
  83. };
  84. // Create a special mouse input adapter, which can store (record) the received
  85. // mouse signals into a file and play back the stored events later.
  86. class RecorderMouseInput: public MouseInput {
  87. std::vector<Event> m_events;
  88. bool m_recording = false, m_playing = false;
  89. public:
  90. void left_click_down() override
  91. {
  92. if (m_recording) m_events.emplace_back(LCLK_D);
  93. if (!m_playing) MouseInput::left_click_down();
  94. }
  95. void left_click_up() override
  96. {
  97. if (m_recording) m_events.emplace_back(LCLK_U);
  98. if (!m_playing) MouseInput::left_click_up();
  99. }
  100. void right_click_down() override
  101. {
  102. if (m_recording) m_events.emplace_back(RCLK_D);
  103. if (!m_playing) MouseInput::right_click_down();
  104. }
  105. void right_click_up() override
  106. {
  107. if (m_recording) m_events.emplace_back(RCLK_U);
  108. if (!m_playing) MouseInput::right_click_up();
  109. }
  110. void double_click() override
  111. {
  112. if (m_recording) m_events.emplace_back(DDCLK);
  113. if (!m_playing) MouseInput::double_click();
  114. }
  115. void scroll(long v, long d, WheelAxis wa) override
  116. {
  117. if (m_recording) m_events.emplace_back(SCRL, v, d);
  118. if (!m_playing) MouseInput::scroll(v, d, wa);
  119. }
  120. void move_to(long x, long y) override
  121. {
  122. if (m_recording) m_events.emplace_back(MV, x, y);
  123. if (!m_playing) MouseInput::move_to(x, y);
  124. }
  125. void save(std::ostream &stream)
  126. {
  127. for (const Event &evt : m_events)
  128. stream << evt.type << " " << evt.a << " " << evt.b << std::endl;
  129. }
  130. void load(std::istream &stream)
  131. {
  132. m_events.clear();
  133. while (stream.good()) {
  134. int type; long a, b;
  135. stream >> type >> a >> b;
  136. m_events.emplace_back(EEvents(type), a, b);
  137. }
  138. }
  139. void record(bool r) { m_recording = r; if (r) m_events.clear(); }
  140. void play()
  141. {
  142. m_playing = true;
  143. for (const Event &evt : m_events) {
  144. switch (evt.type) {
  145. case LCLK_U: MouseInput::left_click_up(); break;
  146. case LCLK_D: MouseInput::left_click_down(); break;
  147. case RCLK_U: MouseInput::right_click_up(); break;
  148. case RCLK_D: MouseInput::right_click_down(); break;
  149. case DDCLK: MouseInput::double_click(); break;
  150. case SCRL: MouseInput::scroll(evt.a, evt.b, WheelAxis::waVertical); break;
  151. case MV: MouseInput::move_to(evt.a, evt.b); break;
  152. }
  153. wxTheApp->Yield();
  154. if (!m_playing)
  155. break;
  156. }
  157. m_playing = false;
  158. }
  159. void stop() { m_playing = false; }
  160. bool is_playing() const { return m_playing; }
  161. };
  162. // The top level frame of the application.
  163. class MyFrame: public wxFrame
  164. {
  165. // Instantiate the 3D engine.
  166. std::shared_ptr<Scene> m_scene; // Model
  167. std::shared_ptr<Canvas> m_canvas; // Views store
  168. std::shared_ptr<OCSGRenderer> m_ocsgdisplay; // View
  169. std::shared_ptr<ShaderCSGRenderer> m_shadercsg_display; // Another view
  170. std::shared_ptr<Controller> m_ctl; // Controller
  171. // Add a status bar with progress indication.
  172. std::shared_ptr<Slic3r::GUI::ProgressStatusBar> m_stbar;
  173. RecorderMouseInput m_mouse;
  174. // When loading a Model from 3mf and preparing it, we use a separate thread.
  175. class SLAJob: public Slic3r::GUI::Job {
  176. MyFrame *m_parent;
  177. std::unique_ptr<Slic3r::SLAPrint> m_print;
  178. std::string m_fname;
  179. public:
  180. SLAJob(MyFrame *frame, const std::string &fname)
  181. : Slic3r::GUI::Job{frame->m_stbar}
  182. , m_parent{frame}
  183. , m_fname{fname}
  184. {}
  185. // Runs in separate thread
  186. void process() override;
  187. const std::string & get_project_fname() const { return m_fname; }
  188. protected:
  189. // Runs in the UI thread.
  190. void finalize() override
  191. {
  192. m_parent->m_scene->set_print(std::move(m_print));
  193. m_parent->m_stbar->set_status_text(
  194. wxString::Format("Model %s loaded.", m_fname));
  195. }
  196. };
  197. std::unique_ptr<SLAJob> m_ui_job;
  198. // To keep track of the running average of measured fps values.
  199. double m_fps_avg = 0.;
  200. // We need the record button across methods
  201. wxToggleButton *m_record_btn;
  202. wxComboBox * m_alg_select;
  203. wxComboBox * m_depth_select;
  204. wxComboBox * m_optimization_select;
  205. wxSpinCtrl * m_convexity_spin;
  206. wxToggleButton *m_csg_toggle;
  207. wxToggleButton *m_ms_toggle;
  208. wxStaticText *m_fpstext;
  209. CSGSettings m_csg_settings;
  210. void read_csg_settings(const wxCmdLineParser &parser);
  211. void set_renderer_algorithm(const wxString &alg);
  212. void activate_canvas_display();
  213. public:
  214. MyFrame(const wxString & title,
  215. const wxPoint & pos,
  216. const wxSize & size,
  217. const wxCmdLineParser &parser);
  218. // Grab a 3mf and load (hollow it out) within the UI job.
  219. void load_model(const std::string &fname) {
  220. m_ui_job = std::make_unique<SLAJob>(this, fname);
  221. m_ui_job->start();
  222. }
  223. // Load a previously stored mouse event log and play it back.
  224. void play_back_mouse(const std::string &events_fname)
  225. {
  226. std::fstream stream(events_fname, std::fstream::in);
  227. if (stream.good()) {
  228. std::string model_name;
  229. std::getline(stream, model_name);
  230. load_model(model_name);
  231. while (!m_ui_job->is_finalized())
  232. wxTheApp->Yield();;
  233. int w, h;
  234. stream >> w >> h;
  235. SetSize(w, h);
  236. m_mouse.load(stream);
  237. if (m_record_btn) m_record_btn->Disable();
  238. m_mouse.play();
  239. }
  240. }
  241. Canvas * canvas() { return m_canvas.get(); }
  242. const Canvas * canvas() const { return m_canvas.get(); }
  243. // Bind the canvas mouse events to a class implementing MouseInput interface
  244. void bind_canvas_events(MouseInput &msinput);
  245. double get_fps_average() const { return m_fps_avg; }
  246. };
  247. // Possible OpenCSG configuration values. Will be used on the command line and
  248. // on the UI widgets.
  249. static const std::vector<wxString> CSG_ALGS = {"Auto", "Goldfeather", "SCS", "EnricoShader"};
  250. static const std::vector<wxString> CSG_DEPTH = {"Off", "OcclusionQuery", "On"};
  251. static const std::vector<wxString> CSG_OPT = { "Default", "ForceOn", "On", "Off" };
  252. inline long get_idx(const wxString &a, const std::vector<wxString> &v)
  253. {
  254. auto it = std::find(v.begin(), v.end(), a.ToStdString());
  255. return it - v.begin();
  256. };
  257. class App : public wxApp {
  258. MyFrame *m_frame = nullptr;
  259. wxString m_fname;
  260. public:
  261. bool OnInit() override {
  262. wxCmdLineParser parser(argc, argv);
  263. parser.AddOption("p", "play", "play back file", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL);
  264. parser.AddOption("a", "algorithm", "OpenCSG algorithm [Auto|Goldfeather|SCS]", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL);
  265. parser.AddOption("d", "depth", "OpenCSG depth strategy [Off|OcclusionQuery|On]", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL);
  266. parser.AddOption("o", "optimization", "OpenCSG optimization strategy [Default|ForceOn|On|Off]", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL);
  267. parser.AddOption("c", "convexity", "OpenCSG convexity parameter for generic meshes", wxCMD_LINE_VAL_NUMBER, wxCMD_LINE_PARAM_OPTIONAL);
  268. parser.AddSwitch("", "disable-csg", "Disable csg rendering", wxCMD_LINE_PARAM_OPTIONAL);
  269. parser.Parse();
  270. bool is_play = parser.Found("play", &m_fname);
  271. m_frame = new MyFrame("PrusaSlicer OpenCSG Demo", wxDefaultPosition, wxSize(1024, 768), parser);
  272. if (is_play) {
  273. Bind(wxEVT_IDLE, &App::Play, this);
  274. m_frame->Show( true );
  275. } else m_frame->Show( true );
  276. return true;
  277. }
  278. void Play(wxIdleEvent &) {
  279. Unbind(wxEVT_IDLE, &App::Play, this);
  280. m_frame->play_back_mouse(m_fname.ToStdString());
  281. m_frame->Destroy();
  282. }
  283. };
  284. wxIMPLEMENT_APP(App);
  285. void MyFrame::read_csg_settings(const wxCmdLineParser &parser)
  286. {
  287. wxString alg;
  288. parser.Found("algorithm", &alg);
  289. wxString depth;
  290. parser.Found("depth", &depth);
  291. wxString opt;
  292. parser.Found("optimization", &opt);
  293. long convexity = 1;
  294. parser.Found("convexity", &convexity);
  295. bool csg_off = parser.Found("disable-csg");
  296. if (auto a = get_idx(alg, CSG_ALGS) < OpenCSG::AlgorithmUnused)
  297. m_csg_settings.set_algo(OpenCSG::Algorithm(a));
  298. if (auto a = get_idx(depth, CSG_DEPTH) < OpenCSG::DepthComplexityAlgorithmUnused)
  299. m_csg_settings.set_depth_algo(OpenCSG::DepthComplexityAlgorithm(a));
  300. if (auto a = get_idx(opt, CSG_OPT) < OpenCSG::OptimizationUnused)
  301. m_csg_settings.set_optimization(OpenCSG::Optimization(a));
  302. m_csg_settings.set_convexity(unsigned(convexity));
  303. m_csg_settings.enable_csg(!csg_off);
  304. if (m_ocsgdisplay) m_ocsgdisplay->apply_csgsettings(m_csg_settings);
  305. }
  306. void MyFrame::set_renderer_algorithm(const wxString &alg)
  307. {
  308. long alg_idx = get_idx(alg, CSG_ALGS);
  309. if (alg_idx < 0 || alg_idx >= long(CSG_ALGS.size())) return;
  310. // If there is a valid display in place, save its camera.
  311. auto cam = m_canvas->get_display() ?
  312. m_canvas->get_display()->get_camera() : nullptr;
  313. if (alg == "EnricoShader") {
  314. m_alg_select->SetSelection(int(alg_idx));
  315. m_depth_select->Disable();
  316. m_optimization_select->Disable();
  317. m_csg_toggle->Disable();
  318. m_ocsgdisplay.reset();
  319. canvas()->set_display(nullptr);
  320. m_shadercsg_display = std::make_shared<ShaderCSGRenderer>(canvas());
  321. canvas()->set_display(m_shadercsg_display);
  322. } else {
  323. if (m_csg_settings.get_algo() > 0) m_depth_select->Enable(true);
  324. m_alg_select->SetSelection(m_csg_settings.get_algo());
  325. m_depth_select->SetSelection(m_csg_settings.get_depth_algo());
  326. m_optimization_select->SetSelection(m_csg_settings.get_optimization());
  327. m_convexity_spin->SetValue(int(m_csg_settings.get_convexity()));
  328. m_csg_toggle->SetValue(m_csg_settings.is_enabled());
  329. m_optimization_select->Enable();
  330. m_csg_toggle->Enable();
  331. m_shadercsg_display.reset();
  332. canvas()->set_display(nullptr);
  333. m_ocsgdisplay = std::make_shared<OCSGRenderer>(canvas());
  334. m_ocsgdisplay->apply_csgsettings(m_csg_settings);
  335. canvas()->set_display(m_ocsgdisplay);
  336. }
  337. if (cam)
  338. m_canvas->get_display()->set_camera(cam);
  339. m_ctl->remove_displays();
  340. m_ctl->add_display(m_canvas->get_display());
  341. m_canvas->get_display()->get_fps_counter().add_listener([this](double fps) {
  342. m_fpstext->SetLabel(wxString::Format("fps: %.2f", fps));
  343. m_fps_avg = 0.9 * m_fps_avg + 0.1 * fps;
  344. });
  345. if (IsShown()) {
  346. activate_canvas_display();
  347. m_canvas->get_display()->on_scene_updated(*m_scene);
  348. }
  349. }
  350. void MyFrame::activate_canvas_display()
  351. {
  352. const wxSize ClientSize = m_canvas->GetClientSize();
  353. m_canvas->get_display()->set_active(ClientSize.x, ClientSize.y);
  354. enable_multisampling(m_ms_toggle->GetValue());
  355. m_canvas->Bind(wxEVT_PAINT, [this](wxPaintEvent &) {
  356. // This is required even though dc is not used otherwise.
  357. wxPaintDC dc(m_canvas.get());
  358. const wxSize csize = m_canvas->GetClientSize();
  359. m_canvas->get_display()->set_screen_size(csize.x, csize.y);
  360. m_canvas->get_display()->repaint();
  361. });
  362. m_canvas->Bind(wxEVT_SIZE, [this](wxSizeEvent &) {
  363. const wxSize csize = m_canvas->GetClientSize();
  364. m_canvas->get_display()->set_screen_size(csize.x, csize.y);
  365. m_canvas->get_display()->repaint();
  366. });
  367. // Do the repaint continuously
  368. m_canvas->Bind(wxEVT_IDLE, [this](wxIdleEvent &evt) {
  369. m_canvas->get_display()->repaint();
  370. evt.RequestMore();
  371. });
  372. bind_canvas_events(m_mouse);
  373. }
  374. MyFrame::MyFrame(const wxString &title, const wxPoint &pos, const wxSize &size,
  375. const wxCmdLineParser &parser):
  376. wxFrame(nullptr, wxID_ANY, title, pos, size)
  377. {
  378. wxMenu *menuFile = new wxMenu;
  379. menuFile->Append(wxID_OPEN);
  380. menuFile->Append(wxID_EXIT);
  381. wxMenuBar *menuBar = new wxMenuBar;
  382. menuBar->Append( menuFile, "&File" );
  383. SetMenuBar( menuBar );
  384. m_stbar = std::make_shared<Slic3r::GUI::ProgressStatusBar>(this);
  385. m_stbar->embed(this);
  386. SetStatusText( "Welcome to wxWidgets!" );
  387. int attribList[] =
  388. {WX_GL_RGBA, WX_GL_DOUBLEBUFFER,
  389. // RGB channels each should be allocated with 8 bit depth. One
  390. // should almost certainly get these bit depths by default.
  391. WX_GL_MIN_RED, 8, WX_GL_MIN_GREEN, 8, WX_GL_MIN_BLUE, 8,
  392. // Requesting an 8 bit alpha channel. Interestingly, the NVIDIA
  393. // drivers would most likely work with some alpha plane, but
  394. // glReadPixels would not return the alpha channel on NVIDIA if
  395. // not requested when the GL context is created.
  396. WX_GL_MIN_ALPHA, 8, WX_GL_DEPTH_SIZE, 8, WX_GL_STENCIL_SIZE, 8,
  397. WX_GL_SAMPLE_BUFFERS, GL_TRUE, WX_GL_SAMPLES, 4, 0};
  398. m_scene = std::make_shared<Scene>();
  399. m_ctl = std::make_shared<Controller>();
  400. m_ctl->set_scene(m_scene);
  401. m_canvas = std::make_shared<Canvas>(this, wxID_ANY, attribList,
  402. wxDefaultPosition, wxDefaultSize,
  403. wxWANTS_CHARS | wxFULL_REPAINT_ON_RESIZE);
  404. read_csg_settings(parser);
  405. wxPanel *control_panel = new wxPanel(this);
  406. auto controlsizer = new wxBoxSizer(wxHORIZONTAL);
  407. auto slider_sizer = new wxBoxSizer(wxVERTICAL);
  408. auto console_sizer = new wxBoxSizer(wxVERTICAL);
  409. auto slider = new wxSlider(control_panel, wxID_ANY, 0, 0, 100,
  410. wxDefaultPosition, wxDefaultSize,
  411. wxSL_VERTICAL);
  412. slider_sizer->Add(slider, 1, wxEXPAND);
  413. m_ms_toggle = new wxToggleButton(control_panel, wxID_ANY, "Multisampling");
  414. console_sizer->Add(m_ms_toggle, 0, wxALL | wxEXPAND, 5);
  415. m_csg_toggle = new wxToggleButton(control_panel, wxID_ANY, "CSG");
  416. m_csg_toggle->SetValue(true);
  417. console_sizer->Add(m_csg_toggle, 0, wxALL | wxEXPAND, 5);
  418. auto add_combobox = [control_panel, console_sizer]
  419. (const wxString &label, const std::vector<wxString> &list)
  420. {
  421. auto widget = new wxComboBox(control_panel, wxID_ANY, list[0],
  422. wxDefaultPosition, wxDefaultSize,
  423. int(list.size()), list.data());
  424. auto sz = new wxBoxSizer(wxHORIZONTAL);
  425. sz->Add(new wxStaticText(control_panel, wxID_ANY, label), 0,
  426. wxALL | wxALIGN_CENTER, 5);
  427. sz->Add(widget, 1, wxALL | wxEXPAND, 5);
  428. console_sizer->Add(sz, 0, wxEXPAND);
  429. return widget;
  430. };
  431. auto add_spinctl = [control_panel, console_sizer]
  432. (const wxString &label, int initial, int min, int max)
  433. {
  434. auto widget = new wxSpinCtrl(
  435. control_panel, wxID_ANY,
  436. wxString::Format("%d", initial),
  437. wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, min, max,
  438. initial);
  439. auto sz = new wxBoxSizer(wxHORIZONTAL);
  440. sz->Add(new wxStaticText(control_panel, wxID_ANY, label), 0,
  441. wxALL | wxALIGN_CENTER, 5);
  442. sz->Add(widget, 1, wxALL | wxEXPAND, 5);
  443. console_sizer->Add(sz, 0, wxEXPAND);
  444. return widget;
  445. };
  446. m_convexity_spin = add_spinctl("Convexity", CSGSettings::DEFAULT_CONVEXITY, 0, 100);
  447. m_alg_select = add_combobox("Algorithm", CSG_ALGS);
  448. m_depth_select = add_combobox("Depth Complexity", CSG_DEPTH);
  449. m_optimization_select = add_combobox("Optimization", CSG_OPT);
  450. m_fpstext = new wxStaticText(control_panel, wxID_ANY, "");
  451. console_sizer->Add(m_fpstext, 0, wxALL, 5);
  452. m_record_btn = new wxToggleButton(control_panel, wxID_ANY, "Record");
  453. console_sizer->Add(m_record_btn, 0, wxALL | wxEXPAND, 5);
  454. controlsizer->Add(slider_sizer, 0, wxEXPAND);
  455. controlsizer->Add(console_sizer, 1, wxEXPAND);
  456. control_panel->SetSizer(controlsizer);
  457. auto sizer = new wxBoxSizer(wxHORIZONTAL);
  458. sizer->Add(m_canvas.get(), 1, wxEXPAND);
  459. sizer->Add(control_panel, 0, wxEXPAND);
  460. SetSizer(sizer);
  461. wxString alg;
  462. if (!parser.Found("algorithm", &alg)) alg = "Auto";
  463. set_renderer_algorithm(alg);
  464. Bind(wxEVT_CLOSE_WINDOW, [this](wxCloseEvent &evt){
  465. if (m_canvas) RemoveChild(m_canvas.get());
  466. m_canvas.reset();
  467. if (!m_mouse.is_playing()) evt.Skip();
  468. else m_mouse.stop();
  469. });
  470. Bind(wxEVT_MENU, [this](wxCommandEvent &) {
  471. wxFileDialog dlg(this, "Select project file", wxEmptyString,
  472. wxEmptyString, "*.3mf", wxFD_OPEN|wxFD_FILE_MUST_EXIST);
  473. if (dlg.ShowModal() == wxID_OK) load_model(dlg.GetPath().ToStdString());
  474. }, wxID_OPEN);
  475. Bind(wxEVT_MENU, [this](wxCommandEvent &) { Close(true); }, wxID_EXIT);
  476. Bind(wxEVT_SHOW, [this](wxShowEvent &) {
  477. activate_canvas_display();
  478. });
  479. Bind(wxEVT_SLIDER, [this, slider](wxCommandEvent &) {
  480. m_ctl->move_clip_plane(double(slider->GetValue()));
  481. });
  482. m_ms_toggle->Bind(wxEVT_TOGGLEBUTTON, [this](wxCommandEvent &){
  483. enable_multisampling(m_ms_toggle->GetValue());
  484. m_canvas->get_display()->repaint();
  485. });
  486. m_csg_toggle->Bind(wxEVT_TOGGLEBUTTON, [this](wxCommandEvent &){
  487. CSGSettings stt = m_ocsgdisplay->get_csgsettings();
  488. stt.enable_csg(m_csg_toggle->GetValue());
  489. m_ocsgdisplay->apply_csgsettings(stt);
  490. });
  491. m_alg_select->Bind(wxEVT_COMBOBOX, [this](wxCommandEvent &) {
  492. wxString alg = m_alg_select->GetValue();
  493. int sel = m_alg_select->GetSelection();
  494. m_csg_settings.set_algo(sel);
  495. set_renderer_algorithm(alg);
  496. });
  497. m_depth_select->Bind(wxEVT_COMBOBOX, [this](wxCommandEvent &) {
  498. int sel = m_depth_select->GetSelection();
  499. m_csg_settings.set_depth_algo(sel);
  500. if (m_ocsgdisplay) m_ocsgdisplay->apply_csgsettings(m_csg_settings);
  501. });
  502. m_optimization_select->Bind(wxEVT_COMBOBOX, [this](wxCommandEvent &) {
  503. int sel = m_optimization_select->GetSelection();
  504. m_csg_settings.set_optimization(sel);
  505. if (m_ocsgdisplay) m_ocsgdisplay->apply_csgsettings(m_csg_settings);
  506. });
  507. m_convexity_spin->Bind(wxEVT_SPINCTRL, [this](wxSpinEvent &) {
  508. int c = m_convexity_spin->GetValue();
  509. if (c > 0) {
  510. m_csg_settings.set_convexity(unsigned(c));
  511. if (m_ocsgdisplay) m_ocsgdisplay->apply_csgsettings(m_csg_settings);
  512. }
  513. });
  514. m_record_btn->Bind(wxEVT_TOGGLEBUTTON, [this](wxCommandEvent &) {
  515. if (!m_ui_job) {
  516. m_stbar->set_status_text("No project loaded!");
  517. return;
  518. }
  519. if (m_record_btn->GetValue()) {
  520. if (auto c = m_canvas->get_display()->get_camera()) reset(*c);
  521. m_ctl->on_scene_updated(*m_scene);
  522. m_mouse.record(true);
  523. } else {
  524. m_mouse.record(false);
  525. wxFileDialog dlg(this, "Select output file",
  526. wxEmptyString, wxEmptyString, "*.events",
  527. wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
  528. if (dlg.ShowModal() == wxID_OK) {
  529. std::fstream stream(dlg.GetPath().ToStdString(),
  530. std::fstream::out);
  531. if (stream.good()) {
  532. stream << m_ui_job->get_project_fname() << "\n";
  533. wxSize winsize = GetSize();
  534. stream << winsize.x << " " << winsize.y << "\n";
  535. m_mouse.save(stream);
  536. }
  537. }
  538. }
  539. });
  540. }
  541. void MyFrame::bind_canvas_events(MouseInput &ms)
  542. {
  543. m_canvas->Bind(wxEVT_MOUSEWHEEL, [&ms](wxMouseEvent &evt) {
  544. ms.scroll(evt.GetWheelRotation(), evt.GetWheelDelta(),
  545. evt.GetWheelAxis() == wxMOUSE_WHEEL_VERTICAL ?
  546. Slic3r::GL::MouseInput::waVertical :
  547. Slic3r::GL::MouseInput::waHorizontal);
  548. });
  549. m_canvas->Bind(wxEVT_MOTION, [&ms](wxMouseEvent &evt) {
  550. ms.move_to(evt.GetPosition().x, evt.GetPosition().y);
  551. });
  552. m_canvas->Bind(wxEVT_RIGHT_DOWN, [&ms](wxMouseEvent & /*evt*/) {
  553. ms.right_click_down();
  554. });
  555. m_canvas->Bind(wxEVT_RIGHT_UP, [&ms](wxMouseEvent & /*evt*/) {
  556. ms.right_click_up();
  557. });
  558. m_canvas->Bind(wxEVT_LEFT_DOWN, [&ms](wxMouseEvent & /*evt*/) {
  559. ms.left_click_down();
  560. });
  561. m_canvas->Bind(wxEVT_LEFT_UP, [&ms](wxMouseEvent & /*evt*/) {
  562. ms.left_click_up();
  563. });
  564. ms.add_listener(m_ctl);
  565. }
  566. void MyFrame::SLAJob::process()
  567. {
  568. using SlStatus = Slic3r::PrintBase::SlicingStatus;
  569. Slic3r::DynamicPrintConfig cfg;
  570. auto model = Slic3r::Model::read_from_file(m_fname, &cfg);
  571. m_print = std::make_unique<Slic3r::SLAPrint>();
  572. m_print->apply(model, cfg);
  573. Slic3r::PrintBase::TaskParams params;
  574. params.to_object_step = Slic3r::slaposHollowing;
  575. m_print->set_task(params);
  576. m_print->set_status_callback([this](const SlStatus &status) {
  577. update_status(status.percent, status.text);
  578. });
  579. try {
  580. m_print->process();
  581. } catch(std::exception &e) {
  582. update_status(0, wxString("Exception during processing: ") + e.what());
  583. }
  584. }
  585. //int main() {}