Viewer.cpp 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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. // We need the window height to transform the mouse click coordinates into viewport-mouse-click coordinates
  627. // for igl::trackball and igl::two_axis_valuator_fixed_up
  628. int width_window, height_window;
  629. glfwGetWindowSize(window, &width_window, &height_window);
  630. switch (mouse_mode)
  631. {
  632. case MouseMode::Rotation:
  633. {
  634. switch(core().rotation_type)
  635. {
  636. default:
  637. assert(false && "Unknown rotation type");
  638. case ViewerCore::ROTATION_TYPE_NO_ROTATION:
  639. break;
  640. case ViewerCore::ROTATION_TYPE_TRACKBALL:
  641. igl::trackball(
  642. core().viewport(2),
  643. core().viewport(3),
  644. 2.0f,
  645. down_rotation,
  646. down_mouse_x - core().viewport(0),
  647. down_mouse_y - (height_window - core().viewport(1) - core().viewport(3)),
  648. mouse_x - core().viewport(0),
  649. mouse_y - (height_window - core().viewport(1) - core().viewport(3)),
  650. core().trackball_angle);
  651. break;
  652. case ViewerCore::ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP:
  653. igl::two_axis_valuator_fixed_up(
  654. core().viewport(2),core().viewport(3),
  655. 2.0,
  656. down_rotation,
  657. down_mouse_x - core().viewport(0),
  658. down_mouse_y - (height_window - core().viewport(1) - core().viewport(3)),
  659. mouse_x - core().viewport(0),
  660. mouse_y - (height_window - core().viewport(1) - core().viewport(3)),
  661. core().trackball_angle);
  662. break;
  663. }
  664. //Eigen::Vector4f snapq = core().trackball_angle;
  665. break;
  666. }
  667. case MouseMode::Translation:
  668. {
  669. //translation
  670. Eigen::Vector3f pos1 = igl::unproject(Eigen::Vector3f(mouse_x, core().viewport[3] - mouse_y, down_mouse_z), core().view, core().proj, core().viewport);
  671. 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);
  672. Eigen::Vector3f diff = pos1 - pos0;
  673. core().camera_translation = down_translation + Eigen::Vector3f(diff[0],diff[1],diff[2]);
  674. break;
  675. }
  676. case MouseMode::Zoom:
  677. {
  678. float delta = 0.001f * (mouse_x - down_mouse_x + mouse_y - down_mouse_y);
  679. core().camera_zoom *= 1 + delta;
  680. down_mouse_x = mouse_x;
  681. down_mouse_y = mouse_y;
  682. break;
  683. }
  684. default:
  685. break;
  686. }
  687. }
  688. return true;
  689. }
  690. IGL_INLINE bool Viewer::mouse_scroll(float delta_y)
  691. {
  692. // Direct the scrolling operation to the appropriate viewport
  693. // (unless the core selection is locked by an ongoing mouse interaction).
  694. if (!down)
  695. select_hovered_core();
  696. scroll_position += delta_y;
  697. for (unsigned int i = 0; i<plugins.size(); ++i)
  698. if (plugins[i]->mouse_scroll(delta_y))
  699. return true;
  700. if (callback_mouse_scroll)
  701. if (callback_mouse_scroll(*this,delta_y))
  702. return true;
  703. // Only zoom if there's actually a change
  704. if(delta_y != 0)
  705. {
  706. float mult = (1.0+((delta_y>0)?1.:-1.)*0.05);
  707. const float min_zoom = 0.1f;
  708. core().camera_zoom = (core().camera_zoom * mult > min_zoom ? core().camera_zoom * mult : min_zoom);
  709. }
  710. return true;
  711. }
  712. IGL_INLINE bool Viewer::load_scene()
  713. {
  714. std::string fname = igl::file_dialog_open();
  715. if(fname.length() == 0)
  716. return false;
  717. return load_scene(fname);
  718. }
  719. IGL_INLINE bool Viewer::load_scene(std::string fname)
  720. {
  721. igl::deserialize(core(),"Core",fname.c_str());
  722. igl::deserialize(data(),"Data",fname.c_str());
  723. return true;
  724. }
  725. IGL_INLINE bool Viewer::save_scene()
  726. {
  727. std::string fname = igl::file_dialog_save();
  728. if (fname.length() == 0)
  729. return false;
  730. return save_scene(fname);
  731. }
  732. IGL_INLINE bool Viewer::save_scene(std::string fname)
  733. {
  734. igl::serialize(core(),"Core",fname.c_str(),true);
  735. igl::serialize(data(),"Data",fname.c_str());
  736. return true;
  737. }
  738. IGL_INLINE void Viewer::draw()
  739. {
  740. using namespace std;
  741. using namespace Eigen;
  742. int width, height;
  743. glfwGetFramebufferSize(window, &width, &height);
  744. int width_window, height_window;
  745. glfwGetWindowSize(window, &width_window, &height_window);
  746. auto highdpi_tmp = (width_window == 0 || width == 0) ? highdpi : (width/width_window);
  747. if(fabs(highdpi_tmp-highdpi)>1e-8)
  748. {
  749. post_resize(width, height);
  750. highdpi=highdpi_tmp;
  751. }
  752. for (auto& core : core_list)
  753. {
  754. core.clear_framebuffers();
  755. }
  756. for (unsigned int i = 0; i<plugins.size(); ++i)
  757. {
  758. if (plugins[i]->pre_draw())
  759. {
  760. return;
  761. }
  762. }
  763. if (callback_pre_draw)
  764. {
  765. if (callback_pre_draw(*this))
  766. {
  767. return;
  768. }
  769. }
  770. for (auto& core : core_list)
  771. {
  772. for (auto& mesh : data_list)
  773. {
  774. if (mesh.is_visible & core.id)
  775. {
  776. core.draw(mesh);
  777. }
  778. }
  779. }
  780. for (unsigned int i = 0; i<plugins.size(); ++i)
  781. {
  782. if (plugins[i]->post_draw())
  783. {
  784. break;
  785. }
  786. }
  787. if (callback_post_draw)
  788. {
  789. if (callback_post_draw(*this))
  790. {
  791. return;
  792. }
  793. }
  794. }
  795. IGL_INLINE void Viewer::resize(int w,int h)
  796. {
  797. if (window) {
  798. glfwSetWindowSize(window, w/highdpi, h/highdpi);
  799. }
  800. post_resize(w, h);
  801. }
  802. IGL_INLINE void Viewer::post_resize(int w,int h)
  803. {
  804. if (core_list.size() == 1)
  805. {
  806. core().viewport = Eigen::Vector4f(0,0,w,h);
  807. }
  808. else
  809. {
  810. // It is up to the user to define the behavior of the post_resize() function
  811. // when there are multiple viewports (through the `callback_post_resize` callback)
  812. }
  813. for (unsigned int i = 0; i<plugins.size(); ++i)
  814. {
  815. plugins[i]->post_resize(w, h);
  816. }
  817. if (callback_post_resize)
  818. {
  819. callback_post_resize(*this, w, h);
  820. }
  821. }
  822. IGL_INLINE void Viewer::snap_to_canonical_quaternion()
  823. {
  824. Eigen::Quaternionf snapq = this->core().trackball_angle;
  825. igl::snap_to_canonical_view_quat(snapq,1.0f,this->core().trackball_angle);
  826. }
  827. IGL_INLINE void Viewer::open_dialog_load_mesh()
  828. {
  829. std::string fname = igl::file_dialog_open();
  830. if (fname.length() == 0)
  831. return;
  832. this->load_mesh_from_file(fname.c_str());
  833. }
  834. IGL_INLINE void Viewer::open_dialog_save_mesh()
  835. {
  836. std::string fname = igl::file_dialog_save();
  837. if(fname.length() == 0)
  838. return;
  839. this->save_mesh_to_file(fname.c_str());
  840. }
  841. IGL_INLINE ViewerData& Viewer::data(int mesh_id /*= -1*/)
  842. {
  843. assert(!data_list.empty() && "data_list should never be empty");
  844. int index;
  845. if (mesh_id == -1)
  846. index = selected_data_index;
  847. else
  848. index = mesh_index(mesh_id);
  849. assert((index >= 0 && index < data_list.size()) &&
  850. "selected_data_index or mesh_id should be in bounds");
  851. return data_list[index];
  852. }
  853. IGL_INLINE int Viewer::append_mesh(bool visible /*= true*/)
  854. {
  855. assert(data_list.size() >= 1);
  856. data_list.emplace_back();
  857. selected_data_index = data_list.size()-1;
  858. data_list.back().id = next_data_id++;
  859. if (visible)
  860. for (int i = 0; i < core_list.size(); i++)
  861. data_list.back().set_visible(true, core_list[i].id);
  862. else
  863. data_list.back().is_visible = 0;
  864. return data_list.back().id;
  865. }
  866. IGL_INLINE bool Viewer::erase_mesh(const size_t index)
  867. {
  868. assert((index >= 0 && index < data_list.size()) && "index should be in bounds");
  869. assert(data_list.size() >= 1);
  870. if(data_list.size() == 1)
  871. {
  872. // Cannot remove last mesh
  873. return false;
  874. }
  875. data_list[index].meshgl.free();
  876. data_list.erase(data_list.begin() + index);
  877. if(selected_data_index >= index && selected_data_index>0)
  878. {
  879. selected_data_index--;
  880. }
  881. return true;
  882. }
  883. IGL_INLINE size_t Viewer::mesh_index(const int id) const {
  884. for (size_t i = 0; i < data_list.size(); ++i)
  885. {
  886. if (data_list[i].id == id)
  887. return i;
  888. }
  889. return 0;
  890. }
  891. IGL_INLINE ViewerCore& Viewer::core(unsigned core_id /*= 0*/)
  892. {
  893. assert(!core_list.empty() && "core_list should never be empty");
  894. int core_ind;
  895. if (core_id == 0)
  896. core_ind = selected_core_index;
  897. else
  898. core_ind = core_index(core_id);
  899. assert((core_ind >= 0 && core_ind < core_list.size()) && "selected_core_index should be in bounds");
  900. return core_list[core_ind];
  901. }
  902. IGL_INLINE bool Viewer::erase_core(const size_t index)
  903. {
  904. assert((index >= 0 && index < core_list.size()) && "index should be in bounds");
  905. assert(data_list.size() >= 1);
  906. if (core_list.size() == 1)
  907. {
  908. // Cannot remove last mesh
  909. return false;
  910. }
  911. core_list[index].shut(); // does nothing
  912. core_list.erase(core_list.begin() + index);
  913. if (selected_core_index >= index && selected_core_index > 0)
  914. {
  915. selected_core_index>>=1;
  916. }
  917. return true;
  918. }
  919. IGL_INLINE size_t Viewer::core_index(const int id) const {
  920. for (size_t i = 0; i < core_list.size(); ++i)
  921. {
  922. if (core_list[i].id == id)
  923. return i;
  924. }
  925. return 0;
  926. }
  927. IGL_INLINE int Viewer::append_core(Eigen::Vector4f viewport, bool append_empty /*= false*/)
  928. {
  929. core_list.push_back(core()); //copies the previous core and only changes the viewport
  930. core_list.back().viewport = viewport;
  931. core_list.back().id = next_core_id;
  932. next_core_id <<= 1;
  933. if(!append_empty)
  934. for (auto &data : data_list)
  935. data.set_visible(true,core_list.back().id);
  936. return core_list.back().id;
  937. }
  938. } // end namespace
  939. } // end namespace
  940. }