Viewer.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <[email protected]>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "Viewer.h"
  9. #include <chrono>
  10. #include <thread>
  11. #include <Eigen/LU>
  12. #include "../gl.h"
  13. #include "../report_gl_error.h"
  14. #include <GLFW/glfw3.h>
  15. #include <cmath>
  16. #include <cstdio>
  17. #include <sstream>
  18. #include <iomanip>
  19. #include <iostream>
  20. #include <fstream>
  21. #include <algorithm>
  22. #include <limits>
  23. #include <cassert>
  24. #include "../../project.h"
  25. #include "../../get_seconds.h"
  26. #include "../../readOBJ.h"
  27. #include "../../read_triangle_mesh.h"
  28. #include "../../adjacency_list.h"
  29. #include "../../writeOBJ.h"
  30. #include "../../writeOFF.h"
  31. #include "../../massmatrix.h"
  32. #include "../../file_dialog_open.h"
  33. #include "../../file_dialog_save.h"
  34. #include "../../quat_mult.h"
  35. #include "../../axis_angle_to_quat.h"
  36. #include "../../trackball.h"
  37. #include "../../two_axis_valuator_fixed_up.h"
  38. #include "../../snap_to_canonical_view_quat.h"
  39. #include "../../unproject.h"
  40. #include "../../serialize.h"
  41. // Internal global variables used for glfw event handling
  42. static igl::opengl::glfw::Viewer * __viewer;
  43. static double highdpiw = 1; // High DPI width
  44. static double highdpih = 1; // High DPI height
  45. static double scroll_x = 0;
  46. static double scroll_y = 0;
  47. static void glfw_mouse_press(GLFWwindow* window, int button, int action, int modifier)
  48. {
  49. igl::opengl::glfw::Viewer::MouseButton mb;
  50. if (button == GLFW_MOUSE_BUTTON_1)
  51. mb = igl::opengl::glfw::Viewer::MouseButton::Left;
  52. else if (button == GLFW_MOUSE_BUTTON_2)
  53. mb = igl::opengl::glfw::Viewer::MouseButton::Right;
  54. else //if (button == GLFW_MOUSE_BUTTON_3)
  55. mb = igl::opengl::glfw::Viewer::MouseButton::Middle;
  56. if (action == GLFW_PRESS)
  57. __viewer->mouse_down(mb,modifier);
  58. else
  59. __viewer->mouse_up(mb,modifier);
  60. }
  61. static void glfw_error_callback(int error, const char* description)
  62. {
  63. fputs(description, stderr);
  64. }
  65. static void glfw_char_mods_callback(GLFWwindow* window, unsigned int codepoint, int modifier)
  66. {
  67. __viewer->key_pressed(codepoint, modifier);
  68. }
  69. static void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int modifier)
  70. {
  71. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  72. glfwSetWindowShouldClose(window, GL_TRUE);
  73. if (action == GLFW_PRESS)
  74. __viewer->key_down(key, modifier);
  75. else if(action == GLFW_RELEASE)
  76. __viewer->key_up(key, modifier);
  77. }
  78. static void glfw_window_size(GLFWwindow* window, int width, int height)
  79. {
  80. int w = width*highdpiw;
  81. int h = height*highdpih;
  82. __viewer->post_resize(w, h);
  83. }
  84. static void glfw_mouse_move(GLFWwindow* window, double x, double y)
  85. {
  86. __viewer->mouse_move(x*highdpiw, y*highdpih);
  87. }
  88. static void glfw_mouse_scroll(GLFWwindow* window, double x, double y)
  89. {
  90. using namespace std;
  91. scroll_x += x;
  92. scroll_y += y;
  93. __viewer->mouse_scroll(y);
  94. }
  95. static void glfw_drop_callback(GLFWwindow *window,int count,const char **filenames)
  96. {
  97. }
  98. namespace igl
  99. {
  100. namespace opengl
  101. {
  102. namespace glfw
  103. {
  104. IGL_INLINE int Viewer::launch(bool resizable /*= true*/, bool fullscreen /*= false*/,
  105. const std::string &name, int windowWidth /*= 0*/, int windowHeight /*= 0*/)
  106. {
  107. // TODO return values are being ignored...
  108. launch_init(resizable,fullscreen,name,windowWidth,windowHeight);
  109. launch_rendering(true);
  110. launch_shut();
  111. return EXIT_SUCCESS;
  112. }
  113. IGL_INLINE int Viewer::launch_init(bool resizable, bool fullscreen,
  114. const std::string &name, int windowWidth, int windowHeight)
  115. {
  116. glfwSetErrorCallback(glfw_error_callback);
  117. if (!glfwInit())
  118. {
  119. return EXIT_FAILURE;
  120. }
  121. glfwWindowHint(GLFW_SAMPLES, 8);
  122. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  123. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  124. #ifdef __APPLE__
  125. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  126. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  127. #endif
  128. if(fullscreen)
  129. {
  130. GLFWmonitor *monitor = glfwGetPrimaryMonitor();
  131. const GLFWvidmode *mode = glfwGetVideoMode(monitor);
  132. window = glfwCreateWindow(mode->width,mode->height,name.c_str(),monitor,nullptr);
  133. windowWidth = mode->width;
  134. windowHeight = mode->height;
  135. }
  136. else
  137. {
  138. // Set default windows width
  139. if (windowWidth <= 0 && core_list.size() == 1 && core().viewport[2] > 0)
  140. windowWidth = core().viewport[2];
  141. else if (windowWidth <= 0)
  142. windowWidth = 1280;
  143. // Set default windows height
  144. if (windowHeight <= 0 && core_list.size() == 1 && core().viewport[3] > 0)
  145. windowHeight = core().viewport[3];
  146. else if (windowHeight <= 0)
  147. windowHeight = 800;
  148. window = glfwCreateWindow(windowWidth,windowHeight,name.c_str(),nullptr,nullptr);
  149. }
  150. if (!window)
  151. {
  152. glfwTerminate();
  153. return EXIT_FAILURE;
  154. }
  155. glfwMakeContextCurrent(window);
  156. // Load OpenGL and its extensions
  157. if (!gladLoadGL((GLADloadfunc) glfwGetProcAddress))
  158. {
  159. printf("Failed to load OpenGL and its extensions\n");
  160. return(-1);
  161. }
  162. #if defined(DEBUG) || defined(_DEBUG)
  163. printf("OpenGL Version %d.%d loaded\n", GLVersion.major, GLVersion.minor);
  164. int major, minor, rev;
  165. major = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
  166. minor = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
  167. rev = glfwGetWindowAttrib(window, GLFW_CONTEXT_REVISION);
  168. printf("OpenGL version received: %d.%d.%d\n", major, minor, rev);
  169. printf("Supported OpenGL is %s\n", (const char*)glGetString(GL_VERSION));
  170. printf("Supported GLSL is %s\n", (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION));
  171. #endif
  172. glfwSetInputMode(window,GLFW_CURSOR,GLFW_CURSOR_NORMAL);
  173. // Initialize FormScreen
  174. __viewer = this;
  175. // Register callbacks
  176. glfwSetKeyCallback(window, glfw_key_callback);
  177. glfwSetCursorPosCallback(window,glfw_mouse_move);
  178. glfwSetWindowSizeCallback(window,glfw_window_size);
  179. glfwSetMouseButtonCallback(window,glfw_mouse_press);
  180. glfwSetScrollCallback(window,glfw_mouse_scroll);
  181. glfwSetCharModsCallback(window,glfw_char_mods_callback);
  182. glfwSetDropCallback(window,glfw_drop_callback);
  183. // Handle retina displays (windows and mac)
  184. int width, height;
  185. glfwGetFramebufferSize(window, &width, &height);
  186. int width_window, height_window;
  187. glfwGetWindowSize(window, &width_window, &height_window);
  188. highdpiw = windowWidth/width_window;
  189. highdpih = windowHeight/height_window;
  190. glfw_window_size(window,width_window,height_window);
  191. // Initialize IGL viewer
  192. init();
  193. for(auto &core : this->core_list)
  194. {
  195. for(auto &data : this->data_list)
  196. {
  197. if(data.is_visible & core.id)
  198. {
  199. this->core(core.id).align_camera_center(data.V, data.F);
  200. }
  201. }
  202. }
  203. return EXIT_SUCCESS;
  204. }
  205. IGL_INLINE bool Viewer::launch_rendering(bool loop)
  206. {
  207. // glfwMakeContextCurrent(window);
  208. // Rendering loop
  209. const int num_extra_frames = 5;
  210. int frame_counter = 0;
  211. while (!glfwWindowShouldClose(window))
  212. {
  213. double tic = get_seconds();
  214. draw();
  215. glfwSwapBuffers(window);
  216. if(core().is_animating || frame_counter++ < num_extra_frames)
  217. {
  218. glfwPollEvents();
  219. }else
  220. {
  221. glfwWaitEvents();
  222. frame_counter = 0;
  223. }
  224. // In microseconds
  225. double duration = 1000000.*(get_seconds()-tic);
  226. const double min_duration = 1000000./core().animation_max_fps;
  227. if(duration<min_duration)
  228. {
  229. std::this_thread::sleep_for(std::chrono::microseconds((int)(min_duration-duration)));
  230. }
  231. if (!loop)
  232. return !glfwWindowShouldClose(window);
  233. #ifdef __APPLE__
  234. static bool first_time_hack = true;
  235. if(first_time_hack) {
  236. glfwHideWindow(window);
  237. glfwShowWindow(window);
  238. first_time_hack = false;
  239. }
  240. #endif
  241. }
  242. return EXIT_SUCCESS;
  243. }
  244. IGL_INLINE void Viewer::launch_shut()
  245. {
  246. for(auto & data : data_list)
  247. {
  248. data.meshgl.free();
  249. }
  250. for(auto &core : this->core_list)
  251. {
  252. core.shut();
  253. }
  254. shutdown_plugins();
  255. glfwDestroyWindow(window);
  256. glfwTerminate();
  257. return;
  258. }
  259. IGL_INLINE void Viewer::init()
  260. {
  261. for(auto &core : this->core_list)
  262. {
  263. core.init();
  264. }
  265. if (callback_init)
  266. if (callback_init(*this))
  267. return;
  268. init_plugins();
  269. }
  270. IGL_INLINE void Viewer::init_plugins()
  271. {
  272. // Init all plugins
  273. for (unsigned int i = 0; i<plugins.size(); ++i)
  274. {
  275. plugins[i]->init(this);
  276. }
  277. }
  278. IGL_INLINE void Viewer::shutdown_plugins()
  279. {
  280. for (unsigned int i = 0; i<plugins.size(); ++i)
  281. {
  282. plugins[i]->shutdown();
  283. }
  284. }
  285. IGL_INLINE Viewer::Viewer():
  286. data_list(1),
  287. selected_data_index(0),
  288. next_data_id(1),
  289. selected_core_index(0),
  290. next_core_id(2)
  291. {
  292. window = nullptr;
  293. data_list.front().id = 0;
  294. core_list.emplace_back(ViewerCore());
  295. core_list.front().id = 1;
  296. // Temporary variables initialization
  297. down = false;
  298. hack_never_moved = true;
  299. scroll_position = 0.0f;
  300. // Per face
  301. data().set_face_based(false);
  302. // C-style callbacks
  303. callback_init = nullptr;
  304. callback_pre_draw = nullptr;
  305. callback_post_draw = nullptr;
  306. callback_mouse_down = nullptr;
  307. callback_mouse_up = nullptr;
  308. callback_mouse_move = nullptr;
  309. callback_mouse_scroll = nullptr;
  310. callback_key_down = nullptr;
  311. callback_key_up = nullptr;
  312. callback_init_data = nullptr;
  313. callback_pre_draw_data = nullptr;
  314. callback_post_draw_data = nullptr;
  315. callback_mouse_down_data = nullptr;
  316. callback_mouse_up_data = nullptr;
  317. callback_mouse_move_data = nullptr;
  318. callback_mouse_scroll_data = nullptr;
  319. callback_key_down_data = nullptr;
  320. callback_key_up_data = nullptr;
  321. #ifndef IGL_VIEWER_VIEWER_QUIET
  322. const std::string usage(R"(igl::opengl::glfw::Viewer usage:
  323. [drag] Rotate scene
  324. A,a Toggle animation (tight draw loop)
  325. D,d Toggle double sided lighting
  326. F,f Toggle face based
  327. I,i Toggle invert normals
  328. L,l Toggle wireframe
  329. O,o Toggle orthographic/perspective projection
  330. S,s Toggle shadows
  331. T,t Toggle filled faces
  332. Z Snap to canonical view
  333. [,] Toggle between rotation control types (trackball, two-axis
  334. valuator with fixed up, 2D mode with no rotation))
  335. <,> Toggle between models
  336. ; Toggle vertex labels
  337. : Toggle face labels)"
  338. );
  339. std::cout<<usage<<std::endl;
  340. #endif
  341. }
  342. IGL_INLINE Viewer::~Viewer()
  343. {
  344. }
  345. IGL_INLINE bool Viewer::load_mesh_from_file(
  346. const std::string & mesh_file_name_string)
  347. {
  348. // first try to load it with a plugin
  349. for (unsigned int i = 0; i<plugins.size(); ++i)
  350. {
  351. if (plugins[i]->load(mesh_file_name_string))
  352. {
  353. return true;
  354. }
  355. }
  356. // Create new data slot and set to selected
  357. if(!(data().F.rows() == 0 && data().V.rows() == 0))
  358. {
  359. append_mesh();
  360. }
  361. data().clear();
  362. size_t last_dot = mesh_file_name_string.rfind('.');
  363. if (last_dot == std::string::npos)
  364. {
  365. std::cerr<<"Error: No file extension found in "<<
  366. mesh_file_name_string<<std::endl;
  367. return false;
  368. }
  369. std::string extension = mesh_file_name_string.substr(last_dot+1);
  370. if (extension == "obj" || extension =="OBJ")
  371. {
  372. Eigen::MatrixXd corner_normals;
  373. Eigen::MatrixXi fNormIndices;
  374. Eigen::MatrixXd UV_V;
  375. Eigen::MatrixXi UV_F;
  376. Eigen::MatrixXd V;
  377. Eigen::MatrixXi F;
  378. if (!(
  379. igl::readOBJ(
  380. mesh_file_name_string,
  381. V, UV_V, corner_normals, F, UV_F, fNormIndices)))
  382. {
  383. return false;
  384. }
  385. data().set_mesh(V,F);
  386. if(UV_V.rows() != 0 && UV_F.rows() != 0)
  387. {
  388. data().set_uv(UV_V,UV_F);
  389. }
  390. }else
  391. {
  392. Eigen::MatrixXd V;
  393. Eigen::MatrixXi F;
  394. if (!igl::read_triangle_mesh(mesh_file_name_string, V, F))
  395. {
  396. // unrecognized file type
  397. printf("Error: %s is not a recognized file type.\n",extension.c_str());
  398. return false;
  399. }
  400. data().set_mesh(V,F);
  401. }
  402. data().compute_normals();
  403. data().uniform_colors(Eigen::Vector3d(51.0/255.0,43.0/255.0,33.3/255.0),
  404. Eigen::Vector3d(255.0/255.0,228.0/255.0,58.0/255.0),
  405. Eigen::Vector3d(255.0/255.0,235.0/255.0,80.0/255.0));
  406. for(int i=0;i<core_list.size(); i++)
  407. core_list[i].align_camera_center(data().V,data().F);
  408. for (unsigned int i = 0; i<plugins.size(); ++i)
  409. if (plugins[i]->post_load())
  410. return true;
  411. return true;
  412. }
  413. IGL_INLINE bool Viewer::save_mesh_to_file(
  414. const std::string & mesh_file_name_string)
  415. {
  416. // first try to load it with a plugin
  417. for (unsigned int i = 0; i<plugins.size(); ++i)
  418. if (plugins[i]->save(mesh_file_name_string))
  419. return true;
  420. size_t last_dot = mesh_file_name_string.rfind('.');
  421. if (last_dot == std::string::npos)
  422. {
  423. // No file type determined
  424. std::cerr<<"Error: No file extension found in "<<
  425. mesh_file_name_string<<std::endl;
  426. return false;
  427. }
  428. std::string extension = mesh_file_name_string.substr(last_dot+1);
  429. if (extension == "off" || extension =="OFF")
  430. {
  431. return igl::writeOFF(
  432. mesh_file_name_string,data().V,data().F);
  433. }
  434. else if (extension == "obj" || extension =="OBJ")
  435. {
  436. Eigen::MatrixXd corner_normals;
  437. Eigen::MatrixXi fNormIndices;
  438. Eigen::MatrixXd UV_V;
  439. Eigen::MatrixXi UV_F;
  440. return igl::writeOBJ(mesh_file_name_string,
  441. data().V,
  442. data().F,
  443. corner_normals, fNormIndices, UV_V, UV_F);
  444. }
  445. else
  446. {
  447. // unrecognized file type
  448. printf("Error: %s is not a recognized file type.\n",extension.c_str());
  449. return false;
  450. }
  451. return true;
  452. }
  453. IGL_INLINE bool Viewer::key_pressed(unsigned int unicode_key,int modifiers)
  454. {
  455. for (unsigned int i = 0; i<plugins.size(); ++i)
  456. {
  457. if (plugins[i]->key_pressed(unicode_key, modifiers))
  458. {
  459. return true;
  460. }
  461. }
  462. if (callback_key_pressed)
  463. if (callback_key_pressed(*this,unicode_key,modifiers))
  464. return true;
  465. switch(unicode_key)
  466. {
  467. case 'A':
  468. case 'a':
  469. {
  470. core().is_animating = !core().is_animating;
  471. return true;
  472. }
  473. case 'D':
  474. case 'd':
  475. {
  476. data().double_sided = !data().double_sided;
  477. return true;
  478. }
  479. case 'F':
  480. case 'f':
  481. {
  482. data().set_face_based(!data().face_based);
  483. return true;
  484. }
  485. case 'I':
  486. case 'i':
  487. {
  488. data().dirty |= MeshGL::DIRTY_NORMAL;
  489. data().invert_normals = !data().invert_normals;
  490. return true;
  491. }
  492. case 'L':
  493. case 'l':
  494. {
  495. core().toggle(data().show_lines);
  496. return true;
  497. }
  498. case 'O':
  499. case 'o':
  500. {
  501. core().orthographic = !core().orthographic;
  502. return true;
  503. }
  504. case 'S':
  505. case 's':
  506. {
  507. if(core().is_directional_light)
  508. {
  509. core().is_shadow_mapping = !core().is_shadow_mapping;
  510. }else
  511. {
  512. if(core().is_shadow_mapping)
  513. {
  514. core().is_shadow_mapping = false;
  515. }else
  516. {
  517. // The light_position when !is_directional_light is interpretted as
  518. // a position relative to the _eye_ (not look-at) position of the
  519. // camera.
  520. //
  521. // Meanwhile shadows only current work in is_directional_light mode.
  522. //
  523. // If the user wants to flip back and forth between [positional lights
  524. // without shadows] and [directional lights with shadows] then they
  525. // can high-jack this key_pressed with a callback.
  526. //
  527. // Until shadows support positional lights, let's switch to
  528. // directional lights here and match the direction best as possible to
  529. // the current light position.
  530. core().is_directional_light = true;
  531. core().light_position = core().light_position + core().camera_eye;
  532. core().is_shadow_mapping = true;
  533. }
  534. }
  535. return true;
  536. }
  537. case 'T':
  538. case 't':
  539. {
  540. core().toggle(data().show_faces);
  541. return true;
  542. }
  543. case 'Z':
  544. {
  545. snap_to_canonical_quaternion();
  546. return true;
  547. }
  548. case '[':
  549. case ']':
  550. {
  551. if(core().rotation_type == ViewerCore::ROTATION_TYPE_TRACKBALL)
  552. core().set_rotation_type(ViewerCore::ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP);
  553. else
  554. core().set_rotation_type(ViewerCore::ROTATION_TYPE_TRACKBALL);
  555. return true;
  556. }
  557. case '<':
  558. case '>':
  559. {
  560. selected_data_index =
  561. (selected_data_index + data_list.size() + (unicode_key=='>'?1:-1))%data_list.size();
  562. return true;
  563. }
  564. case '{':
  565. case '}':
  566. {
  567. selected_core_index =
  568. (selected_core_index + core_list.size() + (unicode_key=='}'?1:-1))%core_list.size();
  569. return true;
  570. }
  571. case ';':
  572. data().show_vertex_labels = !data().show_vertex_labels;
  573. return true;
  574. case ':':
  575. data().show_face_labels = !data().show_face_labels;
  576. return true;
  577. default: break;//do nothing
  578. }
  579. return false;
  580. }
  581. IGL_INLINE bool Viewer::key_down(int key,int modifiers)
  582. {
  583. for (unsigned int i = 0; i<plugins.size(); ++i)
  584. if (plugins[i]->key_down(key, modifiers))
  585. return true;
  586. if (callback_key_down)
  587. if (callback_key_down(*this,key,modifiers))
  588. return true;
  589. return false;
  590. }
  591. IGL_INLINE bool Viewer::key_up(int key,int modifiers)
  592. {
  593. for (unsigned int i = 0; i<plugins.size(); ++i)
  594. if (plugins[i]->key_up(key, modifiers))
  595. return true;
  596. if (callback_key_up)
  597. if (callback_key_up(*this,key,modifiers))
  598. return true;
  599. return false;
  600. }
  601. IGL_INLINE void Viewer::select_hovered_core()
  602. {
  603. int width_window, height_window;
  604. glfwGetFramebufferSize(window, &width_window, &height_window);
  605. for (int i = 0; i < core_list.size(); i++)
  606. {
  607. Eigen::Vector4f viewport = core_list[i].viewport;
  608. if ((current_mouse_x > viewport[0]) &&
  609. (current_mouse_x < viewport[0] + viewport[2]) &&
  610. ((height_window - current_mouse_y) > viewport[1]) &&
  611. ((height_window - current_mouse_y) < viewport[1] + viewport[3]))
  612. {
  613. selected_core_index = i;
  614. break;
  615. }
  616. }
  617. }
  618. IGL_INLINE bool Viewer::mouse_down(MouseButton button,int modifier)
  619. {
  620. // Remember mouse location at down even if used by callback/plugin
  621. down_mouse_x = current_mouse_x;
  622. down_mouse_y = current_mouse_y;
  623. for (unsigned int i = 0; i<plugins.size(); ++i)
  624. if(plugins[i]->mouse_down(static_cast<int>(button),modifier))
  625. return true;
  626. if (callback_mouse_down)
  627. if (callback_mouse_down(*this,static_cast<int>(button),modifier))
  628. return true;
  629. down = true;
  630. // Select the core containing the click location.
  631. select_hovered_core();
  632. down_translation = core().camera_translation;
  633. // Initialization code for the trackball
  634. Eigen::RowVector3d center = Eigen::RowVector3d(0,0,0);
  635. if(data().V.rows() > 0)
  636. {
  637. // be careful that V may be 2D
  638. center.head(data().V.cols()) = data().V.colwise().sum()/data().V.rows();
  639. }
  640. Eigen::Vector3f coord =
  641. igl::project(
  642. Eigen::Vector3f(center(0),center(1),center(2)),
  643. core().view,
  644. core().proj,
  645. core().viewport);
  646. down_mouse_z = coord[2];
  647. down_rotation = core().trackball_angle;
  648. mouse_mode = MouseMode::Rotation;
  649. switch (button)
  650. {
  651. case MouseButton::Left:
  652. if (core().rotation_type == ViewerCore::ROTATION_TYPE_NO_ROTATION) {
  653. mouse_mode = MouseMode::Translation;
  654. } else {
  655. mouse_mode = MouseMode::Rotation;
  656. }
  657. break;
  658. case MouseButton::Right:
  659. mouse_mode = MouseMode::Translation;
  660. break;
  661. default:
  662. mouse_mode = MouseMode::None;
  663. break;
  664. }
  665. return true;
  666. }
  667. IGL_INLINE bool Viewer::mouse_up(MouseButton button,int modifier)
  668. {
  669. down = false;
  670. for (unsigned int i = 0; i<plugins.size(); ++i)
  671. if(plugins[i]->mouse_up(static_cast<int>(button),modifier))
  672. return true;
  673. if (callback_mouse_up)
  674. if (callback_mouse_up(*this,static_cast<int>(button),modifier))
  675. return true;
  676. mouse_mode = MouseMode::None;
  677. return true;
  678. }
  679. IGL_INLINE bool Viewer::mouse_move(int mouse_x,int mouse_y)
  680. {
  681. if(hack_never_moved)
  682. {
  683. down_mouse_x = mouse_x;
  684. down_mouse_y = mouse_y;
  685. hack_never_moved = false;
  686. }
  687. current_mouse_x = mouse_x;
  688. current_mouse_y = mouse_y;
  689. for (unsigned int i = 0; i<plugins.size(); ++i)
  690. if (plugins[i]->mouse_move(mouse_x, mouse_y))
  691. return true;
  692. if (callback_mouse_move)
  693. if (callback_mouse_move(*this, mouse_x, mouse_y))
  694. return true;
  695. if (down)
  696. {
  697. // We need the window height to transform the mouse click coordinates into viewport-mouse-click coordinates
  698. // for igl::trackball and igl::two_axis_valuator_fixed_up
  699. int width_window, height_window;
  700. glfwGetFramebufferSize(window, &width_window, &height_window);
  701. switch (mouse_mode)
  702. {
  703. case MouseMode::Rotation:
  704. {
  705. switch(core().rotation_type)
  706. {
  707. default:
  708. assert(false && "Unknown rotation type");
  709. case ViewerCore::ROTATION_TYPE_NO_ROTATION:
  710. break;
  711. case ViewerCore::ROTATION_TYPE_TRACKBALL:
  712. igl::trackball(
  713. core().viewport(2),
  714. core().viewport(3),
  715. 2.0f,
  716. down_rotation,
  717. down_mouse_x - core().viewport(0),
  718. down_mouse_y - (height_window - core().viewport(1) - core().viewport(3)),
  719. mouse_x - core().viewport(0),
  720. mouse_y - (height_window - core().viewport(1) - core().viewport(3)),
  721. core().trackball_angle);
  722. break;
  723. case ViewerCore::ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP:
  724. igl::two_axis_valuator_fixed_up(
  725. core().viewport(2),core().viewport(3),
  726. 2.0,
  727. down_rotation,
  728. down_mouse_x - core().viewport(0),
  729. down_mouse_y - (height_window - core().viewport(1) - core().viewport(3)),
  730. mouse_x - core().viewport(0),
  731. mouse_y - (height_window - core().viewport(1) - core().viewport(3)),
  732. core().trackball_angle);
  733. break;
  734. }
  735. //Eigen::Vector4f snapq = core().trackball_angle;
  736. break;
  737. }
  738. case MouseMode::Translation:
  739. {
  740. //translation
  741. Eigen::Vector3f pos1 = igl::unproject(Eigen::Vector3f(mouse_x, core().viewport[3] - mouse_y, down_mouse_z), core().view, core().proj, core().viewport);
  742. Eigen::Vector3f pos0 = igl::unproject(Eigen::Vector3f(down_mouse_x, core().viewport[3] - down_mouse_y, down_mouse_z), core().view, core().proj, core().viewport);
  743. Eigen::Vector3f diff = pos1 - pos0;
  744. core().camera_translation = down_translation + Eigen::Vector3f(diff[0],diff[1],diff[2]);
  745. break;
  746. }
  747. case MouseMode::Zoom:
  748. {
  749. float delta = 0.001f * (mouse_x - down_mouse_x + mouse_y - down_mouse_y);
  750. core().camera_zoom *= 1 + delta;
  751. down_mouse_x = mouse_x;
  752. down_mouse_y = mouse_y;
  753. break;
  754. }
  755. default:
  756. break;
  757. }
  758. }
  759. return true;
  760. }
  761. IGL_INLINE bool Viewer::mouse_scroll(float delta_y)
  762. {
  763. // Direct the scrolling operation to the appropriate viewport
  764. // (unless the core selection is locked by an ongoing mouse interaction).
  765. if (!down)
  766. select_hovered_core();
  767. scroll_position += delta_y;
  768. for (unsigned int i = 0; i<plugins.size(); ++i)
  769. if (plugins[i]->mouse_scroll(delta_y))
  770. return true;
  771. if (callback_mouse_scroll)
  772. if (callback_mouse_scroll(*this,delta_y))
  773. return true;
  774. // Only zoom if there's actually a change
  775. if(delta_y != 0)
  776. {
  777. float mult = (1.0+((delta_y>0)?1.:-1.)*0.05);
  778. const float min_zoom = 0.1f;
  779. core().camera_zoom = (core().camera_zoom * mult > min_zoom ? core().camera_zoom * mult : min_zoom);
  780. }
  781. return true;
  782. }
  783. IGL_INLINE bool Viewer::load_scene()
  784. {
  785. std::string fname = igl::file_dialog_open();
  786. if(fname.length() == 0)
  787. return false;
  788. return load_scene(fname);
  789. }
  790. IGL_INLINE bool Viewer::load_scene(std::string fname)
  791. {
  792. igl::deserialize(core(),"Core",fname.c_str());
  793. igl::deserialize(data(),"Data",fname.c_str());
  794. return true;
  795. }
  796. IGL_INLINE bool Viewer::save_scene()
  797. {
  798. std::string fname = igl::file_dialog_save();
  799. if (fname.length() == 0)
  800. return false;
  801. return save_scene(fname);
  802. }
  803. IGL_INLINE bool Viewer::save_scene(std::string fname)
  804. {
  805. igl::serialize(core(),"Core",fname.c_str(),true);
  806. igl::serialize(data(),"Data",fname.c_str());
  807. return true;
  808. }
  809. IGL_INLINE void Viewer::draw()
  810. {
  811. using namespace std;
  812. using namespace Eigen;
  813. int width, height;
  814. glfwGetFramebufferSize(window, &width, &height);
  815. int width_window, height_window;
  816. glfwGetWindowSize(window, &width_window, &height_window);
  817. auto highdpiw_tmp = (width_window == 0 || width == 0) ? highdpiw : (width/width_window);
  818. auto highdpih_tmp = (height_window == 0 || height == 0) ? highdpih : (height/height_window);
  819. if(fabs(highdpiw_tmp-highdpiw)>1e-8 || fabs(highdpih_tmp-highdpih)>1e-8)
  820. {
  821. post_resize(width, height);
  822. highdpiw=highdpiw_tmp;
  823. highdpih=highdpih_tmp;
  824. }
  825. for (auto& core : core_list)
  826. {
  827. core.clear_framebuffers();
  828. }
  829. for (unsigned int i = 0; i<plugins.size(); ++i)
  830. {
  831. if (plugins[i]->pre_draw())
  832. {
  833. return;
  834. }
  835. }
  836. if (callback_pre_draw)
  837. {
  838. if (callback_pre_draw(*this))
  839. {
  840. return;
  841. }
  842. }
  843. // Shadow pass
  844. for (auto& core : core_list)
  845. {
  846. if(core.is_shadow_mapping)
  847. {
  848. core.initialize_shadow_pass();
  849. for (auto& mesh : data_list)
  850. {
  851. if (mesh.is_visible & core.id)
  852. {
  853. core.draw_shadow_pass(mesh);
  854. }
  855. }
  856. core.deinitialize_shadow_pass();
  857. }
  858. }
  859. for (auto& core : core_list)
  860. {
  861. for (auto& mesh : data_list)
  862. {
  863. if (mesh.is_visible & core.id)
  864. {
  865. core.draw(mesh);
  866. }
  867. }
  868. }
  869. for (unsigned int i = 0; i<plugins.size(); ++i)
  870. {
  871. if (plugins[i]->post_draw())
  872. {
  873. break;
  874. }
  875. }
  876. if (callback_post_draw)
  877. {
  878. if (callback_post_draw(*this))
  879. {
  880. return;
  881. }
  882. }
  883. }
  884. template <typename T>
  885. IGL_INLINE void Viewer::draw_buffer(
  886. igl::opengl::ViewerCore & core,
  887. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & R,
  888. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & G,
  889. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & B,
  890. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & A,
  891. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & D)
  892. {
  893. // follows igl::opengl::ViewerCore::draw_buffer, image is transposed from
  894. // typical matrix view
  895. const int width = R.rows() ? R.rows() : core.viewport(2);
  896. const int height = R.cols() ? R.cols() : core.viewport(3);
  897. R.resize(width,height);
  898. G.resize(width,height);
  899. B.resize(width,height);
  900. A.resize(width,height);
  901. D.resize(width,height);
  902. ////////////////////////////////////////////////////////////////////////
  903. // Create an initial multisampled framebuffer
  904. ////////////////////////////////////////////////////////////////////////
  905. unsigned int framebuffer;
  906. unsigned int color_buffer;
  907. unsigned int depth_buffer;
  908. {
  909. glGenFramebuffers(1, &framebuffer);
  910. glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
  911. // create a multisampled color attachment texture (is a texture really
  912. // needed? Could this be a renderbuffer instead?)
  913. glGenTextures(1, &color_buffer);
  914. glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, color_buffer);
  915. glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_RGBA, width, height, GL_TRUE);
  916. glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);
  917. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, color_buffer, 0);
  918. // create a (also multisampled) renderbuffer object for depth and stencil attachments
  919. glGenRenderbuffers(1, &depth_buffer);
  920. glBindRenderbuffer(GL_RENDERBUFFER, depth_buffer);
  921. glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_DEPTH24_STENCIL8, width, height);
  922. glBindRenderbuffer(GL_RENDERBUFFER, 0);
  923. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depth_buffer);
  924. assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
  925. report_gl_error("glCheckFramebufferStatus: ");
  926. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  927. }
  928. ////////////////////////////////////////////////////////////////////////
  929. // configure second post-processing framebuffer
  930. ////////////////////////////////////////////////////////////////////////
  931. unsigned int intermediateFBO;
  932. unsigned int screenTexture, depthTexture;
  933. {
  934. glGenFramebuffers(1, &intermediateFBO);
  935. glBindFramebuffer(GL_FRAMEBUFFER, intermediateFBO);
  936. // create a color attachment texture
  937. glGenTextures(1, &screenTexture);
  938. glBindTexture(GL_TEXTURE_2D, screenTexture);
  939. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  940. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  941. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  942. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, screenTexture, 0);
  943. // create depth attachment texture
  944. glGenTextures(1, &depthTexture);
  945. glBindTexture(GL_TEXTURE_2D, depthTexture);
  946. glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, width, height, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, NULL);
  947. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  948. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  949. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0);
  950. assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
  951. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  952. }
  953. ////////////////////////////////////////////////////////////////////////
  954. // attach initial framebuffer and draw all `data`
  955. ////////////////////////////////////////////////////////////////////////
  956. glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
  957. // Clear the buffer
  958. glClearColor(
  959. core.background_color(0),
  960. core.background_color(1),
  961. core.background_color(2),
  962. core.background_color(3));
  963. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  964. // Save old viewport
  965. Eigen::Vector4f viewport_ori = core.viewport;
  966. core.viewport << 0,0,width,height;
  967. // Draw all `data`
  968. for (auto& data : data_list)
  969. {
  970. if (data.is_visible & core.id)
  971. {
  972. core.draw(data);
  973. }
  974. }
  975. // Restore viewport
  976. core.viewport = viewport_ori;
  977. ////////////////////////////////////////////////////////////////////////
  978. // attach second framebuffer and redraw (for anti-aliasing?)
  979. ////////////////////////////////////////////////////////////////////////
  980. glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
  981. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, intermediateFBO);
  982. report_gl_error("before: ");
  983. glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT, GL_NEAREST);
  984. report_gl_error("glBlitFramebuffer: ");
  985. ////////////////////////////////////////////////////////////////////////
  986. // Read pixel data from framebuffer, write into buffers
  987. ////////////////////////////////////////////////////////////////////////
  988. glBindFramebuffer(GL_FRAMEBUFFER, intermediateFBO);
  989. // Copy back in the given Eigen matrices
  990. {
  991. typedef typename std::conditional< std::is_floating_point<T>::value,GLfloat,GLubyte>::type GLType;
  992. GLenum type = std::is_floating_point<T>::value ? GL_FLOAT : GL_UNSIGNED_BYTE;
  993. GLType* pixels = (GLType*)calloc(width*height*4,sizeof(GLType));
  994. GLType * depth = (GLType*)calloc(width*height*1,sizeof(GLType));
  995. glReadPixels(0, 0,width, height,GL_RGBA, type, pixels);
  996. glReadPixels(0, 0,width, height,GL_DEPTH_COMPONENT, type, depth);
  997. int count = 0;
  998. for (unsigned j=0; j<height; ++j)
  999. {
  1000. for (unsigned i=0; i<width; ++i)
  1001. {
  1002. R(i,j) = pixels[count*4+0];
  1003. G(i,j) = pixels[count*4+1];
  1004. B(i,j) = pixels[count*4+2];
  1005. A(i,j) = pixels[count*4+3];
  1006. D(i,j) = depth[count*1+0];
  1007. ++count;
  1008. }
  1009. }
  1010. // Clean up
  1011. free(pixels);
  1012. free(depth);
  1013. }
  1014. // Clean up
  1015. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  1016. glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
  1017. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  1018. glDeleteTextures(1, &screenTexture);
  1019. glDeleteTextures(1, &depthTexture);
  1020. glDeleteTextures(1, &color_buffer);
  1021. glDeleteRenderbuffers(1, &depth_buffer);
  1022. glDeleteFramebuffers(1, &framebuffer);
  1023. glDeleteFramebuffers(1, &intermediateFBO);
  1024. }
  1025. IGL_INLINE void Viewer::resize(int w,int h)
  1026. {
  1027. if (window) {
  1028. glfwSetWindowSize(window, w/highdpiw, h/highdpih);
  1029. }
  1030. post_resize(w, h);
  1031. }
  1032. IGL_INLINE void Viewer::post_resize(int w,int h)
  1033. {
  1034. if (core_list.size() == 1)
  1035. {
  1036. core().viewport = Eigen::Vector4f(0,0,w,h);
  1037. }
  1038. else
  1039. {
  1040. // It is up to the user to define the behavior of the post_resize() function
  1041. // when there are multiple viewports (through the `callback_post_resize` callback)
  1042. }
  1043. for (unsigned int i = 0; i<plugins.size(); ++i)
  1044. {
  1045. plugins[i]->post_resize(w, h);
  1046. }
  1047. if (callback_post_resize)
  1048. {
  1049. callback_post_resize(*this, w, h);
  1050. }
  1051. }
  1052. IGL_INLINE void Viewer::snap_to_canonical_quaternion()
  1053. {
  1054. Eigen::Quaternionf snapq = this->core().trackball_angle;
  1055. igl::snap_to_canonical_view_quat(snapq,1.0f,this->core().trackball_angle);
  1056. }
  1057. IGL_INLINE void Viewer::open_dialog_load_mesh()
  1058. {
  1059. std::string fname = igl::file_dialog_open();
  1060. if (fname.length() == 0)
  1061. return;
  1062. this->load_mesh_from_file(fname.c_str());
  1063. }
  1064. IGL_INLINE void Viewer::open_dialog_save_mesh()
  1065. {
  1066. std::string fname = igl::file_dialog_save();
  1067. if(fname.length() == 0)
  1068. return;
  1069. this->save_mesh_to_file(fname.c_str());
  1070. }
  1071. IGL_INLINE ViewerData& Viewer::data(int mesh_id /*= -1*/)
  1072. {
  1073. assert(!data_list.empty() && "data_list should never be empty");
  1074. int index;
  1075. if (mesh_id == -1)
  1076. index = selected_data_index;
  1077. else
  1078. index = mesh_index(mesh_id);
  1079. assert((index >= 0 && index < data_list.size()) &&
  1080. "selected_data_index or mesh_id should be in bounds");
  1081. return data_list[index];
  1082. }
  1083. IGL_INLINE const ViewerData& Viewer::data(int mesh_id /*= -1*/) const
  1084. {
  1085. assert(!data_list.empty() && "data_list should never be empty");
  1086. int index;
  1087. if (mesh_id == -1)
  1088. index = selected_data_index;
  1089. else
  1090. index = mesh_index(mesh_id);
  1091. assert((index >= 0 && index < data_list.size()) &&
  1092. "selected_data_index or mesh_id should be in bounds");
  1093. return data_list[index];
  1094. }
  1095. IGL_INLINE int Viewer::append_mesh(bool visible /*= true*/)
  1096. {
  1097. assert(data_list.size() >= 1);
  1098. data_list.emplace_back();
  1099. selected_data_index = data_list.size()-1;
  1100. data_list.back().id = next_data_id++;
  1101. if (visible)
  1102. for (int i = 0; i < core_list.size(); i++)
  1103. data_list.back().set_visible(true, core_list[i].id);
  1104. else
  1105. data_list.back().is_visible = 0;
  1106. return data_list.back().id;
  1107. }
  1108. IGL_INLINE bool Viewer::erase_mesh(const size_t index)
  1109. {
  1110. assert((index >= 0 && index < data_list.size()) && "index should be in bounds");
  1111. assert(data_list.size() >= 1);
  1112. if(data_list.size() == 1)
  1113. {
  1114. // Cannot remove last mesh
  1115. return false;
  1116. }
  1117. data_list[index].meshgl.free();
  1118. data_list.erase(data_list.begin() + index);
  1119. if(selected_data_index >= index && selected_data_index > 0)
  1120. {
  1121. selected_data_index--;
  1122. }
  1123. return true;
  1124. }
  1125. IGL_INLINE size_t Viewer::mesh_index(const int id) const {
  1126. for (size_t i = 0; i < data_list.size(); ++i)
  1127. {
  1128. if (data_list[i].id == id)
  1129. return i;
  1130. }
  1131. return 0;
  1132. }
  1133. IGL_INLINE ViewerCore& Viewer::core(unsigned core_id /*= 0*/)
  1134. {
  1135. assert(!core_list.empty() && "core_list should never be empty");
  1136. int core_index;
  1137. if (core_id == 0)
  1138. core_index = selected_core_index;
  1139. else
  1140. core_index = this->core_index(core_id);
  1141. assert((core_index >= 0 && core_index < core_list.size()) && "selected_core_index should be in bounds");
  1142. return core_list[core_index];
  1143. }
  1144. IGL_INLINE const ViewerCore& Viewer::core(unsigned core_id /*= 0*/) const
  1145. {
  1146. assert(!core_list.empty() && "core_list should never be empty");
  1147. int core_index;
  1148. if (core_id == 0)
  1149. core_index = selected_core_index;
  1150. else
  1151. core_index = this->core_index(core_id);
  1152. assert((core_index >= 0 && core_index < core_list.size()) && "selected_core_index should be in bounds");
  1153. return core_list[core_index];
  1154. }
  1155. IGL_INLINE bool Viewer::erase_core(const size_t index)
  1156. {
  1157. assert((index >= 0 && index < core_list.size()) && "index should be in bounds");
  1158. assert(data_list.size() >= 1);
  1159. if (core_list.size() == 1)
  1160. {
  1161. // Cannot remove last viewport
  1162. return false;
  1163. }
  1164. core_list[index].shut(); // does nothing
  1165. core_list.erase(core_list.begin() + index);
  1166. if (selected_core_index >= index && selected_core_index > 0)
  1167. {
  1168. selected_core_index--;
  1169. }
  1170. return true;
  1171. }
  1172. IGL_INLINE size_t Viewer::core_index(const int id) const {
  1173. for (size_t i = 0; i < core_list.size(); ++i)
  1174. {
  1175. if (core_list[i].id == id)
  1176. return i;
  1177. }
  1178. return 0;
  1179. }
  1180. IGL_INLINE int Viewer::append_core(Eigen::Vector4f viewport, bool append_empty /*= false*/)
  1181. {
  1182. core_list.push_back(core()); // copies the previous active core and only changes the viewport
  1183. core_list.back().viewport = viewport;
  1184. core_list.back().id = next_core_id;
  1185. next_core_id <<= 1;
  1186. if (!append_empty)
  1187. {
  1188. for (auto &data : data_list)
  1189. {
  1190. data.set_visible(true, core_list.back().id);
  1191. data.copy_options(core(), core_list.back());
  1192. }
  1193. }
  1194. selected_core_index = core_list.size()-1;
  1195. return core_list.back().id;
  1196. }
  1197. } // end namespace
  1198. } // end namespace
  1199. }
  1200. #ifdef IGL_STATIC_LIBRARY
  1201. template void igl::opengl::glfw::Viewer::draw_buffer<unsigned char>(igl::opengl::ViewerCore&, Eigen::Matrix<unsigned char, -1, -1, 0, -1, -1>&, Eigen::Matrix<unsigned char, -1, -1, 0, -1, -1>&, Eigen::Matrix<unsigned char, -1, -1, 0, -1, -1>&, Eigen::Matrix<unsigned char, -1, -1, 0, -1, -1>&, Eigen::Matrix<unsigned char, -1, -1, 0, -1, -1>&);
  1202. template void igl::opengl::glfw::Viewer::draw_buffer<double>(igl::opengl::ViewerCore&, Eigen::Matrix<double, -1, -1, 0, -1, -1>&, Eigen::Matrix<double, -1, -1, 0, -1, -1>&, Eigen::Matrix<double, -1, -1, 0, -1, -1>&, Eigen::Matrix<double, -1, -1, 0, -1, -1>&, Eigen::Matrix<double, -1, -1, 0, -1, -1>&);
  1203. template void igl::opengl::glfw::Viewer::draw_buffer<float>(igl::opengl::ViewerCore&, Eigen::Matrix<float, -1, -1, 0, -1, -1>&, Eigen::Matrix<float, -1, -1, 0, -1, -1>&, Eigen::Matrix<float, -1, -1, 0, -1, -1>&, Eigen::Matrix<float, -1, -1, 0, -1, -1>&, Eigen::Matrix<float, -1, -1, 0, -1, -1>&);
  1204. #endif