Viewer.cpp 28 KB

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