example.cpp 17 KB

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