Viewer.cpp 22 KB

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