Viewer.cpp 29 KB

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