Viewer.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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. #ifdef _WIN32
  10. # include <windows.h>
  11. # undef max
  12. # undef min
  13. #endif
  14. #include <chrono>
  15. #include <thread>
  16. #ifndef __APPLE__
  17. # define GLEW_STATIC
  18. # include <GL/glew.h>
  19. #endif
  20. #ifdef __APPLE__
  21. # include <OpenGL/gl3.h>
  22. # define __gl_h_ /* Prevent inclusion of the old gl.h */
  23. #else
  24. # include <GL/gl.h>
  25. #endif
  26. #include <Eigen/LU>
  27. #define GLFW_INCLUDE_GLU
  28. #ifndef _WIN32
  29. #define GLFW_INCLUDE_GLCOREARB
  30. #endif
  31. #include <GLFW/glfw3.h>
  32. #include <cmath>
  33. #include <cstdio>
  34. #include <sstream>
  35. #include <iomanip>
  36. #include <iostream>
  37. #include <fstream>
  38. #include <algorithm>
  39. #include <limits>
  40. #include <cassert>
  41. #ifdef IGL_VIEWER_WITH_NANOGUI
  42. # include <nanogui/formhelper.h>
  43. # include <nanogui/screen.h>
  44. #endif
  45. #include <igl/project.h>
  46. #include <igl/get_seconds.h>
  47. #include <igl/readOBJ.h>
  48. #include <igl/readOFF.h>
  49. #include <igl/adjacency_list.h>
  50. #include <igl/writeOBJ.h>
  51. #include <igl/writeOFF.h>
  52. #include <igl/massmatrix.h>
  53. #include <igl/file_dialog_open.h>
  54. #include <igl/file_dialog_save.h>
  55. #include <igl/quat_mult.h>
  56. #include <igl/axis_angle_to_quat.h>
  57. #include <igl/trackball.h>
  58. #include <igl/two_axis_valuator_fixed_up.h>
  59. #include <igl/snap_to_canonical_view_quat.h>
  60. #include <igl/unproject.h>
  61. #ifdef ENABLE_SERIALIZATION
  62. #include <igl/serialize.h>
  63. #endif
  64. // Internal global variables used for glfw event handling
  65. static igl::viewer::Viewer * __viewer;
  66. static double highdpi = 1;
  67. static double scroll_x = 0;
  68. static double scroll_y = 0;
  69. static int global_KMod = 0;
  70. static void glfw_mouse_press(GLFWwindow* window, int button, int action, int modifier)
  71. {
  72. bool tw_used =
  73. #ifdef IGL_VIEWER_WITH_NANOGUI
  74. __viewer->screen->mouseButtonCallbackEvent(button,action,modifier);
  75. #else
  76. false;
  77. #endif
  78. igl::viewer::Viewer::MouseButton mb;
  79. if (button == GLFW_MOUSE_BUTTON_1)
  80. mb = igl::viewer::Viewer::MouseButton::Left;
  81. else if (button == GLFW_MOUSE_BUTTON_2)
  82. mb = igl::viewer::Viewer::MouseButton::Right;
  83. else //if (button == GLFW_MOUSE_BUTTON_3)
  84. mb = igl::viewer::Viewer::MouseButton::Middle;
  85. if (action == GLFW_PRESS)
  86. {
  87. if(!tw_used)
  88. {
  89. __viewer->mouse_down(mb,modifier);
  90. }
  91. } else
  92. {
  93. // Always call mouse_up on up
  94. __viewer->mouse_up(mb,modifier);
  95. }
  96. }
  97. static void glfw_error_callback(int error, const char* description)
  98. {
  99. fputs(description, stderr);
  100. }
  101. static void glfw_char_mods_callback(GLFWwindow* window, unsigned int codepoint, int modifier)
  102. {
  103. // TODO: pass to nanogui (although it's also using physical key down/up
  104. // rather than character codes...
  105. #ifdef IGL_VIEWER_WITH_NANOGUI
  106. if(! __viewer->screen->charCallbackEvent(codepoint) )
  107. #endif
  108. {
  109. __viewer->key_pressed(codepoint, modifier);
  110. }
  111. }
  112. static void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int modifier)
  113. {
  114. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  115. glfwSetWindowShouldClose(window, GL_TRUE);
  116. #ifdef IGL_VIEWER_WITH_NANOGUI
  117. if (__viewer->screen->keyCallbackEvent(key,scancode,action,modifier) == false)
  118. #endif
  119. {
  120. if (action == GLFW_PRESS)
  121. __viewer->key_down(key, modifier);
  122. else if(action == GLFW_RELEASE)
  123. __viewer->key_up(key, modifier);
  124. }
  125. }
  126. static void glfw_window_size(GLFWwindow* window, int width, int height)
  127. {
  128. int w = width*highdpi;
  129. int h = height*highdpi;
  130. __viewer->resize(w, h);
  131. // TODO: repositioning of the nanogui
  132. }
  133. static void glfw_mouse_move(GLFWwindow* window, double x, double y)
  134. {
  135. if(
  136. #ifdef IGL_VIEWER_WITH_NANOGUI
  137. __viewer->screen->cursorPosCallbackEvent(x,y) == false &&
  138. #endif
  139. true
  140. )
  141. {
  142. __viewer->mouse_move(x*highdpi, y*highdpi);
  143. }
  144. }
  145. static void glfw_mouse_scroll(GLFWwindow* window, double x, double y)
  146. {
  147. using namespace std;
  148. scroll_x += x;
  149. scroll_y += y;
  150. #ifdef IGL_VIEWER_WITH_NANOGUI
  151. if (__viewer->screen->scrollCallbackEvent(x,y) == false)
  152. #endif
  153. {
  154. __viewer->mouse_scroll(y);
  155. }
  156. }
  157. static void glfw_drop_callback(GLFWwindow *window,int count,const char **filenames)
  158. {
  159. #ifdef IGL_VIEWER_WITH_NANOGUI
  160. __viewer->screen->dropCallbackEvent(count,filenames);
  161. #endif
  162. }
  163. namespace igl
  164. {
  165. namespace viewer
  166. {
  167. IGL_INLINE void Viewer::init()
  168. {
  169. #ifdef IGL_VIEWER_WITH_NANOGUI
  170. using namespace nanogui;
  171. ngui->setFixedSize(Eigen::Vector2i(60,20));
  172. // Create nanogui widgets
  173. nanogui::Window *window = ngui->addWindow(Eigen::Vector2i(10,10),"libIGL-Viewer");
  174. // ---------------------- LOADING ----------------------
  175. #ifdef ENABLE_SERIALIZATION
  176. ngui->addGroup("Workspace");
  177. ngui->addButton("Load",[&](){this->load_scene();});
  178. ngui->addButton("Save",[&](){this->save_scene();});
  179. #endif
  180. #ifdef ENABLE_IO
  181. ngui->addGroup("Mesh");
  182. ngui->addButton("Load",[&](){this->open_dialog_load_mesh();});
  183. ngui->addButton("Save",[&](){this->open_dialog_save_mesh();});
  184. #endif
  185. ngui->addGroup("Viewing Options");
  186. ngui->addButton("Center object",[&](){this->core.align_camera_center(this->data.V,this->data.F);});
  187. ngui->addButton("Snap canonical view",[&]()
  188. {
  189. this->snap_to_canonical_quaternion();
  190. });
  191. ngui->addVariable("Zoom", core.camera_zoom);
  192. ngui->addVariable("Orthographic view", core.orthographic);
  193. ngui->addGroup("Draw options");
  194. ngui->addVariable<bool>("Face-based", [&](bool checked)
  195. {
  196. this->data.set_face_based(checked);
  197. },[&]()
  198. {
  199. return this->data.face_based;
  200. });
  201. ngui->addVariable("Show texture",core.show_texture);
  202. ngui->addVariable<bool>("Invert normals",[&](bool checked)
  203. {
  204. this->data.dirty |= ViewerData::DIRTY_NORMAL;
  205. this->core.invert_normals = checked;
  206. },[&]()
  207. {
  208. return this->core.invert_normals;
  209. });
  210. ngui->addVariable("Show overlay", core.show_overlay);
  211. ngui->addVariable("Show overlay depth", core.show_overlay_depth);
  212. ngui->addVariable("Background", (nanogui::Color &) core.background_color);
  213. ngui->addVariable("Line color", (nanogui::Color &) core.line_color);
  214. ngui->addVariable("Shininess", core.shininess);
  215. ngui->addGroup("Overlays");
  216. ngui->addVariable("Wireframe", core.show_lines);
  217. ngui->addVariable("Fill", core.show_faces);
  218. ngui->addVariable("Show vertex labels", core.show_vertid);
  219. ngui->addVariable("Show faces labels", core.show_faceid);
  220. screen->performLayout();
  221. #endif
  222. core.init();
  223. if (callback_init)
  224. if (callback_init(*this))
  225. return;
  226. init_plugins();
  227. }
  228. IGL_INLINE Viewer::Viewer()
  229. {
  230. #ifdef IGL_VIEWER_WITH_NANOGUI
  231. ngui = nullptr;
  232. screen = nullptr;
  233. #endif
  234. // Temporary variables initialization
  235. down = false;
  236. hack_never_moved = true;
  237. scroll_position = 0.0f;
  238. // Per face
  239. data.set_face_based(false);
  240. // C-style callbacks
  241. callback_init = nullptr;
  242. callback_pre_draw = nullptr;
  243. callback_post_draw = nullptr;
  244. callback_mouse_down = nullptr;
  245. callback_mouse_up = nullptr;
  246. callback_mouse_move = nullptr;
  247. callback_mouse_scroll = nullptr;
  248. callback_key_down = nullptr;
  249. callback_key_up = nullptr;
  250. callback_init_data = nullptr;
  251. callback_pre_draw_data = nullptr;
  252. callback_post_draw_data = nullptr;
  253. callback_mouse_down_data = nullptr;
  254. callback_mouse_up_data = nullptr;
  255. callback_mouse_move_data = nullptr;
  256. callback_mouse_scroll_data = nullptr;
  257. callback_key_down_data = nullptr;
  258. callback_key_up_data = nullptr;
  259. }
  260. IGL_INLINE void Viewer::init_plugins()
  261. {
  262. // Init all plugins
  263. for (unsigned int i = 0; i<plugins.size(); ++i)
  264. {
  265. plugins[i]->init(this);
  266. }
  267. }
  268. IGL_INLINE Viewer::~Viewer()
  269. {
  270. }
  271. IGL_INLINE void Viewer::shutdown_plugins()
  272. {
  273. for (unsigned int i = 0; i<plugins.size(); ++i)
  274. {
  275. plugins[i]->shutdown();
  276. }
  277. }
  278. IGL_INLINE bool Viewer::load_mesh_from_file(const char* mesh_file_name)
  279. {
  280. std::string mesh_file_name_string = std::string(mesh_file_name);
  281. // first try to load it with a plugin
  282. for (unsigned int i = 0; i<plugins.size(); ++i)
  283. {
  284. if (plugins[i]->load(mesh_file_name_string))
  285. {
  286. return true;
  287. }
  288. }
  289. data.clear();
  290. size_t last_dot = mesh_file_name_string.rfind('.');
  291. if (last_dot == std::string::npos)
  292. {
  293. printf("Error: No file extension found in %s\n",mesh_file_name);
  294. return false;
  295. }
  296. std::string extension = mesh_file_name_string.substr(last_dot+1);
  297. if (extension == "off" || extension =="OFF")
  298. {
  299. Eigen::MatrixXd V;
  300. Eigen::MatrixXi F;
  301. if (!igl::readOFF(mesh_file_name_string, V, F))
  302. return false;
  303. data.set_mesh(V,F);
  304. }
  305. else if (extension == "obj" || extension =="OBJ")
  306. {
  307. Eigen::MatrixXd corner_normals;
  308. Eigen::MatrixXi fNormIndices;
  309. Eigen::MatrixXd UV_V;
  310. Eigen::MatrixXi UV_F;
  311. Eigen::MatrixXd V;
  312. Eigen::MatrixXi F;
  313. if (!(
  314. igl::readOBJ(
  315. mesh_file_name_string,
  316. V, UV_V, corner_normals, F, UV_F, fNormIndices)))
  317. {
  318. return false;
  319. }
  320. data.set_mesh(V,F);
  321. data.set_uv(UV_V,UV_F);
  322. }
  323. else
  324. {
  325. // unrecognized file type
  326. printf("Error: %s is not a recognized file type.\n",extension.c_str());
  327. return false;
  328. }
  329. data.compute_normals();
  330. data.uniform_colors(Eigen::Vector3d(51.0/255.0,43.0/255.0,33.3/255.0),
  331. Eigen::Vector3d(255.0/255.0,228.0/255.0,58.0/255.0),
  332. Eigen::Vector3d(255.0/255.0,235.0/255.0,80.0/255.0));
  333. if (data.V_uv.rows() == 0)
  334. {
  335. data.grid_texture();
  336. }
  337. core.align_camera_center(data.V,data.F);
  338. for (unsigned int i = 0; i<plugins.size(); ++i)
  339. if (plugins[i]->post_load())
  340. return true;
  341. return true;
  342. }
  343. IGL_INLINE bool Viewer::save_mesh_to_file(const char* mesh_file_name)
  344. {
  345. std::string mesh_file_name_string(mesh_file_name);
  346. // first try to load it with a plugin
  347. for (unsigned int i = 0; i<plugins.size(); ++i)
  348. if (plugins[i]->save(mesh_file_name_string))
  349. return true;
  350. size_t last_dot = mesh_file_name_string.rfind('.');
  351. if (last_dot == std::string::npos)
  352. {
  353. // No file type determined
  354. printf("Error: No file extension found in %s\n",mesh_file_name);
  355. return false;
  356. }
  357. std::string extension = mesh_file_name_string.substr(last_dot+1);
  358. if (extension == "off" || extension =="OFF")
  359. {
  360. return igl::writeOFF(mesh_file_name_string,data.V,data.F);
  361. }
  362. else if (extension == "obj" || extension =="OBJ")
  363. {
  364. Eigen::MatrixXd corner_normals;
  365. Eigen::MatrixXi fNormIndices;
  366. Eigen::MatrixXd UV_V;
  367. Eigen::MatrixXi UV_F;
  368. return igl::writeOBJ(mesh_file_name_string, data.V,
  369. data.F, corner_normals, fNormIndices, UV_V, UV_F);
  370. }
  371. else
  372. {
  373. // unrecognized file type
  374. printf("Error: %s is not a recognized file type.\n",extension.c_str());
  375. return false;
  376. }
  377. return true;
  378. }
  379. IGL_INLINE bool Viewer::key_pressed(unsigned int unicode_key,int modifiers)
  380. {
  381. if (callback_key_pressed)
  382. if (callback_key_pressed(*this,unicode_key,modifiers))
  383. return true;
  384. for (unsigned int i = 0; i<plugins.size(); ++i)
  385. if (plugins[i]->key_pressed(unicode_key, modifiers))
  386. return true;
  387. const std::string usage(R"(igl::viewer::Viewer:
  388. A,a Toggle animation (tight draw loop)
  389. I,i Toggle invert normals
  390. L,l Toggle wireframe
  391. O,o Toggle orthographic/perspective projection
  392. T,t Toggle filled faces
  393. Z Snap to canonical view
  394. [,] Toggle between rotation control types (e.g. trackball, two-axis valuator
  395. with fixed up)
  396. )");
  397. switch(unicode_key)
  398. {
  399. case 'A':
  400. case 'a':
  401. {
  402. core.is_animating = !core.is_animating;
  403. return true;
  404. }
  405. case 'I':
  406. case 'i':
  407. {
  408. data.dirty |= ViewerData::DIRTY_NORMAL;
  409. core.invert_normals = !core.invert_normals;
  410. return true;
  411. }
  412. case 'L':
  413. case 'l':
  414. {
  415. core.show_lines = !core.show_lines;
  416. return true;
  417. }
  418. case 'O':
  419. case 'o':
  420. {
  421. core.orthographic = !core.orthographic;
  422. return true;
  423. }
  424. case 'T':
  425. case 't':
  426. {
  427. core.show_faces = !core.show_faces;
  428. return true;
  429. }
  430. case 'Z':
  431. {
  432. snap_to_canonical_quaternion();
  433. return true;
  434. }
  435. case '[':
  436. case ']':
  437. {
  438. if(core.rotation_type == ViewerCore::ROTATION_TYPE_TRACKBALL)
  439. {
  440. core.set_rotation_type(
  441. ViewerCore::ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP);
  442. }else
  443. {
  444. core.set_rotation_type(ViewerCore::ROTATION_TYPE_TRACKBALL);
  445. }
  446. return true;
  447. }
  448. default: break;//do nothing
  449. }
  450. return false;
  451. }
  452. IGL_INLINE bool Viewer::key_down(int key,int modifiers)
  453. {
  454. if (callback_key_down)
  455. if (callback_key_down(*this,key,modifiers))
  456. return true;
  457. for (unsigned int i = 0; i<plugins.size(); ++i)
  458. if (plugins[i]->key_down(key, modifiers))
  459. return true;
  460. return false;
  461. }
  462. IGL_INLINE bool Viewer::key_up(int key,int modifiers)
  463. {
  464. if (callback_key_up)
  465. if (callback_key_up(*this,key,modifiers))
  466. return true;
  467. for (unsigned int i = 0; i<plugins.size(); ++i)
  468. if (plugins[i]->key_up(key, modifiers))
  469. return true;
  470. return false;
  471. }
  472. IGL_INLINE bool Viewer::mouse_down(MouseButton button,int modifier)
  473. {
  474. if (callback_mouse_down)
  475. if (callback_mouse_down(*this,static_cast<int>(button),modifier))
  476. return true;
  477. for (unsigned int i = 0; i<plugins.size(); ++i)
  478. if(plugins[i]->mouse_down(static_cast<int>(button),modifier))
  479. return true;
  480. down = true;
  481. down_mouse_x = current_mouse_x;
  482. down_mouse_y = current_mouse_y;
  483. down_translation = core.model_translation;
  484. // Initialization code for the trackball
  485. Eigen::RowVector3d center;
  486. if (data.V.rows() == 0)
  487. {
  488. center << 0,0,0;
  489. }else
  490. {
  491. center = data.V.colwise().sum()/data.V.rows();
  492. }
  493. Eigen::Vector3f coord =
  494. igl::project(
  495. Eigen::Vector3f(center(0),center(1),center(2)),
  496. (core.view * core.model).eval(),
  497. core.proj,
  498. core.viewport);
  499. down_mouse_z = coord[2];
  500. down_rotation = core.trackball_angle;
  501. mouse_mode = MouseMode::Rotation;
  502. switch (button)
  503. {
  504. case MouseButton::Left:
  505. mouse_mode = MouseMode::Rotation;
  506. break;
  507. case MouseButton::Right:
  508. mouse_mode = MouseMode::Translation;
  509. break;
  510. default:
  511. mouse_mode = MouseMode::None;
  512. break;
  513. }
  514. return true;
  515. }
  516. IGL_INLINE bool Viewer::mouse_up(MouseButton button,int modifier)
  517. {
  518. down = false;
  519. if (callback_mouse_up)
  520. if (callback_mouse_up(*this,static_cast<int>(button),modifier))
  521. return true;
  522. for (unsigned int i = 0; i<plugins.size(); ++i)
  523. if(plugins[i]->mouse_up(static_cast<int>(button),modifier))
  524. return true;
  525. mouse_mode = MouseMode::None;
  526. return true;
  527. }
  528. IGL_INLINE bool Viewer::mouse_move(int mouse_x,int mouse_y)
  529. {
  530. if(hack_never_moved)
  531. {
  532. down_mouse_x = mouse_x;
  533. down_mouse_y = mouse_y;
  534. hack_never_moved = false;
  535. }
  536. current_mouse_x = mouse_x;
  537. current_mouse_y = mouse_y;
  538. if (callback_mouse_move)
  539. if (callback_mouse_move(*this,mouse_x,mouse_y))
  540. return true;
  541. for (unsigned int i = 0; i<plugins.size(); ++i)
  542. if (plugins[i]->mouse_move(mouse_x, mouse_y))
  543. return true;
  544. if (down)
  545. {
  546. switch (mouse_mode)
  547. {
  548. case MouseMode::Rotation:
  549. {
  550. switch(core.rotation_type)
  551. {
  552. default:
  553. assert(false && "Unknown rotation type");
  554. case ViewerCore::ROTATION_TYPE_TRACKBALL:
  555. igl::trackball(
  556. core.viewport(2),
  557. core.viewport(3),
  558. 2.0f,
  559. down_rotation,
  560. down_mouse_x,
  561. down_mouse_y,
  562. mouse_x,
  563. mouse_y,
  564. core.trackball_angle);
  565. break;
  566. case ViewerCore::ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP:
  567. igl::two_axis_valuator_fixed_up(
  568. core.viewport(2),core.viewport(3),
  569. 2.0,
  570. down_rotation,
  571. down_mouse_x, down_mouse_y, mouse_x, mouse_y,
  572. core.trackball_angle);
  573. break;
  574. }
  575. //Eigen::Vector4f snapq = core.trackball_angle;
  576. break;
  577. }
  578. case MouseMode::Translation:
  579. {
  580. //translation
  581. Eigen::Vector3f pos1 = igl::unproject(Eigen::Vector3f(mouse_x, core.viewport[3] - mouse_y, down_mouse_z), (core.view * core.model).eval(), core.proj, core.viewport);
  582. Eigen::Vector3f pos0 = igl::unproject(Eigen::Vector3f(down_mouse_x, core.viewport[3] - down_mouse_y, down_mouse_z), (core.view * core.model).eval(), core.proj, core.viewport);
  583. Eigen::Vector3f diff = pos1 - pos0;
  584. core.model_translation = down_translation + Eigen::Vector3f(diff[0],diff[1],diff[2]);
  585. break;
  586. }
  587. case MouseMode::Zoom:
  588. {
  589. float delta = 0.001f * (mouse_x - down_mouse_x + mouse_y - down_mouse_y);
  590. core.camera_zoom *= 1 + delta;
  591. down_mouse_x = mouse_x;
  592. down_mouse_y = mouse_y;
  593. break;
  594. }
  595. default:
  596. break;
  597. }
  598. }
  599. return true;
  600. }
  601. IGL_INLINE bool Viewer::mouse_scroll(float delta_y)
  602. {
  603. scroll_position += delta_y;
  604. if (callback_mouse_scroll)
  605. if (callback_mouse_scroll(*this,delta_y))
  606. return true;
  607. for (unsigned int i = 0; i<plugins.size(); ++i)
  608. if (plugins[i]->mouse_scroll(delta_y))
  609. return true;
  610. // Only zoom if there's actually a change
  611. if(delta_y != 0)
  612. {
  613. float mult = (1.0+((delta_y>0)?1.:-1.)*0.05);
  614. const float min_zoom = 0.1f;
  615. core.camera_zoom = (core.camera_zoom * mult > min_zoom ? core.camera_zoom * mult : min_zoom);
  616. }
  617. return true;
  618. }
  619. IGL_INLINE void Viewer::draw()
  620. {
  621. using namespace std;
  622. using namespace Eigen;
  623. core.clear_framebuffers();
  624. if (callback_pre_draw)
  625. if (callback_pre_draw(*this))
  626. return;
  627. for (unsigned int i = 0; i<plugins.size(); ++i)
  628. if (plugins[i]->pre_draw())
  629. return;
  630. core.draw(data,opengl);
  631. if (callback_post_draw)
  632. if (callback_post_draw(*this))
  633. return;
  634. for (unsigned int i = 0; i<plugins.size(); ++i)
  635. if (plugins[i]->post_draw())
  636. break;
  637. #ifdef IGL_VIEWER_WITH_NANOGUI
  638. ngui->refresh();
  639. screen->drawWidgets();
  640. #endif
  641. }
  642. IGL_INLINE bool Viewer::save_scene()
  643. {
  644. std::string fname = igl::file_dialog_save();
  645. if (fname.length() == 0)
  646. return false;
  647. #ifdef ENABLE_SERIALIZATION
  648. igl::serialize(core,"Core",fname.c_str(),true);
  649. #ifndef ENABLE_SERIALIZATION_CORE_ONLY
  650. igl::serialize(data,"Data",fname.c_str());
  651. for(unsigned int i = 0; i <plugins.size(); ++i)
  652. igl::serialize(*plugins[i],plugins[i]->plugin_name,fname.c_str());
  653. #endif
  654. #endif
  655. return true;
  656. }
  657. IGL_INLINE bool Viewer::load_scene()
  658. {
  659. std::string fname = igl::file_dialog_open();
  660. if(fname.length() == 0)
  661. return false;
  662. return load_scene(fname);
  663. }
  664. IGL_INLINE bool Viewer::load_scene(std::string fname)
  665. {
  666. #ifdef ENABLE_SERIALIZATION
  667. igl::deserialize(core,"Core",fname.c_str());
  668. #ifndef ENABLE_SERIALIZATION_CORE_ONLY
  669. igl::deserialize(data,"Data",fname.c_str());
  670. for(unsigned int i = 0; i <plugins.size(); ++i)
  671. igl::deserialize(*plugins[i],plugins[i]->plugin_name,fname.c_str());
  672. #endif
  673. #endif
  674. return true;
  675. }
  676. IGL_INLINE void Viewer::resize(int w,int h)
  677. {
  678. core.viewport = Eigen::Vector4f(0,0,w,h);
  679. }
  680. IGL_INLINE void Viewer::snap_to_canonical_quaternion()
  681. {
  682. Eigen::Quaternionf snapq = this->core.trackball_angle;
  683. igl::snap_to_canonical_view_quat(snapq,1.0f,this->core.trackball_angle);
  684. }
  685. IGL_INLINE void Viewer::open_dialog_load_mesh()
  686. {
  687. std::string fname = igl::file_dialog_open();
  688. if (fname.length() == 0)
  689. return;
  690. this->load_mesh_from_file(fname.c_str());
  691. }
  692. IGL_INLINE void Viewer::open_dialog_save_mesh()
  693. {
  694. std::string fname = igl::file_dialog_save();
  695. if(fname.length() == 0)
  696. return;
  697. this->save_mesh_to_file(fname.c_str());
  698. }
  699. IGL_INLINE int Viewer::launch_init(bool resizable,bool fullscreen)
  700. {
  701. glfwSetErrorCallback(glfw_error_callback);
  702. if (!glfwInit())
  703. return EXIT_FAILURE;
  704. glfwWindowHint(GLFW_SAMPLES, 8);
  705. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  706. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  707. #ifdef __APPLE__
  708. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  709. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  710. #endif
  711. if(fullscreen)
  712. {
  713. GLFWmonitor *monitor = glfwGetPrimaryMonitor();
  714. const GLFWvidmode *mode = glfwGetVideoMode(monitor);
  715. window = glfwCreateWindow(mode->width,mode->height,"libigl viewer",monitor,nullptr);
  716. }
  717. else
  718. {
  719. window = glfwCreateWindow(1280,800,"libigl viewer",nullptr,nullptr);
  720. }
  721. if (!window)
  722. {
  723. glfwTerminate();
  724. return EXIT_FAILURE;
  725. }
  726. glfwMakeContextCurrent(window);
  727. #ifndef __APPLE__
  728. glewExperimental = true;
  729. GLenum err = glewInit();
  730. if(GLEW_OK != err)
  731. {
  732. /* Problem: glewInit failed, something is seriously wrong. */
  733. fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
  734. }
  735. glGetError(); // pull and savely ignonre unhandled errors like GL_INVALID_ENUM
  736. fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
  737. #endif
  738. #if defined(DEBUG) || defined(_DEBUG)
  739. int major, minor, rev;
  740. major = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
  741. minor = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
  742. rev = glfwGetWindowAttrib(window, GLFW_CONTEXT_REVISION);
  743. printf("OpenGL version recieved: %d.%d.%d\n", major, minor, rev);
  744. printf("Supported OpenGL is %s\n", (const char*)glGetString(GL_VERSION));
  745. printf("Supported GLSL is %s\n", (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION));
  746. #endif
  747. glfwSetInputMode(window,GLFW_CURSOR,GLFW_CURSOR_NORMAL);
  748. // Initialize FormScreen
  749. #ifdef IGL_VIEWER_WITH_NANOGUI
  750. screen = new nanogui::Screen();
  751. screen->initialize(window, false);
  752. ngui = new nanogui::FormHelper(screen);
  753. #endif
  754. __viewer = this;
  755. // Register callbacks
  756. glfwSetKeyCallback(window, glfw_key_callback);
  757. glfwSetCursorPosCallback(window,glfw_mouse_move);
  758. glfwSetWindowSizeCallback(window,glfw_window_size);
  759. glfwSetMouseButtonCallback(window,glfw_mouse_press);
  760. glfwSetScrollCallback(window,glfw_mouse_scroll);
  761. glfwSetCharModsCallback(window,glfw_char_mods_callback);
  762. glfwSetDropCallback(window,glfw_drop_callback);
  763. // Handle retina displays (windows and mac)
  764. int width, height;
  765. glfwGetFramebufferSize(window, &width, &height);
  766. int width_window, height_window;
  767. glfwGetWindowSize(window, &width_window, &height_window);
  768. highdpi = width/width_window;
  769. glfw_window_size(window,width_window,height_window);
  770. opengl.init();
  771. core.align_camera_center(data.V,data.F);
  772. // Initialize IGL viewer
  773. init();
  774. return EXIT_SUCCESS;
  775. }
  776. IGL_INLINE bool Viewer::launch_rendering(bool loop)
  777. {
  778. // glfwMakeContextCurrent(window);
  779. // Rendering loop
  780. while (!glfwWindowShouldClose(window))
  781. {
  782. double tic = get_seconds();
  783. draw();
  784. glfwSwapBuffers(window);
  785. if(core.is_animating)
  786. {
  787. glfwPollEvents();
  788. // In microseconds
  789. double duration = 1000000.*(get_seconds()-tic);
  790. const double min_duration = 1000000./core.animation_max_fps;
  791. if(duration<min_duration)
  792. {
  793. std::this_thread::sleep_for(std::chrono::microseconds((int)(min_duration-duration)));
  794. }
  795. }
  796. else
  797. {
  798. glfwWaitEvents();
  799. }
  800. if (!loop)
  801. return !glfwWindowShouldClose(window);
  802. }
  803. return EXIT_SUCCESS;
  804. }
  805. IGL_INLINE void Viewer::launch_shut()
  806. {
  807. opengl.free();
  808. core.shut();
  809. shutdown_plugins();
  810. #ifdef IGL_VIEWER_WITH_NANOGUI
  811. delete ngui;
  812. //delete screen;
  813. screen = nullptr;
  814. ngui = nullptr;
  815. #endif
  816. glfwDestroyWindow(window);
  817. glfwTerminate();
  818. return;
  819. }
  820. IGL_INLINE int Viewer::launch(bool resizable,bool fullscreen)
  821. {
  822. // TODO return values are being ignored...
  823. launch_init(resizable,fullscreen);
  824. launch_rendering(true);
  825. launch_shut();
  826. return EXIT_SUCCESS;
  827. }
  828. } // end namespace
  829. }