Viewer.cpp 23 KB

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