Viewer.cpp 25 KB

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