Viewer.cpp 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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*/, int windowWidth /*= 1280*/, int windowHeight /*= 800*/)
  103. {
  104. // TODO return values are being ignored...
  105. launch_init(resizable,fullscreen,windowWidth,windowHeight);
  106. launch_rendering(true);
  107. launch_shut();
  108. return EXIT_SUCCESS;
  109. }
  110. IGL_INLINE int Viewer::launch_init(bool resizable,bool fullscreen, int windowWidth, int windowHeight)
  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. window = glfwCreateWindow(windowWidth,windowHeight,"libigl viewer",nullptr,nullptr);
  133. }
  134. if (!window)
  135. {
  136. glfwTerminate();
  137. return EXIT_FAILURE;
  138. }
  139. glfwMakeContextCurrent(window);
  140. // Load OpenGL and its extensions
  141. if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress))
  142. {
  143. printf("Failed to load OpenGL and its extensions\n");
  144. return(-1);
  145. }
  146. #if defined(DEBUG) || defined(_DEBUG)
  147. printf("OpenGL Version %d.%d loaded\n", GLVersion.major, GLVersion.minor);
  148. int major, minor, rev;
  149. major = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
  150. minor = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
  151. rev = glfwGetWindowAttrib(window, GLFW_CONTEXT_REVISION);
  152. printf("OpenGL version received: %d.%d.%d\n", major, minor, rev);
  153. printf("Supported OpenGL is %s\n", (const char*)glGetString(GL_VERSION));
  154. printf("Supported GLSL is %s\n", (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION));
  155. #endif
  156. glfwSetInputMode(window,GLFW_CURSOR,GLFW_CURSOR_NORMAL);
  157. // Initialize FormScreen
  158. __viewer = this;
  159. // Register callbacks
  160. glfwSetKeyCallback(window, glfw_key_callback);
  161. glfwSetCursorPosCallback(window,glfw_mouse_move);
  162. glfwSetWindowSizeCallback(window,glfw_window_size);
  163. glfwSetMouseButtonCallback(window,glfw_mouse_press);
  164. glfwSetScrollCallback(window,glfw_mouse_scroll);
  165. glfwSetCharModsCallback(window,glfw_char_mods_callback);
  166. glfwSetDropCallback(window,glfw_drop_callback);
  167. // Handle retina displays (windows and mac)
  168. int width, height;
  169. glfwGetFramebufferSize(window, &width, &height);
  170. int width_window, height_window;
  171. glfwGetWindowSize(window, &width_window, &height_window);
  172. highdpi = windowWidth/width_window;
  173. glfw_window_size(window,width_window,height_window);
  174. //opengl.init();
  175. core().align_camera_center(data().V,data().F);
  176. // Initialize IGL viewer
  177. init();
  178. return EXIT_SUCCESS;
  179. }
  180. IGL_INLINE bool Viewer::launch_rendering(bool loop)
  181. {
  182. // glfwMakeContextCurrent(window);
  183. // Rendering loop
  184. const int num_extra_frames = 5;
  185. int frame_counter = 0;
  186. while (!glfwWindowShouldClose(window))
  187. {
  188. double tic = get_seconds();
  189. draw();
  190. glfwSwapBuffers(window);
  191. if(core().is_animating || frame_counter++ < num_extra_frames)
  192. {
  193. glfwPollEvents();
  194. // In microseconds
  195. double duration = 1000000.*(get_seconds()-tic);
  196. const double min_duration = 1000000./core().animation_max_fps;
  197. if(duration<min_duration)
  198. {
  199. std::this_thread::sleep_for(std::chrono::microseconds((int)(min_duration-duration)));
  200. }
  201. }
  202. else
  203. {
  204. glfwWaitEvents();
  205. frame_counter = 0;
  206. }
  207. if (!loop)
  208. return !glfwWindowShouldClose(window);
  209. #ifdef __APPLE__
  210. static bool first_time_hack = true;
  211. if(first_time_hack) {
  212. glfwHideWindow(window);
  213. glfwShowWindow(window);
  214. first_time_hack = false;
  215. }
  216. #endif
  217. }
  218. return EXIT_SUCCESS;
  219. }
  220. IGL_INLINE void Viewer::launch_shut()
  221. {
  222. for(auto & data : data_list)
  223. {
  224. data.meshgl.free();
  225. }
  226. core().shut(); // Doesn't do anything
  227. shutdown_plugins();
  228. glfwDestroyWindow(window);
  229. glfwTerminate();
  230. return;
  231. }
  232. IGL_INLINE void Viewer::init()
  233. {
  234. core().init(); // Doesn't do anything
  235. if (callback_init)
  236. if (callback_init(*this))
  237. return;
  238. init_plugins();
  239. }
  240. IGL_INLINE void Viewer::init_plugins()
  241. {
  242. // Init all plugins
  243. for (unsigned int i = 0; i<plugins.size(); ++i)
  244. {
  245. plugins[i]->init(this);
  246. }
  247. }
  248. IGL_INLINE void Viewer::shutdown_plugins()
  249. {
  250. for (unsigned int i = 0; i<plugins.size(); ++i)
  251. {
  252. plugins[i]->shutdown();
  253. }
  254. }
  255. IGL_INLINE Viewer::Viewer():
  256. data_list(1),
  257. selected_data_index(0),
  258. next_data_id(1),
  259. selected_core_index(0),
  260. next_core_id(2)
  261. {
  262. window = nullptr;
  263. data_list.front().id = 0;
  264. core_list.emplace_back(ViewerCore());
  265. core_list.front().id = 1;
  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. for(int i=0;i<core_list.size(); i++)
  380. core_list[i].align_camera_center(data().V,data().F);
  381. for (unsigned int i = 0; i<plugins.size(); ++i)
  382. if (plugins[i]->post_load())
  383. return true;
  384. return true;
  385. }
  386. IGL_INLINE bool Viewer::save_mesh_to_file(
  387. const std::string & mesh_file_name_string)
  388. {
  389. // first try to load it with a plugin
  390. for (unsigned int i = 0; i<plugins.size(); ++i)
  391. if (plugins[i]->save(mesh_file_name_string))
  392. return true;
  393. size_t last_dot = mesh_file_name_string.rfind('.');
  394. if (last_dot == std::string::npos)
  395. {
  396. // No file type determined
  397. std::cerr<<"Error: No file extension found in "<<
  398. mesh_file_name_string<<std::endl;
  399. return false;
  400. }
  401. std::string extension = mesh_file_name_string.substr(last_dot+1);
  402. if (extension == "off" || extension =="OFF")
  403. {
  404. return igl::writeOFF(
  405. mesh_file_name_string,data().V,data().F);
  406. }
  407. else if (extension == "obj" || extension =="OBJ")
  408. {
  409. Eigen::MatrixXd corner_normals;
  410. Eigen::MatrixXi fNormIndices;
  411. Eigen::MatrixXd UV_V;
  412. Eigen::MatrixXi UV_F;
  413. return igl::writeOBJ(mesh_file_name_string,
  414. data().V,
  415. data().F,
  416. corner_normals, fNormIndices, UV_V, UV_F);
  417. }
  418. else
  419. {
  420. // unrecognized file type
  421. printf("Error: %s is not a recognized file type.\n",extension.c_str());
  422. return false;
  423. }
  424. return true;
  425. }
  426. IGL_INLINE bool Viewer::key_pressed(unsigned int unicode_key,int modifiers)
  427. {
  428. for (unsigned int i = 0; i<plugins.size(); ++i)
  429. {
  430. if (plugins[i]->key_pressed(unicode_key, modifiers))
  431. {
  432. return true;
  433. }
  434. }
  435. if (callback_key_pressed)
  436. if (callback_key_pressed(*this,unicode_key,modifiers))
  437. return true;
  438. switch(unicode_key)
  439. {
  440. case 'A':
  441. case 'a':
  442. {
  443. core().is_animating = !core().is_animating;
  444. return true;
  445. }
  446. case 'F':
  447. case 'f':
  448. {
  449. data().set_face_based(!data().face_based);
  450. return true;
  451. }
  452. case 'I':
  453. case 'i':
  454. {
  455. data().dirty |= MeshGL::DIRTY_NORMAL;
  456. data().invert_normals = !data().invert_normals;
  457. return true;
  458. }
  459. case 'L':
  460. case 'l':
  461. {
  462. data().show_lines = !data().show_lines;
  463. return true;
  464. }
  465. case 'O':
  466. case 'o':
  467. {
  468. core().orthographic = !core().orthographic;
  469. return true;
  470. }
  471. case 'T':
  472. case 't':
  473. {
  474. data().show_faces = !data().show_faces;
  475. return true;
  476. }
  477. case 'Z':
  478. {
  479. snap_to_canonical_quaternion();
  480. return true;
  481. }
  482. case '[':
  483. case ']':
  484. {
  485. if(core().rotation_type == ViewerCore::ROTATION_TYPE_TRACKBALL)
  486. core().set_rotation_type(ViewerCore::ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP);
  487. else
  488. core().set_rotation_type(ViewerCore::ROTATION_TYPE_TRACKBALL);
  489. return true;
  490. }
  491. case '<':
  492. case '>':
  493. {
  494. selected_data_index =
  495. (selected_data_index + data_list.size() + (unicode_key=='>'?1:-1))%data_list.size();
  496. return true;
  497. }
  498. case ';':
  499. data().show_vertid = !data().show_vertid;
  500. return true;
  501. case ':':
  502. data().show_faceid = !data().show_faceid;
  503. return true;
  504. default: break;//do nothing
  505. }
  506. return false;
  507. }
  508. IGL_INLINE bool Viewer::key_down(int key,int modifiers)
  509. {
  510. for (unsigned int i = 0; i<plugins.size(); ++i)
  511. if (plugins[i]->key_down(key, modifiers))
  512. return true;
  513. if (callback_key_down)
  514. if (callback_key_down(*this,key,modifiers))
  515. return true;
  516. return false;
  517. }
  518. IGL_INLINE bool Viewer::key_up(int key,int modifiers)
  519. {
  520. for (unsigned int i = 0; i<plugins.size(); ++i)
  521. if (plugins[i]->key_up(key, modifiers))
  522. return true;
  523. if (callback_key_up)
  524. if (callback_key_up(*this,key,modifiers))
  525. return true;
  526. return false;
  527. }
  528. IGL_INLINE bool Viewer::mouse_down(MouseButton button,int modifier)
  529. {
  530. // Remember mouse location at down even if used by callback/plugin
  531. down_mouse_x = current_mouse_x;
  532. down_mouse_y = current_mouse_y;
  533. for (unsigned int i = 0; i<plugins.size(); ++i)
  534. if(plugins[i]->mouse_down(static_cast<int>(button),modifier))
  535. return true;
  536. if (callback_mouse_down)
  537. if (callback_mouse_down(*this,static_cast<int>(button),modifier))
  538. return true;
  539. down = true;
  540. down_translation = core().camera_translation;
  541. // Initialization code for the trackball
  542. Eigen::RowVector3d center;
  543. if (data().V.rows() == 0)
  544. {
  545. center << 0,0,0;
  546. }else
  547. {
  548. center = data().V.colwise().sum()/data().V.rows();
  549. }
  550. Eigen::Vector3f coord =
  551. igl::project(
  552. Eigen::Vector3f(center(0),center(1),center(2)),
  553. core().view,
  554. core().proj,
  555. core().viewport);
  556. down_mouse_z = coord[2];
  557. down_rotation = core().trackball_angle;
  558. mouse_mode = MouseMode::Rotation;
  559. switch (button)
  560. {
  561. case MouseButton::Left:
  562. if (core().rotation_type == ViewerCore::ROTATION_TYPE_NO_ROTATION) {
  563. mouse_mode = MouseMode::Translation;
  564. } else {
  565. mouse_mode = MouseMode::Rotation;
  566. }
  567. break;
  568. case MouseButton::Right:
  569. mouse_mode = MouseMode::Translation;
  570. break;
  571. default:
  572. mouse_mode = MouseMode::None;
  573. break;
  574. }
  575. return true;
  576. }
  577. IGL_INLINE bool Viewer::mouse_up(MouseButton button,int modifier)
  578. {
  579. down = false;
  580. for (unsigned int i = 0; i<plugins.size(); ++i)
  581. if(plugins[i]->mouse_up(static_cast<int>(button),modifier))
  582. return true;
  583. if (callback_mouse_up)
  584. if (callback_mouse_up(*this,static_cast<int>(button),modifier))
  585. return true;
  586. mouse_mode = MouseMode::None;
  587. return true;
  588. }
  589. IGL_INLINE bool Viewer::mouse_move(int mouse_x,int mouse_y)
  590. {
  591. if(hack_never_moved)
  592. {
  593. down_mouse_x = mouse_x;
  594. down_mouse_y = mouse_y;
  595. hack_never_moved = false;
  596. }
  597. current_mouse_x = mouse_x;
  598. current_mouse_y = mouse_y;
  599. for (unsigned int i = 0; i<plugins.size(); ++i)
  600. if (plugins[i]->mouse_move(mouse_x, mouse_y))
  601. return true;
  602. if (callback_mouse_move)
  603. if (callback_mouse_move(*this, mouse_x, mouse_y))
  604. return true;
  605. int width_window, height_window;
  606. glfwGetFramebufferSize(window, &width_window, &height_window);
  607. for (int i = 0; i < core_list.size(); i++)
  608. {
  609. Eigen::Vector4f viewport = core_list[i].viewport;
  610. if (mouse_x > viewport[0] && mouse_x < viewport[0] + viewport[2] &&
  611. height_window-mouse_y > viewport[1] && height_window-mouse_y < viewport[1] + viewport[3])
  612. {
  613. selected_core_index = i;
  614. break;
  615. }
  616. }
  617. if (down)
  618. {
  619. switch (mouse_mode)
  620. {
  621. case MouseMode::Rotation:
  622. {
  623. switch(core().rotation_type)
  624. {
  625. default:
  626. assert(false && "Unknown rotation type");
  627. case ViewerCore::ROTATION_TYPE_NO_ROTATION:
  628. break;
  629. case ViewerCore::ROTATION_TYPE_TRACKBALL:
  630. igl::trackball(
  631. core().viewport(2),
  632. core().viewport(3),
  633. 2.0f,
  634. down_rotation,
  635. down_mouse_x,
  636. down_mouse_y,
  637. mouse_x,
  638. mouse_y,
  639. core().trackball_angle);
  640. break;
  641. case ViewerCore::ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP:
  642. igl::two_axis_valuator_fixed_up(
  643. core().viewport(2),core().viewport(3),
  644. 2.0,
  645. down_rotation,
  646. down_mouse_x, down_mouse_y, mouse_x, mouse_y,
  647. core().trackball_angle);
  648. break;
  649. }
  650. //Eigen::Vector4f snapq = core().trackball_angle;
  651. break;
  652. }
  653. case MouseMode::Translation:
  654. {
  655. //translation
  656. Eigen::Vector3f pos1 = igl::unproject(Eigen::Vector3f(mouse_x, core().viewport[3] - mouse_y, down_mouse_z), core().view, core().proj, core().viewport);
  657. 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);
  658. Eigen::Vector3f diff = pos1 - pos0;
  659. core().camera_translation = down_translation + Eigen::Vector3f(diff[0],diff[1],diff[2]);
  660. break;
  661. }
  662. case MouseMode::Zoom:
  663. {
  664. float delta = 0.001f * (mouse_x - down_mouse_x + mouse_y - down_mouse_y);
  665. core().camera_zoom *= 1 + delta;
  666. down_mouse_x = mouse_x;
  667. down_mouse_y = mouse_y;
  668. break;
  669. }
  670. default:
  671. break;
  672. }
  673. }
  674. return true;
  675. }
  676. IGL_INLINE bool Viewer::mouse_scroll(float delta_y)
  677. {
  678. scroll_position += delta_y;
  679. for (unsigned int i = 0; i<plugins.size(); ++i)
  680. if (plugins[i]->mouse_scroll(delta_y))
  681. return true;
  682. if (callback_mouse_scroll)
  683. if (callback_mouse_scroll(*this,delta_y))
  684. return true;
  685. // Only zoom if there's actually a change
  686. if(delta_y != 0)
  687. {
  688. float mult = (1.0+((delta_y>0)?1.:-1.)*0.05);
  689. const float min_zoom = 0.1f;
  690. core().camera_zoom = (core().camera_zoom * mult > min_zoom ? core().camera_zoom * mult : min_zoom);
  691. }
  692. return true;
  693. }
  694. IGL_INLINE bool Viewer::load_scene()
  695. {
  696. std::string fname = igl::file_dialog_open();
  697. if(fname.length() == 0)
  698. return false;
  699. return load_scene(fname);
  700. }
  701. IGL_INLINE bool Viewer::load_scene(std::string fname)
  702. {
  703. igl::deserialize(core(),"Core",fname.c_str());
  704. igl::deserialize(data(),"Data",fname.c_str());
  705. return true;
  706. }
  707. IGL_INLINE bool Viewer::save_scene()
  708. {
  709. std::string fname = igl::file_dialog_save();
  710. if (fname.length() == 0)
  711. return false;
  712. return save_scene(fname);
  713. }
  714. IGL_INLINE bool Viewer::save_scene(std::string fname)
  715. {
  716. igl::serialize(core(),"Core",fname.c_str(),true);
  717. igl::serialize(data(),"Data",fname.c_str());
  718. return true;
  719. }
  720. IGL_INLINE void Viewer::draw()
  721. {
  722. using namespace std;
  723. using namespace Eigen;
  724. int width, height;
  725. glfwGetFramebufferSize(window, &width, &height);
  726. int width_window, height_window;
  727. glfwGetWindowSize(window, &width_window, &height_window);
  728. auto highdpi_tmp = (width_window == 0 || width == 0) ? highdpi : (width/width_window);
  729. if(fabs(highdpi_tmp-highdpi)>1e-8)
  730. {
  731. post_resize(width, height);
  732. highdpi=highdpi_tmp;
  733. }
  734. for (auto& core : core_list)
  735. {
  736. core.clear_framebuffers();
  737. }
  738. for (unsigned int i = 0; i<plugins.size(); ++i)
  739. {
  740. if (plugins[i]->pre_draw())
  741. {
  742. return;
  743. }
  744. }
  745. if (callback_pre_draw)
  746. {
  747. if (callback_pre_draw(*this))
  748. {
  749. return;
  750. }
  751. }
  752. for (auto& core : core_list)
  753. {
  754. for (auto& mesh : data_list)
  755. {
  756. if (mesh.is_visible & core.id)
  757. {
  758. core.draw(mesh);
  759. }
  760. }
  761. }
  762. for (unsigned int i = 0; i<plugins.size(); ++i)
  763. {
  764. if (plugins[i]->post_draw())
  765. {
  766. break;
  767. }
  768. }
  769. if (callback_post_draw)
  770. {
  771. if (callback_post_draw(*this))
  772. {
  773. return;
  774. }
  775. }
  776. }
  777. IGL_INLINE void Viewer::resize(int w,int h)
  778. {
  779. if (window) {
  780. glfwSetWindowSize(window, w/highdpi, h/highdpi);
  781. }
  782. post_resize(w, h);
  783. }
  784. IGL_INLINE void Viewer::post_resize(int w,int h)
  785. {
  786. if (core_list.size() == 1)
  787. {
  788. core().viewport = Eigen::Vector4f(0,0,w,h);
  789. }
  790. else
  791. {
  792. // It is up to the user to define the behavior of the post_resize() function
  793. // when there are multiple viewports (through the `callback_post_resize` callback)
  794. }
  795. for (unsigned int i = 0; i<plugins.size(); ++i)
  796. {
  797. plugins[i]->post_resize(w, h);
  798. }
  799. if (callback_post_resize)
  800. {
  801. callback_post_resize(*this, w, h);
  802. }
  803. }
  804. IGL_INLINE void Viewer::snap_to_canonical_quaternion()
  805. {
  806. Eigen::Quaternionf snapq = this->core().trackball_angle;
  807. igl::snap_to_canonical_view_quat(snapq,1.0f,this->core().trackball_angle);
  808. }
  809. IGL_INLINE void Viewer::open_dialog_load_mesh()
  810. {
  811. std::string fname = igl::file_dialog_open();
  812. if (fname.length() == 0)
  813. return;
  814. this->load_mesh_from_file(fname.c_str());
  815. }
  816. IGL_INLINE void Viewer::open_dialog_save_mesh()
  817. {
  818. std::string fname = igl::file_dialog_save();
  819. if(fname.length() == 0)
  820. return;
  821. this->save_mesh_to_file(fname.c_str());
  822. }
  823. IGL_INLINE ViewerData& Viewer::data(int mesh_id /*= -1*/)
  824. {
  825. assert(!data_list.empty() && "data_list should never be empty");
  826. int index;
  827. if (mesh_id == -1)
  828. index = selected_data_index;
  829. else
  830. index = mesh_index(mesh_id);
  831. assert((index >= 0 && index < data_list.size()) &&
  832. "selected_data_index or mesh_id should be in bounds");
  833. return data_list[index];
  834. }
  835. IGL_INLINE int Viewer::append_mesh(bool visible /*= true*/)
  836. {
  837. assert(data_list.size() >= 1);
  838. data_list.emplace_back();
  839. selected_data_index = data_list.size()-1;
  840. data_list.back().id = next_data_id++;
  841. if (visible)
  842. for (int i = 0; i < core_list.size(); i++)
  843. data_list.back().set_visible(true, core_list[i].id);
  844. else
  845. data_list.back().is_visible = 0;
  846. return data_list.back().id;
  847. }
  848. IGL_INLINE bool Viewer::erase_mesh(const size_t index)
  849. {
  850. assert((index >= 0 && index < data_list.size()) && "index should be in bounds");
  851. assert(data_list.size() >= 1);
  852. if(data_list.size() == 1)
  853. {
  854. // Cannot remove last mesh
  855. return false;
  856. }
  857. data_list[index].meshgl.free();
  858. data_list.erase(data_list.begin() + index);
  859. if(selected_data_index >= index && selected_data_index>0)
  860. {
  861. selected_data_index--;
  862. }
  863. return true;
  864. }
  865. IGL_INLINE size_t Viewer::mesh_index(const int id) const {
  866. for (size_t i = 0; i < data_list.size(); ++i)
  867. {
  868. if (data_list[i].id == id)
  869. return i;
  870. }
  871. return 0;
  872. }
  873. IGL_INLINE ViewerCore& Viewer::core(unsigned core_id /*= 0*/)
  874. {
  875. assert(!core_list.empty() && "core_list should never be empty");
  876. int core_ind;
  877. if (core_id == 0)
  878. core_ind = selected_core_index;
  879. else
  880. core_ind = core_index(core_id);
  881. assert((core_ind >= 0 && core_ind < core_list.size()) && "selected_core_index should be in bounds");
  882. return core_list[core_ind];
  883. }
  884. IGL_INLINE bool Viewer::erase_core(const size_t index)
  885. {
  886. assert((index >= 0 && index < core_list.size()) && "index should be in bounds");
  887. assert(data_list.size() >= 1);
  888. if (core_list.size() == 1)
  889. {
  890. // Cannot remove last mesh
  891. return false;
  892. }
  893. core_list[index].shut(); // does nothing
  894. core_list.erase(core_list.begin() + index);
  895. if (selected_core_index >= index && selected_core_index > 0)
  896. {
  897. selected_core_index>>=1;
  898. }
  899. return true;
  900. }
  901. IGL_INLINE size_t Viewer::core_index(const int id) const {
  902. for (size_t i = 0; i < core_list.size(); ++i)
  903. {
  904. if (core_list[i].id == id)
  905. return i;
  906. }
  907. return 0;
  908. }
  909. IGL_INLINE int Viewer::append_core(Eigen::Vector4f viewport, bool append_empty /*= false*/)
  910. {
  911. core_list.push_back(core()); //copies the previous core and only changes the viewport
  912. core_list.back().viewport = viewport;
  913. core_list.back().id = next_core_id;
  914. next_core_id <<= 1;
  915. if(!append_empty)
  916. for (auto &data : data_list)
  917. data.set_visible(true,core_list.back().id);
  918. return core_list.back().id;
  919. }
  920. } // end namespace
  921. } // end namespace
  922. }