Viewer.cpp 27 KB

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