Viewer.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  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. data().show_lines = !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. data().show_faces = !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. data().show_vertid = !data().show_vertid;
  502. return true;
  503. case ':':
  504. data().show_faceid = !data().show_faceid;
  505. return true;
  506. default: break;//do nothing
  507. }
  508. return false;
  509. }
  510. IGL_INLINE bool Viewer::key_down(int key,int modifiers)
  511. {
  512. for (unsigned int i = 0; i<plugins.size(); ++i)
  513. if (plugins[i]->key_down(key, modifiers))
  514. return true;
  515. if (callback_key_down)
  516. if (callback_key_down(*this,key,modifiers))
  517. return true;
  518. return false;
  519. }
  520. IGL_INLINE bool Viewer::key_up(int key,int modifiers)
  521. {
  522. for (unsigned int i = 0; i<plugins.size(); ++i)
  523. if (plugins[i]->key_up(key, modifiers))
  524. return true;
  525. if (callback_key_up)
  526. if (callback_key_up(*this,key,modifiers))
  527. return true;
  528. return false;
  529. }
  530. IGL_INLINE void Viewer::select_hovered_core()
  531. {
  532. int width_window, height_window;
  533. glfwGetFramebufferSize(window, &width_window, &height_window);
  534. for (int i = 0; i < core_list.size(); i++)
  535. {
  536. Eigen::Vector4f viewport = core_list[i].viewport;
  537. if ((current_mouse_x > viewport[0]) &&
  538. (current_mouse_x < viewport[0] + viewport[2]) &&
  539. ((height_window - current_mouse_y) > viewport[1]) &&
  540. ((height_window - current_mouse_y) < viewport[1] + viewport[3]))
  541. {
  542. selected_core_index = i;
  543. break;
  544. }
  545. }
  546. }
  547. IGL_INLINE bool Viewer::mouse_down(MouseButton button,int modifier)
  548. {
  549. // Remember mouse location at down even if used by callback/plugin
  550. down_mouse_x = current_mouse_x;
  551. down_mouse_y = current_mouse_y;
  552. for (unsigned int i = 0; i<plugins.size(); ++i)
  553. if(plugins[i]->mouse_down(static_cast<int>(button),modifier))
  554. return true;
  555. if (callback_mouse_down)
  556. if (callback_mouse_down(*this,static_cast<int>(button),modifier))
  557. return true;
  558. down = true;
  559. // Select the core containing the click location.
  560. select_hovered_core();
  561. down_translation = core().camera_translation;
  562. // Initialization code for the trackball
  563. Eigen::RowVector3d center;
  564. if (data().V.rows() == 0)
  565. {
  566. center << 0,0,0;
  567. }else
  568. {
  569. center = data().V.colwise().sum()/data().V.rows();
  570. }
  571. Eigen::Vector3f coord =
  572. igl::project(
  573. Eigen::Vector3f(center(0),center(1),center(2)),
  574. core().view,
  575. core().proj,
  576. core().viewport);
  577. down_mouse_z = coord[2];
  578. down_rotation = core().trackball_angle;
  579. mouse_mode = MouseMode::Rotation;
  580. switch (button)
  581. {
  582. case MouseButton::Left:
  583. if (core().rotation_type == ViewerCore::ROTATION_TYPE_NO_ROTATION) {
  584. mouse_mode = MouseMode::Translation;
  585. } else {
  586. mouse_mode = MouseMode::Rotation;
  587. }
  588. break;
  589. case MouseButton::Right:
  590. mouse_mode = MouseMode::Translation;
  591. break;
  592. default:
  593. mouse_mode = MouseMode::None;
  594. break;
  595. }
  596. return true;
  597. }
  598. IGL_INLINE bool Viewer::mouse_up(MouseButton button,int modifier)
  599. {
  600. down = false;
  601. for (unsigned int i = 0; i<plugins.size(); ++i)
  602. if(plugins[i]->mouse_up(static_cast<int>(button),modifier))
  603. return true;
  604. if (callback_mouse_up)
  605. if (callback_mouse_up(*this,static_cast<int>(button),modifier))
  606. return true;
  607. mouse_mode = MouseMode::None;
  608. return true;
  609. }
  610. IGL_INLINE bool Viewer::mouse_move(int mouse_x,int mouse_y)
  611. {
  612. if(hack_never_moved)
  613. {
  614. down_mouse_x = mouse_x;
  615. down_mouse_y = mouse_y;
  616. hack_never_moved = false;
  617. }
  618. current_mouse_x = mouse_x;
  619. current_mouse_y = mouse_y;
  620. for (unsigned int i = 0; i<plugins.size(); ++i)
  621. if (plugins[i]->mouse_move(mouse_x, mouse_y))
  622. return true;
  623. if (callback_mouse_move)
  624. if (callback_mouse_move(*this, mouse_x, mouse_y))
  625. return true;
  626. if (down)
  627. {
  628. // We need the window height to transform the mouse click coordinates into viewport-mouse-click coordinates
  629. // for igl::trackball and igl::two_axis_valuator_fixed_up
  630. int width_window, height_window;
  631. glfwGetFramebufferSize(window, &width_window, &height_window);
  632. switch (mouse_mode)
  633. {
  634. case MouseMode::Rotation:
  635. {
  636. switch(core().rotation_type)
  637. {
  638. default:
  639. assert(false && "Unknown rotation type");
  640. case ViewerCore::ROTATION_TYPE_NO_ROTATION:
  641. break;
  642. case ViewerCore::ROTATION_TYPE_TRACKBALL:
  643. igl::trackball(
  644. core().viewport(2),
  645. core().viewport(3),
  646. 2.0f,
  647. down_rotation,
  648. down_mouse_x - core().viewport(0),
  649. down_mouse_y - (height_window - core().viewport(1) - core().viewport(3)),
  650. mouse_x - core().viewport(0),
  651. mouse_y - (height_window - core().viewport(1) - core().viewport(3)),
  652. core().trackball_angle);
  653. break;
  654. case ViewerCore::ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP:
  655. igl::two_axis_valuator_fixed_up(
  656. core().viewport(2),core().viewport(3),
  657. 2.0,
  658. down_rotation,
  659. down_mouse_x - core().viewport(0),
  660. down_mouse_y - (height_window - core().viewport(1) - core().viewport(3)),
  661. mouse_x - core().viewport(0),
  662. mouse_y - (height_window - core().viewport(1) - core().viewport(3)),
  663. core().trackball_angle);
  664. break;
  665. }
  666. //Eigen::Vector4f snapq = core().trackball_angle;
  667. break;
  668. }
  669. case MouseMode::Translation:
  670. {
  671. //translation
  672. Eigen::Vector3f pos1 = igl::unproject(Eigen::Vector3f(mouse_x, core().viewport[3] - mouse_y, down_mouse_z), core().view, core().proj, core().viewport);
  673. 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);
  674. Eigen::Vector3f diff = pos1 - pos0;
  675. core().camera_translation = down_translation + Eigen::Vector3f(diff[0],diff[1],diff[2]);
  676. break;
  677. }
  678. case MouseMode::Zoom:
  679. {
  680. float delta = 0.001f * (mouse_x - down_mouse_x + mouse_y - down_mouse_y);
  681. core().camera_zoom *= 1 + delta;
  682. down_mouse_x = mouse_x;
  683. down_mouse_y = mouse_y;
  684. break;
  685. }
  686. default:
  687. break;
  688. }
  689. }
  690. return true;
  691. }
  692. IGL_INLINE bool Viewer::mouse_scroll(float delta_y)
  693. {
  694. // Direct the scrolling operation to the appropriate viewport
  695. // (unless the core selection is locked by an ongoing mouse interaction).
  696. if (!down)
  697. select_hovered_core();
  698. scroll_position += delta_y;
  699. for (unsigned int i = 0; i<plugins.size(); ++i)
  700. if (plugins[i]->mouse_scroll(delta_y))
  701. return true;
  702. if (callback_mouse_scroll)
  703. if (callback_mouse_scroll(*this,delta_y))
  704. return true;
  705. // Only zoom if there's actually a change
  706. if(delta_y != 0)
  707. {
  708. float mult = (1.0+((delta_y>0)?1.:-1.)*0.05);
  709. const float min_zoom = 0.1f;
  710. core().camera_zoom = (core().camera_zoom * mult > min_zoom ? core().camera_zoom * mult : min_zoom);
  711. }
  712. return true;
  713. }
  714. IGL_INLINE bool Viewer::load_scene()
  715. {
  716. std::string fname = igl::file_dialog_open();
  717. if(fname.length() == 0)
  718. return false;
  719. return load_scene(fname);
  720. }
  721. IGL_INLINE bool Viewer::load_scene(std::string fname)
  722. {
  723. igl::deserialize(core(),"Core",fname.c_str());
  724. igl::deserialize(data(),"Data",fname.c_str());
  725. return true;
  726. }
  727. IGL_INLINE bool Viewer::save_scene()
  728. {
  729. std::string fname = igl::file_dialog_save();
  730. if (fname.length() == 0)
  731. return false;
  732. return save_scene(fname);
  733. }
  734. IGL_INLINE bool Viewer::save_scene(std::string fname)
  735. {
  736. igl::serialize(core(),"Core",fname.c_str(),true);
  737. igl::serialize(data(),"Data",fname.c_str());
  738. return true;
  739. }
  740. IGL_INLINE void Viewer::draw()
  741. {
  742. using namespace std;
  743. using namespace Eigen;
  744. int width, height;
  745. glfwGetFramebufferSize(window, &width, &height);
  746. int width_window, height_window;
  747. glfwGetWindowSize(window, &width_window, &height_window);
  748. auto highdpi_tmp = (width_window == 0 || width == 0) ? highdpi : (width/width_window);
  749. if(fabs(highdpi_tmp-highdpi)>1e-8)
  750. {
  751. post_resize(width, height);
  752. highdpi=highdpi_tmp;
  753. }
  754. for (auto& core : core_list)
  755. {
  756. core.clear_framebuffers();
  757. }
  758. for (unsigned int i = 0; i<plugins.size(); ++i)
  759. {
  760. if (plugins[i]->pre_draw())
  761. {
  762. return;
  763. }
  764. }
  765. if (callback_pre_draw)
  766. {
  767. if (callback_pre_draw(*this))
  768. {
  769. return;
  770. }
  771. }
  772. for (auto& core : core_list)
  773. {
  774. for (auto& mesh : data_list)
  775. {
  776. if (mesh.is_visible & core.id)
  777. {
  778. core.draw(mesh);
  779. }
  780. }
  781. }
  782. for (unsigned int i = 0; i<plugins.size(); ++i)
  783. {
  784. if (plugins[i]->post_draw())
  785. {
  786. break;
  787. }
  788. }
  789. if (callback_post_draw)
  790. {
  791. if (callback_post_draw(*this))
  792. {
  793. return;
  794. }
  795. }
  796. }
  797. IGL_INLINE void Viewer::resize(int w,int h)
  798. {
  799. if (window) {
  800. glfwSetWindowSize(window, w/highdpi, h/highdpi);
  801. }
  802. post_resize(w, h);
  803. }
  804. IGL_INLINE void Viewer::post_resize(int w,int h)
  805. {
  806. if (core_list.size() == 1)
  807. {
  808. core().viewport = Eigen::Vector4f(0,0,w,h);
  809. }
  810. else
  811. {
  812. // It is up to the user to define the behavior of the post_resize() function
  813. // when there are multiple viewports (through the `callback_post_resize` callback)
  814. }
  815. for (unsigned int i = 0; i<plugins.size(); ++i)
  816. {
  817. plugins[i]->post_resize(w, h);
  818. }
  819. if (callback_post_resize)
  820. {
  821. callback_post_resize(*this, w, h);
  822. }
  823. }
  824. IGL_INLINE void Viewer::snap_to_canonical_quaternion()
  825. {
  826. Eigen::Quaternionf snapq = this->core().trackball_angle;
  827. igl::snap_to_canonical_view_quat(snapq,1.0f,this->core().trackball_angle);
  828. }
  829. IGL_INLINE void Viewer::open_dialog_load_mesh()
  830. {
  831. std::string fname = igl::file_dialog_open();
  832. if (fname.length() == 0)
  833. return;
  834. this->load_mesh_from_file(fname.c_str());
  835. }
  836. IGL_INLINE void Viewer::open_dialog_save_mesh()
  837. {
  838. std::string fname = igl::file_dialog_save();
  839. if(fname.length() == 0)
  840. return;
  841. this->save_mesh_to_file(fname.c_str());
  842. }
  843. IGL_INLINE ViewerData& Viewer::data(int mesh_id /*= -1*/)
  844. {
  845. assert(!data_list.empty() && "data_list should never be empty");
  846. int index;
  847. if (mesh_id == -1)
  848. index = selected_data_index;
  849. else
  850. index = mesh_index(mesh_id);
  851. assert((index >= 0 && index < data_list.size()) &&
  852. "selected_data_index or mesh_id should be in bounds");
  853. return data_list[index];
  854. }
  855. IGL_INLINE int Viewer::append_mesh(bool visible /*= true*/)
  856. {
  857. assert(data_list.size() >= 1);
  858. data_list.emplace_back();
  859. selected_data_index = data_list.size()-1;
  860. data_list.back().id = next_data_id++;
  861. if (visible)
  862. for (int i = 0; i < core_list.size(); i++)
  863. data_list.back().set_visible(true, core_list[i].id);
  864. else
  865. data_list.back().is_visible = 0;
  866. return data_list.back().id;
  867. }
  868. IGL_INLINE bool Viewer::erase_mesh(const size_t index)
  869. {
  870. assert((index >= 0 && index < data_list.size()) && "index should be in bounds");
  871. assert(data_list.size() >= 1);
  872. if(data_list.size() == 1)
  873. {
  874. // Cannot remove last mesh
  875. return false;
  876. }
  877. data_list[index].meshgl.free();
  878. data_list.erase(data_list.begin() + index);
  879. if(selected_data_index >= index && selected_data_index>0)
  880. {
  881. selected_data_index--;
  882. }
  883. return true;
  884. }
  885. IGL_INLINE size_t Viewer::mesh_index(const int id) const {
  886. for (size_t i = 0; i < data_list.size(); ++i)
  887. {
  888. if (data_list[i].id == id)
  889. return i;
  890. }
  891. return 0;
  892. }
  893. IGL_INLINE ViewerCore& Viewer::core(unsigned core_id /*= 0*/)
  894. {
  895. assert(!core_list.empty() && "core_list should never be empty");
  896. int core_ind;
  897. if (core_id == 0)
  898. core_ind = selected_core_index;
  899. else
  900. core_ind = core_index(core_id);
  901. assert((core_ind >= 0 && core_ind < core_list.size()) && "selected_core_index should be in bounds");
  902. return core_list[core_ind];
  903. }
  904. IGL_INLINE bool Viewer::erase_core(const size_t index)
  905. {
  906. assert((index >= 0 && index < core_list.size()) && "index should be in bounds");
  907. assert(data_list.size() >= 1);
  908. if (core_list.size() == 1)
  909. {
  910. // Cannot remove last mesh
  911. return false;
  912. }
  913. core_list[index].shut(); // does nothing
  914. core_list.erase(core_list.begin() + index);
  915. if (selected_core_index >= index && selected_core_index > 0)
  916. {
  917. selected_core_index>>=1;
  918. }
  919. return true;
  920. }
  921. IGL_INLINE size_t Viewer::core_index(const int id) const {
  922. for (size_t i = 0; i < core_list.size(); ++i)
  923. {
  924. if (core_list[i].id == id)
  925. return i;
  926. }
  927. return 0;
  928. }
  929. IGL_INLINE int Viewer::append_core(Eigen::Vector4f viewport, bool append_empty /*= false*/)
  930. {
  931. core_list.push_back(core()); //copies the previous core and only changes the viewport
  932. core_list.back().viewport = viewport;
  933. core_list.back().id = next_core_id;
  934. next_core_id <<= 1;
  935. if(!append_empty)
  936. for (auto &data : data_list)
  937. data.set_visible(true,core_list.back().id);
  938. return core_list.back().id;
  939. }
  940. } // end namespace
  941. } // end namespace
  942. }