Viewer.cpp 23 KB

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