Viewer.cpp 22 KB

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