Viewer.cpp 23 KB

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