Viewer.cpp 28 KB

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