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. ; Toggle vertex labels
  300. : Toggle face labels
  301. #endif
  302. )");
  303. std::cout<<usage;
  304. #endif
  305. }
  306. IGL_INLINE void Viewer::init_plugins()
  307. {
  308. // Init all plugins
  309. for (unsigned int i = 0; i<plugins.size(); ++i)
  310. {
  311. plugins[i]->init(this);
  312. }
  313. }
  314. IGL_INLINE Viewer::~Viewer()
  315. {
  316. }
  317. IGL_INLINE void Viewer::shutdown_plugins()
  318. {
  319. for (unsigned int i = 0; i<plugins.size(); ++i)
  320. {
  321. plugins[i]->shutdown();
  322. }
  323. }
  324. IGL_INLINE bool Viewer::load_mesh_from_file(const char* mesh_file_name)
  325. {
  326. std::string mesh_file_name_string = std::string(mesh_file_name);
  327. // first try to load it with a plugin
  328. for (unsigned int i = 0; i<plugins.size(); ++i)
  329. {
  330. if (plugins[i]->load(mesh_file_name_string))
  331. {
  332. return true;
  333. }
  334. }
  335. data.clear();
  336. size_t last_dot = mesh_file_name_string.rfind('.');
  337. if (last_dot == std::string::npos)
  338. {
  339. printf("Error: No file extension found in %s\n",mesh_file_name);
  340. return false;
  341. }
  342. std::string extension = mesh_file_name_string.substr(last_dot+1);
  343. if (extension == "off" || extension =="OFF")
  344. {
  345. Eigen::MatrixXd V;
  346. Eigen::MatrixXi F;
  347. if (!igl::readOFF(mesh_file_name_string, V, F))
  348. return false;
  349. data.set_mesh(V,F);
  350. }
  351. else if (extension == "obj" || extension =="OBJ")
  352. {
  353. Eigen::MatrixXd corner_normals;
  354. Eigen::MatrixXi fNormIndices;
  355. Eigen::MatrixXd UV_V;
  356. Eigen::MatrixXi UV_F;
  357. Eigen::MatrixXd V;
  358. Eigen::MatrixXi F;
  359. if (!(
  360. igl::readOBJ(
  361. mesh_file_name_string,
  362. V, UV_V, corner_normals, F, UV_F, fNormIndices)))
  363. {
  364. return false;
  365. }
  366. data.set_mesh(V,F);
  367. data.set_uv(UV_V,UV_F);
  368. }
  369. else
  370. {
  371. // unrecognized file type
  372. printf("Error: %s is not a recognized file type.\n",extension.c_str());
  373. return false;
  374. }
  375. data.compute_normals();
  376. data.uniform_colors(Eigen::Vector3d(51.0/255.0,43.0/255.0,33.3/255.0),
  377. Eigen::Vector3d(255.0/255.0,228.0/255.0,58.0/255.0),
  378. Eigen::Vector3d(255.0/255.0,235.0/255.0,80.0/255.0));
  379. if (data.V_uv.rows() == 0)
  380. {
  381. data.grid_texture();
  382. }
  383. core.align_camera_center(data.V,data.F);
  384. for (unsigned int i = 0; i<plugins.size(); ++i)
  385. if (plugins[i]->post_load())
  386. return true;
  387. return true;
  388. }
  389. IGL_INLINE bool Viewer::save_mesh_to_file(const char* mesh_file_name)
  390. {
  391. std::string mesh_file_name_string(mesh_file_name);
  392. // first try to load it with a plugin
  393. for (unsigned int i = 0; i<plugins.size(); ++i)
  394. if (plugins[i]->save(mesh_file_name_string))
  395. return true;
  396. size_t last_dot = mesh_file_name_string.rfind('.');
  397. if (last_dot == std::string::npos)
  398. {
  399. // No file type determined
  400. printf("Error: No file extension found in %s\n",mesh_file_name);
  401. return false;
  402. }
  403. std::string extension = mesh_file_name_string.substr(last_dot+1);
  404. if (extension == "off" || extension =="OFF")
  405. {
  406. return igl::writeOFF(mesh_file_name_string,data.V,data.F);
  407. }
  408. else if (extension == "obj" || extension =="OBJ")
  409. {
  410. Eigen::MatrixXd corner_normals;
  411. Eigen::MatrixXi fNormIndices;
  412. Eigen::MatrixXd UV_V;
  413. Eigen::MatrixXi UV_F;
  414. return igl::writeOBJ(mesh_file_name_string, data.V,
  415. data.F, corner_normals, fNormIndices, UV_V, UV_F);
  416. }
  417. else
  418. {
  419. // unrecognized file type
  420. printf("Error: %s is not a recognized file type.\n",extension.c_str());
  421. return false;
  422. }
  423. return true;
  424. }
  425. IGL_INLINE bool Viewer::key_pressed(unsigned int unicode_key,int modifiers)
  426. {
  427. if (callback_key_pressed)
  428. if (callback_key_pressed(*this,unicode_key,modifiers))
  429. return true;
  430. for (unsigned int i = 0; i<plugins.size(); ++i)
  431. {
  432. if (plugins[i]->key_pressed(unicode_key, modifiers))
  433. {
  434. return true;
  435. }
  436. }
  437. switch(unicode_key)
  438. {
  439. case 'A':
  440. case 'a':
  441. {
  442. core.is_animating = !core.is_animating;
  443. return true;
  444. }
  445. case 'I':
  446. case 'i':
  447. {
  448. data.dirty |= ViewerData::DIRTY_NORMAL;
  449. core.invert_normals = !core.invert_normals;
  450. return true;
  451. }
  452. case 'L':
  453. case 'l':
  454. {
  455. core.show_lines = !core.show_lines;
  456. return true;
  457. }
  458. case 'O':
  459. case 'o':
  460. {
  461. core.orthographic = !core.orthographic;
  462. return true;
  463. }
  464. case 'T':
  465. case 't':
  466. {
  467. core.show_faces = !core.show_faces;
  468. return true;
  469. }
  470. case 'Z':
  471. {
  472. snap_to_canonical_quaternion();
  473. return true;
  474. }
  475. case '[':
  476. case ']':
  477. {
  478. if(core.rotation_type == ViewerCore::ROTATION_TYPE_TRACKBALL)
  479. {
  480. core.set_rotation_type(
  481. ViewerCore::ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP);
  482. }else
  483. {
  484. core.set_rotation_type(ViewerCore::ROTATION_TYPE_TRACKBALL);
  485. }
  486. return true;
  487. }
  488. #ifdef IGL_VIEWER_WITH_NANOGUI
  489. case ';':
  490. core.show_vertid = !core.show_vertid;
  491. return true;
  492. case ':':
  493. core.show_faceid = !core.show_faceid;
  494. return true;
  495. #endif
  496. default: break;//do nothing
  497. }
  498. return false;
  499. }
  500. IGL_INLINE bool Viewer::key_down(int key,int modifiers)
  501. {
  502. if (callback_key_down)
  503. if (callback_key_down(*this,key,modifiers))
  504. return true;
  505. for (unsigned int i = 0; i<plugins.size(); ++i)
  506. if (plugins[i]->key_down(key, modifiers))
  507. return true;
  508. return false;
  509. }
  510. IGL_INLINE bool Viewer::key_up(int key,int modifiers)
  511. {
  512. if (callback_key_up)
  513. if (callback_key_up(*this,key,modifiers))
  514. return true;
  515. for (unsigned int i = 0; i<plugins.size(); ++i)
  516. if (plugins[i]->key_up(key, modifiers))
  517. return true;
  518. return false;
  519. }
  520. IGL_INLINE bool Viewer::mouse_down(MouseButton button,int modifier)
  521. {
  522. // Remember mouse location at down even if used by callback/plugin
  523. down_mouse_x = current_mouse_x;
  524. down_mouse_y = current_mouse_y;
  525. if (callback_mouse_down)
  526. if (callback_mouse_down(*this,static_cast<int>(button),modifier))
  527. return true;
  528. for (unsigned int i = 0; i<plugins.size(); ++i)
  529. if(plugins[i]->mouse_down(static_cast<int>(button),modifier))
  530. return true;
  531. down = true;
  532. down_translation = core.model_translation;
  533. // Initialization code for the trackball
  534. Eigen::RowVector3d center;
  535. if (data.V.rows() == 0)
  536. {
  537. center << 0,0,0;
  538. }else
  539. {
  540. center = data.V.colwise().sum()/data.V.rows();
  541. }
  542. Eigen::Vector3f coord =
  543. igl::project(
  544. Eigen::Vector3f(center(0),center(1),center(2)),
  545. (core.view * core.model).eval(),
  546. core.proj,
  547. core.viewport);
  548. down_mouse_z = coord[2];
  549. down_rotation = core.trackball_angle;
  550. mouse_mode = MouseMode::Rotation;
  551. switch (button)
  552. {
  553. case MouseButton::Left:
  554. mouse_mode = MouseMode::Rotation;
  555. break;
  556. case MouseButton::Right:
  557. mouse_mode = MouseMode::Translation;
  558. break;
  559. default:
  560. mouse_mode = MouseMode::None;
  561. break;
  562. }
  563. return true;
  564. }
  565. IGL_INLINE bool Viewer::mouse_up(MouseButton button,int modifier)
  566. {
  567. down = false;
  568. if (callback_mouse_up)
  569. if (callback_mouse_up(*this,static_cast<int>(button),modifier))
  570. return true;
  571. for (unsigned int i = 0; i<plugins.size(); ++i)
  572. if(plugins[i]->mouse_up(static_cast<int>(button),modifier))
  573. return true;
  574. mouse_mode = MouseMode::None;
  575. return true;
  576. }
  577. IGL_INLINE bool Viewer::mouse_move(int mouse_x,int mouse_y)
  578. {
  579. if(hack_never_moved)
  580. {
  581. down_mouse_x = mouse_x;
  582. down_mouse_y = mouse_y;
  583. hack_never_moved = false;
  584. }
  585. current_mouse_x = mouse_x;
  586. current_mouse_y = mouse_y;
  587. if (callback_mouse_move)
  588. if (callback_mouse_move(*this,mouse_x,mouse_y))
  589. return true;
  590. for (unsigned int i = 0; i<plugins.size(); ++i)
  591. if (plugins[i]->mouse_move(mouse_x, mouse_y))
  592. return true;
  593. if (down)
  594. {
  595. switch (mouse_mode)
  596. {
  597. case MouseMode::Rotation:
  598. {
  599. switch(core.rotation_type)
  600. {
  601. default:
  602. assert(false && "Unknown rotation type");
  603. case ViewerCore::ROTATION_TYPE_TRACKBALL:
  604. igl::trackball(
  605. core.viewport(2),
  606. core.viewport(3),
  607. 2.0f,
  608. down_rotation,
  609. down_mouse_x,
  610. down_mouse_y,
  611. mouse_x,
  612. mouse_y,
  613. core.trackball_angle);
  614. break;
  615. case ViewerCore::ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP:
  616. igl::two_axis_valuator_fixed_up(
  617. core.viewport(2),core.viewport(3),
  618. 2.0,
  619. down_rotation,
  620. down_mouse_x, down_mouse_y, mouse_x, mouse_y,
  621. core.trackball_angle);
  622. break;
  623. }
  624. //Eigen::Vector4f snapq = core.trackball_angle;
  625. break;
  626. }
  627. case MouseMode::Translation:
  628. {
  629. //translation
  630. 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);
  631. 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);
  632. Eigen::Vector3f diff = pos1 - pos0;
  633. core.model_translation = down_translation + Eigen::Vector3f(diff[0],diff[1],diff[2]);
  634. break;
  635. }
  636. case MouseMode::Zoom:
  637. {
  638. float delta = 0.001f * (mouse_x - down_mouse_x + mouse_y - down_mouse_y);
  639. core.camera_zoom *= 1 + delta;
  640. down_mouse_x = mouse_x;
  641. down_mouse_y = mouse_y;
  642. break;
  643. }
  644. default:
  645. break;
  646. }
  647. }
  648. return true;
  649. }
  650. IGL_INLINE bool Viewer::mouse_scroll(float delta_y)
  651. {
  652. scroll_position += delta_y;
  653. if (callback_mouse_scroll)
  654. if (callback_mouse_scroll(*this,delta_y))
  655. return true;
  656. for (unsigned int i = 0; i<plugins.size(); ++i)
  657. if (plugins[i]->mouse_scroll(delta_y))
  658. return true;
  659. // Only zoom if there's actually a change
  660. if(delta_y != 0)
  661. {
  662. float mult = (1.0+((delta_y>0)?1.:-1.)*0.05);
  663. const float min_zoom = 0.1f;
  664. core.camera_zoom = (core.camera_zoom * mult > min_zoom ? core.camera_zoom * mult : min_zoom);
  665. }
  666. return true;
  667. }
  668. IGL_INLINE void Viewer::draw()
  669. {
  670. using namespace std;
  671. using namespace Eigen;
  672. core.clear_framebuffers();
  673. if (callback_pre_draw)
  674. if (callback_pre_draw(*this))
  675. return;
  676. for (unsigned int i = 0; i<plugins.size(); ++i)
  677. if (plugins[i]->pre_draw())
  678. return;
  679. core.draw(data,opengl);
  680. if (callback_post_draw)
  681. if (callback_post_draw(*this))
  682. return;
  683. for (unsigned int i = 0; i<plugins.size(); ++i)
  684. if (plugins[i]->post_draw())
  685. break;
  686. #ifdef IGL_VIEWER_WITH_NANOGUI
  687. ngui->refresh();
  688. screen->drawWidgets();
  689. #endif
  690. }
  691. IGL_INLINE bool Viewer::save_scene()
  692. {
  693. std::string fname = igl::file_dialog_save();
  694. if (fname.length() == 0)
  695. return false;
  696. #ifdef ENABLE_SERIALIZATION
  697. igl::serialize(core,"Core",fname.c_str(),true);
  698. #ifndef ENABLE_SERIALIZATION_CORE_ONLY
  699. igl::serialize(data,"Data",fname.c_str());
  700. for(unsigned int i = 0; i <plugins.size(); ++i)
  701. igl::serialize(*plugins[i],plugins[i]->plugin_name,fname.c_str());
  702. #endif
  703. #endif
  704. return true;
  705. }
  706. IGL_INLINE bool Viewer::load_scene()
  707. {
  708. std::string fname = igl::file_dialog_open();
  709. if(fname.length() == 0)
  710. return false;
  711. return load_scene(fname);
  712. }
  713. IGL_INLINE bool Viewer::load_scene(std::string fname)
  714. {
  715. #ifdef ENABLE_SERIALIZATION
  716. igl::deserialize(core,"Core",fname.c_str());
  717. #ifndef ENABLE_SERIALIZATION_CORE_ONLY
  718. igl::deserialize(data,"Data",fname.c_str());
  719. for(unsigned int i = 0; i <plugins.size(); ++i)
  720. igl::deserialize(*plugins[i],plugins[i]->plugin_name,fname.c_str());
  721. #endif
  722. #endif
  723. return true;
  724. }
  725. IGL_INLINE void Viewer::resize(int w,int h)
  726. {
  727. core.viewport = Eigen::Vector4f(0,0,w,h);
  728. }
  729. IGL_INLINE void Viewer::snap_to_canonical_quaternion()
  730. {
  731. Eigen::Quaternionf snapq = this->core.trackball_angle;
  732. igl::snap_to_canonical_view_quat(snapq,1.0f,this->core.trackball_angle);
  733. }
  734. IGL_INLINE void Viewer::open_dialog_load_mesh()
  735. {
  736. std::string fname = igl::file_dialog_open();
  737. if (fname.length() == 0)
  738. return;
  739. this->load_mesh_from_file(fname.c_str());
  740. }
  741. IGL_INLINE void Viewer::open_dialog_save_mesh()
  742. {
  743. std::string fname = igl::file_dialog_save();
  744. if(fname.length() == 0)
  745. return;
  746. this->save_mesh_to_file(fname.c_str());
  747. }
  748. IGL_INLINE int Viewer::launch_init(bool resizable,bool fullscreen)
  749. {
  750. glfwSetErrorCallback(glfw_error_callback);
  751. if (!glfwInit())
  752. return EXIT_FAILURE;
  753. glfwWindowHint(GLFW_SAMPLES, 8);
  754. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  755. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  756. #ifdef __APPLE__
  757. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  758. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  759. #endif
  760. if(fullscreen)
  761. {
  762. GLFWmonitor *monitor = glfwGetPrimaryMonitor();
  763. const GLFWvidmode *mode = glfwGetVideoMode(monitor);
  764. window = glfwCreateWindow(mode->width,mode->height,"libigl viewer",monitor,nullptr);
  765. }
  766. else
  767. {
  768. window = glfwCreateWindow(1280,800,"libigl viewer",nullptr,nullptr);
  769. }
  770. if (!window)
  771. {
  772. glfwTerminate();
  773. return EXIT_FAILURE;
  774. }
  775. glfwMakeContextCurrent(window);
  776. #ifndef __APPLE__
  777. glewExperimental = true;
  778. GLenum err = glewInit();
  779. if(GLEW_OK != err)
  780. {
  781. /* Problem: glewInit failed, something is seriously wrong. */
  782. fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
  783. }
  784. glGetError(); // pull and savely ignonre unhandled errors like GL_INVALID_ENUM
  785. fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
  786. #endif
  787. #if defined(DEBUG) || defined(_DEBUG)
  788. int major, minor, rev;
  789. major = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
  790. minor = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
  791. rev = glfwGetWindowAttrib(window, GLFW_CONTEXT_REVISION);
  792. printf("OpenGL version recieved: %d.%d.%d\n", major, minor, rev);
  793. printf("Supported OpenGL is %s\n", (const char*)glGetString(GL_VERSION));
  794. printf("Supported GLSL is %s\n", (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION));
  795. #endif
  796. glfwSetInputMode(window,GLFW_CURSOR,GLFW_CURSOR_NORMAL);
  797. // Initialize FormScreen
  798. #ifdef IGL_VIEWER_WITH_NANOGUI
  799. screen = new nanogui::Screen();
  800. screen->initialize(window, false);
  801. ngui = new nanogui::FormHelper(screen);
  802. #endif
  803. __viewer = this;
  804. // Register callbacks
  805. glfwSetKeyCallback(window, glfw_key_callback);
  806. glfwSetCursorPosCallback(window,glfw_mouse_move);
  807. glfwSetWindowSizeCallback(window,glfw_window_size);
  808. glfwSetMouseButtonCallback(window,glfw_mouse_press);
  809. glfwSetScrollCallback(window,glfw_mouse_scroll);
  810. glfwSetCharModsCallback(window,glfw_char_mods_callback);
  811. glfwSetDropCallback(window,glfw_drop_callback);
  812. // Handle retina displays (windows and mac)
  813. int width, height;
  814. glfwGetFramebufferSize(window, &width, &height);
  815. int width_window, height_window;
  816. glfwGetWindowSize(window, &width_window, &height_window);
  817. highdpi = width/width_window;
  818. glfw_window_size(window,width_window,height_window);
  819. opengl.init();
  820. core.align_camera_center(data.V,data.F);
  821. // Initialize IGL viewer
  822. init();
  823. return EXIT_SUCCESS;
  824. }
  825. IGL_INLINE bool Viewer::launch_rendering(bool loop)
  826. {
  827. // glfwMakeContextCurrent(window);
  828. // Rendering loop
  829. while (!glfwWindowShouldClose(window))
  830. {
  831. double tic = get_seconds();
  832. draw();
  833. glfwSwapBuffers(window);
  834. if(core.is_animating)
  835. {
  836. glfwPollEvents();
  837. // In microseconds
  838. double duration = 1000000.*(get_seconds()-tic);
  839. const double min_duration = 1000000./core.animation_max_fps;
  840. if(duration<min_duration)
  841. {
  842. std::this_thread::sleep_for(std::chrono::microseconds((int)(min_duration-duration)));
  843. }
  844. }
  845. else
  846. {
  847. glfwWaitEvents();
  848. }
  849. if (!loop)
  850. return !glfwWindowShouldClose(window);
  851. }
  852. return EXIT_SUCCESS;
  853. }
  854. IGL_INLINE void Viewer::launch_shut()
  855. {
  856. opengl.free();
  857. core.shut();
  858. shutdown_plugins();
  859. #ifdef IGL_VIEWER_WITH_NANOGUI
  860. delete ngui;
  861. //delete screen;
  862. screen = nullptr;
  863. ngui = nullptr;
  864. #endif
  865. glfwDestroyWindow(window);
  866. glfwTerminate();
  867. return;
  868. }
  869. IGL_INLINE int Viewer::launch(bool resizable,bool fullscreen)
  870. {
  871. // TODO return values are being ignored...
  872. launch_init(resizable,fullscreen);
  873. launch_rendering(true);
  874. launch_shut();
  875. return EXIT_SUCCESS;
  876. }
  877. IGL_INLINE bool Viewer::igl_with_nanogui_defined_at_compile()
  878. {
  879. #ifdef IGL_VIEWER_WITH_NANOGUI
  880. return true;
  881. #else
  882. return false;
  883. #endif
  884. }
  885. IGL_INLINE bool Viewer::igl_with_nanogui_defined_consistently()
  886. {
  887. return
  888. igl_with_nanogui_defined_at_compile() ==
  889. igl_with_nanogui_defined_at_include();
  890. }
  891. } // end namespace
  892. }