Viewer.cpp 26 KB

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