Viewer.cpp 25 KB

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