Viewer.cpp 24 KB

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