example.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. #include <igl/svd3x3/arap.h>
  2. #include <igl/writeDMAT.h>
  3. #include <igl/partition.h>
  4. #include <igl/cotmatrix.h>
  5. #include <igl/massmatrix.h>
  6. #include <igl/invert_diag.h>
  7. #include <igl/OpenGL_convenience.h>
  8. #include <igl/per_face_normals.h>
  9. #include <igl/per_vertex_normals.h>
  10. #include <igl/two_axis_valuator_fixed_up.h>
  11. #include <igl/normalize_row_lengths.h>
  12. #include <igl/draw_mesh.h>
  13. #include <igl/draw_floor.h>
  14. #include <igl/quat_to_mat.h>
  15. #include <igl/report_gl_error.h>
  16. #include <igl/readOBJ.h>
  17. #include <igl/readDMAT.h>
  18. #include <igl/readOFF.h>
  19. #include <igl/readMESH.h>
  20. #include <igl/jet.h>
  21. #include <igl/readWRL.h>
  22. #include <igl/trackball.h>
  23. #include <igl/list_to_matrix.h>
  24. #include <igl/snap_to_canonical_view_quat.h>
  25. #include <igl/snap_to_fixed_up.h>
  26. #include <igl/triangulate.h>
  27. #include <igl/material_colors.h>
  28. #include <igl/barycenter.h>
  29. #include <igl/matlab_format.h>
  30. #include <igl/ReAntTweakBar.h>
  31. #include <igl/pathinfo.h>
  32. #include <igl/Camera.h>
  33. #include <igl/get_seconds.h>
  34. #include <igl/PI.h>
  35. #include <igl/STR.h>
  36. #include <YImage.hpp>
  37. #ifdef __APPLE__
  38. # include <GLUT/glut.h>
  39. #else
  40. # include <GL/glut.h>
  41. #endif
  42. #include <Eigen/Core>
  43. #include <vector>
  44. #include <iostream>
  45. #include <algorithm>
  46. struct State
  47. {
  48. igl::Camera camera;
  49. } s;
  50. enum RotationType
  51. {
  52. ROTATION_TYPE_IGL_TRACKBALL = 0,
  53. ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP = 1,
  54. NUM_ROTATION_TYPES = 2,
  55. } rotation_type;
  56. bool is_rotating = false;
  57. int down_x,down_y;
  58. igl::Camera down_camera;
  59. bool is_animating = false;
  60. double animation_start_time = 0;
  61. double ANIMATION_DURATION = 0.5;
  62. Eigen::Quaterniond animation_from_quat;
  63. Eigen::Quaterniond animation_to_quat;
  64. // Use vector for range-based `for`
  65. std::vector<State> undo_stack;
  66. std::vector<State> redo_stack;
  67. void push_undo()
  68. {
  69. undo_stack.push_back(s);
  70. // Clear
  71. redo_stack = std::vector<State>();
  72. }
  73. void undo()
  74. {
  75. using namespace std;
  76. if(!undo_stack.empty())
  77. {
  78. redo_stack.push_back(s);
  79. s = undo_stack.front();
  80. undo_stack.pop_back();
  81. }
  82. }
  83. void redo()
  84. {
  85. using namespace std;
  86. if(!redo_stack.empty())
  87. {
  88. undo_stack.push_back(s);
  89. s = redo_stack.front();
  90. redo_stack.pop_back();
  91. }
  92. }
  93. void TW_CALL set_rotation_type(const void * value, void * clientData)
  94. {
  95. using namespace Eigen;
  96. using namespace std;
  97. using namespace igl;
  98. const RotationType old_rotation_type = rotation_type;
  99. rotation_type = *(const RotationType *)(value);
  100. if(rotation_type == ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP &&
  101. old_rotation_type != ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP)
  102. {
  103. push_undo();
  104. animation_from_quat = s.camera.m_rotation_conj;
  105. snap_to_fixed_up(animation_from_quat,animation_to_quat);
  106. // start animation
  107. animation_start_time = get_seconds();
  108. is_animating = true;
  109. }
  110. }
  111. void TW_CALL get_rotation_type(void * value, void *clientData)
  112. {
  113. RotationType * rt = (RotationType *)(value);
  114. *rt = rotation_type;
  115. }
  116. // Width and height of window
  117. int width,height;
  118. // Position of light
  119. float light_pos[4] = {0.1,0.1,-0.9,0};
  120. // Vertex positions, normals, colors and centroid
  121. Eigen::MatrixXd V,U,N,mid;
  122. Eigen::VectorXi S;
  123. igl::ARAPData arap_data;
  124. Eigen::MatrixXi F;
  125. int selected_col = 0;
  126. // Faces
  127. // Bounding box diagonal length
  128. double bbd;
  129. int tot_num_samples = 0;
  130. #define REBAR_NAME "temp.rbr"
  131. igl::ReTwBar rebar; // Pointer to the tweak bar
  132. bool flip_y = false;
  133. bool rotate_xy = false;
  134. int num_in_selection(const Eigen::VectorXi & S)
  135. {
  136. int count = 0;
  137. for(int v = 0;v<S.rows(); v++)
  138. {
  139. if(S(v) >= 0)
  140. {
  141. count++;
  142. }
  143. }
  144. return count;
  145. }
  146. bool harmonic(
  147. const Eigen::MatrixXd & V,
  148. const Eigen::MatrixXi & F,
  149. const Eigen::VectorXi & b,
  150. const Eigen::MatrixXd & bc,
  151. const int k,
  152. Eigen::MatrixXd & W)
  153. {
  154. using namespace igl;
  155. using namespace Eigen;
  156. SparseMatrix<double> L,M,Mi;
  157. cotmatrix(V,F,L);
  158. massmatrix(V,F,MASSMATRIX_VORONOI,M);
  159. invert_diag(M,Mi);
  160. SparseMatrix<double> Q = -L;
  161. for(int p = 1;p<k;p++)
  162. {
  163. Q = (Q*Mi*-L).eval();
  164. }
  165. const VectorXd B = VectorXd::Zero(V.rows(),1);
  166. min_quad_with_fixed_data<double> data;
  167. min_quad_with_fixed_precompute(Q,b,SparseMatrix<double>(),true,data);
  168. W.resize(V.rows(),bc.cols());
  169. for(int w = 0;w<bc.cols();w++)
  170. {
  171. const VectorXd bcw = bc.col(w);
  172. VectorXd Ww;
  173. if(!min_quad_with_fixed_solve(data,B,bcw,VectorXd(),Ww))
  174. {
  175. return false;
  176. }
  177. W.col(w) = Ww;
  178. }
  179. return true;
  180. }
  181. bool init_arap()
  182. {
  183. using namespace igl;
  184. using namespace Eigen;
  185. using namespace std;
  186. VectorXi b(num_in_selection(S));
  187. assert(S.rows() == V.rows());
  188. MatrixXd bc = MatrixXd::Zero(b.size(),S.maxCoeff()+1);
  189. // get b from S
  190. {
  191. int bi = 0;
  192. for(int v = 0;v<S.rows(); v++)
  193. {
  194. if(S(v) >= 0)
  195. {
  196. b(bi) = v;
  197. bc(bi,S(v)) = 1;
  198. bi++;
  199. }
  200. }
  201. }
  202. // Store current mesh
  203. U = V;
  204. VectorXi _S;
  205. VectorXd _D;
  206. MatrixXd W;
  207. if(!harmonic(V,F,b,bc,1,W))
  208. {
  209. return false;
  210. }
  211. partition(W,100,arap_data.G,_S,_D);
  212. return arap_precomputation(V,F,b,arap_data);
  213. }
  214. bool update_arap()
  215. {
  216. using namespace Eigen;
  217. using namespace igl;
  218. using namespace std;
  219. MatrixXd bc(num_in_selection(S),V.cols());
  220. // get b from S
  221. {
  222. int bi = 0;
  223. for(int v = 0;v<S.rows(); v++)
  224. {
  225. if(S(v) >= 0)
  226. {
  227. bc.row(bi) = V.row(v);
  228. switch(S(v))
  229. {
  230. case 0:
  231. {
  232. const double r = mid(0)*0.25;
  233. bc(bi,0) += r*cos(0.5*get_seconds()*2.*PI);
  234. bc(bi,1) -= r+r*sin(0.5*get_seconds()*2.*PI);
  235. break;
  236. }
  237. case 1:
  238. {
  239. const double r = mid(1)*0.15;
  240. bc(bi,1) += r+r*cos(0.15*get_seconds()*2.*PI);
  241. bc(bi,2) -= r*sin(0.15*get_seconds()*2.*PI);
  242. break;
  243. }
  244. default:
  245. break;
  246. }
  247. bi++;
  248. }
  249. }
  250. }
  251. if(!arap_solve(bc,arap_data,U))
  252. {
  253. cerr<<"arap_solve failed."<<endl;
  254. return false;
  255. }
  256. per_face_normals(U,F,N);
  257. return true;
  258. }
  259. void reshape(int width,int height)
  260. {
  261. using namespace std;
  262. // Save width and height
  263. ::width = width;
  264. ::height = height;
  265. glMatrixMode(GL_PROJECTION);
  266. glLoadIdentity();
  267. glViewport(0,0,width,height);
  268. // Send the new window size to AntTweakBar
  269. TwWindowSize(width, height);
  270. // Set aspect for all cameras
  271. s.camera.m_aspect = (double)width/(double)height;
  272. for(auto & s : undo_stack)
  273. {
  274. s.camera.m_aspect = (double)width/(double)height;
  275. }
  276. for(auto & s : redo_stack)
  277. {
  278. s.camera.m_aspect = (double)width/(double)height;
  279. }
  280. }
  281. void push_scene()
  282. {
  283. using namespace igl;
  284. using namespace std;
  285. glMatrixMode(GL_PROJECTION);
  286. glPushMatrix();
  287. glLoadIdentity();
  288. auto & camera = s.camera;
  289. gluPerspective(camera.m_angle,camera.m_aspect,camera.m_near,camera.m_far);
  290. glMatrixMode(GL_MODELVIEW);
  291. glPushMatrix();
  292. glLoadIdentity();
  293. gluLookAt(
  294. camera.eye()(0), camera.eye()(1), camera.eye()(2),
  295. camera.at()(0), camera.at()(1), camera.at()(2),
  296. camera.up()(0), camera.up()(1), camera.up()(2));
  297. }
  298. void pop_scene()
  299. {
  300. glMatrixMode(GL_PROJECTION);
  301. glPopMatrix();
  302. glMatrixMode(GL_MODELVIEW);
  303. glPopMatrix();
  304. }
  305. void pop_object()
  306. {
  307. glPopMatrix();
  308. }
  309. // Scale and shift for object
  310. void push_object()
  311. {
  312. glPushMatrix();
  313. glScaled(2./bbd,2./bbd,2./bbd);
  314. glTranslated(-mid(0,0),-mid(0,1),-mid(0,2));
  315. }
  316. // Set up double-sided lights
  317. void lights()
  318. {
  319. using namespace std;
  320. glEnable(GL_LIGHTING);
  321. glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
  322. glEnable(GL_LIGHT0);
  323. glEnable(GL_LIGHT1);
  324. float amb[4];
  325. amb[0] = amb[1] = amb[2] = 0;
  326. amb[3] = 1.0;
  327. float diff[4] = {0.0,0.0,0.0,0.0};
  328. diff[0] = diff[1] = diff[2] = (1.0 - 0/0.4);;
  329. diff[3] = 1.0;
  330. float zeros[4] = {0.0,0.0,0.0,0.0};
  331. float pos[4];
  332. copy(light_pos,light_pos+4,pos);
  333. glLightfv(GL_LIGHT0,GL_AMBIENT,amb);
  334. glLightfv(GL_LIGHT0,GL_DIFFUSE,diff);
  335. glLightfv(GL_LIGHT0,GL_SPECULAR,zeros);
  336. glLightfv(GL_LIGHT0,GL_POSITION,pos);
  337. pos[0] *= -1;
  338. pos[1] *= -1;
  339. pos[2] *= -1;
  340. glLightfv(GL_LIGHT1,GL_AMBIENT,amb);
  341. glLightfv(GL_LIGHT1,GL_DIFFUSE,diff);
  342. glLightfv(GL_LIGHT1,GL_SPECULAR,zeros);
  343. glLightfv(GL_LIGHT1,GL_POSITION,pos);
  344. }
  345. const float back[4] = {30.0/255.0,30.0/255.0,50.0/255.0,0};
  346. void display()
  347. {
  348. using namespace Eigen;
  349. using namespace igl;
  350. using namespace std;
  351. // Update
  352. update_arap();
  353. glClearColor(back[0],back[1],back[2],0);
  354. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  355. if(is_animating)
  356. {
  357. double t = (get_seconds() - animation_start_time)/ANIMATION_DURATION;
  358. if(t > 1)
  359. {
  360. t = 1;
  361. is_animating = false;
  362. }
  363. const Quaterniond q = animation_from_quat.slerp(t,animation_to_quat).normalized();
  364. s.camera.orbit(q.conjugate());
  365. }
  366. glDisable(GL_LIGHTING);
  367. lights();
  368. push_scene();
  369. glEnable(GL_DEPTH_TEST);
  370. glDepthFunc(GL_LESS);
  371. glEnable(GL_NORMALIZE);
  372. push_object();
  373. // Draw the model
  374. // Set material properties
  375. glDisable(GL_COLOR_MATERIAL);
  376. glMaterialfv(GL_FRONT, GL_AMBIENT, GOLD_AMBIENT);
  377. glMaterialfv(GL_FRONT, GL_DIFFUSE, GOLD_DIFFUSE );
  378. glMaterialfv(GL_FRONT, GL_SPECULAR, GOLD_SPECULAR);
  379. glMaterialf (GL_FRONT, GL_SHININESS, 128);
  380. glMaterialfv(GL_BACK, GL_AMBIENT, SILVER_AMBIENT);
  381. glMaterialfv(GL_BACK, GL_DIFFUSE, FAST_GREEN_DIFFUSE );
  382. glMaterialfv(GL_BACK, GL_SPECULAR, SILVER_SPECULAR);
  383. glMaterialf (GL_BACK, GL_SHININESS, 128);
  384. draw_mesh(U,F,N);
  385. glDisable(GL_COLOR_MATERIAL);
  386. pop_object();
  387. // Draw a nice floor
  388. glPushMatrix();
  389. const double floor_offset =
  390. -2./bbd*(V.col(1).maxCoeff()-mid(1));
  391. glTranslated(0,floor_offset,0);
  392. const float GREY[4] = {0.5,0.5,0.6,1.0};
  393. const float DARK_GREY[4] = {0.2,0.2,0.3,1.0};
  394. draw_floor(GREY,DARK_GREY);
  395. glPopMatrix();
  396. pop_scene();
  397. report_gl_error();
  398. TwDraw();
  399. glutSwapBuffers();
  400. //if(is_animating)
  401. //{
  402. glutPostRedisplay();
  403. //}
  404. }
  405. void mouse_wheel(int wheel, int direction, int mouse_x, int mouse_y)
  406. {
  407. using namespace std;
  408. using namespace igl;
  409. using namespace Eigen;
  410. GLint viewport[4];
  411. glGetIntegerv(GL_VIEWPORT,viewport);
  412. if(wheel == 0 && TwMouseMotion(mouse_x, viewport[3] - mouse_y))
  413. {
  414. static double mouse_scroll_y = 0;
  415. const double delta_y = 0.125*direction;
  416. mouse_scroll_y += delta_y;
  417. TwMouseWheel(mouse_scroll_y);
  418. return;
  419. }
  420. push_undo();
  421. auto & camera = s.camera;
  422. if(wheel==0)
  423. {
  424. // factor of zoom change
  425. double s = (1.-0.01*direction);
  426. //// FOV zoom: just widen angle. This is hardly ever appropriate.
  427. //camera.m_angle *= s;
  428. //camera.m_angle = min(max(camera.m_angle,1),89);
  429. camera.push_away(s);
  430. }else
  431. {
  432. // Dolly zoom:
  433. camera.dolly_zoom((double)direction*1.0);
  434. }
  435. }
  436. void mouse(int glutButton, int glutState, int mouse_x, int mouse_y)
  437. {
  438. using namespace std;
  439. using namespace Eigen;
  440. using namespace igl;
  441. bool tw_using = TwEventMouseButtonGLUT(glutButton,glutState,mouse_x,mouse_y);
  442. switch(glutButton)
  443. {
  444. case GLUT_RIGHT_BUTTON:
  445. case GLUT_LEFT_BUTTON:
  446. {
  447. switch(glutState)
  448. {
  449. case 1:
  450. // up
  451. glutSetCursor(GLUT_CURSOR_LEFT_ARROW);
  452. is_rotating = false;
  453. break;
  454. case 0:
  455. // down
  456. if(!tw_using)
  457. {
  458. glutSetCursor(GLUT_CURSOR_CYCLE);
  459. // collect information for trackball
  460. is_rotating = true;
  461. down_camera = s.camera;
  462. down_x = mouse_x;
  463. down_y = mouse_y;
  464. }
  465. break;
  466. }
  467. break;
  468. }
  469. // Scroll down
  470. case 3:
  471. {
  472. mouse_wheel(0,-1,mouse_x,mouse_y);
  473. break;
  474. }
  475. // Scroll up
  476. case 4:
  477. {
  478. mouse_wheel(0,1,mouse_x,mouse_y);
  479. break;
  480. }
  481. // Scroll left
  482. case 5:
  483. {
  484. mouse_wheel(1,-1,mouse_x,mouse_y);
  485. break;
  486. }
  487. // Scroll right
  488. case 6:
  489. {
  490. mouse_wheel(1,1,mouse_x,mouse_y);
  491. break;
  492. }
  493. }
  494. glutPostRedisplay();
  495. }
  496. void mouse_drag(int mouse_x, int mouse_y)
  497. {
  498. using namespace igl;
  499. using namespace Eigen;
  500. if(is_rotating)
  501. {
  502. glutSetCursor(GLUT_CURSOR_CYCLE);
  503. Quaterniond q;
  504. auto & camera = s.camera;
  505. switch(rotation_type)
  506. {
  507. case ROTATION_TYPE_IGL_TRACKBALL:
  508. {
  509. // Rotate according to trackball
  510. igl::trackball<double>(
  511. width,
  512. height,
  513. 2.0,
  514. down_camera.m_rotation_conj.coeffs().data(),
  515. down_x,
  516. down_y,
  517. mouse_x,
  518. mouse_y,
  519. q.coeffs().data());
  520. break;
  521. }
  522. case ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP:
  523. {
  524. // Rotate according to two axis valuator with fixed up vector
  525. two_axis_valuator_fixed_up(
  526. width, height,
  527. 2.0,
  528. down_camera.m_rotation_conj,
  529. down_x, down_y, mouse_x, mouse_y,
  530. q);
  531. break;
  532. }
  533. default:
  534. break;
  535. }
  536. camera.orbit(q.conjugate());
  537. }else
  538. {
  539. TwEventMouseMotionGLUT(mouse_x, mouse_y);
  540. }
  541. glutPostRedisplay();
  542. }
  543. void key(unsigned char key, int mouse_x, int mouse_y)
  544. {
  545. using namespace std;
  546. switch(key)
  547. {
  548. // ESC
  549. case char(27):
  550. rebar.save(REBAR_NAME);
  551. // ^C
  552. case char(3):
  553. exit(0);
  554. default:
  555. if(!TwEventKeyboardGLUT(key,mouse_x,mouse_y))
  556. {
  557. cout<<"Unknown key command: "<<key<<" "<<int(key)<<endl;
  558. }
  559. }
  560. glutPostRedisplay();
  561. }
  562. int main(int argc, char * argv[])
  563. {
  564. using namespace Eigen;
  565. using namespace igl;
  566. using namespace std;
  567. // init mesh
  568. string filename = "../shared/decimated-knight.obj";
  569. string sfilename = "../shared/decimated-knight-selection.dmat";
  570. if(argc < 3)
  571. {
  572. cerr<<"Usage:"<<endl<<" ./example input.obj selection.dmat"<<endl;
  573. cout<<endl<<"Opening default mesh..."<<endl;
  574. }else
  575. {
  576. // Read and prepare mesh
  577. filename = argv[1];
  578. sfilename = argv[2];
  579. }
  580. vector<vector<double > > vV,vN,vTC;
  581. vector<vector<int > > vF,vTF,vFN;
  582. // Convert extension to lower case
  583. if(!igl::readOBJ(filename,vV,vTC,vN,vF,vTF,vFN))
  584. {
  585. return 1;
  586. }
  587. if(vV.size() > 0)
  588. {
  589. if(!list_to_matrix(vV,V))
  590. {
  591. cerr<<"Bad V"<<endl;
  592. return 1;
  593. }
  594. triangulate(vF,F);
  595. }
  596. per_face_normals(V,F,N);
  597. if(!readDMAT(sfilename,S))
  598. {
  599. return 1;
  600. }
  601. // Compute normals, centroid, colors, bounding box diagonal
  602. mid = 0.5*(V.colwise().maxCoeff() + V.colwise().minCoeff());
  603. bbd = (V.colwise().maxCoeff() - V.colwise().minCoeff()).maxCoeff();
  604. // Init glut
  605. glutInit(&argc,argv);
  606. if( !TwInit(TW_OPENGL, NULL) )
  607. {
  608. // A fatal error occured
  609. fprintf(stderr, "AntTweakBar initialization failed: %s\n", TwGetLastError());
  610. return 1;
  611. }
  612. // Create a tweak bar
  613. rebar.TwNewBar("TweakBar");
  614. rebar.TwAddVarRW("camera_rotation", TW_TYPE_QUAT4D,
  615. s.camera.m_rotation_conj.coeffs().data(), "open readonly=true");
  616. s.camera.push_away(3);
  617. s.camera.dolly_zoom(25-s.camera.m_angle);
  618. TwType RotationTypeTW = ReTwDefineEnumFromString("RotationType",
  619. "igl_trackball,two-a...-fixed-up");
  620. rebar.TwAddVarCB( "rotation_type", RotationTypeTW,
  621. set_rotation_type,get_rotation_type,NULL,"keyIncr=] keyDecr=[");
  622. rebar.TwAddVarRW("flip_y", TW_TYPE_BOOLCPP, &flip_y,"key=f");
  623. rebar.TwAddVarRW("rotate_xy", TW_TYPE_BOOLCPP, &rotate_xy,"key=r");
  624. rebar.load(REBAR_NAME);
  625. glutInitDisplayString( "rgba depth double samples>=8 ");
  626. glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH)/2.0,glutGet(GLUT_SCREEN_HEIGHT));
  627. glutCreateWindow("colored-mesh");
  628. glutDisplayFunc(display);
  629. glutReshapeFunc(reshape);
  630. glutKeyboardFunc(key);
  631. glutMouseFunc(mouse);
  632. glutMotionFunc(mouse_drag);
  633. glutPassiveMotionFunc(
  634. [](int x, int y)
  635. {
  636. TwEventMouseMotionGLUT(x,y);
  637. glutPostRedisplay();
  638. });
  639. static std::function<void(int)> timer_bounce;
  640. auto timer = [] (int ms) {
  641. timer_bounce(ms);
  642. };
  643. timer_bounce = [&] (int ms) {
  644. glutTimerFunc(ms, timer, ms);
  645. glutPostRedisplay();
  646. };
  647. glutTimerFunc(500, timer, 500);
  648. if(!init_arap())
  649. {
  650. cerr<<"Initializing arap failed."<<endl;
  651. return 1;
  652. }
  653. glutMainLoop();
  654. return 0;
  655. }