Viewer.cpp 38 KB

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