Viewer.cpp 25 KB

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