Viewer.cpp 30 KB

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