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. #ifndef __APPLE__
  156. if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress))
  157. {
  158. printf("Failed to load OpenGL and its extensions\n");
  159. return(-1);
  160. }
  161. #endif
  162. #if defined(DEBUG) || defined(_DEBUG)
  163. printf("OpenGL Version %d.%d loaded\n", GLVersion.major, GLVersion.minor);
  164. int major, minor, rev;
  165. major = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
  166. minor = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
  167. rev = glfwGetWindowAttrib(window, GLFW_CONTEXT_REVISION);
  168. printf("OpenGL version received: %d.%d.%d\n", major, minor, rev);
  169. printf("Supported OpenGL is %s\n", (const char*)glGetString(GL_VERSION));
  170. printf("Supported GLSL is %s\n", (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION));
  171. #endif
  172. glfwSetInputMode(window,GLFW_CURSOR,GLFW_CURSOR_NORMAL);
  173. // Initialize FormScreen
  174. __viewer = this;
  175. // Register callbacks
  176. glfwSetKeyCallback(window, glfw_key_callback);
  177. glfwSetCursorPosCallback(window,glfw_mouse_move);
  178. glfwSetWindowSizeCallback(window,glfw_window_size);
  179. glfwSetMouseButtonCallback(window,glfw_mouse_press);
  180. glfwSetScrollCallback(window,glfw_mouse_scroll);
  181. glfwSetCharModsCallback(window,glfw_char_mods_callback);
  182. glfwSetDropCallback(window,glfw_drop_callback);
  183. // Handle retina displays (windows and mac)
  184. int width, height;
  185. glfwGetFramebufferSize(window, &width, &height);
  186. int width_window, height_window;
  187. glfwGetWindowSize(window, &width_window, &height_window);
  188. highdpi = windowWidth/width_window;
  189. glfw_window_size(window,width_window,height_window);
  190. // Initialize IGL viewer
  191. init();
  192. for(auto &core : this->core_list)
  193. {
  194. for(auto &data : this->data_list)
  195. {
  196. if(data.is_visible & core.id)
  197. {
  198. this->core(core.id).align_camera_center(data.V, data.F);
  199. }
  200. }
  201. }
  202. return EXIT_SUCCESS;
  203. }
  204. IGL_INLINE bool Viewer::launch_rendering(bool loop)
  205. {
  206. // glfwMakeContextCurrent(window);
  207. // Rendering loop
  208. const int num_extra_frames = 5;
  209. int frame_counter = 0;
  210. while (!glfwWindowShouldClose(window))
  211. {
  212. double tic = get_seconds();
  213. draw();
  214. glfwSwapBuffers(window);
  215. if(core().is_animating || frame_counter++ < num_extra_frames)
  216. {
  217. glfwPollEvents();
  218. // In microseconds
  219. double duration = 1000000.*(get_seconds()-tic);
  220. const double min_duration = 1000000./core().animation_max_fps;
  221. if(duration<min_duration)
  222. {
  223. std::this_thread::sleep_for(std::chrono::microseconds((int)(min_duration-duration)));
  224. }
  225. }
  226. else
  227. {
  228. glfwWaitEvents();
  229. frame_counter = 0;
  230. }
  231. if (!loop)
  232. return !glfwWindowShouldClose(window);
  233. #ifdef __APPLE__
  234. static bool first_time_hack = true;
  235. if(first_time_hack) {
  236. glfwHideWindow(window);
  237. glfwShowWindow(window);
  238. first_time_hack = false;
  239. }
  240. #endif
  241. }
  242. return EXIT_SUCCESS;
  243. }
  244. IGL_INLINE void Viewer::launch_shut()
  245. {
  246. for(auto & data : data_list)
  247. {
  248. data.meshgl.free();
  249. }
  250. core().shut(); // Doesn't do anything
  251. shutdown_plugins();
  252. glfwDestroyWindow(window);
  253. glfwTerminate();
  254. return;
  255. }
  256. IGL_INLINE void Viewer::init()
  257. {
  258. core().init(); // Doesn't do anything
  259. if (callback_init)
  260. if (callback_init(*this))
  261. return;
  262. init_plugins();
  263. }
  264. IGL_INLINE void Viewer::init_plugins()
  265. {
  266. // Init all plugins
  267. for (unsigned int i = 0; i<plugins.size(); ++i)
  268. {
  269. plugins[i]->init(this);
  270. }
  271. }
  272. IGL_INLINE void Viewer::shutdown_plugins()
  273. {
  274. for (unsigned int i = 0; i<plugins.size(); ++i)
  275. {
  276. plugins[i]->shutdown();
  277. }
  278. }
  279. IGL_INLINE Viewer::Viewer():
  280. data_list(1),
  281. selected_data_index(0),
  282. next_data_id(1),
  283. selected_core_index(0),
  284. next_core_id(2)
  285. {
  286. window = nullptr;
  287. data_list.front().id = 0;
  288. core_list.emplace_back(ViewerCore());
  289. core_list.front().id = 1;
  290. // Temporary variables initialization
  291. down = false;
  292. hack_never_moved = true;
  293. scroll_position = 0.0f;
  294. // Per face
  295. data().set_face_based(false);
  296. // C-style callbacks
  297. callback_init = nullptr;
  298. callback_pre_draw = nullptr;
  299. callback_post_draw = nullptr;
  300. callback_mouse_down = nullptr;
  301. callback_mouse_up = nullptr;
  302. callback_mouse_move = nullptr;
  303. callback_mouse_scroll = nullptr;
  304. callback_key_down = nullptr;
  305. callback_key_up = nullptr;
  306. callback_init_data = nullptr;
  307. callback_pre_draw_data = nullptr;
  308. callback_post_draw_data = nullptr;
  309. callback_mouse_down_data = nullptr;
  310. callback_mouse_up_data = nullptr;
  311. callback_mouse_move_data = nullptr;
  312. callback_mouse_scroll_data = nullptr;
  313. callback_key_down_data = nullptr;
  314. callback_key_up_data = nullptr;
  315. #ifndef IGL_VIEWER_VIEWER_QUIET
  316. const std::string usage(R"(igl::opengl::glfw::Viewer usage:
  317. [drag] Rotate scene
  318. A,a Toggle animation (tight draw loop)
  319. D,d Toggle double sided lighting
  320. F,f Toggle face based
  321. I,i Toggle invert normals
  322. L,l Toggle wireframe
  323. O,o Toggle orthographic/perspective projection
  324. T,t Toggle filled faces
  325. Z Snap to canonical view
  326. [,] Toggle between rotation control types (trackball, two-axis
  327. valuator with fixed up, 2D mode with no rotation))
  328. <,> Toggle between models
  329. ; Toggle vertex labels
  330. : Toggle face labels)"
  331. );
  332. std::cout<<usage<<std::endl;
  333. #endif
  334. }
  335. IGL_INLINE Viewer::~Viewer()
  336. {
  337. }
  338. IGL_INLINE bool Viewer::load_mesh_from_file(
  339. const std::string & mesh_file_name_string)
  340. {
  341. // first try to load it with a plugin
  342. for (unsigned int i = 0; i<plugins.size(); ++i)
  343. {
  344. if (plugins[i]->load(mesh_file_name_string))
  345. {
  346. return true;
  347. }
  348. }
  349. // Create new data slot and set to selected
  350. if(!(data().F.rows() == 0 && data().V.rows() == 0))
  351. {
  352. append_mesh();
  353. }
  354. data().clear();
  355. size_t last_dot = mesh_file_name_string.rfind('.');
  356. if (last_dot == std::string::npos)
  357. {
  358. std::cerr<<"Error: No file extension found in "<<
  359. mesh_file_name_string<<std::endl;
  360. return false;
  361. }
  362. std::string extension = mesh_file_name_string.substr(last_dot+1);
  363. if (extension == "obj" || extension =="OBJ")
  364. {
  365. Eigen::MatrixXd corner_normals;
  366. Eigen::MatrixXi fNormIndices;
  367. Eigen::MatrixXd UV_V;
  368. Eigen::MatrixXi UV_F;
  369. Eigen::MatrixXd V;
  370. Eigen::MatrixXi F;
  371. if (!(
  372. igl::readOBJ(
  373. mesh_file_name_string,
  374. V, UV_V, corner_normals, F, UV_F, fNormIndices)))
  375. {
  376. return false;
  377. }
  378. data().set_mesh(V,F);
  379. if(UV_V.rows() != 0 && UV_F.rows() != 0)
  380. {
  381. data().set_uv(UV_V,UV_F);
  382. }
  383. }else
  384. {
  385. Eigen::MatrixXd V;
  386. Eigen::MatrixXi F;
  387. if (!igl::read_triangle_mesh(mesh_file_name_string, V, F))
  388. {
  389. // unrecognized file type
  390. printf("Error: %s is not a recognized file type.\n",extension.c_str());
  391. return false;
  392. }
  393. data().set_mesh(V,F);
  394. }
  395. data().compute_normals();
  396. data().uniform_colors(Eigen::Vector3d(51.0/255.0,43.0/255.0,33.3/255.0),
  397. Eigen::Vector3d(255.0/255.0,228.0/255.0,58.0/255.0),
  398. Eigen::Vector3d(255.0/255.0,235.0/255.0,80.0/255.0));
  399. for(int i=0;i<core_list.size(); i++)
  400. core_list[i].align_camera_center(data().V,data().F);
  401. for (unsigned int i = 0; i<plugins.size(); ++i)
  402. if (plugins[i]->post_load())
  403. return true;
  404. return true;
  405. }
  406. IGL_INLINE bool Viewer::save_mesh_to_file(
  407. const std::string & mesh_file_name_string)
  408. {
  409. // first try to load it with a plugin
  410. for (unsigned int i = 0; i<plugins.size(); ++i)
  411. if (plugins[i]->save(mesh_file_name_string))
  412. return true;
  413. size_t last_dot = mesh_file_name_string.rfind('.');
  414. if (last_dot == std::string::npos)
  415. {
  416. // No file type determined
  417. std::cerr<<"Error: No file extension found in "<<
  418. mesh_file_name_string<<std::endl;
  419. return false;
  420. }
  421. std::string extension = mesh_file_name_string.substr(last_dot+1);
  422. if (extension == "off" || extension =="OFF")
  423. {
  424. return igl::writeOFF(
  425. mesh_file_name_string,data().V,data().F);
  426. }
  427. else if (extension == "obj" || extension =="OBJ")
  428. {
  429. Eigen::MatrixXd corner_normals;
  430. Eigen::MatrixXi fNormIndices;
  431. Eigen::MatrixXd UV_V;
  432. Eigen::MatrixXi UV_F;
  433. return igl::writeOBJ(mesh_file_name_string,
  434. data().V,
  435. data().F,
  436. corner_normals, fNormIndices, UV_V, UV_F);
  437. }
  438. else
  439. {
  440. // unrecognized file type
  441. printf("Error: %s is not a recognized file type.\n",extension.c_str());
  442. return false;
  443. }
  444. return true;
  445. }
  446. IGL_INLINE bool Viewer::key_pressed(unsigned int unicode_key,int modifiers)
  447. {
  448. for (unsigned int i = 0; i<plugins.size(); ++i)
  449. {
  450. if (plugins[i]->key_pressed(unicode_key, modifiers))
  451. {
  452. return true;
  453. }
  454. }
  455. if (callback_key_pressed)
  456. if (callback_key_pressed(*this,unicode_key,modifiers))
  457. return true;
  458. switch(unicode_key)
  459. {
  460. case 'A':
  461. case 'a':
  462. {
  463. core().is_animating = !core().is_animating;
  464. return true;
  465. }
  466. case 'D':
  467. case 'd':
  468. {
  469. data().double_sided = !data().double_sided;
  470. return true;
  471. }
  472. case 'F':
  473. case 'f':
  474. {
  475. data().set_face_based(!data().face_based);
  476. return true;
  477. }
  478. case 'I':
  479. case 'i':
  480. {
  481. data().dirty |= MeshGL::DIRTY_NORMAL;
  482. data().invert_normals = !data().invert_normals;
  483. return true;
  484. }
  485. case 'L':
  486. case 'l':
  487. {
  488. core().toggle(data().show_lines);
  489. return true;
  490. }
  491. case 'O':
  492. case 'o':
  493. {
  494. core().orthographic = !core().orthographic;
  495. return true;
  496. }
  497. case 'T':
  498. case 't':
  499. {
  500. core().toggle(data().show_faces);
  501. return true;
  502. }
  503. case 'Z':
  504. {
  505. snap_to_canonical_quaternion();
  506. return true;
  507. }
  508. case '[':
  509. case ']':
  510. {
  511. if(core().rotation_type == ViewerCore::ROTATION_TYPE_TRACKBALL)
  512. core().set_rotation_type(ViewerCore::ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP);
  513. else
  514. core().set_rotation_type(ViewerCore::ROTATION_TYPE_TRACKBALL);
  515. return true;
  516. }
  517. case '<':
  518. case '>':
  519. {
  520. selected_data_index =
  521. (selected_data_index + data_list.size() + (unicode_key=='>'?1:-1))%data_list.size();
  522. return true;
  523. }
  524. case '{':
  525. case '}':
  526. {
  527. selected_core_index =
  528. (selected_core_index + core_list.size() + (unicode_key=='}'?1:-1))%core_list.size();
  529. return true;
  530. }
  531. case ';':
  532. data().show_vertex_labels = !data().show_vertex_labels;
  533. return true;
  534. case ':':
  535. data().show_face_labels = !data().show_face_labels;
  536. return true;
  537. default: break;//do nothing
  538. }
  539. return false;
  540. }
  541. IGL_INLINE bool Viewer::key_down(int key,int modifiers)
  542. {
  543. for (unsigned int i = 0; i<plugins.size(); ++i)
  544. if (plugins[i]->key_down(key, modifiers))
  545. return true;
  546. if (callback_key_down)
  547. if (callback_key_down(*this,key,modifiers))
  548. return true;
  549. return false;
  550. }
  551. IGL_INLINE bool Viewer::key_up(int key,int modifiers)
  552. {
  553. for (unsigned int i = 0; i<plugins.size(); ++i)
  554. if (plugins[i]->key_up(key, modifiers))
  555. return true;
  556. if (callback_key_up)
  557. if (callback_key_up(*this,key,modifiers))
  558. return true;
  559. return false;
  560. }
  561. IGL_INLINE void Viewer::select_hovered_core()
  562. {
  563. int width_window, height_window;
  564. glfwGetFramebufferSize(window, &width_window, &height_window);
  565. for (int i = 0; i < core_list.size(); i++)
  566. {
  567. Eigen::Vector4f viewport = core_list[i].viewport;
  568. if ((current_mouse_x > viewport[0]) &&
  569. (current_mouse_x < viewport[0] + viewport[2]) &&
  570. ((height_window - current_mouse_y) > viewport[1]) &&
  571. ((height_window - current_mouse_y) < viewport[1] + viewport[3]))
  572. {
  573. selected_core_index = i;
  574. break;
  575. }
  576. }
  577. }
  578. IGL_INLINE bool Viewer::mouse_down(MouseButton button,int modifier)
  579. {
  580. // Remember mouse location at down even if used by callback/plugin
  581. down_mouse_x = current_mouse_x;
  582. down_mouse_y = current_mouse_y;
  583. for (unsigned int i = 0; i<plugins.size(); ++i)
  584. if(plugins[i]->mouse_down(static_cast<int>(button),modifier))
  585. return true;
  586. if (callback_mouse_down)
  587. if (callback_mouse_down(*this,static_cast<int>(button),modifier))
  588. return true;
  589. down = true;
  590. // Select the core containing the click location.
  591. select_hovered_core();
  592. down_translation = core().camera_translation;
  593. // Initialization code for the trackball
  594. Eigen::RowVector3d center = Eigen::RowVector3d(0,0,0);
  595. if(data().V.rows() > 0)
  596. {
  597. // be careful that V may be 2D
  598. center.head(data().V.cols()) = 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. }