Viewer.cpp 23 KB

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