example.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. #include <igl/RotateWidget.h>
  2. #include <igl/two_axis_valuator_fixed_up.h>
  3. #include <igl/readOBJ.h>
  4. #include <igl/readTGF.h>
  5. #include <igl/writeOBJ.h>
  6. #include <igl/writeOFF.h>
  7. #include <igl/readWRL.h>
  8. #include <igl/report_gl_error.h>
  9. #include <igl/triangulate.h>
  10. #include <igl/readOFF.h>
  11. #include <igl/readMESH.h>
  12. #include <igl/draw_mesh.h>
  13. #include <igl/draw_floor.h>
  14. #include <igl/pathinfo.h>
  15. #include <igl/list_to_matrix.h>
  16. #include <igl/quat_to_mat.h>
  17. #include <igl/per_face_normals.h>
  18. #include <igl/material_colors.h>
  19. #include <igl/trackball.h>
  20. #include <igl/snap_to_canonical_view_quat.h>
  21. #include <igl/snap_to_fixed_up.h>
  22. #include <igl/REDRUM.h>
  23. #include <igl/Camera.h>
  24. #include <igl/ReAntTweakBar.h>
  25. #include <igl/get_seconds.h>
  26. #include <Eigen/Core>
  27. #include <Eigen/Geometry>
  28. #include <GLUT/glut.h>
  29. #include <string>
  30. #include <vector>
  31. #include <list>
  32. #include <iostream>
  33. Eigen::MatrixXd V,N,C,W,M;
  34. Eigen::VectorXd Vmid,Vmin,Vmax;
  35. double bbd = 1.0;
  36. Eigen::MatrixXi F,BE;
  37. Eigen::VectorXi P;
  38. struct State
  39. {
  40. igl::Camera camera;
  41. igl::RotateWidget widget;
  42. } s;
  43. bool wireframe = false;
  44. bool widget_on_top = false;
  45. // See README for descriptions
  46. enum RotationType
  47. {
  48. ROTATION_TYPE_IGL_TRACKBALL = 0,
  49. ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP = 1,
  50. NUM_ROTATION_TYPES = 2,
  51. } rotation_type;
  52. std::list<State> undo_stack;
  53. std::list<State> redo_stack;
  54. bool is_rotating = false;
  55. int down_x,down_y;
  56. igl::Camera down_camera;
  57. bool is_animating = false;
  58. double animation_start_time = 0;
  59. double ANIMATION_DURATION = 0.5;
  60. Eigen::Quaterniond animation_from_quat;
  61. Eigen::Quaterniond animation_to_quat;
  62. int width,height;
  63. Eigen::Vector4f light_pos(-0.1,-0.1,0.9,0);
  64. #define REBAR_NAME "temp.rbr"
  65. igl::ReTwBar rebar;
  66. void push_undo()
  67. {
  68. undo_stack.push_back(s);
  69. // Clear
  70. redo_stack = std::list<State>();
  71. }
  72. // No-op setter, does nothing
  73. void TW_CALL no_op(const void * /*value*/, void * /*clientData*/)
  74. {
  75. }
  76. void TW_CALL set_rotation_type(const void * value, void * clientData)
  77. {
  78. using namespace Eigen;
  79. using namespace std;
  80. using namespace igl;
  81. const RotationType old_rotation_type = rotation_type;
  82. rotation_type = *(const RotationType *)(value);
  83. if(rotation_type == ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP &&
  84. old_rotation_type != ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP)
  85. {
  86. push_undo();
  87. animation_from_quat = s.camera.m_rotation_conj;
  88. snap_to_fixed_up(animation_from_quat,animation_to_quat);
  89. // start animation
  90. animation_start_time = get_seconds();
  91. is_animating = true;
  92. }
  93. }
  94. void TW_CALL get_rotation_type(void * value, void *clientData)
  95. {
  96. RotationType * rt = (RotationType *)(value);
  97. *rt = rotation_type;
  98. }
  99. void reshape(int width, int height)
  100. {
  101. ::width = width;
  102. ::height = height;
  103. glViewport(0,0,width,height);
  104. // Send the new window size to AntTweakBar
  105. TwWindowSize(width, height);
  106. s.camera.m_aspect = (double)width/(double)height;
  107. for(auto & s : undo_stack)
  108. {
  109. s.camera.m_aspect = (double)width/(double)height;
  110. }
  111. for(auto & s : redo_stack)
  112. {
  113. s.camera.m_aspect = (double)width/(double)height;
  114. }
  115. }
  116. void push_scene()
  117. {
  118. using namespace igl;
  119. using namespace std;
  120. glMatrixMode(GL_PROJECTION);
  121. glPushMatrix();
  122. glLoadIdentity();
  123. auto & camera = s.camera;
  124. gluPerspective(camera.m_angle,camera.m_aspect,camera.m_near,camera.m_far);
  125. glMatrixMode(GL_MODELVIEW);
  126. glPushMatrix();
  127. glLoadIdentity();
  128. gluLookAt(
  129. camera.eye()(0), camera.eye()(1), camera.eye()(2),
  130. camera.at()(0), camera.at()(1), camera.at()(2),
  131. camera.up()(0), camera.up()(1), camera.up()(2));
  132. }
  133. void push_object()
  134. {
  135. using namespace igl;
  136. glPushMatrix();
  137. glScaled(2./bbd,2./bbd,2./bbd);
  138. glTranslated(-Vmid(0),-Vmid(1),-Vmid(2));
  139. }
  140. void pop_object()
  141. {
  142. glPopMatrix();
  143. }
  144. void pop_scene()
  145. {
  146. glMatrixMode(GL_PROJECTION);
  147. glPopMatrix();
  148. glMatrixMode(GL_MODELVIEW);
  149. glPopMatrix();
  150. }
  151. // Set up double-sided lights
  152. void lights()
  153. {
  154. using namespace std;
  155. using namespace Eigen;
  156. glEnable(GL_LIGHTING);
  157. glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
  158. glEnable(GL_LIGHT0);
  159. glEnable(GL_LIGHT1);
  160. float WHITE[4] = {0.8,0.8,0.8,1.};
  161. float GREY[4] = {0.4,0.4,0.4,1.};
  162. float BLACK[4] = {0.,0.,0.,1.};
  163. Vector4f pos = light_pos;
  164. glLightfv(GL_LIGHT0,GL_AMBIENT,GREY);
  165. glLightfv(GL_LIGHT0,GL_DIFFUSE,WHITE);
  166. glLightfv(GL_LIGHT0,GL_SPECULAR,BLACK);
  167. glLightfv(GL_LIGHT0,GL_POSITION,pos.data());
  168. pos(0) *= -1;
  169. pos(1) *= -1;
  170. pos(2) *= -1;
  171. glLightfv(GL_LIGHT1,GL_AMBIENT,GREY);
  172. glLightfv(GL_LIGHT1,GL_DIFFUSE,WHITE);
  173. glLightfv(GL_LIGHT1,GL_SPECULAR,BLACK);
  174. glLightfv(GL_LIGHT1,GL_POSITION,pos.data());
  175. }
  176. void display()
  177. {
  178. using namespace igl;
  179. using namespace std;
  180. using namespace Eigen;
  181. const float back[4] = {30.0/255.0,30.0/255.0,50.0/255.0,0};
  182. glClearColor(back[0],back[1],back[2],0);
  183. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  184. if(is_animating)
  185. {
  186. double t = (get_seconds() - animation_start_time)/ANIMATION_DURATION;
  187. if(t > 1)
  188. {
  189. t = 1;
  190. is_animating = false;
  191. }
  192. Quaterniond q = animation_from_quat.slerp(t,animation_to_quat).normalized();
  193. auto & camera = s.camera;
  194. camera.orbit(q.conjugate());
  195. }
  196. glEnable(GL_DEPTH_TEST);
  197. glEnable(GL_NORMALIZE);
  198. lights();
  199. push_scene();
  200. // Draw a nice floor
  201. glEnable(GL_DEPTH_TEST);
  202. glPushMatrix();
  203. const double floor_offset =
  204. -2./bbd*(V.col(1).maxCoeff()-Vmid(1));
  205. glTranslated(0,floor_offset,0);
  206. const float GREY[4] = {0.5,0.5,0.6,1.0};
  207. const float DARK_GREY[4] = {0.2,0.2,0.3,1.0};
  208. glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  209. draw_floor(GREY,DARK_GREY);
  210. glPopMatrix();
  211. push_object();
  212. // Set material properties
  213. glDisable(GL_COLOR_MATERIAL);
  214. glMaterialfv(GL_FRONT, GL_AMBIENT, GOLD_AMBIENT);
  215. glMaterialfv(GL_FRONT, GL_DIFFUSE, GOLD_DIFFUSE );
  216. glMaterialfv(GL_FRONT, GL_SPECULAR, GOLD_SPECULAR);
  217. glMaterialf (GL_FRONT, GL_SHININESS, 128);
  218. glMaterialfv(GL_BACK, GL_AMBIENT, SILVER_AMBIENT);
  219. glMaterialfv(GL_BACK, GL_DIFFUSE, FAST_GREEN_DIFFUSE );
  220. glMaterialfv(GL_BACK, GL_SPECULAR, SILVER_SPECULAR);
  221. glMaterialf (GL_BACK, GL_SHININESS, 128);
  222. glPushMatrix();
  223. glTranslated( s.widget.pos(0), s.widget.pos(1), s.widget.pos(2));
  224. glMultMatrixd(Affine3d(s.widget.rot).matrix().data());
  225. glTranslated( -s.widget.pos(0), -s.widget.pos(1), -s.widget.pos(2));
  226. draw_mesh(V,F,N);
  227. glPopMatrix();
  228. if(widget_on_top)
  229. {
  230. glDisable(GL_DEPTH_TEST);
  231. }
  232. s.widget.draw();
  233. pop_object();
  234. pop_scene();
  235. report_gl_error();
  236. TwDraw();
  237. glutSwapBuffers();
  238. }
  239. void mouse_wheel(int wheel, int direction, int mouse_x, int mouse_y)
  240. {
  241. using namespace std;
  242. using namespace igl;
  243. using namespace Eigen;
  244. GLint viewport[4];
  245. glGetIntegerv(GL_VIEWPORT,viewport);
  246. if(wheel == 0 && TwMouseMotion(mouse_x, viewport[3] - mouse_y))
  247. {
  248. static double mouse_scroll_y = 0;
  249. const double delta_y = 0.125*direction;
  250. mouse_scroll_y += delta_y;
  251. TwMouseWheel(mouse_scroll_y);
  252. return;
  253. }
  254. push_undo();
  255. auto & camera = s.camera;
  256. if(wheel==0)
  257. {
  258. // factor of zoom change
  259. double s = (1.-0.01*direction);
  260. //// FOV zoom: just widen angle. This is hardly ever appropriate.
  261. //camera.m_angle *= s;
  262. //camera.m_angle = min(max(camera.m_angle,1),89);
  263. camera.push_away(s);
  264. }else
  265. {
  266. // Dolly zoom:
  267. camera.dolly_zoom((double)direction*1.0);
  268. }
  269. glutPostRedisplay();
  270. }
  271. void mouse(int glutButton, int glutState, int mouse_x, int mouse_y)
  272. {
  273. using namespace std;
  274. using namespace Eigen;
  275. using namespace igl;
  276. bool tw_using = TwEventMouseButtonGLUT(glutButton,glutState,mouse_x,mouse_y);
  277. push_scene();
  278. push_object();
  279. switch(glutButton)
  280. {
  281. case GLUT_RIGHT_BUTTON:
  282. case GLUT_LEFT_BUTTON:
  283. {
  284. switch(glutState)
  285. {
  286. case 1:
  287. // up
  288. s.widget.up(mouse_x,mouse_y);
  289. glutSetCursor(GLUT_CURSOR_INHERIT);
  290. is_rotating = false;
  291. break;
  292. case 0:
  293. bool widget_using = s.widget.down(mouse_x,height-mouse_y);
  294. if(widget_using)
  295. {
  296. push_undo();
  297. }
  298. if(!tw_using && !widget_using)
  299. {
  300. push_undo();
  301. glutSetCursor(GLUT_CURSOR_CYCLE);
  302. // collect information for trackball
  303. is_rotating = true;
  304. down_camera = s.camera;
  305. down_x = mouse_x;
  306. down_y = mouse_y;
  307. }
  308. break;
  309. }
  310. break;
  311. }
  312. // Scroll down
  313. case 3:
  314. {
  315. mouse_wheel(0,-1,mouse_x,mouse_y);
  316. break;
  317. }
  318. // Scroll up
  319. case 4:
  320. {
  321. mouse_wheel(0,1,mouse_x,mouse_y);
  322. break;
  323. }
  324. // Scroll left
  325. case 5:
  326. {
  327. mouse_wheel(1,-1,mouse_x,mouse_y);
  328. break;
  329. }
  330. // Scroll right
  331. case 6:
  332. {
  333. mouse_wheel(1,1,mouse_x,mouse_y);
  334. break;
  335. }
  336. }
  337. pop_object();
  338. pop_scene();
  339. glutPostRedisplay();
  340. }
  341. void mouse_drag(int mouse_x, int mouse_y)
  342. {
  343. using namespace igl;
  344. using namespace std;
  345. using namespace Eigen;
  346. push_scene();
  347. push_object();
  348. s.widget.drag(mouse_x,height-mouse_y);
  349. pop_object();
  350. pop_scene();
  351. if(is_rotating)
  352. {
  353. glutSetCursor(GLUT_CURSOR_CYCLE);
  354. Quaterniond q;
  355. auto & camera = s.camera;
  356. switch(rotation_type)
  357. {
  358. case ROTATION_TYPE_IGL_TRACKBALL:
  359. {
  360. // Rotate according to trackball
  361. igl::trackball<double>(
  362. width,
  363. height,
  364. 2.0,
  365. down_camera.m_rotation_conj.coeffs().data(),
  366. down_x,
  367. down_y,
  368. mouse_x,
  369. mouse_y,
  370. q.coeffs().data());
  371. break;
  372. }
  373. case ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP:
  374. {
  375. // Rotate according to two axis valuator with fixed up vector
  376. two_axis_valuator_fixed_up(
  377. width, height,
  378. 2.0,
  379. down_camera.m_rotation_conj,
  380. down_x, down_y, mouse_x, mouse_y,
  381. q);
  382. break;
  383. }
  384. default:
  385. break;
  386. }
  387. camera.orbit(q.conjugate());
  388. }
  389. glutPostRedisplay();
  390. }
  391. void init_relative()
  392. {
  393. using namespace Eigen;
  394. using namespace igl;
  395. per_face_normals(V,F,N);
  396. Vmax = V.colwise().maxCoeff();
  397. Vmin = V.colwise().minCoeff();
  398. Vmid = 0.5*(Vmax + Vmin);
  399. bbd = (Vmax-Vmin).norm();
  400. s.widget.pos = Vmin+0.3*(Vmax-Vmin);
  401. }
  402. void undo()
  403. {
  404. using namespace std;
  405. if(!undo_stack.empty())
  406. {
  407. redo_stack.push_back(s);
  408. s = undo_stack.back();
  409. undo_stack.pop_back();
  410. }
  411. }
  412. void redo()
  413. {
  414. using namespace std;
  415. if(!redo_stack.empty())
  416. {
  417. undo_stack.push_back(s);
  418. s = redo_stack.back();
  419. redo_stack.pop_back();
  420. }
  421. }
  422. void key(unsigned char key, int mouse_x, int mouse_y)
  423. {
  424. using namespace std;
  425. using namespace igl;
  426. using namespace Eigen;
  427. int mod = glutGetModifiers();
  428. const bool command_down = GLUT_ACTIVE_COMMAND & mod;
  429. const bool shift_down = GLUT_ACTIVE_SHIFT & mod;
  430. switch(key)
  431. {
  432. // ESC
  433. case char(27):
  434. rebar.save(REBAR_NAME);
  435. // ^C
  436. case char(3):
  437. exit(0);
  438. case 'z':
  439. case 'Z':
  440. if(command_down)
  441. {
  442. if(shift_down)
  443. {
  444. redo();
  445. }else
  446. {
  447. undo();
  448. }
  449. break;
  450. }else
  451. {
  452. push_undo();
  453. Quaterniond q;
  454. snap_to_canonical_view_quat(s.camera.m_rotation_conj,1.0,q);
  455. s.camera.orbit(q.conjugate());
  456. }
  457. default:
  458. if(!TwEventKeyboardGLUT(key,mouse_x,mouse_y))
  459. {
  460. cout<<"Unknown key command: "<<key<<" "<<int(key)<<endl;
  461. }
  462. }
  463. glutPostRedisplay();
  464. }
  465. int main(int argc, char * argv[])
  466. {
  467. using namespace std;
  468. using namespace Eigen;
  469. using namespace igl;
  470. string filename = "../shared/cheburashka.off";
  471. if(argc < 3)
  472. {
  473. cerr<<"Usage:"<<endl<<" ./example input.obj"<<endl;
  474. cout<<endl<<"Opening default mesh..."<<endl;
  475. }else
  476. {
  477. // Read and prepare mesh
  478. filename = argv[1];
  479. }
  480. // print key commands
  481. cout<<"[Click] and [drag] Rotate model using trackball."<<endl;
  482. cout<<"[Z,z] Snap rotation to canonical view."<<endl;
  483. cout<<"[⌘ Z] Undo."<<endl;
  484. cout<<"[⇧ ⌘ Z] Redo."<<endl;
  485. cout<<"[^C,ESC] Exit."<<endl;
  486. // dirname, basename, extension and filename
  487. string d,b,ext,f;
  488. pathinfo(filename,d,b,ext,f);
  489. // Convert extension to lower case
  490. transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
  491. vector<vector<double > > vV,vN,vTC;
  492. vector<vector<int > > vF,vFTC,vFN;
  493. if(ext == "obj")
  494. {
  495. // Convert extension to lower case
  496. if(!igl::readOBJ(filename,vV,vTC,vN,vF,vFTC,vFN))
  497. {
  498. return 1;
  499. }
  500. }else if(ext == "off")
  501. {
  502. // Convert extension to lower case
  503. if(!igl::readOFF(filename,vV,vF,vN))
  504. {
  505. return 1;
  506. }
  507. }else if(ext == "wrl")
  508. {
  509. // Convert extension to lower case
  510. if(!igl::readWRL(filename,vV,vF))
  511. {
  512. return 1;
  513. }
  514. //}else
  515. //{
  516. // // Convert extension to lower case
  517. // MatrixXi T;
  518. // if(!igl::readMESH(filename,V,T,F))
  519. // {
  520. // return 1;
  521. // }
  522. // //if(F.size() > T.size() || F.size() == 0)
  523. // {
  524. // boundary_faces(T,F);
  525. // }
  526. }
  527. if(vV.size() > 0)
  528. {
  529. if(!list_to_matrix(vV,V))
  530. {
  531. return 1;
  532. }
  533. triangulate(vF,F);
  534. }
  535. init_relative();
  536. // Init glut
  537. glutInit(&argc,argv);
  538. if( !TwInit(TW_OPENGL, NULL) )
  539. {
  540. // A fatal error occured
  541. fprintf(stderr, "AntTweakBar initialization failed: %s\n", TwGetLastError());
  542. return 1;
  543. }
  544. // Create a tweak bar
  545. rebar.TwNewBar("TweakBar");
  546. rebar.TwAddVarRW("camera_rotation", TW_TYPE_QUAT4D,
  547. s.camera.m_rotation_conj.coeffs().data(), "open readonly=true");
  548. TwType RotationTypeTW = ReTwDefineEnumFromString("RotationType",
  549. "igl_trackball,two-a...-fixed-up");
  550. rebar.TwAddVarCB( "rotation_type", RotationTypeTW,
  551. set_rotation_type,get_rotation_type,NULL,"keyIncr=] keyDecr=[");
  552. rebar.TwAddVarRW("widget_on_top", TW_TYPE_BOOLCPP,&widget_on_top,"key=O");
  553. rebar.TwAddVarRW("wireframe", TW_TYPE_BOOLCPP,&wireframe,"key=l");
  554. rebar.load(REBAR_NAME);
  555. // Init antweakbar
  556. glutInitDisplayString( "rgba depth double samples>=8 ");
  557. glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH)/2.0,glutGet(GLUT_SCREEN_HEIGHT)/2.0);
  558. glutCreateWindow("rotate-widget");
  559. glutDisplayFunc(display);
  560. glutReshapeFunc(reshape);
  561. glutKeyboardFunc(key);
  562. glutMouseFunc(mouse);
  563. glutMotionFunc(mouse_drag);
  564. glutPassiveMotionFunc(
  565. [](int x, int y)
  566. {
  567. TwEventMouseMotionGLUT(x,y);
  568. glutPostRedisplay();
  569. });
  570. static std::function<void(int)> timer_bounce;
  571. auto timer = [] (int ms) {
  572. timer_bounce(ms);
  573. };
  574. timer_bounce = [&] (int ms) {
  575. glutTimerFunc(ms, timer, ms);
  576. glutPostRedisplay();
  577. };
  578. glutTimerFunc(500, timer, 500);
  579. s.camera.dolly_zoom(25-s.camera.m_angle);
  580. glutMainLoop();
  581. return 0;
  582. }