Viewer.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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,bool fullscreen)
  103. {
  104. // TODO return values are being ignored...
  105. launch_init(resizable,fullscreen);
  106. launch_rendering(true);
  107. launch_shut();
  108. return EXIT_SUCCESS;
  109. }
  110. IGL_INLINE int Viewer::launch_init(bool resizable,bool fullscreen)
  111. {
  112. glfwSetErrorCallback(glfw_error_callback);
  113. if (!glfwInit())
  114. {
  115. return EXIT_FAILURE;
  116. }
  117. glfwWindowHint(GLFW_SAMPLES, 8);
  118. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  119. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  120. #ifdef __APPLE__
  121. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  122. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  123. #endif
  124. if(fullscreen)
  125. {
  126. GLFWmonitor *monitor = glfwGetPrimaryMonitor();
  127. const GLFWvidmode *mode = glfwGetVideoMode(monitor);
  128. window = glfwCreateWindow(mode->width,mode->height,"libigl viewer",monitor,nullptr);
  129. }
  130. else
  131. {
  132. if (core.viewport.tail<2>().any()) {
  133. window = glfwCreateWindow(core.viewport(2),core.viewport(3),"libigl viewer",nullptr,nullptr);
  134. } else {
  135. window = glfwCreateWindow(1280,800,"libigl viewer",nullptr,nullptr);
  136. }
  137. }
  138. if (!window)
  139. {
  140. glfwTerminate();
  141. return EXIT_FAILURE;
  142. }
  143. glfwMakeContextCurrent(window);
  144. // Load OpenGL and its extensions
  145. if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress))
  146. {
  147. printf("Failed to load OpenGL and its extensions\n");
  148. return(-1);
  149. }
  150. #if defined(DEBUG) || defined(_DEBUG)
  151. printf("OpenGL Version %d.%d loaded\n", GLVersion.major, GLVersion.minor);
  152. int major, minor, rev;
  153. major = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
  154. minor = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
  155. rev = glfwGetWindowAttrib(window, GLFW_CONTEXT_REVISION);
  156. printf("OpenGL version received: %d.%d.%d\n", major, minor, rev);
  157. printf("Supported OpenGL is %s\n", (const char*)glGetString(GL_VERSION));
  158. printf("Supported GLSL is %s\n", (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION));
  159. #endif
  160. glfwSetInputMode(window,GLFW_CURSOR,GLFW_CURSOR_NORMAL);
  161. // Initialize FormScreen
  162. __viewer = this;
  163. // Register callbacks
  164. glfwSetKeyCallback(window, glfw_key_callback);
  165. glfwSetCursorPosCallback(window,glfw_mouse_move);
  166. glfwSetWindowSizeCallback(window,glfw_window_size);
  167. glfwSetMouseButtonCallback(window,glfw_mouse_press);
  168. glfwSetScrollCallback(window,glfw_mouse_scroll);
  169. glfwSetCharModsCallback(window,glfw_char_mods_callback);
  170. glfwSetDropCallback(window,glfw_drop_callback);
  171. // Handle retina displays (windows and mac)
  172. int width, height;
  173. glfwGetFramebufferSize(window, &width, &height);
  174. int width_window, height_window;
  175. glfwGetWindowSize(window, &width_window, &height_window);
  176. highdpi = width/width_window;
  177. glfw_window_size(window,width_window,height_window);
  178. //opengl.init();
  179. core.align_camera_center(data().V,data().F);
  180. // Initialize IGL viewer
  181. init();
  182. return EXIT_SUCCESS;
  183. }
  184. IGL_INLINE bool Viewer::launch_rendering(bool loop)
  185. {
  186. // glfwMakeContextCurrent(window);
  187. // Rendering loop
  188. const int num_extra_frames = 5;
  189. int frame_counter = 0;
  190. while (!glfwWindowShouldClose(window))
  191. {
  192. double tic = get_seconds();
  193. draw();
  194. glfwSwapBuffers(window);
  195. if(core.is_animating || frame_counter++ < num_extra_frames)
  196. {
  197. glfwPollEvents();
  198. // In microseconds
  199. double duration = 1000000.*(get_seconds()-tic);
  200. const double min_duration = 1000000./core.animation_max_fps;
  201. if(duration<min_duration)
  202. {
  203. std::this_thread::sleep_for(std::chrono::microseconds((int)(min_duration-duration)));
  204. }
  205. }
  206. else
  207. {
  208. glfwWaitEvents();
  209. frame_counter = 0;
  210. }
  211. if (!loop)
  212. return !glfwWindowShouldClose(window);
  213. #ifdef __APPLE__
  214. static bool first_time_hack = true;
  215. if(first_time_hack) {
  216. glfwHideWindow(window);
  217. glfwShowWindow(window);
  218. first_time_hack = false;
  219. }
  220. #endif
  221. }
  222. return EXIT_SUCCESS;
  223. }
  224. IGL_INLINE void Viewer::launch_shut()
  225. {
  226. for(auto & data : data_list)
  227. {
  228. data.meshgl.free();
  229. }
  230. core.shut();
  231. shutdown_plugins();
  232. glfwDestroyWindow(window);
  233. glfwTerminate();
  234. return;
  235. }
  236. IGL_INLINE void Viewer::init()
  237. {
  238. core.init();
  239. if (callback_init)
  240. if (callback_init(*this))
  241. return;
  242. init_plugins();
  243. }
  244. IGL_INLINE void Viewer::init_plugins()
  245. {
  246. // Init all plugins
  247. for (unsigned int i = 0; i<plugins.size(); ++i)
  248. {
  249. plugins[i]->init(this);
  250. }
  251. }
  252. IGL_INLINE void Viewer::shutdown_plugins()
  253. {
  254. for (unsigned int i = 0; i<plugins.size(); ++i)
  255. {
  256. plugins[i]->shutdown();
  257. }
  258. }
  259. IGL_INLINE Viewer::Viewer():
  260. data_list(1),
  261. selected_data_index(0),
  262. next_data_id(1)
  263. {
  264. window = nullptr;
  265. data_list.front().id = 0;
  266. // Temporary variables initialization
  267. down = false;
  268. hack_never_moved = true;
  269. scroll_position = 0.0f;
  270. // Per face
  271. data().set_face_based(false);
  272. // C-style callbacks
  273. callback_init = nullptr;
  274. callback_pre_draw = nullptr;
  275. callback_post_draw = nullptr;
  276. callback_mouse_down = nullptr;
  277. callback_mouse_up = nullptr;
  278. callback_mouse_move = nullptr;
  279. callback_mouse_scroll = nullptr;
  280. callback_key_down = nullptr;
  281. callback_key_up = nullptr;
  282. callback_init_data = nullptr;
  283. callback_pre_draw_data = nullptr;
  284. callback_post_draw_data = nullptr;
  285. callback_mouse_down_data = nullptr;
  286. callback_mouse_up_data = nullptr;
  287. callback_mouse_move_data = nullptr;
  288. callback_mouse_scroll_data = nullptr;
  289. callback_key_down_data = nullptr;
  290. callback_key_up_data = nullptr;
  291. #ifndef IGL_VIEWER_VIEWER_QUIET
  292. const std::string usage(R"(igl::opengl::glfw::Viewer usage:
  293. [drag] Rotate scene
  294. A,a Toggle animation (tight draw loop)
  295. F,f Toggle face based
  296. I,i Toggle invert normals
  297. L,l Toggle wireframe
  298. O,o Toggle orthographic/perspective projection
  299. T,t Toggle filled faces
  300. Z Snap to canonical view
  301. [,] Toggle between rotation control types (trackball, two-axis
  302. valuator with fixed up, 2D mode with no rotation))
  303. <,> Toggle between models
  304. ; Toggle vertex labels
  305. : Toggle face labels)"
  306. );
  307. std::cout<<usage<<std::endl;
  308. #endif
  309. }
  310. IGL_INLINE Viewer::~Viewer()
  311. {
  312. }
  313. IGL_INLINE bool Viewer::load_mesh_from_file(
  314. const std::string & mesh_file_name_string)
  315. {
  316. // first try to load it with a plugin
  317. for (unsigned int i = 0; i<plugins.size(); ++i)
  318. {
  319. if (plugins[i]->load(mesh_file_name_string))
  320. {
  321. return true;
  322. }
  323. }
  324. // Create new data slot and set to selected
  325. if(!(data().F.rows() == 0 && data().V.rows() == 0))
  326. {
  327. append_mesh();
  328. }
  329. data().clear();
  330. size_t last_dot = mesh_file_name_string.rfind('.');
  331. if (last_dot == std::string::npos)
  332. {
  333. std::cerr<<"Error: No file extension found in "<<
  334. mesh_file_name_string<<std::endl;
  335. return false;
  336. }
  337. std::string extension = mesh_file_name_string.substr(last_dot+1);
  338. if (extension == "off" || extension =="OFF")
  339. {
  340. Eigen::MatrixXd V;
  341. Eigen::MatrixXi F;
  342. if (!igl::readOFF(mesh_file_name_string, V, F))
  343. return false;
  344. data().set_mesh(V,F);
  345. }
  346. else if (extension == "obj" || extension =="OBJ")
  347. {
  348. Eigen::MatrixXd corner_normals;
  349. Eigen::MatrixXi fNormIndices;
  350. Eigen::MatrixXd UV_V;
  351. Eigen::MatrixXi UV_F;
  352. Eigen::MatrixXd V;
  353. Eigen::MatrixXi F;
  354. if (!(
  355. igl::readOBJ(
  356. mesh_file_name_string,
  357. V, UV_V, corner_normals, F, UV_F, fNormIndices)))
  358. {
  359. return false;
  360. }
  361. data().set_mesh(V,F);
  362. data().set_uv(UV_V,UV_F);
  363. }
  364. else
  365. {
  366. // unrecognized file type
  367. printf("Error: %s is not a recognized file type.\n",extension.c_str());
  368. return false;
  369. }
  370. data().compute_normals();
  371. data().uniform_colors(Eigen::Vector3d(51.0/255.0,43.0/255.0,33.3/255.0),
  372. Eigen::Vector3d(255.0/255.0,228.0/255.0,58.0/255.0),
  373. Eigen::Vector3d(255.0/255.0,235.0/255.0,80.0/255.0));
  374. // Alec: why?
  375. if (data().V_uv.rows() == 0)
  376. {
  377. data().grid_texture();
  378. }
  379. core.align_camera_center(data().V,data().F);
  380. for (unsigned int i = 0; i<plugins.size(); ++i)
  381. if (plugins[i]->post_load())
  382. return true;
  383. return true;
  384. }
  385. IGL_INLINE bool Viewer::save_mesh_to_file(
  386. const std::string & mesh_file_name_string)
  387. {
  388. // first try to load it with a plugin
  389. for (unsigned int i = 0; i<plugins.size(); ++i)
  390. if (plugins[i]->save(mesh_file_name_string))
  391. return true;
  392. size_t last_dot = mesh_file_name_string.rfind('.');
  393. if (last_dot == std::string::npos)
  394. {
  395. // No file type determined
  396. std::cerr<<"Error: No file extension found in "<<
  397. mesh_file_name_string<<std::endl;
  398. return false;
  399. }
  400. std::string extension = mesh_file_name_string.substr(last_dot+1);
  401. if (extension == "off" || extension =="OFF")
  402. {
  403. return igl::writeOFF(
  404. mesh_file_name_string,data().V,data().F);
  405. }
  406. else if (extension == "obj" || extension =="OBJ")
  407. {
  408. Eigen::MatrixXd corner_normals;
  409. Eigen::MatrixXi fNormIndices;
  410. Eigen::MatrixXd UV_V;
  411. Eigen::MatrixXi UV_F;
  412. return igl::writeOBJ(mesh_file_name_string,
  413. data().V,
  414. data().F,
  415. corner_normals, fNormIndices, UV_V, UV_F);
  416. }
  417. else
  418. {
  419. // unrecognized file type
  420. printf("Error: %s is not a recognized file type.\n",extension.c_str());
  421. return false;
  422. }
  423. return true;
  424. }
  425. IGL_INLINE bool Viewer::key_pressed(unsigned int unicode_key,int modifiers)
  426. {
  427. for (unsigned int i = 0; i<plugins.size(); ++i)
  428. {
  429. if (plugins[i]->key_pressed(unicode_key, modifiers))
  430. {
  431. return true;
  432. }
  433. }
  434. if (callback_key_pressed)
  435. if (callback_key_pressed(*this,unicode_key,modifiers))
  436. return true;
  437. switch(unicode_key)
  438. {
  439. case 'A':
  440. case 'a':
  441. {
  442. core.is_animating = !core.is_animating;
  443. return true;
  444. }
  445. case 'F':
  446. case 'f':
  447. {
  448. data().set_face_based(!data().face_based);
  449. return true;
  450. }
  451. case 'I':
  452. case 'i':
  453. {
  454. data().dirty |= MeshGL::DIRTY_NORMAL;
  455. data().invert_normals = !data().invert_normals;
  456. return true;
  457. }
  458. case 'L':
  459. case 'l':
  460. {
  461. data().show_lines = !data().show_lines;
  462. return true;
  463. }
  464. case 'O':
  465. case 'o':
  466. {
  467. core.orthographic = !core.orthographic;
  468. return true;
  469. }
  470. case 'T':
  471. case 't':
  472. {
  473. data().show_faces = !data().show_faces;
  474. return true;
  475. }
  476. case 'Z':
  477. {
  478. snap_to_canonical_quaternion();
  479. return true;
  480. }
  481. case '[':
  482. case ']':
  483. {
  484. if(core.rotation_type == ViewerCore::ROTATION_TYPE_TRACKBALL)
  485. core.set_rotation_type(ViewerCore::ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP);
  486. else
  487. core.set_rotation_type(ViewerCore::ROTATION_TYPE_TRACKBALL);
  488. return true;
  489. }
  490. case '<':
  491. case '>':
  492. {
  493. selected_data_index =
  494. (selected_data_index + data_list.size() + (unicode_key=='>'?1:-1))%data_list.size();
  495. return true;
  496. }
  497. case ';':
  498. data().show_vertid = !data().show_vertid;
  499. return true;
  500. case ':':
  501. data().show_faceid = !data().show_faceid;
  502. return true;
  503. default: break;//do nothing
  504. }
  505. return false;
  506. }
  507. IGL_INLINE bool Viewer::key_down(int key,int modifiers)
  508. {
  509. for (unsigned int i = 0; i<plugins.size(); ++i)
  510. if (plugins[i]->key_down(key, modifiers))
  511. return true;
  512. if (callback_key_down)
  513. if (callback_key_down(*this,key,modifiers))
  514. return true;
  515. return false;
  516. }
  517. IGL_INLINE bool Viewer::key_up(int key,int modifiers)
  518. {
  519. for (unsigned int i = 0; i<plugins.size(); ++i)
  520. if (plugins[i]->key_up(key, modifiers))
  521. return true;
  522. if (callback_key_up)
  523. if (callback_key_up(*this,key,modifiers))
  524. return true;
  525. return false;
  526. }
  527. IGL_INLINE bool Viewer::mouse_down(MouseButton button,int modifier)
  528. {
  529. // Remember mouse location at down even if used by callback/plugin
  530. down_mouse_x = current_mouse_x;
  531. down_mouse_y = current_mouse_y;
  532. for (unsigned int i = 0; i<plugins.size(); ++i)
  533. if(plugins[i]->mouse_down(static_cast<int>(button),modifier))
  534. return true;
  535. if (callback_mouse_down)
  536. if (callback_mouse_down(*this,static_cast<int>(button),modifier))
  537. return true;
  538. down = true;
  539. down_translation = core.camera_translation;
  540. // Initialization code for the trackball
  541. Eigen::RowVector3d center;
  542. if (data().V.rows() == 0)
  543. {
  544. center << 0,0,0;
  545. }else
  546. {
  547. center = data().V.colwise().sum()/data().V.rows();
  548. }
  549. Eigen::Vector3f coord =
  550. igl::project(
  551. Eigen::Vector3f(center(0),center(1),center(2)),
  552. core.view,
  553. core.proj,
  554. core.viewport);
  555. down_mouse_z = coord[2];
  556. down_rotation = core.trackball_angle;
  557. mouse_mode = MouseMode::Rotation;
  558. switch (button)
  559. {
  560. case MouseButton::Left:
  561. if (core.rotation_type == ViewerCore::ROTATION_TYPE_NO_ROTATION) {
  562. mouse_mode = MouseMode::Translation;
  563. } else {
  564. mouse_mode = MouseMode::Rotation;
  565. }
  566. break;
  567. case MouseButton::Right:
  568. mouse_mode = MouseMode::Translation;
  569. break;
  570. default:
  571. mouse_mode = MouseMode::None;
  572. break;
  573. }
  574. return true;
  575. }
  576. IGL_INLINE bool Viewer::mouse_up(MouseButton button,int modifier)
  577. {
  578. down = false;
  579. for (unsigned int i = 0; i<plugins.size(); ++i)
  580. if(plugins[i]->mouse_up(static_cast<int>(button),modifier))
  581. return true;
  582. if (callback_mouse_up)
  583. if (callback_mouse_up(*this,static_cast<int>(button),modifier))
  584. return true;
  585. mouse_mode = MouseMode::None;
  586. return true;
  587. }
  588. IGL_INLINE bool Viewer::mouse_move(int mouse_x,int mouse_y)
  589. {
  590. if(hack_never_moved)
  591. {
  592. down_mouse_x = mouse_x;
  593. down_mouse_y = mouse_y;
  594. hack_never_moved = false;
  595. }
  596. current_mouse_x = mouse_x;
  597. current_mouse_y = mouse_y;
  598. for (unsigned int i = 0; i<plugins.size(); ++i)
  599. if (plugins[i]->mouse_move(mouse_x, mouse_y))
  600. return true;
  601. if (callback_mouse_move)
  602. if (callback_mouse_move(*this,mouse_x,mouse_y))
  603. return true;
  604. if (down)
  605. {
  606. switch (mouse_mode)
  607. {
  608. case MouseMode::Rotation:
  609. {
  610. switch(core.rotation_type)
  611. {
  612. default:
  613. assert(false && "Unknown rotation type");
  614. case ViewerCore::ROTATION_TYPE_NO_ROTATION:
  615. break;
  616. case ViewerCore::ROTATION_TYPE_TRACKBALL:
  617. igl::trackball(
  618. core.viewport(2),
  619. core.viewport(3),
  620. 2.0f,
  621. down_rotation,
  622. down_mouse_x,
  623. down_mouse_y,
  624. mouse_x,
  625. mouse_y,
  626. core.trackball_angle);
  627. break;
  628. case ViewerCore::ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP:
  629. igl::two_axis_valuator_fixed_up(
  630. core.viewport(2),core.viewport(3),
  631. 2.0,
  632. down_rotation,
  633. down_mouse_x, down_mouse_y, mouse_x, mouse_y,
  634. core.trackball_angle);
  635. break;
  636. }
  637. //Eigen::Vector4f snapq = core.trackball_angle;
  638. break;
  639. }
  640. case MouseMode::Translation:
  641. {
  642. //translation
  643. Eigen::Vector3f pos1 = igl::unproject(Eigen::Vector3f(mouse_x, core.viewport[3] - mouse_y, down_mouse_z), core.view, core.proj, core.viewport);
  644. 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);
  645. Eigen::Vector3f diff = pos1 - pos0;
  646. core.camera_translation = down_translation + Eigen::Vector3f(diff[0],diff[1],diff[2]);
  647. break;
  648. }
  649. case MouseMode::Zoom:
  650. {
  651. float delta = 0.001f * (mouse_x - down_mouse_x + mouse_y - down_mouse_y);
  652. core.camera_zoom *= 1 + delta;
  653. down_mouse_x = mouse_x;
  654. down_mouse_y = mouse_y;
  655. break;
  656. }
  657. default:
  658. break;
  659. }
  660. }
  661. return true;
  662. }
  663. IGL_INLINE bool Viewer::mouse_scroll(float delta_y)
  664. {
  665. scroll_position += delta_y;
  666. for (unsigned int i = 0; i<plugins.size(); ++i)
  667. if (plugins[i]->mouse_scroll(delta_y))
  668. return true;
  669. if (callback_mouse_scroll)
  670. if (callback_mouse_scroll(*this,delta_y))
  671. return true;
  672. // Only zoom if there's actually a change
  673. if(delta_y != 0)
  674. {
  675. float mult = (1.0+((delta_y>0)?1.:-1.)*0.05);
  676. const float min_zoom = 0.1f;
  677. core.camera_zoom = (core.camera_zoom * mult > min_zoom ? core.camera_zoom * mult : min_zoom);
  678. }
  679. return true;
  680. }
  681. IGL_INLINE bool Viewer::load_scene()
  682. {
  683. std::string fname = igl::file_dialog_open();
  684. if(fname.length() == 0)
  685. return false;
  686. return load_scene(fname);
  687. }
  688. IGL_INLINE bool Viewer::load_scene(std::string fname)
  689. {
  690. igl::deserialize(core,"Core",fname.c_str());
  691. igl::deserialize(data(),"Data",fname.c_str());
  692. return true;
  693. }
  694. IGL_INLINE bool Viewer::save_scene()
  695. {
  696. std::string fname = igl::file_dialog_save();
  697. if (fname.length() == 0)
  698. return false;
  699. return save_scene(fname);
  700. }
  701. IGL_INLINE bool Viewer::save_scene(std::string fname)
  702. {
  703. igl::serialize(core,"Core",fname.c_str(),true);
  704. igl::serialize(data(),"Data",fname.c_str());
  705. return true;
  706. }
  707. IGL_INLINE void Viewer::draw()
  708. {
  709. using namespace std;
  710. using namespace Eigen;
  711. int width, height;
  712. glfwGetFramebufferSize(window, &width, &height);
  713. int width_window, height_window;
  714. glfwGetWindowSize(window, &width_window, &height_window);
  715. auto highdpi_tmp = (width_window == 0 || width == 0) ? highdpi : (width/width_window);
  716. if(fabs(highdpi_tmp-highdpi)>1e-8)
  717. {
  718. post_resize(width, height);
  719. highdpi=highdpi_tmp;
  720. }
  721. core.clear_framebuffers();
  722. for (unsigned int i = 0; i<plugins.size(); ++i)
  723. {
  724. if (plugins[i]->pre_draw())
  725. {
  726. return;
  727. }
  728. }
  729. if (callback_pre_draw)
  730. {
  731. if (callback_pre_draw(*this))
  732. {
  733. return;
  734. }
  735. }
  736. for(int i = 0;i<data_list.size();i++)
  737. {
  738. core.draw(data_list[i]);
  739. }
  740. for (unsigned int i = 0; i<plugins.size(); ++i)
  741. {
  742. if (plugins[i]->post_draw())
  743. {
  744. break;
  745. }
  746. }
  747. if (callback_post_draw)
  748. {
  749. if (callback_post_draw(*this))
  750. {
  751. return;
  752. }
  753. }
  754. }
  755. IGL_INLINE void Viewer::resize(int w,int h)
  756. {
  757. if (window) {
  758. glfwSetWindowSize(window, w/highdpi, h/highdpi);
  759. }
  760. post_resize(w, h);
  761. }
  762. IGL_INLINE void Viewer::post_resize(int w,int h)
  763. {
  764. core.viewport = Eigen::Vector4f(0,0,w,h);
  765. for (unsigned int i = 0; i<plugins.size(); ++i)
  766. {
  767. plugins[i]->post_resize(w, h);
  768. }
  769. }
  770. IGL_INLINE void Viewer::snap_to_canonical_quaternion()
  771. {
  772. Eigen::Quaternionf snapq = this->core.trackball_angle;
  773. igl::snap_to_canonical_view_quat(snapq,1.0f,this->core.trackball_angle);
  774. }
  775. IGL_INLINE void Viewer::open_dialog_load_mesh()
  776. {
  777. std::string fname = igl::file_dialog_open();
  778. if (fname.length() == 0)
  779. return;
  780. this->load_mesh_from_file(fname.c_str());
  781. }
  782. IGL_INLINE void Viewer::open_dialog_save_mesh()
  783. {
  784. std::string fname = igl::file_dialog_save();
  785. if(fname.length() == 0)
  786. return;
  787. this->save_mesh_to_file(fname.c_str());
  788. }
  789. IGL_INLINE ViewerData& Viewer::data()
  790. {
  791. assert(!data_list.empty() && "data_list should never be empty");
  792. assert(
  793. (selected_data_index >= 0 && selected_data_index < data_list.size()) &&
  794. "selected_data_index should be in bounds");
  795. return data_list[selected_data_index];
  796. }
  797. IGL_INLINE int Viewer::append_mesh()
  798. {
  799. assert(data_list.size() >= 1);
  800. data_list.emplace_back();
  801. selected_data_index = data_list.size()-1;
  802. data_list.back().id = next_data_id++;
  803. return data_list.back().id;
  804. }
  805. IGL_INLINE bool Viewer::erase_mesh(const size_t index)
  806. {
  807. assert((index >= 0 && index < data_list.size()) && "index should be in bounds");
  808. assert(data_list.size() >= 1);
  809. if(data_list.size() == 1)
  810. {
  811. // Cannot remove last mesh
  812. return false;
  813. }
  814. data_list[index].meshgl.free();
  815. data_list.erase(data_list.begin() + index);
  816. if(selected_data_index >= index && selected_data_index>0)
  817. {
  818. selected_data_index--;
  819. }
  820. return true;
  821. }
  822. IGL_INLINE size_t Viewer::mesh_index(const int id) const {
  823. for (size_t i = 0; i < data_list.size(); ++i)
  824. {
  825. if (data_list[i].id == id)
  826. return i;
  827. }
  828. return 0;
  829. }
  830. } // end namespace
  831. } // end namespace
  832. }