Viewer.cpp 26 KB

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