Viewer.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  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. const int num_extra_frames = 5;
  242. int frame_counter = 0;
  243. while (!glfwWindowShouldClose(window))
  244. {
  245. double tic = get_seconds();
  246. draw();
  247. glfwSwapBuffers(window);
  248. if(core.is_animating || frame_counter++ < num_extra_frames)
  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. frame_counter = 0;
  263. }
  264. if (!loop)
  265. return !glfwWindowShouldClose(window);
  266. }
  267. return EXIT_SUCCESS;
  268. }
  269. IGL_INLINE void Viewer::launch_shut()
  270. {
  271. for(auto & data : data_list)
  272. {
  273. data.meshgl.free();
  274. }
  275. core.shut();
  276. shutdown_plugins();
  277. #ifdef IGL_VIEWER_WITH_NANOGUI
  278. delete ngui;
  279. //delete screen;
  280. screen = nullptr;
  281. ngui = nullptr;
  282. #endif
  283. glfwDestroyWindow(window);
  284. glfwTerminate();
  285. return;
  286. }
  287. IGL_INLINE void Viewer::init()
  288. {
  289. #ifdef IGL_VIEWER_WITH_NANOGUI
  290. using namespace nanogui;
  291. ngui->setFixedSize(Eigen::Vector2i(60,20));
  292. // Create nanogui widgets
  293. /* nanogui::Window *window = */ ngui->addWindow(Eigen::Vector2i(10,10),"libIGL-Viewer");
  294. // ---------------------- LOADING ----------------------
  295. #ifdef ENABLE_SERIALIZATION
  296. ngui->addGroup("Workspace");
  297. ngui->addButton("Load",[&](){this->load_scene();});
  298. ngui->addButton("Save",[&](){this->save_scene();});
  299. #endif
  300. #ifdef ENABLE_IO
  301. ngui->addGroup("Mesh");
  302. ngui->addButton("Load",[&](){this->open_dialog_load_mesh();});
  303. ngui->addButton("Save",[&](){this->open_dialog_save_mesh();});
  304. #endif
  305. ngui->addGroup("Viewing Options");
  306. ngui->addButton("Center object",[&](){this->core.align_camera_center(
  307. this->data().V,this->data().F);});
  308. ngui->addButton("Snap canonical view",[&]()
  309. {
  310. this->snap_to_canonical_quaternion();
  311. });
  312. ngui->addVariable("Zoom", core.camera_zoom);
  313. ngui->addVariable("Orthographic view", core.orthographic);
  314. ngui->addGroup("Draw options");
  315. ngui->addVariable<bool>("Face-based", [&](bool checked)
  316. {
  317. this->data().set_face_based(checked);
  318. },[&]()
  319. {
  320. return this->data().face_based;
  321. });
  322. ngui->addVariable("Show texture",this->data().show_texture);
  323. ngui->addVariable<bool>("Invert normals",[&](bool checked)
  324. {
  325. this->data().dirty |= MeshGL::DIRTY_NORMAL;
  326. this->data().invert_normals = checked;
  327. },[&]()
  328. {
  329. return this->data().invert_normals;
  330. });
  331. // Alec: This will probably just attach to the data() at the time
  332. // that addVariable is called. We probably need to use a callback here.
  333. ngui->addVariable("Show overlay", data().show_overlay);
  334. ngui->addVariable("Show overlay depth", data().show_overlay_depth);
  335. ngui->addVariable("Line color", (nanogui::Color &) data().line_color);
  336. ngui->addVariable("Background", (nanogui::Color &) core.background_color);
  337. ngui->addVariable("Shininess", data().shininess);
  338. ngui->addGroup("Overlays");
  339. ngui->addVariable("Wireframe", data().show_lines);
  340. ngui->addVariable("Fill", data().show_faces);
  341. ngui->addVariable("Show vertex labels", data().show_vertid);
  342. ngui->addVariable("Show faces labels", data().show_faceid);
  343. screen->setVisible(true);
  344. screen->performLayout();
  345. #endif
  346. core.init();
  347. if (callback_init)
  348. if (callback_init(*this))
  349. return;
  350. init_plugins();
  351. }
  352. IGL_INLINE void Viewer::init_plugins()
  353. {
  354. // Init all plugins
  355. for (unsigned int i = 0; i<plugins.size(); ++i)
  356. {
  357. plugins[i]->init(this);
  358. }
  359. }
  360. IGL_INLINE void Viewer::shutdown_plugins()
  361. {
  362. for (unsigned int i = 0; i<plugins.size(); ++i)
  363. {
  364. plugins[i]->shutdown();
  365. }
  366. }
  367. IGL_INLINE Viewer::Viewer():
  368. data_list(1),
  369. selected_data_index(0)
  370. {
  371. window = nullptr;
  372. #ifdef IGL_VIEWER_WITH_NANOGUI
  373. ngui = nullptr;
  374. screen = nullptr;
  375. #endif
  376. // Temporary variables initialization
  377. down = false;
  378. hack_never_moved = true;
  379. scroll_position = 0.0f;
  380. // Per face
  381. data().set_face_based(false);
  382. // C-style callbacks
  383. callback_init = nullptr;
  384. callback_pre_draw = nullptr;
  385. callback_post_draw = nullptr;
  386. callback_mouse_down = nullptr;
  387. callback_mouse_up = nullptr;
  388. callback_mouse_move = nullptr;
  389. callback_mouse_scroll = nullptr;
  390. callback_key_down = nullptr;
  391. callback_key_up = nullptr;
  392. callback_init_data = nullptr;
  393. callback_pre_draw_data = nullptr;
  394. callback_post_draw_data = nullptr;
  395. callback_mouse_down_data = nullptr;
  396. callback_mouse_up_data = nullptr;
  397. callback_mouse_move_data = nullptr;
  398. callback_mouse_scroll_data = nullptr;
  399. callback_key_down_data = nullptr;
  400. callback_key_up_data = nullptr;
  401. #ifndef IGL_VIEWER_VIEWER_QUIET
  402. const std::string usage(R"(igl::opengl::glfw::Viewer usage:
  403. [drag] Rotate scene
  404. A,a Toggle animation (tight draw loop)
  405. F,f Toggle face based
  406. I,i Toggle invert normals
  407. L,l Toggle wireframe
  408. O,o Toggle orthographic/perspective projection
  409. T,t Toggle filled faces
  410. Z Snap to canonical view
  411. [,] Toggle between rotation control types (trackball, two-axis
  412. valuator with fixed up, 2D mode with no rotation))
  413. <,> Toggle between models
  414. ; Toggle vertex labels
  415. : Toggle face labels)"
  416. );
  417. std::cout<<usage<<std::endl;
  418. #endif
  419. }
  420. IGL_INLINE Viewer::~Viewer()
  421. {
  422. }
  423. IGL_INLINE bool Viewer::load_mesh_from_file(
  424. const std::string & mesh_file_name_string)
  425. {
  426. // first try to load it with a plugin
  427. for (unsigned int i = 0; i<plugins.size(); ++i)
  428. {
  429. if (plugins[i]->load(mesh_file_name_string))
  430. {
  431. return true;
  432. }
  433. }
  434. // Create new data slot and set to selected
  435. if(!(data().F.rows() == 0 && data().V.rows() == 0))
  436. {
  437. append_mesh();
  438. }
  439. data().clear();
  440. size_t last_dot = mesh_file_name_string.rfind('.');
  441. if (last_dot == std::string::npos)
  442. {
  443. std::cerr<<"Error: No file extension found in "<<
  444. mesh_file_name_string<<std::endl;
  445. return false;
  446. }
  447. std::string extension = mesh_file_name_string.substr(last_dot+1);
  448. if (extension == "off" || extension =="OFF")
  449. {
  450. Eigen::MatrixXd V;
  451. Eigen::MatrixXi F;
  452. if (!igl::readOFF(mesh_file_name_string, V, F))
  453. return false;
  454. data().set_mesh(V,F);
  455. }
  456. else if (extension == "obj" || extension =="OBJ")
  457. {
  458. Eigen::MatrixXd corner_normals;
  459. Eigen::MatrixXi fNormIndices;
  460. Eigen::MatrixXd UV_V;
  461. Eigen::MatrixXi UV_F;
  462. Eigen::MatrixXd V;
  463. Eigen::MatrixXi F;
  464. if (!(
  465. igl::readOBJ(
  466. mesh_file_name_string,
  467. V, UV_V, corner_normals, F, UV_F, fNormIndices)))
  468. {
  469. return false;
  470. }
  471. data().set_mesh(V,F);
  472. data().set_uv(UV_V,UV_F);
  473. }
  474. else
  475. {
  476. // unrecognized file type
  477. printf("Error: %s is not a recognized file type.\n",extension.c_str());
  478. return false;
  479. }
  480. data().compute_normals();
  481. data().uniform_colors(Eigen::Vector3d(51.0/255.0,43.0/255.0,33.3/255.0),
  482. Eigen::Vector3d(255.0/255.0,228.0/255.0,58.0/255.0),
  483. Eigen::Vector3d(255.0/255.0,235.0/255.0,80.0/255.0));
  484. if (data().V_uv.rows() == 0)
  485. {
  486. data().grid_texture();
  487. }
  488. core.align_camera_center(data().V,data().F);
  489. for (unsigned int i = 0; i<plugins.size(); ++i)
  490. if (plugins[i]->post_load())
  491. return true;
  492. return true;
  493. }
  494. IGL_INLINE bool Viewer::save_mesh_to_file(
  495. const std::string & mesh_file_name_string)
  496. {
  497. // first try to load it with a plugin
  498. for (unsigned int i = 0; i<plugins.size(); ++i)
  499. if (plugins[i]->save(mesh_file_name_string))
  500. return true;
  501. size_t last_dot = mesh_file_name_string.rfind('.');
  502. if (last_dot == std::string::npos)
  503. {
  504. // No file type determined
  505. std::cerr<<"Error: No file extension found in "<<
  506. mesh_file_name_string<<std::endl;
  507. return false;
  508. }
  509. std::string extension = mesh_file_name_string.substr(last_dot+1);
  510. if (extension == "off" || extension =="OFF")
  511. {
  512. return igl::writeOFF(
  513. mesh_file_name_string,data().V,data().F);
  514. }
  515. else if (extension == "obj" || extension =="OBJ")
  516. {
  517. Eigen::MatrixXd corner_normals;
  518. Eigen::MatrixXi fNormIndices;
  519. Eigen::MatrixXd UV_V;
  520. Eigen::MatrixXi UV_F;
  521. return igl::writeOBJ(mesh_file_name_string,
  522. data().V,
  523. data().F,
  524. corner_normals, fNormIndices, UV_V, UV_F);
  525. }
  526. else
  527. {
  528. // unrecognized file type
  529. printf("Error: %s is not a recognized file type.\n",extension.c_str());
  530. return false;
  531. }
  532. return true;
  533. }
  534. IGL_INLINE bool Viewer::key_pressed(unsigned int unicode_key,int modifiers)
  535. {
  536. if (callback_key_pressed)
  537. if (callback_key_pressed(*this,unicode_key,modifiers))
  538. return true;
  539. for (unsigned int i = 0; i<plugins.size(); ++i)
  540. {
  541. if (plugins[i]->key_pressed(unicode_key, modifiers))
  542. {
  543. return true;
  544. }
  545. }
  546. switch(unicode_key)
  547. {
  548. case 'A':
  549. case 'a':
  550. {
  551. core.is_animating = !core.is_animating;
  552. return true;
  553. }
  554. case 'F':
  555. case 'f':
  556. {
  557. data().set_face_based(!data().face_based);
  558. return true;
  559. }
  560. case 'I':
  561. case 'i':
  562. {
  563. data().dirty |= MeshGL::DIRTY_NORMAL;
  564. data().invert_normals = !data().invert_normals;
  565. return true;
  566. }
  567. case 'L':
  568. case 'l':
  569. {
  570. data().show_lines = !data().show_lines;
  571. return true;
  572. }
  573. case 'O':
  574. case 'o':
  575. {
  576. core.orthographic = !core.orthographic;
  577. return true;
  578. }
  579. case 'T':
  580. case 't':
  581. {
  582. data().show_faces = !data().show_faces;
  583. return true;
  584. }
  585. case 'Z':
  586. {
  587. snap_to_canonical_quaternion();
  588. return true;
  589. }
  590. case '[':
  591. case ']':
  592. {
  593. if(core.rotation_type == ViewerCore::ROTATION_TYPE_TRACKBALL)
  594. core.set_rotation_type(ViewerCore::ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP);
  595. else
  596. core.set_rotation_type(ViewerCore::ROTATION_TYPE_TRACKBALL);
  597. return true;
  598. }
  599. case '<':
  600. case '>':
  601. {
  602. selected_data_index =
  603. (selected_data_index + data_list.size() + (unicode_key=='>'?1:-1))%data_list.size();
  604. return true;
  605. }
  606. case ';':
  607. data().show_vertid = !data().show_vertid;
  608. return true;
  609. case ':':
  610. data().show_faceid = !data().show_faceid;
  611. return true;
  612. default: break;//do nothing
  613. }
  614. return false;
  615. }
  616. IGL_INLINE bool Viewer::key_down(int key,int modifiers)
  617. {
  618. if (callback_key_down)
  619. if (callback_key_down(*this,key,modifiers))
  620. return true;
  621. for (unsigned int i = 0; i<plugins.size(); ++i)
  622. if (plugins[i]->key_down(key, modifiers))
  623. return true;
  624. return false;
  625. }
  626. IGL_INLINE bool Viewer::key_up(int key,int modifiers)
  627. {
  628. if (callback_key_up)
  629. if (callback_key_up(*this,key,modifiers))
  630. return true;
  631. for (unsigned int i = 0; i<plugins.size(); ++i)
  632. if (plugins[i]->key_up(key, modifiers))
  633. return true;
  634. return false;
  635. }
  636. IGL_INLINE bool Viewer::mouse_down(MouseButton button,int modifier)
  637. {
  638. // Remember mouse location at down even if used by callback/plugin
  639. down_mouse_x = current_mouse_x;
  640. down_mouse_y = current_mouse_y;
  641. if (callback_mouse_down)
  642. if (callback_mouse_down(*this,static_cast<int>(button),modifier))
  643. return true;
  644. for (unsigned int i = 0; i<plugins.size(); ++i)
  645. if(plugins[i]->mouse_down(static_cast<int>(button),modifier))
  646. return true;
  647. down = true;
  648. down_translation = core.model_translation;
  649. // Initialization code for the trackball
  650. Eigen::RowVector3d center;
  651. if (data().V.rows() == 0)
  652. {
  653. center << 0,0,0;
  654. }else
  655. {
  656. center = data().V.colwise().sum()/data().V.rows();
  657. }
  658. Eigen::Vector3f coord =
  659. igl::project(
  660. Eigen::Vector3f(center(0),center(1),center(2)),
  661. (core.view * core.model).eval(),
  662. core.proj,
  663. core.viewport);
  664. down_mouse_z = coord[2];
  665. down_rotation = core.trackball_angle;
  666. mouse_mode = MouseMode::Rotation;
  667. switch (button)
  668. {
  669. case MouseButton::Left:
  670. if (core.rotation_type == ViewerCore::ROTATION_TYPE_NO_ROTATION) {
  671. mouse_mode = MouseMode::Translation;
  672. } else {
  673. mouse_mode = MouseMode::Rotation;
  674. }
  675. break;
  676. case MouseButton::Right:
  677. mouse_mode = MouseMode::Translation;
  678. break;
  679. default:
  680. mouse_mode = MouseMode::None;
  681. break;
  682. }
  683. return true;
  684. }
  685. IGL_INLINE bool Viewer::mouse_up(MouseButton button,int modifier)
  686. {
  687. down = false;
  688. if (callback_mouse_up)
  689. if (callback_mouse_up(*this,static_cast<int>(button),modifier))
  690. return true;
  691. for (unsigned int i = 0; i<plugins.size(); ++i)
  692. if(plugins[i]->mouse_up(static_cast<int>(button),modifier))
  693. return true;
  694. mouse_mode = MouseMode::None;
  695. return true;
  696. }
  697. IGL_INLINE bool Viewer::mouse_move(int mouse_x,int mouse_y)
  698. {
  699. if(hack_never_moved)
  700. {
  701. down_mouse_x = mouse_x;
  702. down_mouse_y = mouse_y;
  703. hack_never_moved = false;
  704. }
  705. current_mouse_x = mouse_x;
  706. current_mouse_y = mouse_y;
  707. if (callback_mouse_move)
  708. if (callback_mouse_move(*this,mouse_x,mouse_y))
  709. return true;
  710. for (unsigned int i = 0; i<plugins.size(); ++i)
  711. if (plugins[i]->mouse_move(mouse_x, mouse_y))
  712. return true;
  713. if (down)
  714. {
  715. switch (mouse_mode)
  716. {
  717. case MouseMode::Rotation:
  718. {
  719. switch(core.rotation_type)
  720. {
  721. default:
  722. assert(false && "Unknown rotation type");
  723. case ViewerCore::ROTATION_TYPE_NO_ROTATION:
  724. break;
  725. case ViewerCore::ROTATION_TYPE_TRACKBALL:
  726. igl::trackball(
  727. core.viewport(2),
  728. core.viewport(3),
  729. 2.0f,
  730. down_rotation,
  731. down_mouse_x,
  732. down_mouse_y,
  733. mouse_x,
  734. mouse_y,
  735. core.trackball_angle);
  736. break;
  737. case ViewerCore::ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP:
  738. igl::two_axis_valuator_fixed_up(
  739. core.viewport(2),core.viewport(3),
  740. 2.0,
  741. down_rotation,
  742. down_mouse_x, down_mouse_y, mouse_x, mouse_y,
  743. core.trackball_angle);
  744. break;
  745. }
  746. //Eigen::Vector4f snapq = core.trackball_angle;
  747. break;
  748. }
  749. case MouseMode::Translation:
  750. {
  751. //translation
  752. 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);
  753. 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);
  754. Eigen::Vector3f diff = pos1 - pos0;
  755. core.model_translation = down_translation + Eigen::Vector3f(diff[0],diff[1],diff[2]);
  756. break;
  757. }
  758. case MouseMode::Zoom:
  759. {
  760. float delta = 0.001f * (mouse_x - down_mouse_x + mouse_y - down_mouse_y);
  761. core.camera_zoom *= 1 + delta;
  762. down_mouse_x = mouse_x;
  763. down_mouse_y = mouse_y;
  764. break;
  765. }
  766. default:
  767. break;
  768. }
  769. }
  770. return true;
  771. }
  772. IGL_INLINE bool Viewer::mouse_scroll(float delta_y)
  773. {
  774. scroll_position += delta_y;
  775. if (callback_mouse_scroll)
  776. if (callback_mouse_scroll(*this,delta_y))
  777. return true;
  778. for (unsigned int i = 0; i<plugins.size(); ++i)
  779. if (plugins[i]->mouse_scroll(delta_y))
  780. return true;
  781. // Only zoom if there's actually a change
  782. if(delta_y != 0)
  783. {
  784. float mult = (1.0+((delta_y>0)?1.:-1.)*0.05);
  785. const float min_zoom = 0.1f;
  786. core.camera_zoom = (core.camera_zoom * mult > min_zoom ? core.camera_zoom * mult : min_zoom);
  787. }
  788. return true;
  789. }
  790. IGL_INLINE bool Viewer::load_scene()
  791. {
  792. std::string fname = igl::file_dialog_open();
  793. if(fname.length() == 0)
  794. {
  795. return false;
  796. }
  797. return load_scene(fname);
  798. }
  799. IGL_INLINE bool Viewer::load_scene(std::string fname)
  800. {
  801. #ifdef ENABLE_SERIALIZATION
  802. igl::deserialize(core,"Core",fname.c_str());
  803. #ifndef ENABLE_SERIALIZATION_CORE_ONLY
  804. igl::deserialize(data(),"Data",fname.c_str());
  805. for(unsigned int i = 0; i <plugins.size(); ++i)
  806. {
  807. igl::deserialize(*plugins[i],plugins[i]->plugin_name,fname.c_str());
  808. }
  809. #endif
  810. #endif
  811. return true;
  812. }
  813. IGL_INLINE bool Viewer::save_scene()
  814. {
  815. std::string fname = igl::file_dialog_save();
  816. if (fname.length() == 0)
  817. return false;
  818. #ifdef ENABLE_SERIALIZATION
  819. igl::serialize(core,"Core",fname.c_str(),true);
  820. #ifndef ENABLE_SERIALIZATION_CORE_ONLY
  821. igl::serialize(data(),"Data",fname.c_str());
  822. for(unsigned int i = 0; i <plugins.size(); ++i)
  823. igl::serialize(*plugins[i],plugins[i]->plugin_name,fname.c_str());
  824. #endif
  825. #endif
  826. return true;
  827. }
  828. IGL_INLINE void Viewer::draw()
  829. {
  830. using namespace std;
  831. using namespace Eigen;
  832. int width, height;
  833. glfwGetFramebufferSize(window, &width, &height);
  834. int width_window, height_window;
  835. glfwGetWindowSize(window, &width_window, &height_window);
  836. auto highdpi_tmp = width/width_window;
  837. if(fabs(highdpi_tmp-highdpi)>1e-8)
  838. {
  839. post_resize(width, height);
  840. highdpi=highdpi_tmp;
  841. }
  842. core.clear_framebuffers();
  843. if (callback_pre_draw)
  844. {
  845. if (callback_pre_draw(*this))
  846. {
  847. return;
  848. }
  849. }
  850. for (unsigned int i = 0; i<plugins.size(); ++i)
  851. {
  852. if (plugins[i]->pre_draw())
  853. {
  854. return;
  855. }
  856. }
  857. for(int i = 0;i<data_list.size();i++)
  858. {
  859. core.draw(data_list[i]);
  860. }
  861. if (callback_post_draw)
  862. {
  863. if (callback_post_draw(*this))
  864. {
  865. return;
  866. }
  867. }
  868. for (unsigned int i = 0; i<plugins.size(); ++i)
  869. {
  870. if (plugins[i]->post_draw())
  871. {
  872. break;
  873. }
  874. }
  875. #ifdef IGL_VIEWER_WITH_NANOGUI
  876. screen->drawContents();
  877. screen->drawWidgets();
  878. #endif
  879. }
  880. IGL_INLINE void Viewer::resize(int w,int h)
  881. {
  882. if (window) {
  883. glfwSetWindowSize(window, w/highdpi, h/highdpi);
  884. }
  885. post_resize(w, h);
  886. }
  887. IGL_INLINE void Viewer::post_resize(int w,int h)
  888. {
  889. core.viewport = Eigen::Vector4f(0,0,w,h);
  890. for (unsigned int i = 0; i<plugins.size(); ++i)
  891. {
  892. plugins[i]->post_resize(w, h);
  893. }
  894. }
  895. IGL_INLINE void Viewer::snap_to_canonical_quaternion()
  896. {
  897. Eigen::Quaternionf snapq = this->core.trackball_angle;
  898. igl::snap_to_canonical_view_quat(snapq,1.0f,this->core.trackball_angle);
  899. }
  900. IGL_INLINE void Viewer::open_dialog_load_mesh()
  901. {
  902. std::string fname = igl::file_dialog_open();
  903. if (fname.length() == 0)
  904. return;
  905. this->load_mesh_from_file(fname.c_str());
  906. }
  907. IGL_INLINE void Viewer::open_dialog_save_mesh()
  908. {
  909. std::string fname = igl::file_dialog_save();
  910. if(fname.length() == 0)
  911. return;
  912. this->save_mesh_to_file(fname.c_str());
  913. }
  914. IGL_INLINE ViewerData& Viewer::data()
  915. {
  916. assert(!data_list.empty() && "data_list should never be empty");
  917. assert(
  918. (selected_data_index >= 0 && selected_data_index < data_list.size()) &&
  919. "selected_data_index should be in bounds");
  920. return data_list[selected_data_index];
  921. }
  922. IGL_INLINE size_t Viewer::append_mesh()
  923. {
  924. assert(data_list.size() >= 1);
  925. data_list.emplace_back();
  926. selected_data_index = data_list.size()-1;
  927. return data_list.size();
  928. }
  929. IGL_INLINE bool Viewer::erase_mesh(const size_t index)
  930. {
  931. assert((index >= 0 && index < data_list.size()) && "index should be in bounds");
  932. assert(data_list.size() >= 1);
  933. if(data_list.size() == 1)
  934. {
  935. // Cannot remove last mesh
  936. return false;
  937. }
  938. data_list[index].meshgl.free();
  939. data_list.erase(data_list.begin() + index);
  940. if(selected_data_index >= index && selected_data_index>0)
  941. {
  942. selected_data_index--;
  943. }
  944. return true;
  945. }
  946. } // end namespace
  947. } // end namespace
  948. }