Viewer.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <daniele.panozzo@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "Viewer.h"
  9. #ifdef _WIN32
  10. # include <windows.h>
  11. # undef max
  12. # undef min
  13. #endif
  14. #include <chrono>
  15. #include <thread>
  16. #ifndef __APPLE__
  17. # define GLEW_STATIC
  18. # include <GL/glew.h>
  19. #endif
  20. #ifdef __APPLE__
  21. # include <OpenGL/gl3.h>
  22. # define __gl_h_ /* Prevent inclusion of the old gl.h */
  23. #else
  24. # include <GL/gl.h>
  25. #endif
  26. #include <Eigen/LU>
  27. #define GLFW_INCLUDE_GLU
  28. #ifndef _WIN32
  29. #define GLFW_INCLUDE_GLCOREARB
  30. #endif
  31. #include <GLFW/glfw3.h>
  32. #include <cmath>
  33. #include <cstdio>
  34. #include <sstream>
  35. #include <iomanip>
  36. #include <iostream>
  37. #include <fstream>
  38. #include <algorithm>
  39. #include <limits>
  40. #include <cassert>
  41. #ifdef IGL_VIEWER_WITH_NANOGUI
  42. # include <nanogui/formhelper.h>
  43. # include <nanogui/screen.h>
  44. #endif
  45. #include <igl/project.h>
  46. #include <igl/get_seconds.h>
  47. #include <igl/readOBJ.h>
  48. #include <igl/readOFF.h>
  49. #include <igl/adjacency_list.h>
  50. #include <igl/writeOBJ.h>
  51. #include <igl/writeOFF.h>
  52. #include <igl/massmatrix.h>
  53. #include <igl/file_dialog_open.h>
  54. #include <igl/file_dialog_save.h>
  55. #include <igl/quat_mult.h>
  56. #include <igl/axis_angle_to_quat.h>
  57. #include <igl/trackball.h>
  58. #include <igl/two_axis_valuator_fixed_up.h>
  59. #include <igl/snap_to_canonical_view_quat.h>
  60. #include <igl/unproject.h>
  61. #ifdef ENABLE_SERIALIZATION
  62. #include <igl/serialize.h>
  63. #endif
  64. // Internal global variables used for glfw event handling
  65. static igl::viewer::Viewer * __viewer;
  66. static double highdpi = 1;
  67. static double scroll_x = 0;
  68. static double scroll_y = 0;
  69. static int global_KMod = 0;
  70. static void glfw_mouse_press(GLFWwindow* window, int button, int action, int modifier)
  71. {
  72. bool tw_used =
  73. #ifdef IGL_VIEWER_WITH_NANOGUI
  74. __viewer->screen->mouseButtonCallbackEvent(button,action,modifier);
  75. #else
  76. false;
  77. #endif
  78. igl::viewer::Viewer::MouseButton mb;
  79. if (button == GLFW_MOUSE_BUTTON_1)
  80. mb = igl::viewer::Viewer::MouseButton::Left;
  81. else if (button == GLFW_MOUSE_BUTTON_2)
  82. mb = igl::viewer::Viewer::MouseButton::Right;
  83. else //if (button == GLFW_MOUSE_BUTTON_3)
  84. mb = igl::viewer::Viewer::MouseButton::Middle;
  85. if (action == GLFW_PRESS)
  86. {
  87. if(!tw_used)
  88. {
  89. __viewer->mouse_down(mb,modifier);
  90. }
  91. } else
  92. {
  93. // Always call mouse_up on up
  94. __viewer->mouse_up(mb,modifier);
  95. }
  96. }
  97. static void glfw_error_callback(int error, const char* description)
  98. {
  99. fputs(description, stderr);
  100. }
  101. static void glfw_char_mods_callback(GLFWwindow* window, unsigned int codepoint, int modifier)
  102. {
  103. // TODO: pass to nanogui (although it's also using physical key down/up
  104. // rather than character codes...
  105. #ifdef IGL_VIEWER_WITH_NANOGUI
  106. if(! __viewer->screen->charCallbackEvent(codepoint) )
  107. #endif
  108. {
  109. __viewer->key_pressed(codepoint, modifier);
  110. }
  111. }
  112. static void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int modifier)
  113. {
  114. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  115. glfwSetWindowShouldClose(window, GL_TRUE);
  116. #ifdef IGL_VIEWER_WITH_NANOGUI
  117. if (__viewer->screen->keyCallbackEvent(key,scancode,action,modifier) == false)
  118. #endif
  119. {
  120. if (action == GLFW_PRESS)
  121. __viewer->key_down(key, modifier);
  122. else if(action == GLFW_RELEASE)
  123. __viewer->key_up(key, modifier);
  124. }
  125. }
  126. static void glfw_window_size(GLFWwindow* window, int width, int height)
  127. {
  128. int w = width*highdpi;
  129. int h = height*highdpi;
  130. __viewer->resize(w, h);
  131. // TODO: repositioning of the nanogui
  132. }
  133. static void glfw_mouse_move(GLFWwindow* window, double x, double y)
  134. {
  135. if(
  136. #ifdef IGL_VIEWER_WITH_NANOGUI
  137. __viewer->screen->cursorPosCallbackEvent(x,y) == false ||
  138. #endif
  139. __viewer->down)
  140. {
  141. __viewer->mouse_move(x*highdpi, y*highdpi);
  142. }
  143. }
  144. static void glfw_mouse_scroll(GLFWwindow* window, double x, double y)
  145. {
  146. using namespace std;
  147. scroll_x += x;
  148. scroll_y += y;
  149. #ifdef IGL_VIEWER_WITH_NANOGUI
  150. if (__viewer->screen->scrollCallbackEvent(x,y) == false)
  151. #endif
  152. {
  153. __viewer->mouse_scroll(y);
  154. }
  155. }
  156. static void glfw_drop_callback(GLFWwindow *window,int count,const char **filenames)
  157. {
  158. #ifdef IGL_VIEWER_WITH_NANOGUI
  159. __viewer->screen->dropCallbackEvent(count,filenames);
  160. #endif
  161. }
  162. namespace igl
  163. {
  164. namespace viewer
  165. {
  166. IGL_INLINE void Viewer::init()
  167. {
  168. #ifdef IGL_VIEWER_WITH_NANOGUI
  169. using namespace nanogui;
  170. ngui->setFixedSize(Eigen::Vector2i(60,20));
  171. // Create nanogui widgets
  172. nanogui::Window *window = ngui->addWindow(Eigen::Vector2i(10,10),"libIGL-Viewer");
  173. // ---------------------- LOADING ----------------------
  174. #ifdef ENABLE_SERIALIZATION
  175. ngui->addGroup("Workspace");
  176. ngui->addButton("Load",[&](){this->load_scene();});
  177. ngui->addButton("Save",[&](){this->save_scene();});
  178. #endif
  179. #ifdef ENABLE_IO
  180. ngui->addGroup("Mesh");
  181. ngui->addButton("Load",[&](){this->open_dialog_load_mesh();});
  182. ngui->addButton("Save",[&](){this->open_dialog_save_mesh();});
  183. #endif
  184. ngui->addGroup("Viewing Options");
  185. ngui->addButton("Center object",[&](){this->core.align_camera_center(this->data.V,this->data.F);});
  186. ngui->addButton("Snap canonical view",[&]()
  187. {
  188. this->snap_to_canonical_quaternion();
  189. });
  190. ngui->addVariable("Zoom", core.camera_zoom);
  191. ngui->addVariable("Orthographic view", core.orthographic);
  192. ngui->addGroup("Draw options");
  193. ngui->addVariable<bool>("Face-based", [&](bool checked)
  194. {
  195. this->data.set_face_based(checked);
  196. },[&]()
  197. {
  198. return this->data.face_based;
  199. });
  200. ngui->addVariable("Show texture",core.show_texture);
  201. ngui->addVariable<bool>("Invert normals",[&](bool checked)
  202. {
  203. this->data.dirty |= ViewerData::DIRTY_NORMAL;
  204. this->core.invert_normals = checked;
  205. },[&]()
  206. {
  207. return this->core.invert_normals;
  208. });
  209. ngui->addVariable("Show overlay", core.show_overlay);
  210. ngui->addVariable("Show overlay depth", core.show_overlay_depth);
  211. ngui->addVariable("Background", (nanogui::Color &) core.background_color);
  212. ngui->addVariable("Line color", (nanogui::Color &) core.line_color);
  213. ngui->addVariable("Shininess", core.shininess);
  214. ngui->addGroup("Overlays");
  215. ngui->addVariable("Wireframe", core.show_lines);
  216. ngui->addVariable("Fill", core.show_faces);
  217. ngui->addVariable("Show vertex labels", core.show_vertid);
  218. ngui->addVariable("Show faces labels", core.show_faceid);
  219. screen->performLayout();
  220. #endif
  221. core.init();
  222. if (callback_init)
  223. if (callback_init(*this))
  224. return;
  225. init_plugins();
  226. }
  227. IGL_INLINE Viewer::Viewer()
  228. {
  229. #ifdef IGL_VIEWER_WITH_NANOGUI
  230. ngui = nullptr;
  231. screen = nullptr;
  232. #endif
  233. // Temporary variables initialization
  234. down = false;
  235. hack_never_moved = true;
  236. scroll_position = 0.0f;
  237. // Per face
  238. data.set_face_based(false);
  239. // C-style callbacks
  240. callback_init = nullptr;
  241. callback_pre_draw = nullptr;
  242. callback_post_draw = nullptr;
  243. callback_mouse_down = nullptr;
  244. callback_mouse_up = nullptr;
  245. callback_mouse_move = nullptr;
  246. callback_mouse_scroll = nullptr;
  247. callback_key_down = nullptr;
  248. callback_key_up = nullptr;
  249. callback_init_data = nullptr;
  250. callback_pre_draw_data = nullptr;
  251. callback_post_draw_data = nullptr;
  252. callback_mouse_down_data = nullptr;
  253. callback_mouse_up_data = nullptr;
  254. callback_mouse_move_data = nullptr;
  255. callback_mouse_scroll_data = nullptr;
  256. callback_key_down_data = nullptr;
  257. callback_key_up_data = nullptr;
  258. }
  259. IGL_INLINE void Viewer::init_plugins()
  260. {
  261. // Init all plugins
  262. for (unsigned int i = 0; i<plugins.size(); ++i)
  263. {
  264. plugins[i]->init(this);
  265. }
  266. }
  267. IGL_INLINE Viewer::~Viewer()
  268. {
  269. }
  270. IGL_INLINE void Viewer::shutdown_plugins()
  271. {
  272. for (unsigned int i = 0; i<plugins.size(); ++i)
  273. {
  274. plugins[i]->shutdown();
  275. }
  276. }
  277. IGL_INLINE bool Viewer::load_mesh_from_file(const char* mesh_file_name)
  278. {
  279. std::string mesh_file_name_string = std::string(mesh_file_name);
  280. // first try to load it with a plugin
  281. for (unsigned int i = 0; i<plugins.size(); ++i)
  282. {
  283. if (plugins[i]->load(mesh_file_name_string))
  284. {
  285. return true;
  286. }
  287. }
  288. data.clear();
  289. size_t last_dot = mesh_file_name_string.rfind('.');
  290. if (last_dot == std::string::npos)
  291. {
  292. printf("Error: No file extension found in %s\n",mesh_file_name);
  293. return false;
  294. }
  295. std::string extension = mesh_file_name_string.substr(last_dot+1);
  296. if (extension == "off" || extension =="OFF")
  297. {
  298. Eigen::MatrixXd V;
  299. Eigen::MatrixXi F;
  300. if (!igl::readOFF(mesh_file_name_string, V, F))
  301. return false;
  302. data.set_mesh(V,F);
  303. }
  304. else if (extension == "obj" || extension =="OBJ")
  305. {
  306. Eigen::MatrixXd corner_normals;
  307. Eigen::MatrixXi fNormIndices;
  308. Eigen::MatrixXd UV_V;
  309. Eigen::MatrixXi UV_F;
  310. Eigen::MatrixXd V;
  311. Eigen::MatrixXi F;
  312. if (!(
  313. igl::readOBJ(
  314. mesh_file_name_string,
  315. V, UV_V, corner_normals, F, UV_F, fNormIndices)))
  316. {
  317. return false;
  318. }
  319. data.set_mesh(V,F);
  320. data.set_uv(UV_V,UV_F);
  321. }
  322. else
  323. {
  324. // unrecognized file type
  325. printf("Error: %s is not a recognized file type.\n",extension.c_str());
  326. return false;
  327. }
  328. data.compute_normals();
  329. data.uniform_colors(Eigen::Vector3d(51.0/255.0,43.0/255.0,33.3/255.0),
  330. Eigen::Vector3d(255.0/255.0,228.0/255.0,58.0/255.0),
  331. Eigen::Vector3d(255.0/255.0,235.0/255.0,80.0/255.0));
  332. if (data.V_uv.rows() == 0)
  333. {
  334. data.grid_texture();
  335. }
  336. core.align_camera_center(data.V,data.F);
  337. for (unsigned int i = 0; i<plugins.size(); ++i)
  338. if (plugins[i]->post_load())
  339. return true;
  340. return true;
  341. }
  342. IGL_INLINE bool Viewer::save_mesh_to_file(const char* mesh_file_name)
  343. {
  344. std::string mesh_file_name_string(mesh_file_name);
  345. // first try to load it with a plugin
  346. for (unsigned int i = 0; i<plugins.size(); ++i)
  347. if (plugins[i]->save(mesh_file_name_string))
  348. return true;
  349. size_t last_dot = mesh_file_name_string.rfind('.');
  350. if (last_dot == std::string::npos)
  351. {
  352. // No file type determined
  353. printf("Error: No file extension found in %s\n",mesh_file_name);
  354. return false;
  355. }
  356. std::string extension = mesh_file_name_string.substr(last_dot+1);
  357. if (extension == "off" || extension =="OFF")
  358. {
  359. return igl::writeOFF(mesh_file_name_string,data.V,data.F);
  360. }
  361. else if (extension == "obj" || extension =="OBJ")
  362. {
  363. Eigen::MatrixXd corner_normals;
  364. Eigen::MatrixXi fNormIndices;
  365. Eigen::MatrixXd UV_V;
  366. Eigen::MatrixXi UV_F;
  367. return igl::writeOBJ(mesh_file_name_string, data.V,
  368. data.F, corner_normals, fNormIndices, UV_V, UV_F);
  369. }
  370. else
  371. {
  372. // unrecognized file type
  373. printf("Error: %s is not a recognized file type.\n",extension.c_str());
  374. return false;
  375. }
  376. return true;
  377. }
  378. IGL_INLINE bool Viewer::key_pressed(unsigned int unicode_key,int modifiers)
  379. {
  380. if (callback_key_pressed)
  381. if (callback_key_pressed(*this,unicode_key,modifiers))
  382. return true;
  383. for (unsigned int i = 0; i<plugins.size(); ++i)
  384. if (plugins[i]->key_pressed(unicode_key, modifiers))
  385. return true;
  386. return false;
  387. }
  388. IGL_INLINE bool Viewer::key_down(int key,int modifiers)
  389. {
  390. if (callback_key_down)
  391. if (callback_key_down(*this,key,modifiers))
  392. return true;
  393. for (unsigned int i = 0; i<plugins.size(); ++i)
  394. if (plugins[i]->key_down(key, modifiers))
  395. return true;
  396. return false;
  397. }
  398. IGL_INLINE bool Viewer::key_up(int key,int modifiers)
  399. {
  400. if (callback_key_up)
  401. if (callback_key_up(*this,key,modifiers))
  402. return true;
  403. for (unsigned int i = 0; i<plugins.size(); ++i)
  404. if (plugins[i]->key_up(key, modifiers))
  405. return true;
  406. return false;
  407. }
  408. IGL_INLINE bool Viewer::mouse_down(MouseButton button,int modifier)
  409. {
  410. if (callback_mouse_down)
  411. if (callback_mouse_down(*this,static_cast<int>(button),modifier))
  412. return true;
  413. for (unsigned int i = 0; i<plugins.size(); ++i)
  414. if(plugins[i]->mouse_down(static_cast<int>(button),modifier))
  415. return true;
  416. down = true;
  417. down_mouse_x = current_mouse_x;
  418. down_mouse_y = current_mouse_y;
  419. down_translation = core.model_translation;
  420. // Initialization code for the trackball
  421. Eigen::RowVector3d center;
  422. if (data.V.rows() == 0)
  423. {
  424. center << 0,0,0;
  425. }else
  426. {
  427. center = data.V.colwise().sum()/data.V.rows();
  428. }
  429. Eigen::Vector3f coord =
  430. igl::project(
  431. Eigen::Vector3f(center(0),center(1),center(2)),
  432. (core.view * core.model).eval(),
  433. core.proj,
  434. core.viewport);
  435. down_mouse_z = coord[2];
  436. down_rotation = core.trackball_angle;
  437. mouse_mode = MouseMode::Rotation;
  438. switch (button)
  439. {
  440. case MouseButton::Left:
  441. mouse_mode = MouseMode::Rotation;
  442. break;
  443. case MouseButton::Right:
  444. mouse_mode = MouseMode::Translation;
  445. break;
  446. default:
  447. mouse_mode = MouseMode::None;
  448. break;
  449. }
  450. return true;
  451. }
  452. IGL_INLINE bool Viewer::mouse_up(MouseButton button,int modifier)
  453. {
  454. down = false;
  455. if (callback_mouse_up)
  456. if (callback_mouse_up(*this,static_cast<int>(button),modifier))
  457. return true;
  458. for (unsigned int i = 0; i<plugins.size(); ++i)
  459. if(plugins[i]->mouse_up(static_cast<int>(button),modifier))
  460. return true;
  461. mouse_mode = MouseMode::None;
  462. return true;
  463. }
  464. IGL_INLINE bool Viewer::mouse_move(int mouse_x,int mouse_y)
  465. {
  466. if(hack_never_moved)
  467. {
  468. down_mouse_x = mouse_x;
  469. down_mouse_y = mouse_y;
  470. hack_never_moved = false;
  471. }
  472. current_mouse_x = mouse_x;
  473. current_mouse_y = mouse_y;
  474. if (callback_mouse_move)
  475. if (callback_mouse_move(*this,mouse_x,mouse_y))
  476. return true;
  477. for (unsigned int i = 0; i<plugins.size(); ++i)
  478. if (plugins[i]->mouse_move(mouse_x, mouse_y))
  479. return true;
  480. if (down)
  481. {
  482. switch (mouse_mode)
  483. {
  484. case MouseMode::Rotation:
  485. {
  486. switch(core.rotation_type)
  487. {
  488. default:
  489. assert(false && "Unknown rotation type");
  490. case ViewerCore::ROTATION_TYPE_TRACKBALL:
  491. igl::trackball(
  492. core.viewport(2),
  493. core.viewport(3),
  494. 2.0f,
  495. down_rotation,
  496. down_mouse_x,
  497. down_mouse_y,
  498. mouse_x,
  499. mouse_y,
  500. core.trackball_angle);
  501. break;
  502. case ViewerCore::ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP:
  503. igl::two_axis_valuator_fixed_up(
  504. core.viewport(2),core.viewport(3),
  505. 2.0,
  506. down_rotation,
  507. down_mouse_x, down_mouse_y, mouse_x, mouse_y,
  508. core.trackball_angle);
  509. break;
  510. }
  511. //Eigen::Vector4f snapq = core.trackball_angle;
  512. break;
  513. }
  514. case MouseMode::Translation:
  515. {
  516. //translation
  517. Eigen::Vector3f pos1 = igl::unproject(Eigen::Vector3f(mouse_x, core.viewport[3] - mouse_y, down_mouse_z), (core.view * core.model).eval(), core.proj, core.viewport);
  518. Eigen::Vector3f pos0 = igl::unproject(Eigen::Vector3f(down_mouse_x, core.viewport[3] - down_mouse_y, down_mouse_z), (core.view * core.model).eval(), core.proj, core.viewport);
  519. Eigen::Vector3f diff = pos1 - pos0;
  520. core.model_translation = down_translation + Eigen::Vector3f(diff[0],diff[1],diff[2]);
  521. break;
  522. }
  523. case MouseMode::Zoom:
  524. {
  525. float delta = 0.001f * (mouse_x - down_mouse_x + mouse_y - down_mouse_y);
  526. core.camera_zoom *= 1 + delta;
  527. down_mouse_x = mouse_x;
  528. down_mouse_y = mouse_y;
  529. break;
  530. }
  531. default:
  532. break;
  533. }
  534. }
  535. return true;
  536. }
  537. IGL_INLINE bool Viewer::mouse_scroll(float delta_y)
  538. {
  539. scroll_position += delta_y;
  540. if (callback_mouse_scroll)
  541. if (callback_mouse_scroll(*this,delta_y))
  542. return true;
  543. for (unsigned int i = 0; i<plugins.size(); ++i)
  544. if (plugins[i]->mouse_scroll(delta_y))
  545. return true;
  546. // Only zoom if there's actually a change
  547. if(delta_y != 0)
  548. {
  549. float mult = (1.0+((delta_y>0)?1.:-1.)*0.05);
  550. const float min_zoom = 0.1f;
  551. core.camera_zoom = (core.camera_zoom * mult > min_zoom ? core.camera_zoom * mult : min_zoom);
  552. }
  553. return true;
  554. }
  555. IGL_INLINE void Viewer::draw()
  556. {
  557. using namespace std;
  558. using namespace Eigen;
  559. core.clear_framebuffers();
  560. if (callback_pre_draw)
  561. if (callback_pre_draw(*this))
  562. return;
  563. for (unsigned int i = 0; i<plugins.size(); ++i)
  564. if (plugins[i]->pre_draw())
  565. return;
  566. core.draw(data,opengl);
  567. if (callback_post_draw)
  568. if (callback_post_draw(*this))
  569. return;
  570. for (unsigned int i = 0; i<plugins.size(); ++i)
  571. if (plugins[i]->post_draw())
  572. break;
  573. #ifdef IGL_VIEWER_WITH_NANOGUI
  574. ngui->refresh();
  575. screen->drawWidgets();
  576. #endif
  577. }
  578. IGL_INLINE bool Viewer::save_scene()
  579. {
  580. std::string fname = igl::file_dialog_save();
  581. if (fname.length() == 0)
  582. return false;
  583. #ifdef ENABLE_SERIALIZATION
  584. igl::serialize(core,"Core",fname.c_str(),true);
  585. #ifndef ENABLE_SERIALIZATION_CORE_ONLY
  586. igl::serialize(data,"Data",fname.c_str());
  587. for(unsigned int i = 0; i <plugins.size(); ++i)
  588. igl::serialize(*plugins[i],plugins[i]->plugin_name,fname.c_str());
  589. #endif
  590. #endif
  591. return true;
  592. }
  593. IGL_INLINE bool Viewer::load_scene()
  594. {
  595. std::string fname = igl::file_dialog_open();
  596. if(fname.length() == 0)
  597. return false;
  598. return load_scene(fname);
  599. }
  600. IGL_INLINE bool Viewer::load_scene(std::string fname)
  601. {
  602. #ifdef ENABLE_SERIALIZATION
  603. igl::deserialize(core,"Core",fname.c_str());
  604. #ifndef ENABLE_SERIALIZATION_CORE_ONLY
  605. igl::deserialize(data,"Data",fname.c_str());
  606. for(unsigned int i = 0; i <plugins.size(); ++i)
  607. igl::deserialize(*plugins[i],plugins[i]->plugin_name,fname.c_str());
  608. #endif
  609. #endif
  610. return true;
  611. }
  612. IGL_INLINE void Viewer::resize(int w,int h)
  613. {
  614. core.viewport = Eigen::Vector4f(0,0,w,h);
  615. }
  616. IGL_INLINE void Viewer::snap_to_canonical_quaternion()
  617. {
  618. Eigen::Quaternionf snapq = this->core.trackball_angle;
  619. igl::snap_to_canonical_view_quat(snapq,1.0f,this->core.trackball_angle);
  620. }
  621. IGL_INLINE void Viewer::open_dialog_load_mesh()
  622. {
  623. std::string fname = igl::file_dialog_open();
  624. if (fname.length() == 0)
  625. return;
  626. this->load_mesh_from_file(fname.c_str());
  627. }
  628. IGL_INLINE void Viewer::open_dialog_save_mesh()
  629. {
  630. std::string fname = igl::file_dialog_save();
  631. if(fname.length() == 0)
  632. return;
  633. this->save_mesh_to_file(fname.c_str());
  634. }
  635. IGL_INLINE int Viewer::launch_init(bool resizable,bool fullscreen)
  636. {
  637. glfwSetErrorCallback(glfw_error_callback);
  638. if (!glfwInit())
  639. return EXIT_FAILURE;
  640. glfwWindowHint(GLFW_SAMPLES, 8);
  641. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  642. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  643. #ifdef __APPLE__
  644. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  645. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  646. #endif
  647. if(fullscreen)
  648. {
  649. GLFWmonitor *monitor = glfwGetPrimaryMonitor();
  650. const GLFWvidmode *mode = glfwGetVideoMode(monitor);
  651. window = glfwCreateWindow(mode->width,mode->height,"libigl viewer",monitor,nullptr);
  652. }
  653. else
  654. {
  655. window = glfwCreateWindow(1280,800,"libigl viewer",nullptr,nullptr);
  656. }
  657. if (!window)
  658. {
  659. glfwTerminate();
  660. return EXIT_FAILURE;
  661. }
  662. glfwMakeContextCurrent(window);
  663. #ifndef __APPLE__
  664. glewExperimental = true;
  665. GLenum err = glewInit();
  666. if(GLEW_OK != err)
  667. {
  668. /* Problem: glewInit failed, something is seriously wrong. */
  669. fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
  670. }
  671. glGetError(); // pull and savely ignonre unhandled errors like GL_INVALID_ENUM
  672. fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
  673. #endif
  674. #if defined(DEBUG) || defined(_DEBUG)
  675. int major, minor, rev;
  676. major = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
  677. minor = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
  678. rev = glfwGetWindowAttrib(window, GLFW_CONTEXT_REVISION);
  679. printf("OpenGL version recieved: %d.%d.%d\n", major, minor, rev);
  680. printf("Supported OpenGL is %s\n", (const char*)glGetString(GL_VERSION));
  681. printf("Supported GLSL is %s\n", (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION));
  682. #endif
  683. glfwSetInputMode(window,GLFW_CURSOR,GLFW_CURSOR_NORMAL);
  684. // Initialize FormScreen
  685. #ifdef IGL_VIEWER_WITH_NANOGUI
  686. screen = new nanogui::Screen();
  687. screen->initialize(window, false);
  688. ngui = new nanogui::FormHelper(screen);
  689. #endif
  690. __viewer = this;
  691. // Register callbacks
  692. glfwSetKeyCallback(window, glfw_key_callback);
  693. glfwSetCursorPosCallback(window,glfw_mouse_move);
  694. glfwSetWindowSizeCallback(window,glfw_window_size);
  695. glfwSetMouseButtonCallback(window,glfw_mouse_press);
  696. glfwSetScrollCallback(window,glfw_mouse_scroll);
  697. glfwSetCharModsCallback(window,glfw_char_mods_callback);
  698. glfwSetDropCallback(window,glfw_drop_callback);
  699. // Handle retina displays (windows and mac)
  700. int width, height;
  701. glfwGetFramebufferSize(window, &width, &height);
  702. int width_window, height_window;
  703. glfwGetWindowSize(window, &width_window, &height_window);
  704. highdpi = width/width_window;
  705. glfw_window_size(window,width_window,height_window);
  706. opengl.init();
  707. core.align_camera_center(data.V,data.F);
  708. // Initialize IGL viewer
  709. init();
  710. return EXIT_SUCCESS;
  711. }
  712. IGL_INLINE bool Viewer::launch_rendering(bool loop)
  713. {
  714. // glfwMakeContextCurrent(window);
  715. // Rendering loop
  716. while (!glfwWindowShouldClose(window))
  717. {
  718. double tic = get_seconds();
  719. draw();
  720. glfwSwapBuffers(window);
  721. if(core.is_animating)
  722. {
  723. glfwPollEvents();
  724. // In microseconds
  725. double duration = 1000000.*(get_seconds()-tic);
  726. const double min_duration = 1000000./core.animation_max_fps;
  727. if(duration<min_duration)
  728. {
  729. std::this_thread::sleep_for(std::chrono::microseconds((int)(min_duration-duration)));
  730. }
  731. }
  732. else
  733. {
  734. glfwWaitEvents();
  735. }
  736. if (!loop)
  737. return !glfwWindowShouldClose(window);
  738. }
  739. return EXIT_SUCCESS;
  740. }
  741. IGL_INLINE void Viewer::launch_shut()
  742. {
  743. opengl.free();
  744. core.shut();
  745. shutdown_plugins();
  746. #ifdef IGL_VIEWER_WITH_NANOGUI
  747. delete ngui;
  748. //delete screen;
  749. screen = nullptr;
  750. ngui = nullptr;
  751. #endif
  752. glfwDestroyWindow(window);
  753. glfwTerminate();
  754. return;
  755. }
  756. IGL_INLINE int Viewer::launch(bool resizable,bool fullscreen)
  757. {
  758. // TODO return values are being ignored...
  759. launch_init(resizable,fullscreen);
  760. launch_rendering(true);
  761. launch_shut();
  762. return EXIT_SUCCESS;
  763. }
  764. } // end namespace
  765. }