Viewer.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  1. #include "Viewer.h"
  2. #include <igl/get_seconds.h>
  3. #ifdef _WIN32
  4. # include <windows.h>
  5. # undef max
  6. # undef min
  7. #endif
  8. // Todo: windows equivalent for `usleep`
  9. #include <unistd.h>
  10. #ifndef __APPLE__
  11. # define GLEW_STATIC
  12. # include <GL/glew.h>
  13. #endif
  14. #ifdef __APPLE__
  15. # include <OpenGL/gl3.h>
  16. # define __gl_h_ /* Prevent inclusion of the old gl.h */
  17. #else
  18. # ifdef _WIN32
  19. # include <windows.h>
  20. # endif
  21. # include <GL/gl.h>
  22. #endif
  23. #include <Eigen/LU>
  24. #define GLFW_INCLUDE_GLU
  25. #include <GLFW/glfw3.h>
  26. #include <cmath>
  27. #include <cstdio>
  28. #include <sstream>
  29. #include <iomanip>
  30. #include <iostream>
  31. #include <fstream>
  32. #include <algorithm>
  33. #include <igl/project.h>
  34. Eigen::Matrix4f lookAt (
  35. const Eigen::Vector3f& eye,
  36. const Eigen::Vector3f& center,
  37. const Eigen::Vector3f& up)
  38. {
  39. Eigen::Vector3f f = (center - eye).normalized();
  40. Eigen::Vector3f s = f.cross(up).normalized();
  41. Eigen::Vector3f u = s.cross(f);
  42. Eigen::Matrix4f Result = Eigen::Matrix4f::Identity();
  43. Result(0,0) = s(0);
  44. Result(0,1) = s(1);
  45. Result(0,2) = s(2);
  46. Result(1,0) = u(0);
  47. Result(1,1) = u(1);
  48. Result(1,2) = u(2);
  49. Result(2,0) =-f(0);
  50. Result(2,1) =-f(1);
  51. Result(2,2) =-f(2);
  52. Result(0,3) =-s.transpose() * eye;
  53. Result(1,3) =-u.transpose() * eye;
  54. Result(2,3) = f.transpose() * eye;
  55. return Result;
  56. }
  57. Eigen::Matrix4f ortho (
  58. const float left,
  59. const float right,
  60. const float bottom,
  61. const float top,
  62. const float zNear,
  63. const float zFar
  64. )
  65. {
  66. Eigen::Matrix4f Result = Eigen::Matrix4f::Identity();
  67. Result(0,0) = 2.0f / (right - left);
  68. Result(1,1) = 2.0f / (top - bottom);
  69. Result(2,2) = - 2.0f / (zFar - zNear);
  70. Result(0,3) = - (right + left) / (right - left);
  71. Result(1,3) = - (top + bottom) / (top - bottom);
  72. Result(2,3) = - (zFar + zNear) / (zFar - zNear);
  73. return Result;
  74. }
  75. Eigen::Matrix4f frustum (
  76. const float left,
  77. const float right,
  78. const float bottom,
  79. const float top,
  80. const float nearVal,
  81. const float farVal)
  82. {
  83. Eigen::Matrix4f Result = Eigen::Matrix4f::Zero();
  84. Result(0,0) = (2.0f * nearVal) / (right - left);
  85. Result(1,1) = (2.0f * nearVal) / (top - bottom);
  86. Result(0,2) = (right + left) / (right - left);
  87. Result(1,2) = (top + bottom) / (top - bottom);
  88. Result(2,2) = -(farVal + nearVal) / (farVal - nearVal);
  89. Result(3,2) = -1.0f;
  90. Result(2,3) = -(2.0f * farVal * nearVal) / (farVal - nearVal);
  91. return Result;
  92. }
  93. Eigen::Matrix4f scale (const Eigen::Matrix4f& m,
  94. const Eigen::Vector3f& v)
  95. {
  96. Eigen::Matrix4f Result;
  97. Result.col(0) = m.col(0).array() * v(0);
  98. Result.col(1) = m.col(1).array() * v(1);
  99. Result.col(2) = m.col(2).array() * v(2);
  100. Result.col(3) = m.col(3);
  101. return Result;
  102. }
  103. Eigen::Matrix4f translate(
  104. const Eigen::Matrix4f& m,
  105. const Eigen::Vector3f& v)
  106. {
  107. Eigen::Matrix4f Result = m;
  108. Result.col(3) = m.col(0).array() * v(0) + m.col(1).array() * v(1) + m.col(2).array() * v(2) + m.col(3).array();
  109. return Result;
  110. }
  111. #include <limits>
  112. #include <cassert>
  113. #ifdef ENABLE_XML_SERIALIZATION
  114. #include "igl/xml/XMLSerializer.h"
  115. #endif
  116. #include <igl/readOBJ.h>
  117. #include <igl/readOFF.h>
  118. #include <igl/adjacency_list.h>
  119. #include <igl/writeOBJ.h>
  120. #include <igl/writeOFF.h>
  121. #include <igl/massmatrix.h>
  122. #include <igl/file_dialog_open.h>
  123. #include <igl/file_dialog_save.h>
  124. #include <igl/quat_to_mat.h>
  125. #include <igl/quat_mult.h>
  126. #include <igl/axis_angle_to_quat.h>
  127. #include <igl/trackball.h>
  128. #include <igl/snap_to_canonical_view_quat.h>
  129. #include <igl/unproject.h>
  130. #include <igl/viewer/TextRenderer.h>
  131. // Internal global variables used for glfw event handling
  132. static igl::Viewer * __viewer;
  133. static double highdpi = 1;
  134. static double scroll_x = 0;
  135. static double scroll_y = 0;
  136. static igl::TextRenderer __font_renderer;
  137. static void glfw_mouse_press(GLFWwindow* window, int button, int action, int modifier)
  138. {
  139. bool tw_used = TwEventMouseButtonGLFW(button, action);
  140. igl::Viewer::MouseButton mb;
  141. if (button == GLFW_MOUSE_BUTTON_1)
  142. mb = igl::Viewer::IGL_LEFT;
  143. else if (button == GLFW_MOUSE_BUTTON_2)
  144. mb = igl::Viewer::IGL_RIGHT;
  145. else //if (button == GLFW_MOUSE_BUTTON_3)
  146. mb = igl::Viewer::IGL_MIDDLE;
  147. if (action == GLFW_PRESS)
  148. {
  149. if(!tw_used)
  150. {
  151. __viewer->mouse_down(mb,modifier);
  152. }
  153. } else
  154. {
  155. // Always call mouse_up on up
  156. __viewer->mouse_up(mb,modifier);
  157. }
  158. }
  159. static void glfw_error_callback(int error, const char* description)
  160. {
  161. fputs(description, stderr);
  162. }
  163. int global_KMod = 0;
  164. int TwEventKeyGLFW3(int glfwKey, int glfwAction)
  165. {
  166. int handled = 0;
  167. // Register of modifiers state
  168. if (glfwAction==GLFW_PRESS)
  169. {
  170. switch (glfwKey)
  171. {
  172. case GLFW_KEY_LEFT_SHIFT:
  173. case GLFW_KEY_RIGHT_SHIFT:
  174. global_KMod |= TW_KMOD_SHIFT;
  175. break;
  176. case GLFW_KEY_LEFT_CONTROL:
  177. case GLFW_KEY_RIGHT_CONTROL:
  178. global_KMod |= TW_KMOD_CTRL;
  179. break;
  180. case GLFW_KEY_LEFT_ALT:
  181. case GLFW_KEY_RIGHT_ALT:
  182. global_KMod |= TW_KMOD_ALT;
  183. break;
  184. }
  185. }
  186. else
  187. {
  188. switch (glfwKey)
  189. {
  190. case GLFW_KEY_LEFT_SHIFT:
  191. case GLFW_KEY_RIGHT_SHIFT:
  192. global_KMod &= ~TW_KMOD_SHIFT;
  193. break;
  194. case GLFW_KEY_LEFT_CONTROL:
  195. case GLFW_KEY_RIGHT_CONTROL:
  196. global_KMod &= ~TW_KMOD_CTRL;
  197. break;
  198. case GLFW_KEY_LEFT_ALT:
  199. case GLFW_KEY_RIGHT_ALT:
  200. global_KMod &= ~TW_KMOD_ALT;
  201. break;
  202. }
  203. }
  204. // Process key pressed
  205. if (glfwAction==GLFW_PRESS)
  206. {
  207. int mod = global_KMod;
  208. int testkp = ((mod&TW_KMOD_CTRL) || (mod&TW_KMOD_ALT)) ? 1 : 0;
  209. if ((mod&TW_KMOD_CTRL) && glfwKey>0 && glfwKey<GLFW_KEY_ESCAPE ) // CTRL cases
  210. handled = TwKeyPressed(glfwKey, mod);
  211. else if (glfwKey>=GLFW_KEY_ESCAPE )
  212. {
  213. int k = 0;
  214. if (glfwKey>=GLFW_KEY_F1 && glfwKey<=GLFW_KEY_F15)
  215. k = TW_KEY_F1 + (glfwKey-GLFW_KEY_F1);
  216. else if (testkp && glfwKey>=GLFW_KEY_KP_0 && glfwKey<=GLFW_KEY_KP_9)
  217. k = '0' + (glfwKey-GLFW_KEY_KP_0);
  218. else
  219. {
  220. switch (glfwKey)
  221. {
  222. case GLFW_KEY_ESCAPE :
  223. k = TW_KEY_ESCAPE;
  224. break;
  225. case GLFW_KEY_UP:
  226. k = TW_KEY_UP;
  227. break;
  228. case GLFW_KEY_DOWN:
  229. k = TW_KEY_DOWN;
  230. break;
  231. case GLFW_KEY_LEFT:
  232. k = TW_KEY_LEFT;
  233. break;
  234. case GLFW_KEY_RIGHT:
  235. k = TW_KEY_RIGHT;
  236. break;
  237. case GLFW_KEY_TAB:
  238. k = TW_KEY_TAB;
  239. break;
  240. case GLFW_KEY_ENTER:
  241. k = TW_KEY_RETURN;
  242. break;
  243. case GLFW_KEY_BACKSPACE:
  244. k = TW_KEY_BACKSPACE;
  245. break;
  246. case GLFW_KEY_INSERT:
  247. k = TW_KEY_INSERT;
  248. break;
  249. case GLFW_KEY_DELETE:
  250. k = TW_KEY_DELETE;
  251. break;
  252. case GLFW_KEY_PAGE_UP:
  253. k = TW_KEY_PAGE_UP;
  254. break;
  255. case GLFW_KEY_PAGE_DOWN:
  256. k = TW_KEY_PAGE_DOWN;
  257. break;
  258. case GLFW_KEY_HOME:
  259. k = TW_KEY_HOME;
  260. break;
  261. case GLFW_KEY_END:
  262. k = TW_KEY_END;
  263. break;
  264. case GLFW_KEY_KP_ENTER:
  265. k = TW_KEY_RETURN;
  266. break;
  267. case GLFW_KEY_KP_DIVIDE:
  268. if (testkp)
  269. k = '/';
  270. break;
  271. case GLFW_KEY_KP_MULTIPLY:
  272. if (testkp)
  273. k = '*';
  274. break;
  275. case GLFW_KEY_KP_SUBTRACT:
  276. if (testkp)
  277. k = '-';
  278. break;
  279. case GLFW_KEY_KP_ADD:
  280. if (testkp)
  281. k = '+';
  282. break;
  283. case GLFW_KEY_KP_DECIMAL:
  284. if (testkp)
  285. k = '.';
  286. break;
  287. case GLFW_KEY_KP_EQUAL:
  288. if (testkp)
  289. k = '=';
  290. break;
  291. }
  292. }
  293. if (k>0)
  294. handled = TwKeyPressed(k, mod);
  295. }
  296. }
  297. return handled;
  298. }
  299. static void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int modifier)
  300. {
  301. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  302. glfwSetWindowShouldClose(window, GL_TRUE);
  303. if (!TwEventKeyGLFW3(key,action))
  304. {
  305. if (action == GLFW_PRESS)
  306. __viewer->key_down(key, modifier);
  307. else
  308. __viewer->key_up(key, modifier);
  309. }
  310. }
  311. static void glfw_window_size(GLFWwindow* window, int width, int height)
  312. {
  313. int w = width*highdpi;
  314. int h = height*highdpi;
  315. __viewer->resize(w, h);
  316. TwWindowSize(w, h);
  317. const auto & bar = __viewer->bar;
  318. // Keep AntTweakBar on right side of screen and height == opengl height
  319. // get the current position of a bar
  320. int size[2];
  321. TwGetParam(bar, NULL, "size", TW_PARAM_INT32, 2, size);
  322. int pos[2];
  323. // Place bar on left side of opengl rect (padded by 10 pixels)
  324. pos[0] = 10;//max(10,(int)width - size[0] - 10);
  325. // place bar at top (padded by 10 pixels)
  326. pos[1] = 10;
  327. // Set height to new height of window (padded by 10 pixels on bottom)
  328. size[1] = highdpi*(height-pos[1]-10);
  329. TwSetParam(bar, NULL, "position", TW_PARAM_INT32, 2, pos);
  330. TwSetParam(bar, NULL, "size", TW_PARAM_INT32, 2,size);
  331. }
  332. static void glfw_mouse_move(GLFWwindow* window, double x, double y)
  333. {
  334. if(!TwEventMousePosGLFW(x*highdpi,y*highdpi) || __viewer->down)
  335. {
  336. // Call if TwBar hasn't used or if down
  337. __viewer->mouse_move(x*highdpi, y*highdpi);
  338. }
  339. }
  340. static void glfw_mouse_scroll(GLFWwindow* window, double x, double y)
  341. {
  342. using namespace std;
  343. scroll_x += x;
  344. scroll_y += y;
  345. if (!TwEventMouseWheelGLFW(scroll_y))
  346. __viewer->mouse_scroll(y);
  347. }
  348. static void glfw_char_callback(GLFWwindow* window, unsigned int c)
  349. {
  350. if ((c & 0xff00)==0)
  351. TwKeyPressed(c, global_KMod);
  352. }
  353. namespace igl
  354. {
  355. void Viewer::init()
  356. {
  357. // Create a tweak bar
  358. bar = TwNewBar("libIGL-Viewer");
  359. TwDefine(" libIGL-Viewer help='This is a simple 3D mesh viewer.' "); // Message added to the help bar->
  360. TwDefine(" libIGL-Viewer size='200 685'"); // change default tweak bar size
  361. TwDefine(" libIGL-Viewer color='76 76 127' "); // change default tweak bar color
  362. TwDefine(" libIGL-Viewer refresh=0.5"); // change refresh rate
  363. // ---------------------- LOADING ----------------------
  364. #ifdef ENABLE_XML_SERIALIZATION
  365. TwAddButton(bar,"Load Scene", load_scene_cb, this, "group='Workspace'");
  366. TwAddButton(bar,"Save Scene", save_scene_cb, this, "group='Workspace'");
  367. #endif
  368. #ifdef ENABLE_IO
  369. TwAddButton(bar,"Load Mesh", open_dialog_mesh, this, "group='Mesh' key=o");
  370. #endif
  371. // ---------------------- SCENE ----------------------
  372. TwAddButton(bar,"Center object", align_camera_center_cb, this,
  373. " group='Viewing Options'"
  374. " label='Center object' key=A help='Set the center of the camera to the mesh center.'");
  375. TwAddVarRW(bar, "Zoom", TW_TYPE_FLOAT, &(options.camera_zoom),
  376. " min=0.05 max=50 step=0.1 keyIncr=+ keyDecr=- help='Scale the object (1=original size).' group='Scene'");
  377. TwAddButton(bar,"SnapView", snap_to_canonical_quaternion_cb, this,
  378. " group='Scene'"
  379. " label='Snap to canonical view' key=Z "
  380. " help='Snaps view to nearest canonical view.'");
  381. TwAddVarRW(bar,"LightDir", TW_TYPE_DIR3F, options.light_position.data(),
  382. " group='Scene'"
  383. " label='Light direction' open help='Change the light direction.' ");
  384. // ---------------------- DRAW OPTIONS ----------------------
  385. TwAddVarRW(bar, "ToggleOrthographic", TW_TYPE_BOOLCPP, &(options.orthographic),
  386. " group='Viewing Options'"
  387. " label='Orthographic view' "
  388. " help='Toggles orthographic / perspective view. Default: perspective.'");
  389. TwAddVarRW(bar, "Rotation", TW_TYPE_QUAT4F, &(options.trackball_angle),
  390. " group='Viewing Options'"
  391. " label='Rotation'"
  392. " help='Rotates view.'");
  393. TwAddVarCB(bar,"Face-based Normals/Colors", TW_TYPE_BOOLCPP, set_face_based_cb, get_face_based_cb, this,
  394. " group='Draw options'"
  395. " label='Face-based' key=T help='Toggle per face shading/colors.' ");
  396. TwAddVarRW(bar,"Show texture", TW_TYPE_BOOLCPP, &(options.show_texture),
  397. " group='Draw options'");
  398. TwAddVarCB(bar,"Invert Normals", TW_TYPE_BOOLCPP, set_invert_normals_cb, get_invert_normals_cb, this,
  399. " group='Draw options'"
  400. " label='Invert normals' key=i help='Invert normal directions for inside out meshes.' ");
  401. TwAddVarRW(bar,"ShowOverlay", TW_TYPE_BOOLCPP, &(options.show_overlay),
  402. " group='Draw options'"
  403. " label='Show overlay' key=o help='Show the overlay layers.' ");
  404. TwAddVarRW(bar,"ShowOverlayDepth", TW_TYPE_BOOLCPP, &(options.show_overlay_depth),
  405. " group='Draw options'"
  406. " label='Show overlay depth test' help='Enable the depth test for overlay layer.' ");
  407. TwAddVarRW(bar,"Background color", TW_TYPE_COLOR3F,
  408. options.background_color.data(),
  409. " help='Select a background color' colormode=hls group='Draw options'");
  410. TwAddVarRW(bar, "LineColor", TW_TYPE_COLOR3F,
  411. options.line_color.data(),
  412. " label='Line color' help='Select a outline color' group='Draw options'");
  413. TwAddVarRW(bar,"Shininess",TW_TYPE_FLOAT,&options.shininess," group='Draw options'"
  414. " min=1 max=128");
  415. // ---------------------- Overlays ----------------------
  416. TwAddVarRW(bar,"Wireframe", TW_TYPE_BOOLCPP, &(options.show_lines),
  417. " group='Overlays'"
  418. " label='Wireframe' key=l help='Toggle wire frame of mesh'");
  419. TwAddVarRW(bar,"Fill", TW_TYPE_BOOLCPP, &(options.show_faces),
  420. " group='Overlays'"
  421. " label='Fill' key=t help='Display filled polygons of mesh'");
  422. TwAddVarRW(bar,"ShowVertexId", TW_TYPE_BOOLCPP, &(options.show_vertid),
  423. " group='Overlays'"
  424. " label='Show Vertex Labels' key=';' help='Toggle vertex indices'");
  425. TwAddVarRW(bar,"ShowFaceId", TW_TYPE_BOOLCPP, &(options.show_faceid),
  426. " group='Overlays'"
  427. " label='Show Faces Labels' key='CTRL+;' help='Toggle face"
  428. " indices'");
  429. __font_renderer.Init();
  430. init_plugins();
  431. }
  432. Viewer::Viewer()
  433. {
  434. // Default shininess
  435. options.shininess = 35.0f;
  436. // Default colors
  437. options.background_color << 0.3f, 0.3f, 0.5f;
  438. options.line_color << 0.0f, 0.0f, 0.0f;
  439. // Default lights settings
  440. options.light_position << 0.0f, -0.30f, -5.0f;
  441. options.lighting_factor = 1.0f; //on
  442. // Default trackball
  443. options.trackball_angle << 0.0f, 0.0f, 0.0f, 1.0f;
  444. // Defalut model viewing parameters
  445. options.model_zoom = 1.0f;
  446. options.model_translation << 0,0,0;
  447. // Camera parameters
  448. options.camera_zoom = 1.0f;
  449. options.orthographic = false;
  450. options.camera_view_angle = 45.0;
  451. options.camera_dnear = 1.0;
  452. options.camera_dfar = 100.0;
  453. options.camera_eye << 0, 0, 5;
  454. options.camera_center << 0, 0, 0;
  455. options.camera_up << 0, 1, 0;
  456. // Default visualization options
  457. options.show_faces = true;
  458. options.show_lines = true;
  459. options.invert_normals = false;
  460. options.show_overlay = true;
  461. options.show_overlay_depth = true;
  462. options.show_vertid = false;
  463. options.show_faceid = false;
  464. options.show_texture = false;
  465. // Default point size / line width
  466. options.point_size = 15;
  467. options.line_width = 0.5f;
  468. data.face_based = false;
  469. options.is_animating = false;
  470. options.animation_max_fps = 30.;
  471. // Temporary variables initialization
  472. down = false;
  473. hack_never_moved = true;
  474. scroll_position = 0.0f;
  475. // Per face
  476. set_face_based(false);
  477. // C-style callbacks
  478. callback_pre_draw = 0;
  479. callback_post_draw = 0;
  480. callback_mouse_down = 0;
  481. callback_mouse_up = 0;
  482. callback_mouse_move = 0;
  483. callback_mouse_scroll = 0;
  484. callback_key_down = 0;
  485. callback_key_up = 0;
  486. callback_pre_draw_data = 0;
  487. callback_post_draw = 0;
  488. callback_mouse_down = 0;
  489. callback_mouse_up = 0;
  490. callback_mouse_move = 0;
  491. callback_mouse_scroll = 0;
  492. callback_key_down = 0;
  493. callback_key_up = 0;
  494. }
  495. void Viewer::init_plugins()
  496. {
  497. // Init all plugins
  498. for (unsigned int i = 0; i<plugins.size(); ++i)
  499. plugins[i]->init(this);
  500. }
  501. Viewer::~Viewer()
  502. {
  503. }
  504. void Viewer::shutdown_plugins()
  505. {
  506. for (unsigned int i = 0; i<plugins.size(); ++i)
  507. plugins[i]->shutdown();
  508. }
  509. bool Viewer::load_mesh_from_file(const char* mesh_file_name)
  510. {
  511. std::string mesh_file_name_string = std::string(mesh_file_name);
  512. // first try to load it with a plugin
  513. for (unsigned int i = 0; i<plugins.size(); ++i)
  514. if (plugins[i]->load(mesh_file_name_string))
  515. return true;
  516. data.clear();
  517. size_t last_dot = mesh_file_name_string.rfind('.');
  518. if (last_dot == std::string::npos)
  519. {
  520. printf("Error: No file extension found in %s\n",mesh_file_name);
  521. return false;
  522. }
  523. std::string extension = mesh_file_name_string.substr(last_dot+1);
  524. if (extension == "off" || extension =="OFF")
  525. {
  526. if (!igl::readOFF(mesh_file_name_string, data.V, data.F))
  527. return false;
  528. }
  529. else if (extension == "obj" || extension =="OBJ")
  530. {
  531. Eigen::MatrixXd corner_normals;
  532. Eigen::MatrixXi fNormIndices;
  533. Eigen::MatrixXd UV_V;
  534. Eigen::MatrixXi UV_F;
  535. if (!(igl::readOBJ(mesh_file_name_string, data.V, data.F, corner_normals, fNormIndices, UV_V, UV_F)))
  536. return false;
  537. }
  538. else
  539. {
  540. // unrecognized file type
  541. printf("Error: %s is not a recognized file type.\n",extension.c_str());
  542. return false;
  543. }
  544. data.compute_normals();
  545. data.uniform_colors(Eigen::Vector3d(51.0/255.0,43.0/255.0,33.3/255.0),
  546. Eigen::Vector3d(255.0/255.0,228.0/255.0,58.0/255.0),
  547. Eigen::Vector3d(255.0/255.0,235.0/255.0,80.0/255.0));
  548. if (data.V_uv.rows() == 0)
  549. data.grid_texture();
  550. align_camera_center();
  551. for (unsigned int i = 0; i<plugins.size(); ++i)
  552. if (plugins[i]->post_load())
  553. return true;
  554. return true;
  555. }
  556. bool Viewer::save_mesh_to_file(const char* mesh_file_name)
  557. {
  558. std::string mesh_file_name_string(mesh_file_name);
  559. // first try to load it with a plugin
  560. for (unsigned int i = 0; i<plugins.size(); ++i)
  561. if (plugins[i]->save(mesh_file_name_string))
  562. return true;
  563. size_t last_dot = mesh_file_name_string.rfind('.');
  564. if (last_dot == std::string::npos)
  565. {
  566. // No file type determined
  567. printf("Error: No file extension found in %s\n",mesh_file_name);
  568. return false;
  569. }
  570. std::string extension = mesh_file_name_string.substr(last_dot+1);
  571. if (extension == "off" || extension =="OFF")
  572. {
  573. return igl::writeOFF(mesh_file_name_string,data.V,data.F);
  574. }
  575. else if (extension == "obj" || extension =="OBJ")
  576. {
  577. Eigen::MatrixXd corner_normals;
  578. Eigen::MatrixXi fNormIndices;
  579. Eigen::MatrixXd UV_V;
  580. Eigen::MatrixXi UV_F;
  581. return igl::writeOBJ(mesh_file_name_string, data.V,
  582. data.F, corner_normals, fNormIndices, UV_V, UV_F);
  583. }
  584. else
  585. {
  586. // unrecognized file type
  587. printf("Error: %s is not a recognized file type.\n",extension.c_str());
  588. return false;
  589. }
  590. return true;
  591. }
  592. void Viewer::clear()
  593. {
  594. data.clear();
  595. }
  596. bool Viewer::key_down(unsigned char key, int modifiers)
  597. {
  598. if (callback_key_down)
  599. if (callback_key_down(*this,key,modifiers))
  600. return true;
  601. for (unsigned int i = 0; i<plugins.size(); ++i)
  602. if (plugins[i]->key_down(key, modifiers))
  603. return true;
  604. if (key == 'S')
  605. mouse_scroll(1);
  606. if (key == 'A')
  607. mouse_scroll(-1);
  608. // Why aren't these handled view AntTweakBar?
  609. if (key == 'z') // Don't use 'Z' because that clobbers snap_to_canonical_view_quat
  610. options.trackball_angle << 0.0f, 0.0f, 0.0f, 1.0f;
  611. if (key == 'y')
  612. options.trackball_angle << -sqrt(2.0f)/2.0f, 0.0f, 0.0f, sqrt(2.0f)/2.0f;
  613. if (key == 'x')
  614. options.trackball_angle << -0.5f, -0.5f, -0.5f, 0.5f;
  615. return false;
  616. }
  617. bool Viewer::key_up(unsigned char key, int modifiers)
  618. {
  619. if (callback_key_up)
  620. if (callback_key_up(*this,key,modifiers))
  621. return true;
  622. for (unsigned int i = 0; i<plugins.size(); ++i)
  623. if (plugins[i]->key_up(key, modifiers))
  624. return true;
  625. return false;
  626. }
  627. bool Viewer::mouse_down(MouseButton button, int modifier)
  628. {
  629. if (callback_mouse_down)
  630. if (callback_mouse_down(*this,button,modifier))
  631. return true;
  632. for (unsigned int i = 0; i<plugins.size(); ++i)
  633. if (plugins[i]->mouse_down(button,modifier))
  634. return true;
  635. down = true;
  636. down_mouse_x = current_mouse_x;
  637. down_mouse_y = current_mouse_y;
  638. down_translation = options.model_translation;
  639. // Initialization code for the trackball
  640. Eigen::RowVector3d center;
  641. if (data.V.rows() == 0)
  642. center << 0,0,0;
  643. else
  644. center = data.V.colwise().sum()/data.V.rows();
  645. Eigen::Vector3f coord = igl::project(Eigen::Vector3f(center(0),center(1),center(2)), view * model, proj, viewport);
  646. down_mouse_z = coord[2];
  647. down_rotation = options.trackball_angle;
  648. mouse_mode = ROTATION;
  649. switch (button)
  650. {
  651. case IGL_LEFT:
  652. mouse_mode = ROTATION;
  653. break;
  654. case IGL_RIGHT:
  655. mouse_mode = TRANSLATE;
  656. break;
  657. default:
  658. mouse_mode = NOTHING;
  659. break;
  660. }
  661. return true;
  662. }
  663. bool Viewer::mouse_up(MouseButton button, int modifier)
  664. {
  665. down = false;
  666. if (callback_mouse_up)
  667. if (callback_mouse_up(*this,button,modifier))
  668. return true;
  669. for (unsigned int i = 0; i<plugins.size(); ++i)
  670. if (plugins[i]->mouse_up(button,modifier))
  671. return true;
  672. mouse_mode = NOTHING;
  673. return true;
  674. }
  675. bool Viewer::mouse_move(int mouse_x, int mouse_y)
  676. {
  677. if(hack_never_moved)
  678. {
  679. down_mouse_x = mouse_x;
  680. down_mouse_y = mouse_y;
  681. hack_never_moved = false;
  682. }
  683. current_mouse_x = mouse_x;
  684. current_mouse_y = mouse_y;
  685. if (callback_mouse_move)
  686. if (callback_mouse_move(*this,mouse_x,mouse_y))
  687. return true;
  688. for (unsigned int i = 0; i<plugins.size(); ++i)
  689. if (plugins[i]->mouse_move(mouse_x, mouse_y))
  690. return true;
  691. if (down)
  692. {
  693. switch (mouse_mode)
  694. {
  695. case ROTATION :
  696. {
  697. igl::trackball(width,
  698. height,
  699. 2.0f,
  700. down_rotation.data(),
  701. down_mouse_x,
  702. down_mouse_y,
  703. mouse_x,
  704. mouse_y,
  705. options.trackball_angle.data());
  706. //Eigen::Vector4f snapq = options.trackball_angle;
  707. break;
  708. }
  709. case TRANSLATE:
  710. {
  711. //translation
  712. Eigen::Vector3f pos1 = igl::unproject(Eigen::Vector3f(mouse_x, viewport[3] - mouse_y, down_mouse_z), view * model, proj, viewport);
  713. Eigen::Vector3f pos0 = igl::unproject(Eigen::Vector3f(down_mouse_x, viewport[3] - down_mouse_y, down_mouse_z), view * model, proj, viewport);
  714. Eigen::Vector3f diff = pos1 - pos0;
  715. options.model_translation = down_translation + Eigen::Vector3f(diff[0],diff[1],diff[2]);
  716. break;
  717. }
  718. case ZOOM:
  719. {
  720. //float delta = 0.001f * (mouse_x - down_mouse_x + mouse_y - down_mouse_y);
  721. float delta = 0.001f * (mouse_x - down_mouse_x + mouse_y - down_mouse_y);
  722. options.camera_zoom *= 1 + delta;
  723. down_mouse_x = mouse_x;
  724. down_mouse_y = mouse_y;
  725. break;
  726. }
  727. default:
  728. break;
  729. }
  730. }
  731. return true;
  732. }
  733. bool Viewer::mouse_scroll(float delta_y)
  734. {
  735. scroll_position += delta_y;
  736. if (callback_mouse_scroll)
  737. if (callback_mouse_scroll(*this,delta_y))
  738. return true;
  739. for (unsigned int i = 0; i<plugins.size(); ++i)
  740. if (plugins[i]->mouse_scroll(delta_y))
  741. return true;
  742. // Only zoom if there's actually a change
  743. if(delta_y != 0)
  744. {
  745. float mult = (1.0+((delta_y>0)?1.:-1.)*0.05);
  746. const float min_zoom = 0.1f;
  747. options.camera_zoom = (options.camera_zoom * mult > min_zoom ? options.camera_zoom * mult : min_zoom);
  748. }
  749. return true;
  750. }
  751. void Viewer::draw()
  752. {
  753. using namespace std;
  754. using namespace Eigen;
  755. glClearColor(options.background_color[0],
  756. options.background_color[1],
  757. options.background_color[2],
  758. 1.0f);
  759. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  760. glEnable(GL_DEPTH_TEST);
  761. if (callback_pre_draw)
  762. if (callback_pre_draw(*this))
  763. return;
  764. for (unsigned int i = 0; i<plugins.size(); ++i)
  765. if (plugins[i]->pre_draw())
  766. return;
  767. /* Bind and potentially refresh mesh/line/point data */
  768. if (data.dirty)
  769. {
  770. opengl.set_data(data, options.invert_normals);
  771. data.dirty = ViewerData::DIRTY_NONE;
  772. }
  773. opengl.bind_mesh();
  774. // Initialize uniform
  775. glViewport(0, 0, width, height);
  776. model = Eigen::Matrix4f::Identity();
  777. view = Eigen::Matrix4f::Identity();
  778. proj = Eigen::Matrix4f::Identity();
  779. // Set view
  780. view = lookAt(Eigen::Vector3f(options.camera_eye[0], options.camera_eye[1], options.camera_eye[2]),
  781. Eigen::Vector3f(options.camera_center[0], options.camera_center[1], options.camera_center[2]),
  782. Eigen::Vector3f(options.camera_up[0], options.camera_up[1], options.camera_up[2]));
  783. // Set projection
  784. if (options.orthographic)
  785. {
  786. float length = (options.camera_eye - options.camera_center).norm();
  787. float h = tan(options.camera_view_angle/360.0 * M_PI) * (length);
  788. proj = ortho(-h*width/height, h*width/height, -h, h, options.camera_dnear, options.camera_dfar);
  789. }
  790. else
  791. {
  792. float fH = tan(options.camera_view_angle / 360.0 * M_PI) * options.camera_dnear;
  793. float fW = fH * (double)width/(double)height;
  794. proj = frustum(-fW, fW, -fH, fH, options.camera_dnear, options.camera_dfar);
  795. }
  796. // end projection
  797. // Set model transformation
  798. float mat[16];
  799. igl::quat_to_mat(options.trackball_angle.data(), mat);
  800. for (unsigned i=0;i<4;++i)
  801. for (unsigned j=0;j<4;++j)
  802. model(i,j) = mat[i+4*j];
  803. model = scale(model, Eigen::Vector3f(options.camera_zoom,options.camera_zoom,options.camera_zoom));
  804. model = scale(model, Eigen::Vector3f(options.model_zoom,options.model_zoom,options.model_zoom));
  805. model = translate(model, Eigen::Vector3f(options.model_translation[0],options.model_translation[1],options.model_translation[2]));
  806. // Send transformations to the GPU
  807. GLint modeli = opengl.shader_mesh.uniform("model");
  808. GLint viewi = opengl.shader_mesh.uniform("view");
  809. GLint proji = opengl.shader_mesh.uniform("proj");
  810. glUniformMatrix4fv(modeli, 1, GL_FALSE, model.data());
  811. glUniformMatrix4fv(viewi, 1, GL_FALSE, view.data());
  812. glUniformMatrix4fv(proji, 1, GL_FALSE, proj.data());
  813. // Light parameters
  814. GLint specular_exponenti = opengl.shader_mesh.uniform("specular_exponent");
  815. GLint light_position_worldi = opengl.shader_mesh.uniform("light_position_world");
  816. GLint lighting_factori = opengl.shader_mesh.uniform("lighting_factor");
  817. GLint fixed_colori = opengl.shader_mesh.uniform("fixed_color");
  818. GLint texture_factori = opengl.shader_mesh.uniform("texture_factor");
  819. glUniform1f(specular_exponenti, options.shininess);
  820. Vector3f rev_light = -1.*options.light_position;
  821. glUniform3fv(light_position_worldi, 1, rev_light.data());
  822. glUniform1f(lighting_factori, options.lighting_factor); // enables lighting
  823. glUniform4f(fixed_colori, 0.0, 0.0, 0.0, 0.0);
  824. if (data.V.rows()>0)
  825. {
  826. // Render fill
  827. if (options.show_faces)
  828. {
  829. // Texture
  830. glUniform1f(texture_factori, options.show_texture ? 1.0f : 0.0f);
  831. opengl.draw_mesh(true);
  832. glUniform1f(texture_factori, 0.0f);
  833. }
  834. // Render wireframe
  835. if (options.show_lines)
  836. {
  837. glLineWidth(options.line_width);
  838. glUniform4f(fixed_colori, options.line_color[0], options.line_color[1],
  839. options.line_color[2], 1.0f);
  840. opengl.draw_mesh(false);
  841. glUniform4f(fixed_colori, 0.0f, 0.0f, 0.0f, 0.0f);
  842. }
  843. if (options.show_vertid)
  844. {
  845. __font_renderer.BeginDraw(view*model, proj, viewport, data.object_scale);
  846. for (int i=0; i<data.V.rows(); ++i)
  847. __font_renderer.DrawText(data.V.row(i), data.V_normals.row(i), to_string(i));
  848. __font_renderer.EndDraw();
  849. }
  850. if (options.show_faceid)
  851. {
  852. __font_renderer.BeginDraw(view*model, proj, viewport, data.object_scale);
  853. for (int i=0; i<data.F.rows(); ++i)
  854. {
  855. Eigen::RowVector3d p = Eigen::RowVector3d::Zero();
  856. for (int j=0;j<data.F.cols();++j)
  857. p += data.V.row(data.F(i,j));
  858. p /= data.F.cols();
  859. __font_renderer.DrawText(p, data.F_normals.row(i), to_string(i));
  860. }
  861. __font_renderer.EndDraw();
  862. }
  863. }
  864. if (options.show_overlay)
  865. {
  866. if (options.show_overlay_depth)
  867. glEnable(GL_DEPTH_TEST);
  868. else
  869. glDisable(GL_DEPTH_TEST);
  870. if (data.lines.rows() > 0)
  871. {
  872. opengl.bind_overlay_lines();
  873. modeli = opengl.shader_overlay_lines.uniform("model");
  874. viewi = opengl.shader_overlay_lines.uniform("view");
  875. proji = opengl.shader_overlay_lines.uniform("proj");
  876. glUniformMatrix4fv(modeli, 1, GL_FALSE, model.data());
  877. glUniformMatrix4fv(viewi, 1, GL_FALSE, view.data());
  878. glUniformMatrix4fv(proji, 1, GL_FALSE, proj.data());
  879. // This must be enabled, otherwise glLineWidth has no effect
  880. glEnable(GL_LINE_SMOOTH);
  881. glLineWidth(options.line_width);
  882. opengl.draw_overlay_lines();
  883. }
  884. if (data.points.rows() > 0)
  885. {
  886. opengl.bind_overlay_points();
  887. modeli = opengl.shader_overlay_points.uniform("model");
  888. viewi = opengl.shader_overlay_points.uniform("view");
  889. proji = opengl.shader_overlay_points.uniform("proj");
  890. glUniformMatrix4fv(modeli, 1, GL_FALSE, model.data());
  891. glUniformMatrix4fv(viewi, 1, GL_FALSE, view.data());
  892. glUniformMatrix4fv(proji, 1, GL_FALSE, proj.data());
  893. glPointSize(options.point_size);
  894. opengl.draw_overlay_points();
  895. }
  896. if (data.labels_positions.rows() > 0)
  897. {
  898. __font_renderer.BeginDraw(view*model, proj, viewport, data.object_scale);
  899. for (int i=0; i<data.labels_positions.rows(); ++i)
  900. __font_renderer.DrawText(data.labels_positions.row(i), Eigen::Vector3d(0.0,0.0,0.0),
  901. data.labels_strings[i]);
  902. __font_renderer.EndDraw();
  903. }
  904. glEnable(GL_DEPTH_TEST);
  905. }
  906. if (callback_post_draw)
  907. if (callback_post_draw(*this))
  908. return;
  909. for (unsigned int i = 0; i<plugins.size(); ++i)
  910. if (plugins[i]->post_draw())
  911. break;
  912. TwDraw();
  913. }
  914. bool Viewer::save_scene()
  915. {
  916. #ifdef ENABLE_XML_SERIALIZATION
  917. string fname = igl::file_dialog_save();
  918. if (fname.length() == 0)
  919. return false;
  920. ::igl::XMLSerializer serializer("Viewer");
  921. serializer.Add(data,"Data");
  922. serializer.Add(options,"Options");
  923. if (plugin_manager)
  924. for (unsigned int i = 0; i <plugin_manager->plugin_list.size(); ++i)
  925. serializer.Add(*(plugin_manager->plugin_list[i]),plugin_manager->plugin_list[i]->plugin_name);
  926. serializer.Save(fname.c_str(),true);
  927. #endif
  928. return true;
  929. }
  930. bool Viewer::load_scene()
  931. {
  932. #ifdef ENABLE_XML_SERIALIZATION
  933. string fname = igl::file_dialog_open();
  934. if (fname.length() == 0)
  935. return false;
  936. ::igl::XMLSerializer serializer("Viewer");
  937. serializer.Add(data,"Data");
  938. serializer.Add(options,"Options");
  939. if (plugin_manager)
  940. for (unsigned int i = 0; i <plugin_manager->plugin_list.size(); ++i)
  941. serializer.Add(*(plugin_manager->plugin_list[i]),plugin_manager->plugin_list[i]->plugin_name);
  942. serializer.Load(fname.c_str());
  943. #endif
  944. return true;
  945. }
  946. void Viewer::align_camera_center()
  947. {
  948. get_scale_and_shift_to_fit_mesh(data.V,data.F,options.model_zoom,options.model_translation);
  949. data.object_scale = (data.V.colwise().maxCoeff() - data.V.colwise().minCoeff()).norm();
  950. }
  951. void Viewer::resize(int w, int h)
  952. {
  953. width = w;
  954. height = h;
  955. viewport = Eigen::Vector4f(0,0,width,height);
  956. }
  957. void Viewer::get_scale_and_shift_to_fit_mesh(
  958. const Eigen::MatrixXd& V,
  959. const Eigen::MatrixXi& F,
  960. float& zoom,
  961. Eigen::Vector3f& shift)
  962. {
  963. if (V.rows() == 0)
  964. return;
  965. //Compute mesh centroid
  966. Eigen::SparseMatrix<double> M;
  967. igl::massmatrix(V,F,igl::MASSMATRIX_TYPE_VORONOI,M);
  968. const auto & MV = M*V;
  969. Eigen::RowVector3d centroid = MV.colwise().sum()/M.diagonal().sum();
  970. Eigen::RowVector3d min_point = V.colwise().minCoeff();
  971. Eigen::RowVector3d max_point = V.colwise().maxCoeff();
  972. shift = -centroid.cast<float>();
  973. double x_scale = fabs(max_point[0] - min_point[0]);
  974. double y_scale = fabs(max_point[1] - min_point[1]);
  975. double z_scale = fabs(max_point[2] - min_point[2]);
  976. zoom = 2.0/ std::max(z_scale,std::max(x_scale,y_scale));
  977. }
  978. void TW_CALL Viewer::snap_to_canonical_quaternion_cb(void *clientData)
  979. {
  980. Eigen::Vector4f snapq = static_cast<Viewer *>(clientData)->options.trackball_angle;
  981. igl::snap_to_canonical_view_quat<float>(snapq.data(),1,static_cast<Viewer *>(clientData)->options.trackball_angle.data());
  982. }
  983. void TW_CALL Viewer::align_camera_center_cb(void *clientData)
  984. {
  985. static_cast<Viewer *>(clientData)->align_camera_center();
  986. }
  987. void TW_CALL Viewer::save_scene_cb(void *clientData)
  988. {
  989. static_cast<Viewer *>(clientData)->save_scene();
  990. }
  991. void TW_CALL Viewer::load_scene_cb(void *clientData)
  992. {
  993. static_cast<Viewer *>(clientData)->load_scene();
  994. }
  995. void TW_CALL Viewer::set_invert_normals_cb(const void *param, void *clientData)
  996. {
  997. Viewer *viewer = static_cast<Viewer *>(clientData);
  998. viewer->data.dirty |= ViewerData::DIRTY_NORMAL;
  999. viewer->options.invert_normals = *((bool *) param);
  1000. }
  1001. void TW_CALL Viewer::get_invert_normals_cb(void *param, void *clientData)
  1002. {
  1003. *((bool *) param) = static_cast<Viewer *>(clientData)->options.invert_normals;
  1004. }
  1005. void TW_CALL Viewer::set_face_based_cb(const void *param, void *clientData)
  1006. {
  1007. Viewer *viewer = static_cast<Viewer *>(clientData);
  1008. viewer->set_face_based(*((bool *) param));
  1009. }
  1010. void TW_CALL Viewer::get_face_based_cb(void *param, void *clientData)
  1011. {
  1012. *((bool *) param) = static_cast<Viewer *>(clientData)->data.face_based;
  1013. }
  1014. void TW_CALL Viewer::open_dialog_mesh(void *clientData)
  1015. {
  1016. std::string fname = igl::file_dialog_open();
  1017. if (fname.length() == 0)
  1018. return;
  1019. static_cast<Viewer *>(clientData)->load_mesh_from_file(fname.c_str());
  1020. }
  1021. // Serialization
  1022. void Viewer::Options::InitSerialization()
  1023. {
  1024. #ifdef ENABLE_XML_SERIALIZATION
  1025. xmlSerializer->Add(shininess, "shininess");
  1026. xmlSerializer->Add(background_color, "background_color");
  1027. xmlSerializer->Add(line_color, "line_color");
  1028. xmlSerializer->Add(light_position, "light_position");
  1029. xmlSerializer->Add(lighting_factor, "lighting_factor");
  1030. xmlSerializer->Add(trackball_angle, "trackball_angle");
  1031. xmlSerializer->Add(model_zoom, "model_zoom");
  1032. xmlSerializer->Add(model_translation, "model_translation");
  1033. xmlSerializer->Add(model_zoom_uv, "model_zoom_uv");
  1034. xmlSerializer->Add(model_translation_uv, "model_translation_uv");
  1035. xmlSerializer->Add(camera_zoom, "camera_zoom");
  1036. xmlSerializer->Add(orthographic, "orthographic");
  1037. xmlSerializer->Add(camera_eye, "camera_eye");
  1038. xmlSerializer->Add(camera_up, "camera_up");
  1039. xmlSerializer->Add(camera_center, "camera_center");
  1040. xmlSerializer->Add(camera_view_angle, "camera_view_angle");
  1041. xmlSerializer->Add(camera_dnear, "camera_dnear");
  1042. xmlSerializer->Add(camera_dfar, "camera_dfar");
  1043. xmlSerializer->Add(show_overlay, "show_overlay");
  1044. xmlSerializer->Add(show_overlay_depth, "show_overlay_depth");
  1045. xmlSerializer->Add(show_texture, "show_texture");
  1046. xmlSerializer->Add(show_faces, "show_faces");
  1047. xmlSerializer->Add(show_lines, "show_lines");
  1048. xmlSerializer->Add(show_vertid, "show_vertid");
  1049. xmlSerializer->Add(show_faceid, "show_faceid");
  1050. xmlSerializer->Add(point_size, "point_size");
  1051. xmlSerializer->Add(line_width, "line_width");
  1052. xmlSerializer->Add(invert_normals, "invert_normals");
  1053. xmlSerializer->Add(face_based, "face_based");
  1054. #endif
  1055. }
  1056. void Viewer::set_face_based(bool newvalue)
  1057. {
  1058. data.set_face_based(newvalue);
  1059. }
  1060. void Viewer::compute_normals()
  1061. {
  1062. data.compute_normals();
  1063. }
  1064. void Viewer::set_mesh(const Eigen::MatrixXd& V, const Eigen::MatrixXi& F)
  1065. {
  1066. data.set_mesh(V,F);
  1067. align_camera_center();
  1068. }
  1069. void Viewer::set_vertices(const Eigen::MatrixXd& V)
  1070. {
  1071. data.set_vertices(V);
  1072. }
  1073. void Viewer::set_normals(const Eigen::MatrixXd& N)
  1074. {
  1075. data.set_normals(N);
  1076. }
  1077. void Viewer::set_colors(const Eigen::MatrixXd &C)
  1078. {
  1079. data.set_colors(C);
  1080. }
  1081. void Viewer::set_uv(const Eigen::MatrixXd& UV)
  1082. {
  1083. data.set_uv(UV);
  1084. }
  1085. void Viewer::set_uv(const Eigen::MatrixXd& UV_V, const Eigen::MatrixXi& UV_F)
  1086. {
  1087. data.set_uv(UV_V,UV_F);
  1088. }
  1089. void Viewer::set_texture(
  1090. const Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& R,
  1091. const Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& G,
  1092. const Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& B)
  1093. {
  1094. data.set_texture(R,G,B);
  1095. }
  1096. void Viewer::add_points(const Eigen::MatrixXd& P, const Eigen::MatrixXd& C)
  1097. {
  1098. data.add_points(P,C);
  1099. }
  1100. void Viewer::set_edges(
  1101. const Eigen::MatrixXd& P,
  1102. const Eigen::MatrixXi& E,
  1103. const Eigen::MatrixXd& C)
  1104. {
  1105. data.set_edges(P,E,C);
  1106. }
  1107. void Viewer::add_edges(const Eigen::MatrixXd& P1, const Eigen::MatrixXd& P2, const Eigen::MatrixXd& C)
  1108. {
  1109. data.add_edges(P1,P2,C);
  1110. }
  1111. void Viewer::add_label(const Eigen::VectorXd& P, const std::string& str)
  1112. {
  1113. data.add_label(P,str);
  1114. }
  1115. int Viewer::launch(std::string filename)
  1116. {
  1117. GLFWwindow* window;
  1118. glfwSetErrorCallback(glfw_error_callback);
  1119. if (!glfwInit())
  1120. return EXIT_FAILURE;
  1121. glfwWindowHint(GLFW_SAMPLES, 16);
  1122. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  1123. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  1124. #ifdef __APPLE__
  1125. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  1126. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  1127. #endif
  1128. window = glfwCreateWindow(1280, 800, "IGL Viewer", NULL, NULL);
  1129. if (!window)
  1130. {
  1131. glfwTerminate();
  1132. return EXIT_FAILURE;
  1133. }
  1134. glfwMakeContextCurrent(window);
  1135. #ifndef __APPLE__
  1136. glewExperimental = true;
  1137. GLenum err = glewInit();
  1138. if (GLEW_OK != err)
  1139. {
  1140. /* Problem: glewInit failed, something is seriously wrong. */
  1141. fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
  1142. }
  1143. fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
  1144. #endif
  1145. #ifdef DEBUG
  1146. int major, minor, rev;
  1147. major = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
  1148. minor = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
  1149. rev = glfwGetWindowAttrib(window, GLFW_CONTEXT_REVISION);
  1150. printf("OpenGL version recieved: %d.%d.%d\n", major, minor, rev);
  1151. printf("Supported OpenGL is %s\n", (const char*)glGetString(GL_VERSION));
  1152. printf("Supported GLSL is %s\n", (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION));
  1153. #endif
  1154. glfwSetInputMode(window,GLFW_CURSOR,GLFW_CURSOR_NORMAL);
  1155. // Initialize AntTweakBar
  1156. TwInit(TW_OPENGL_CORE, NULL);
  1157. // Initialize IGL viewer
  1158. init();
  1159. __viewer = this;
  1160. // Register callbacks
  1161. glfwSetKeyCallback(window, glfw_key_callback); glfwSetCursorPosCallback(window,glfw_mouse_move); glfwSetWindowSizeCallback(window,glfw_window_size); glfwSetMouseButtonCallback(window,glfw_mouse_press); glfwSetScrollCallback(window,glfw_mouse_scroll); glfwSetCharCallback(window, glfw_char_callback);
  1162. // Handle retina displays (windows and mac)
  1163. int width, height;
  1164. glfwGetFramebufferSize(window, &width, &height);
  1165. int width_window, height_window;
  1166. glfwGetWindowSize(window, &width_window, &height_window);
  1167. highdpi = width/width_window;
  1168. glfw_window_size(window,width_window,height_window);
  1169. opengl.init();
  1170. // Load the mesh passed as input
  1171. if (filename.size() > 0)
  1172. load_mesh_from_file(filename.c_str());
  1173. // Rendering loop
  1174. while (!glfwWindowShouldClose(window))
  1175. {
  1176. double tic = get_seconds();
  1177. draw();
  1178. glfwSwapBuffers(window);
  1179. if(options.is_animating)
  1180. {
  1181. glfwPollEvents();
  1182. // In microseconds
  1183. double duration = 1000000.*(get_seconds()-tic);
  1184. const double min_duration = 1000000./options.animation_max_fps;
  1185. if(duration<min_duration)
  1186. {
  1187. // TODO: windows equivalent
  1188. usleep(min_duration-duration);
  1189. }
  1190. }
  1191. else
  1192. {
  1193. glfwWaitEvents();
  1194. }
  1195. }
  1196. opengl.free();
  1197. __font_renderer.Shut();
  1198. shutdown_plugins();
  1199. glfwDestroyWindow(window);
  1200. glfwTerminate();
  1201. return EXIT_SUCCESS;
  1202. }
  1203. } // end namespace