Viewer.cpp 28 KB

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