Viewer.cpp 30 KB

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