Viewer.cpp 28 KB

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