Viewer.cpp 25 KB

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